<?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.swzdr.mapper.RyXsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="ryXsResultMap" type="org.springblade.swzdr.pojo.entity.RyXsEntity">
        <result column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="xsnr" property="xsnr"/>
        <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="selectRyXsPage" resultType="org.springblade.swzdr.pojo.vo.RyInfoVO">
        SELECT
            xs.id,
            ry.id AS userId,
            ry.wklb,
            ry.xm,
            ry.xb,
            ry.LEVEL,
            string_agg ( dh.lxdh, ',' ) AS lxdh,
            ry.gmsfhm,
            ry.jzdxz,
            pcs.dept_name
        FROM
            swzdr_ry_xs xs
        LEFT JOIN swzdr_ry_info ry ON ry.ID = xs.user_id
        LEFT JOIN blade_dept pcs ON pcs.ID = ry.pcs_id
        LEFT JOIN swzdr_ry_lxdh dh ON dh.user_id = ry.ID
        WHERE
            xs.is_deleted = 0
          AND ( xs.xsnr IS NULL OR xs.xsnr = '' )
          AND ry.mj_id = #{params.userId} :: int8
        <if test="null != params.gmsfhm and '' != params.gmsfhm">
          AND ry.gmsfhm = #{params.gmsfhm}
        </if>
        <if test="null != params.xm and '' != params.xm">
            AND ry.xm = #{params.xm}
        </if>
        <if test="null != params.jz and '' != params.jz">
            AND ry.wklb LIKE #{params.jz} || '%'
        </if>
        <if test="null != params.type">
            AND xs.type = #{params.type} :: int
        </if>
        GROUP BY
            xs.ID,
            ry.ID,
            ry.wklb,
            ry.xm,
            ry.xb,
            ry.LEVEL,
            ry.gmsfhm,
            ry.jzdxz,
            pcs.dept_name
    </select>

    <select id="exportRyXs" resultType="org.springblade.swzdr.excel.RyXsExcel">
        SELECT * FROM swzdr_ry_xs ${ew.customSqlSegment}
    </select>

    <select id="getXsList" resultType="org.springblade.swzdr.pojo.vo.RyXsVO">
        SELECT
            xs.xsnr,
            xs.type,
            xs.update_time,
            bu.jh,
            bu.name,
            pcs.dept_name
        FROM
            swzdr_ry_info ry
        LEFT JOIN swzdr_ry_xs xs ON xs.user_id = ry.id
        LEFT JOIN blade_user bu ON bu.id = xs.update_user
        LEFT JOIN blade_dept sq ON sq.id = SPLIT_PART( bu.dept_id, ',', 1 ) :: int8
	    LEFT JOIN blade_dept pcs ON pcs.id = sq.parent_id
        WHERE
            ry.is_deleted = 0
          AND xs.xsnr IS NOT NULL
          AND xs.xsnr != ''
          AND ry.id = #{ryId}
        ORDER BY
            xs.update_time DESC
    </select>

</mapper>
