<?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.sqjw.userInfo.mapper.UserInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="userInfoResultMap" type="org.springblade.modules.sqjw.userInfo.entity.UserInfoEntity">
        <result column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="zp" property="zp"/>
        <result column="xm" property="xm"/>
        <result column="xb" property="xb"/>
        <result column="sfzh" property="sfzh"/>
        <result column="csrq" property="csrq"/>
        <result column="mz" property="mz"/>
        <result column="whcd" property="whcd"/>
        <result column="hyzk" property="hyzk"/>
        <result column="jtzz" property="jtzz"/>
        <result column="lxdh" property="lxdh"/>
        <result column="jh" property="jh"/>
        <result column="zzmm" property="zzmm"/>
        <result column="gzdwmc" property="gzdwmc"/>
        <result column="sszrq" property="sszrq"/>
        <result column="ssjg" property="ssjg"/>
        <result column="zwlb" property="zwlb"/>
        <result column="gwzz" property="gwzz"/>
        <result column="zhzhmj" property="zhzhmj"/>
        <result column="rszrq" property="rszrq"/>
        <result column="zj" property="zj"/>
        <result column="jbzzw" property="jbzzw"/>
        <result column="jbzrq" property="jbzrq"/>
        <result column="bcsl" property="bcsl"/>
        <result column="ydw" property="ydw"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>


    <select id="selectUserInfoPage" resultType="org.springblade.modules.sqjw.userInfo.vo.UserInfoVO">
        SELECT
            bui.*,
            bu.id AS userId,
            pcs1.id AS sspcsBm,
            fj1.id AS ssfjBm,
            sj1.id AS sssjBm
        FROM
            jdrh.blade_user bu
        LEFT JOIN jdrh.blade_user_info bui ON bui.user_id = bu.ID
        LEFT JOIN jdrh.blade_dept bd ON bd.id :: text = (string_to_array( bu.dept_id, ',' )) [1]
        LEFT JOIN jdrh.blade_dept pcs1 ON pcs1.id = COALESCE(NULLIF(bui.gzdwmc, '') :: int8, 0)
        LEFT JOIN jdrh.blade_dept fj1 ON fj1.id = pcs1.parent_id
        LEFT JOIN jdrh.blade_dept sj1 ON sj1.id = fj1.parent_id
        WHERE
            bu.is_deleted = 0
        AND bu.role_id IN ( '1677215117370056705', '1677215233615192065', '1677215613229064194', '1709400559812902914' )
        AND ((bd.id :: text = #{params.deptId}) OR (bd.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.sssjBm and '' != params.sssjBm">
            AND bd.ancestors LIKE '%' || #{params.sssjBm} || '%'
        </if>
        <if test="null != params.ssfjBm and '' != params.ssfjBm">
            AND bd.ancestors LIKE '%' || #{params.ssfjBm} || '%'
        </if>
        <if test="null != params.sspcsBm and '' != params.sspcsBm">
            AND (bd.id = #{params.sspcsBm} :: int8 OR bd.parent_id = #{params.sspcsBm} :: int8)
        </if>
        <if test="null != params.sfzh and '' != params.sfzh">
        AND bui.sfzh LIKE '%' || #{params.sfzh} || '%'
        </if>
        <if test="null != params.xm and '' != params.xm">
        AND bui.xm LIKE '%' || #{params.xm} || '%'
        </if>
        <if test="null != params.jh and '' != params.jh">
        AND bui.jh LIKE '%' || #{params.jh} || '%'
        </if>
        <if test="null != params.lxdh and '' != params.lxdh">
        AND bui.lxdh LIKE '%' || #{params.lxdh} || '%'
        </if>
        <if test="null != params.gzdwmc and '' != params.gzdwmc">
        AND bui.gzdwmc LIKE '%' || #{params.gzdwmc} || '%'
        </if>
        <if test="null != params.zwlb and '' != params.zwlb">
        AND bui.zwlb = #{params.zwlb}
        </if>
    </select>

    <select id="exportUserInfo" resultType="org.springblade.modules.sqjw.userInfo.excel.UserInfoExcel">
        SELECT
        bui.*,
        bu.id AS userId
        FROM
        jdrh.blade_user bu
        LEFT JOIN jdrh.blade_user_info bui ON bui.user_id = bu.ID
        LEFT JOIN blade_dept bd ON bd.id :: text = (string_to_array( bu.dept_id, ',' )) [1]
        WHERE
        bu.is_deleted = 0
        AND bu.role_id IN ( '1677215117370056705', '1677215233615192065', '1677215613229064194', '1694608532769734657', '1709400559812902914' )
        AND ((bd.id :: text = #{params.deptId}) OR (bd.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.sfzh and '' != params.sfzh">
            AND bui.sfzh LIKE '%' || #{params.sfzh} || '%'
        </if>
        <if test="null != params.xm and '' != params.xm">
            AND bui.xm LIKE '%' || #{params.xm} || '%'
        </if>
        <if test="null != params.jh and '' != params.jh">
            AND bui.jh LIKE '%' || #{params.jh} || '%'
        </if>
        <if test="null != params.lxdh and '' != params.lxdh">
            AND bui.lxdh LIKE '%' || #{params.lxdh} || '%'
        </if>
        <if test="null != params.gzdwmc and '' != params.gzdwmc">
            AND bui.gzdwmc LIKE '%' || #{params.gzdwmc} || '%'
        </if>
        <if test="null != params.zwlb and '' != params.zwlb">
            AND bui.zwlb = #{params.zwlb}
        </if>
    </select>

    <select id="getDetail" resultType="org.springblade.modules.sqjw.userInfo.vo.UserInfoVO">
        SELECT
            bui.*,
            pcs1.id AS sspcsBm,
            fj1.id AS ssfjBm,
            sj1.id AS sssjBm,
            fj2.id AS fjYdw,
            sj2.id AS sjYdw
        FROM
            (SELECT * FROM jdrh.blade_user_info WHERE id = #{params.id} :: int8) bui
        LEFT JOIN jdrh.blade_dept pcs1 ON pcs1.id = COALESCE(NULLIF(bui.gzdwmc, '') :: int8, 0)
        LEFT JOIN jdrh.blade_dept fj1 ON fj1.id = pcs1.parent_id
        LEFT JOIN jdrh.blade_dept sj1 ON sj1.id = fj1.parent_id
        LEFT JOIN jdrh.blade_dept pcs2 ON pcs2.id = COALESCE(NULLIF(bui.ydw, '') :: int8, 0)
        LEFT JOIN jdrh.blade_dept fj2 ON fj2.id = pcs2.parent_id
        LEFT JOIN jdrh.blade_dept sj2 ON sj2.id = fj2.parent_id
    </select>

    <update id="mfjgl">
        UPDATE jdrh.blade_user
        SET mj_id = #{params.userId} :: int8,
            update_time = now( )
        WHERE
            account = #{params.fjZh}
          AND is_deleted = 0
    </update>

    <update id="updateSszrqByUserId">
        UPDATE jdrh.blade_user_info
        SET sszrq = #{deptIds},
            update_time = now()
        WHERE
            user_id = #{userId}
          AND is_deleted = 0
    </update>

    <select id="getSzNum" resultType="int">
        SELECT
            COUNT(1)
        FROM
            jdrh.blade_user_info bui
        LEFT JOIN jdrh.blade_user bu ON bu.ID = bui.user_id
        WHERE
            bu.is_deleted = 0
          AND bui.zwlb = '1760218852527259650'
          AND bui.gzdwmc = #{gzdwmc}
          AND bui.jh != #{jh}
    </select>
</mapper>
