<?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.RyInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="ryInfoResultMap" type="org.springblade.swzdr.pojo.entity.RyInfoEntity">
        <result column="id" property="id"/>
        <result column="gmsfhm" property="gmsfhm"/>
        <result column="xm" property="xm"/>
        <result column="xb" property="xb"/>
        <result column="csrq" property="csrq"/>
        <result column="mz" property="mz"/>
        <result column="hjdxz" property="hjdxz"/>
        <result column="hjdpcs" property="hjdpcs"/>
        <result column="jzdxz" property="jzdxz"/>
        <result column="jzdpcs" property="jzdpcs"/>
        <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="selectRyInfoPage" resultType="org.springblade.swzdr.pojo.vo.RyInfoVO">
        SELECT
            ry.id,
            ry.xm,
            ry.level,
            ry.xb,
            ry.gmsfhm,
            string_agg ( dh.lxdh, ',' ) AS lxdh,
            ry.hjdxz,
            ry.jzdxz
        FROM
            swzdr_ry_info ry
        LEFT JOIN blade_dept pcs ON pcs.id = ry.pcs_id
        LEFT JOIN swzdr_ry_lxdh dh ON dh.user_id = ry.id
        LEFT JOIN swzdr_ry_lhsp sp ON sp.user_id = ry.id
        LEFT JOIN swzdr_ry_rwzl rw ON rw.ry_id = ry.id
        WHERE
            ry.is_deleted = 0
        AND (sp.id IS NULL OR sp.spjg = 1)
        AND (rw.id IS NULL OR rw.zt != 0)
        AND (ry.mj_id = #{params.userId} :: int8 OR pcs.id = #{params.deptId} :: int8 OR pcs.ancestors ILIKE '%' || #{params.deptId} || '%')
        <if test="null != params.gmsfhm and '' != params.gmsfhm">
            AND ry.gmsfhm ILIKE '%' || #{params.gmsfhm} || '%'
        </if>
        <if test="null != params.xm and '' != params.xm">
            AND ry.xm ILIKE '%' || #{params.xm} || '%'
        </if>
        <if test="null != params.level and '' != params.level">
            AND ry.level = #{params.level}
        </if>
        GROUP BY
            ry.id,
            ry.xm,
            ry.level,
            ry.xb,
            ry.gmsfhm,
            ry.hjdxz,
            ry.jzdxz
        ORDER BY
            ry.create_time DESC
    </select>

    <select id="exportRyInfo" resultType="org.springblade.swzdr.excel.RyInfoExcel">
        SELECT * FROM swzdr_ry_info ${ew.customSqlSegment}
    </select>

    <select id="getSwzdrList" resultType="java.lang.Long">
        SELECT
            id
        FROM
            swzdr_ry_info
        WHERE
            is_deleted = 0
    </select>

</mapper>
