<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.springblade.modules.jwtdJfjl.mapper.JwtdJfjlMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="jwtdJfjlResultMap" type="org.springblade.modules.jwtdJfjl.pojo.entity.JwtdJfjlEntity">
        <result column="user_id" property="userId"/>
        <result column="type" property="type"/>
        <result column="jf_id" property="jfId"/>
        <result column="jf" property="jf"/>
    </resultMap>

    <select id="selectJwtdJfjlPage" resultMap="jwtdJfjlResultMap">
        SELECT
            bu.id,
            bu.name,
            bu.sfzh,
            bu.phone,
            bu.dz,
            SUM ( jj.jf ) + bu.jf AS zjf,
            SUM ( jj.jf ) AS dhjf,
            bu.jf
        FROM
            blade_user bu
        LEFT JOIN jwtd_jfjl jj ON bu.id = jj.user_id
        WHERE
            bu.is_deleted = 0
        <if test="jwtdJfjl.name != null and jwtdJfjl.name != ''">
            and bu.name = #{jwtdJfjl.name}
        </if>
        <if test="jwtdJfjl.sfzh != null and jwtdJfjl.sfzh != ''">
            and bu.sfzh = #{jwtdJfjl.sfzh}
        </if>
        <if test="jwtdJfjl.phone != null and jwtdJfjl.phone != ''">
            and bu.phone = #{jwtdJfjl.phone}
        </if>
        GROUP BY
            bu.id,
            bu.name,
            bu.sfzh,
            bu.phone,
            bu.dz,
            bu.jf
    </select>

    <select id="exportJwtdJfjl" resultType="org.springblade.modules.jwtdJfjl.excel.JwtdJfjlExcel">
        SELECT * FROM jwtd_jfjl ${ew.customSqlSegment}
    </select>

    <select id="getJlList" resultMap="jwtdJfjlResultMap">
        select
            case when jf.xcbt is null then js.spmc when js.spmc is null then jf.xcbt end as name,
            jj.create_time,
            jj.type,
            jj.jf,
            jsd.ddbh
        from jwtd_jfjl jj
        left join jwtd_ffxc jf on jj.jf_id = jf.id
        left join jwtd_spgl_dhjl jsd on jj.jf_id = jsd.id
        left join jwtd_spgl js on js.id = jsd.sp_id
        where jj.is_deleted = 0 and jj.user_id = #{userId}
        <if test="type == '积分获取'">
            and jj.type = '1'
        </if>
        <if test="type == '积分兑换'">
            and jj.type = '2'
        </if>
    </select>

    <select id="getHqjl" resultMap="jwtdJfjlResultMap">
        select
            jf.xcbt,
            jf.fbdw,
            jf.create_time,
            jj.jf
        from jwtd_jfjl jj
        left join jwtd_ffxc jf on jj.jf_id = jf.id
        where jj.is_deleted = 0 and jj.user_id = #{userId} and jj.type = '1'
    </select>

    <select id="getDhjl" resultMap="jwtdJfjlResultMap">
        SELECT
            jsd.ddbh,
            SUM ( jsd.spsl ) AS spsl,
            SUM ( jsd.xhjf ) AS xhjf,
            jsd.lqzt,
            jsd.create_time,
            jsd.update_time,
            bu.name as fhr
        FROM
            jwtd_spgl_dhjl jsd
        LEFT JOIN blade_user bu ON bu.id = jsd.fhr
        WHERE
            user_id = #{userId}
        GROUP BY
            jsd.ddbh,
            jsd.lqzt,
            jsd.create_time,
            jsd.update_time,
            bu.name
    </select>

    <select id="getDhjf" resultType="java.lang.Integer">
        SELECT
            sum(jj.jf) as dhjf
        FROM
            jwtd_jfjl jj
        LEFT JOIN blade_dept bd ON jj.create_dept = bd.ID
        LEFT JOIN blade_user bu ON jj.create_user = bu.ID
        WHERE jj.is_deleted = 0 and bu.is_deleted = 0 and bd.ancestors like '%' || #{deptId} || '%' and jj.type = '2'
    </select>

    <select id="getJf" resultType="java.lang.Integer">
        select
            sum(bu.jf) as jf
        from blade_user bu
        left join blade_dept bd on bu.dept_id :: int8 = bd.id
        where bu.is_deleted = 0 and bd.ancestors like '%' || #{deptId} || '%'
    </select>
</mapper>
