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

    <!-- 通用查询映射结果 -->
    <resultMap id="ryCgspResultMap" type="org.springblade.swzdr.pojo.entity.RyCgspEntity">
        <result column="id" property="id"/>
        <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"/>
        <result column="ry_id" property="ryId"/>
        <result column="czsm" property="czsm"/>
        <result column="cgyy" property="cgyy"/>
        <result column="bzs" property="bzs"/>
    </resultMap>

    <select id="selectRyCgspPage" resultType="org.springblade.swzdr.pojo.vo.RyCgspVO">
        SELECT
            cg.id,
            ry.id AS ryId,
            ry.xm,
            ry.xb,
            ry.gmsfhm,
            string_agg ( dh.lxdh, ',' ) AS lxdh,
            ry.hjdxz,
            ry.jzdxz,
            cg.spjg,
            pcs.dept_name AS pcsName
        FROM
            swzdr_ry_cgsp cg
        LEFT JOIN swzdr_ry_info ry ON ry.id = cg.ry_id
        LEFT JOIN swzdr_ry_lxdh dh ON dh.user_id = ry.id
        LEFT JOIN blade_dept pcs ON pcs.id = ry.pcs_id
        WHERE
            (ry.pcs_id = #{params.deptId} :: int8 OR ry.mj_id = #{params.userId} :: int8 OR ry.wkdw = #{params.deptId} :: int8)
        <if test="null != params.spjg">
            AND cg.spjg = #{params.spjg} :: int
        </if>
        <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 LIKE '%' || #{params.xm} || '%'
        </if>
        <if test="null != params.lxdh and '' != params.lxdh">
            AND EXISTS (
            SELECT 1 FROM swzdr_ry_lxdh dh_inner
            WHERE dh_inner.user_id = ry.ID AND dh_inner.lxdh ILIKE '%' || #{params.lxdh} || '%'
            )
        </if>
        GROUP BY
            cg.id,
            ry.id,
            ry.xm,
            ry.xb,
            ry.gmsfhm,
            ry.hjdxz,
            ry.jzdxz,
            cg.spjg,
            pcs.dept_name
    </select>

    <select id="exportRyCgsp" resultType="org.springblade.swzdr.excel.RyCgspExcel">
        SELECT * FROM swzdr_ry_cgsp ${ew.customSqlSegment}
    </select>

    <select id="getListByRyId" resultType="org.springblade.swzdr.pojo.vo.RyCgspVO">
        SELECT
            cg.czsm,
            cg.cgyy,
            cg.bzs,
            cg.create_time,
            cu.jh AS cuJh,
            cu.name AS cuName,
            pcs.dept_name AS cuDeptName,
            cg.spjg,
            cg.jjyy,
            cg.update_time,
            uu.jh AS uuJh,
            uu.name AS uuName
        FROM
            swzdr_ry_cgsp cg
        LEFT JOIN blade_user cu ON cu.id = cg.create_user
        LEFT JOIN blade_dept sq ON sq.id = SPLIT_PART( cu.dept_id, ',', 1 ) :: int8
	    LEFT JOIN blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN blade_user uu ON uu.id = cg.update_user
        WHERE
            ry_id = #{ryId}
    </select>
</mapper>
