<?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.rhzf.fwRy.mapper.FwRyMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="fwRyResultMap" type="org.springblade.modules.rhzf.fwRy.entity.FwRyEntity">
        <result column="id" property="id"/>
        <result column="fw_id" property="fwId"/>
        <result column="ry_id" property="ryId"/>
        <result column="ry_lb" property="ryType"/>
        <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="selectFwRyPage" resultMap="fwRyResultMap">
        select * from rhzf_fw_ry where is_deleted = 0
    </select>


    <select id="exportFwRy" resultType="org.springblade.modules.rhzf.fwRy.excel.FwRyExcel">
        SELECT * FROM rhzf_fw_ry ${ew.customSqlSegment}
    </select>

    <select id="getCountByRyXlh" resultType="int">
        SELECT
            COUNT( * )
        FROM
            rhzf_fw_ry
        WHERE
            ryjbxxxlh = #{ryjbxxxlh}
    </select>

    <select id="countByDzbm" resultType="int">
        SELECT
            COUNT( * )
        FROM
            rhzf_fw_ry
        WHERE
            dzbm = #{dzbm}
        AND is_deleted = 0
    </select>

    <select id="getSyrkList" resultType="org.springblade.modules.rhzf.fwInfo.vo.SyjzryVO">
        SELECT
            xlh AS ryjbxxxlh,
            rytype AS ryType,
            ryxm AS ryXm,
            rydz AS dzbm
        FROM
            sspt.syrk
        WHERE
            rydz = #{params.dzbm}
    </select>

    <update id="updateFwId">
        UPDATE jdrh.rhzf_fw_ry
        SET fw_id = #{fwId},
            update_time = now()
        WHERE
            dzbm = #{dzbm}
          AND ry_type = #{ryType}
          AND ry_id = #{ryId}
    </update>

    <update id="deleteByRyidAndType">
        UPDATE jdrh.rhzf_fw_ry
        SET is_deleted = 1,
            update_time = now()
        WHERE
            ry_id = #{params.ryId} :: int8
          AND ry_type = #{params.ryType}
    </update>

    <select id="getRyls" resultType="org.springblade.modules.rhzf.fwInfo.vo.SyjzryVO">
        SELECT
            *
        FROM
            (
                SELECT
                    'czrk' AS ryType,
                    cz.ry_xm AS ryXm,
                    cz.is_deleted AS isDeleted,
                    cz.update_time AS updateTime
                FROM
                    jdrh.rhzf_ry_czrk cz
                WHERE
                    cz.fw_id = #{params.fwId} :: int8 UNION ALL
                SELECT
                    'jwrk' AS ryType,
                    jw.ry_mz || ' ' || jw.ry_xs AS ryXm,
                    jw.is_deleted AS isDeleted,
                    jw.update_time AS updateTime
                FROM
                    jdrh.rhzf_ry_jwrk jw
                WHERE
                    jw.fw_id = #{params.fwId} :: int8 UNION ALL
                SELECT
                    'jzrk' AS ryType,
                    jz.ry_xm AS ryXm,
                    jz.is_deleted AS isDeleted,
                    jz.update_time AS updateTime
                FROM
                    jdrh.rhzf_ry_jzrk jz
                WHERE
                    jz.fw_id = #{params.fwId} :: int8 UNION ALL
                SELECT
                    'wlrk' AS ryType,
                    wl.ry_xm AS ryXm,
                    wl.is_deleted AS isDeleted,
                    wl.update_time AS updateTime
                FROM
                    jdrh.rhzf_ry_wlrk wl
                WHERE
                    wl.fw_id = #{params.fwId} :: int8 UNION ALL
                SELECT
                    'zzrk' AS ryType,
                    zz.ry_xm AS ryXm,
                    zz.is_deleted AS isDeleted,
                    zz.update_time AS updateTime
                FROM
                    jdrh.rhzf_ry_zzrk zz
                WHERE
                    zz.fw_id = #{params.fwId} :: int8
            ) rys
        ORDER BY
            rys.updateTime DESC
    </select>

    <select id="getFwry" resultType="org.springblade.modules.rhzf.fwInfo.vo.SyjzryVO">
        SELECT DISTINCT
            *
        FROM
            (
                SELECT
                    cz.ry_xm AS ryXm,
                    cz.dzbm AS dzbm,
                    '常住' AS ryType,
                    cz.ry_sfzh AS rySfzh,
                    cz.ry_dh AS ryDh,
                    cz.ry_hzgx AS ryHzgx,
                    cz.ry_hyzk AS ryHyzk,
                    cz.ry_whcd AS ryWhcd,
                    cz.ry_zzmm AS ryZzmm,
                    cz.ry_byzk AS ryByzk,
                    cz.ry_ts_lb AS ryTsLb,
                    cz.ry_ts_jtqk AS ryTsJtqk,
                    cz.bz AS bz,
                    cz.create_user,
                    cz.create_time,
                    cz.update_user,
                    cz.update_time,
                    sj.id as sjid,
                    fj.id as fjid,
                    pcs.id as pcsid,
                    sq.id as sqid
                FROM
                    jdrh.rhzf_ry_czrk cz
                LEFT JOIN jdrh.blade_dept sq ON sq.id = cz.create_dept
                LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
                LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
                LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
                WHERE
                    cz.is_deleted = 0
                    and (sq.id = #{syjzryVO.deptId} :: int8 or sq.ancestors like '%' || #{syjzryVO.deptId} || '%')
                UNION ALL
                SELECT
                    jw.ry_mz || ' ' || jw.ry_xs AS ryXm,
                    jw.dzbm AS dzbm,
                    '境外' AS ryType,
                    jw.ry_zjhm AS rySfzh,
                    jw.ry_dh AS ryDh,
                    '' AS ryHzgx,
                    '' AS ryHyzk,
                    '' AS ryWhcd,
                    '' AS ryZzmm,
                    '' AS ryByzk,
                    jw.ry_ts_lb AS ryTsLb,
                    jw.ry_ts_jtqk AS ryTsJtqk,
                    jw.bz AS bz,
                    jw.create_user,
                    jw.create_time,
                    jw.update_user,
                    jw.update_time,
                    sj.id as sjid,
                    fj.id as fjid,
                    pcs.id as pcsid,
                    sq.id as sqid
                FROM
                    jdrh.rhzf_ry_jwrk jw
                LEFT JOIN jdrh.blade_dept sq ON sq.id = jw.create_dept
                LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
                LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
                LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
                WHERE
                    jw.is_deleted = 0
                    and (sq.id = #{syjzryVO.deptId} :: int8 or sq.ancestors like '%' || #{syjzryVO.deptId} || '%')
                UNION ALL
                SELECT
                    jz.ry_xm AS ryXm,
                    jz.dzbm AS dzbm,
                    '寄住' AS ryType,
                    jz.ry_sfzh AS rySfzh,
                    jz.ry_dh AS ryDh,
                    jz.ry_hzgx AS ryHzgx,
                    jz.ry_hyzk AS ryHyzk,
                    jz.ry_whcd AS ryWhcd,
                    jz.ry_zzmm AS ryZzmm,
                    jz.ry_byzk AS ryByzk,
                    jz.ry_ts_lb AS ryTsLb,
                    jz.ry_ts_jtqk AS ryTsJtqk,
                    jz.bz AS bz,
                    jz.create_user,
                    jz.create_time,
                    jz.update_user,
                    jz.update_time,
                    sj.id as sjid,
                    fj.id as fjid,
                    pcs.id as pcsid,
                    sq.id as sqid
                FROM
                    jdrh.rhzf_ry_jzrk jz
                LEFT JOIN jdrh.blade_dept sq ON sq.id = jz.create_dept
                LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
                LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
                LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
                WHERE
                    jz.is_deleted = 0
                    and (sq.id = #{syjzryVO.deptId} :: int8 or sq.ancestors like '%' || #{syjzryVO.deptId} || '%')
                UNION ALL
                SELECT
                    wl.ry_xm AS ryXm,
                    wl.dzbm AS dzbm,
                    '未落' AS ryType,
                    wl.ry_sfzh AS rySfzh,
                    wl.ry_dh AS ryDh,
                    wl.ry_hzgx AS ryHzgx,
                    wl.ry_hyzk AS ryHyzk,
                    wl.ry_whcd AS ryWhcd,
                    wl.ry_zzmm AS ryZzmm,
                    wl.ry_byzk AS ryByzk,
                    wl.ry_ts_lb AS ryTsLb,
                    wl.ry_ts_jtqk AS ryTsJtqk,
                    wl.bz AS bz,
                    wl.create_user,
                    wl.create_time,
                    wl.update_user,
                    wl.update_time,
                    sj.id as sjid,
                    fj.id as fjid,
                    pcs.id as pcsid,
                    sq.id as sqid
                FROM
                    jdrh.rhzf_ry_wlrk wl
                LEFT JOIN jdrh.blade_dept sq ON sq.id = wl.create_dept
                LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
                LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
                LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
                WHERE
                    wl.is_deleted = 0
                    and (sq.id = #{syjzryVO.deptId} :: int8 or sq.ancestors like '%' || #{syjzryVO.deptId} || '%')
                UNION ALL
                SELECT
                    zz.ry_xm AS ryXm,
                    zz.dzbm AS dzbm,
                    '暂住' AS ryType,
                    zz.ry_sfzh AS rySfzh,
                    zz.ry_dh AS ryDh,
                    zz.ry_hzgx AS ryHzgx,
                    zz.ry_hyzk AS ryHyzk,
                    zz.ry_whcd AS ryWhcd,
                    zz.ry_zzmm AS ryZzmm,
                    zz.ry_byzk AS ryByzk,
                    zz.ry_ts_lb AS ryTsLb,
                    zz.ry_ts_jtqk AS ryTsJtqk,
                    zz.bz AS bz,
                    zz.create_user,
                    zz.create_time,
                    zz.update_user,
                    zz.update_time,
                    sj.id as sjid,
                    fj.id as fjid,
                    pcs.id as pcsid,
                    sq.id as sqid
                FROM
                    jdrh.rhzf_ry_zzrk zz
                LEFT JOIN jdrh.blade_dept sq ON sq.id = zz.create_dept
                LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
                LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
                LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
                WHERE
                    zz.is_deleted = 0
                    and (sq.id = #{syjzryVO.deptId} :: int8 or sq.ancestors like '%' || #{syjzryVO.deptId} || '%')
            ) rys
        WHERE 1 = 1
        <if test="syjzryVO.ryType != null and syjzryVO.ryType != ''">
            and rys.ryType = #{syjzryVO.ryType}
        </if>
        <if test="syjzryVO.dzbm != null and syjzryVO.dzbm != ''">
            and rys.dzbm = #{syjzryVO.dzbm}
        </if>
        <if test="syjzryVO.ryXm != null and syjzryVO.ryXm != ''">
            and rys.ryXm = #{syjzryVO.ryXm}
        </if>
        <if test="syjzryVO.rySfzh != null and syjzryVO.rySfzh != ''">
            and rys.rySfzh = #{syjzryVO.rySfzh}
        </if>
        <if test="syjzryVO.ryTsLb != null and syjzryVO.ryTsLb != ''">
            and rys.ryTsLb like '%' || #{syjzryVO.ryTsLb} || '%'
        </if>
        <if test="syjzryVO.bz != null and syjzryVO.bz != ''">
            and rys.bz like '%' || #{syjzryVO.bz} || '%'
        </if>
        <if test="syjzryVO.sssjName != null and syjzryVO.sssjName != ''">
            and rys.sjid = #{syjzryVO.sssjName}
        </if>
        <if test="syjzryVO.ssfjName != null and syjzryVO.ssfjName != ''">
            and rys.fjid = #{syjzryVO.ssfjName}
        </if>
        <if test="syjzryVO.sspcsName != null and syjzryVO.sspcsName != ''">
            and rys.pcsid = #{syjzryVO.sspcsName}
        </if>
        <if test="syjzryVO.sssqName != null and syjzryVO.sssqName != ''">
            and rys.sqid = #{syjzryVO.sssqName}
        </if>
    </select>

    <select id="fwryList" resultType="org.springblade.modules.rhzf.fwInfo.vo.SyjzryVO">
        SELECT
        *
        FROM
        (
        SELECT
        cz.ry_xm AS ryXm,
        cz.dzbm AS dzbm,
        '常住' AS ryType,
        cz.ry_sfzh AS rySfzh,
        cz.ry_dh AS ryDh,
        cz.ry_hzgx AS ryHzgx,
        cz.ry_hyzk AS ryHyzk,
        cz.ry_whcd AS ryWhcd,
        cz.ry_zzmm AS ryZzmm,
        cz.ry_byzk AS ryByzk,
        cz.ry_ts_lb AS ryTsLb,
        cz.ry_ts_jtqk AS ryTsJtqk,
        cz.bz AS bz
        FROM
        jdrh.rhzf_ry_czrk cz
        LEFT JOIN jdrh.blade_dept sq ON sq.id = cz.create_dept
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
        WHERE
        cz.is_deleted = 0
        and (sq.id = #{deptId} :: int8 or sq.ancestors like '%' || #{deptId} || '%')
        UNION ALL
        SELECT
        jw.ry_mz || ' ' || jw.ry_xs AS ryXm,
        jw.dzbm AS dzbm,
        '境外' AS ryType,
        jw.ry_zjhm AS rySfzh,
        jw.ry_dh AS ryDh,
        '' AS ryHzgx,
        '' AS ryHyzk,
        '' AS ryWhcd,
        '' AS ryZzmm,
        '' AS ryByzk,
        jw.ry_ts_lb AS ryTsLb,
        jw.ry_ts_jtqk AS ryTsJtqk,
        jw.bz AS bz
        FROM
        jdrh.rhzf_ry_jwrk jw
        LEFT JOIN jdrh.blade_dept sq ON sq.id = jw.create_dept
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
        WHERE
        jw.is_deleted = 0
        and (sq.id = #{deptId} :: int8 or sq.ancestors like '%' || #{deptId} || '%')
        UNION ALL
        SELECT
        jz.ry_xm AS ryXm,
        jz.dzbm AS dzbm,
        '寄住' AS ryType,
        jz.ry_sfzh AS rySfzh,
        jz.ry_dh AS ryDh,
        jz.ry_hzgx AS ryHzgx,
        jz.ry_hyzk AS ryHyzk,
        jz.ry_whcd AS ryWhcd,
        jz.ry_zzmm AS ryZzmm,
        jz.ry_byzk AS ryByzk,
        jz.ry_ts_lb AS ryTsLb,
        jz.ry_ts_jtqk AS ryTsJtqk,
        jz.bz AS bz
        FROM
        jdrh.rhzf_ry_jzrk jz
        LEFT JOIN jdrh.blade_dept sq ON sq.id = jz.create_dept
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
        WHERE
        jz.is_deleted = 0
        and (sq.id = #{deptId} :: int8 or sq.ancestors like '%' || #{deptId} || '%')
        UNION ALL
        SELECT
        wl.ry_xm AS ryXm,
        wl.dzbm AS dzbm,
        '未落' AS ryType,
        wl.ry_sfzh AS rySfzh,
        wl.ry_dh AS ryDh,
        wl.ry_hzgx AS ryHzgx,
        wl.ry_hyzk AS ryHyzk,
        wl.ry_whcd AS ryWhcd,
        wl.ry_zzmm AS ryZzmm,
        wl.ry_byzk AS ryByzk,
        wl.ry_ts_lb AS ryTsLb,
        wl.ry_ts_jtqk AS ryTsJtqk,
        wl.bz AS bz
        FROM
        jdrh.rhzf_ry_wlrk wl
        LEFT JOIN jdrh.blade_dept sq ON sq.id = wl.create_dept
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
        WHERE
        wl.is_deleted = 0
        and (sq.id = #{deptId} :: int8 or sq.ancestors like '%' || #{deptId} || '%')
        UNION ALL
        SELECT
        zz.ry_xm AS ryXm,
        zz.dzbm AS dzbm,
        '暂住' AS ryType,
        zz.ry_sfzh AS rySfzh,
        zz.ry_dh AS ryDh,
        zz.ry_hzgx AS ryHzgx,
        zz.ry_hyzk AS ryHyzk,
        zz.ry_whcd AS ryWhcd,
        zz.ry_zzmm AS ryZzmm,
        zz.ry_byzk AS ryByzk,
        zz.ry_ts_lb AS ryTsLb,
        zz.ry_ts_jtqk AS ryTsJtqk,
        zz.bz AS bz
        FROM
        jdrh.rhzf_ry_zzrk zz
        LEFT JOIN jdrh.blade_dept sq ON sq.id = zz.create_dept
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
        WHERE
        zz.is_deleted = 0
        and (sq.id = #{deptId} :: int8 or sq.ancestors like '%' || #{deptId} || '%')
        ) rys
        WHERE rys.dzbm = #{dzbm}
    </select>

    <select id="exportRyxq" resultType="org.springblade.modules.rhzf.fwRy.excel.RyxqExcel">
        SELECT
        *
        FROM
        (
        SELECT
        cz.ry_xm AS ryXm,
        cz.dzbm AS dzbm,
        '常住' AS ryType,
        cz.ry_sfzh AS rySfzh,
        cz.ry_dh AS ryDh,
        cz.ry_hzgx AS ryHzgx,
        cz.ry_hyzk AS ryHyzk,
        cz.ry_whcd AS ryWhcd,
        cz.ry_zzmm AS ryZzmm,
        cz.ry_byzk AS ryByzk,
        cz.ry_ts_lb AS ryTsLb,
        cz.ry_ts_jtqk AS ryTsJtqk,
        cz.bz AS bz,
        cz.create_user,
        cz.create_time,
        cz.update_user,
        cz.update_time
        FROM
        jdrh.rhzf_ry_czrk cz
        LEFT JOIN jdrh.blade_dept sq ON sq.id = cz.create_dept
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
        WHERE
        cz.is_deleted = 0
        and (sq.id = #{params.deptId} :: int8 or sq.ancestors like '%' || #{params.deptId} || '%')
        <if test="params.ryXm != null and params.ryXm != ''">
            and cz.ry_xm = #{params.ryXm}
        </if>
        <if test="params.rySfzh != null and params.rySfzh != ''">
            and cz.ry_sfzh = #{params.rySfzh}
        </if>
        <if test="params.ryTsLb != null and params.ryTsLb != ''">
            and cz.ry_ts_lb like '%' || #{params.ryTsLb} || '%'
        </if>
        <if test="params.bz != null and params.bz != ''">
            and cz.bz like '%' || #{params.bz} || '%'
        </if>
        <if test="params.sssjName != null and params.sssjName != ''">
            and sj.id = #{params.sssjName}
        </if>
        <if test="params.ssfjName != null and params.ssfjName != ''">
            and fj.id = #{params.ssfjName}
        </if>
        <if test="params.sspcsName != null and params.sspcsName != ''">
            and pcs.id = #{params.sspcsName}
        </if>
        <if test="params.sssqName != null and params.sssqName != ''">
            and sq.id = #{params.sssqName}
        </if>
        UNION ALL
        SELECT
        jw.ry_mz || ' ' || jw.ry_xs AS ryXm,
        jw.dzbm AS dzbm,
        '境外' AS ryType,
        jw.ry_zjhm AS rySfzh,
        jw.ry_dh AS ryDh,
        '' AS ryHzgx,
        '' AS ryHyzk,
        '' AS ryWhcd,
        '' AS ryZzmm,
        '' AS ryByzk,
        jw.ry_ts_lb AS ryTsLb,
        jw.ry_ts_jtqk AS ryTsJtqk,
        jw.bz AS bz,
        jw.create_user,
        jw.create_time,
        jw.update_user,
        jw.update_time
        FROM
        jdrh.rhzf_ry_jwrk jw
        LEFT JOIN jdrh.blade_dept sq ON sq.id = jw.create_dept
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
        WHERE
        jw.is_deleted = 0
        and (sq.id = #{params.deptId} :: int8 or sq.ancestors like '%' || #{params.deptId} || '%')
        <if test="params.ryXm != null and params.ryXm != ''">
            and jw.ry_mz = #{params.ryXm}
        </if>
        <if test="params.rySfzh != null and params.rySfzh != ''">
            and jw.ry_zjhm = #{params.rySfzh}
        </if>
        <if test="params.ryTsLb != null and params.ryTsLb != ''">
            and jw.ry_ts_lb like '%' || #{params.ryTsLb} || '%'
        </if>
        <if test="params.bz != null and params.bz != ''">
            and jw.bz like '%' || #{params.bz} || '%'
        </if>
        <if test="params.sssjName != null and params.sssjName != ''">
            and sj.id = #{params.sssjName}
        </if>
        <if test="params.ssfjName != null and params.ssfjName != ''">
            and fj.id = #{params.ssfjName}
        </if>
        <if test="params.sspcsName != null and params.sspcsName != ''">
            and pcs.id = #{params.sspcsName}
        </if>
        <if test="params.sssqName != null and params.sssqName != ''">
            and sq.id = #{params.sssqName}
        </if>
        UNION ALL
        SELECT
        jz.ry_xm AS ryXm,
        jz.dzbm AS dzbm,
        '寄住' AS ryType,
        jz.ry_sfzh AS rySfzh,
        jz.ry_dh AS ryDh,
        jz.ry_hzgx AS ryHzgx,
        jz.ry_hyzk AS ryHyzk,
        jz.ry_whcd AS ryWhcd,
        jz.ry_zzmm AS ryZzmm,
        jz.ry_byzk AS ryByzk,
        jz.ry_ts_lb AS ryTsLb,
        jz.ry_ts_jtqk AS ryTsJtqk,
        jz.bz AS bz,
        jz.create_user,
        jz.create_time,
        jz.update_user,
        jz.update_time
        FROM
        jdrh.rhzf_ry_jzrk jz
        LEFT JOIN jdrh.blade_dept sq ON sq.id = jz.create_dept
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
        WHERE
        jz.is_deleted = 0
        and (sq.id = #{params.deptId} :: int8 or sq.ancestors like '%' || #{params.deptId} || '%')
        <if test="params.ryXm != null and params.ryXm != ''">
            and jz.ry_xm = #{params.ryXm}
        </if>
        <if test="params.rySfzh != null and params.rySfzh != ''">
            and jz.ry_sfzh = #{params.rySfzh}
        </if>
        <if test="params.ryTsLb != null and params.ryTsLb != ''">
            and jz.ry_ts_lb like '%' || #{params.ryTsLb} || '%'
        </if>
        <if test="params.bz != null and params.bz != ''">
            and jz.bz like '%' || #{params.bz} || '%'
        </if>
        <if test="params.sssjName != null and params.sssjName != ''">
            and sj.id = #{params.sssjName}
        </if>
        <if test="params.ssfjName != null and params.ssfjName != ''">
            and fj.id = #{params.ssfjName}
        </if>
        <if test="params.sspcsName != null and params.sspcsName != ''">
            and pcs.id = #{params.sspcsName}
        </if>
        <if test="params.sssqName != null and params.sssqName != ''">
            and sq.id = #{params.sssqName}
        </if>
        UNION ALL
        SELECT
        wl.ry_xm AS ryXm,
        wl.dzbm AS dzbm,
        '未落' AS ryType,
        wl.ry_sfzh AS rySfzh,
        wl.ry_dh AS ryDh,
        wl.ry_hzgx AS ryHzgx,
        wl.ry_hyzk AS ryHyzk,
        wl.ry_whcd AS ryWhcd,
        wl.ry_zzmm AS ryZzmm,
        wl.ry_byzk AS ryByzk,
        wl.ry_ts_lb AS ryTsLb,
        wl.ry_ts_jtqk AS ryTsJtqk,
        wl.bz AS bz,
        wl.create_user,
        wl.create_time,
        wl.update_user,
        wl.update_time
        FROM
        jdrh.rhzf_ry_wlrk wl
        LEFT JOIN jdrh.blade_dept sq ON sq.id = wl.create_dept
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
        WHERE
        wl.is_deleted = 0
        and (sq.id = #{params.deptId} :: int8 or sq.ancestors like '%' || #{params.deptId} || '%')
        <if test="params.ryXm != null and params.ryXm != ''">
            and wl.ry_xm = #{params.ryXm}
        </if>
        <if test="params.rySfzh != null and params.rySfzh != ''">
            and wl.ry_sfzh = #{params.rySfzh}
        </if>
        <if test="params.ryTsLb != null and params.ryTsLb != ''">
            and wl.ry_ts_lb like '%' || #{params.ryTsLb} || '%'
        </if>
        <if test="params.bz != null and params.bz != ''">
            and wl.bz like '%' || #{params.bz} || '%'
        </if>
        <if test="params.sssjName != null and params.sssjName != ''">
            and sj.id = #{params.sssjName}
        </if>
        <if test="params.ssfjName != null and params.ssfjName != ''">
            and fj.id = #{params.ssfjName}
        </if>
        <if test="params.sspcsName != null and params.sspcsName != ''">
            and pcs.id = #{params.sspcsName}
        </if>
        <if test="params.sssqName != null and params.sssqName != ''">
            and sq.id = #{params.sssqName}
        </if>
        UNION ALL
        SELECT
        zz.ry_xm AS ryXm,
        zz.dzbm AS dzbm,
        '暂住' AS ryType,
        zz.ry_sfzh AS rySfzh,
        zz.ry_dh AS ryDh,
        zz.ry_hzgx AS ryHzgx,
        zz.ry_hyzk AS ryHyzk,
        zz.ry_whcd AS ryWhcd,
        zz.ry_zzmm AS ryZzmm,
        zz.ry_byzk AS ryByzk,
        zz.ry_ts_lb AS ryTsLb,
        zz.ry_ts_jtqk AS ryTsJtqk,
        zz.bz AS bz,
        zz.create_user,
        zz.create_time,
        zz.update_user,
        zz.update_time
        FROM
        jdrh.rhzf_ry_zzrk zz
        LEFT JOIN jdrh.blade_dept sq ON sq.id = zz.create_dept
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj ON sj.id = fj.parent_id
        WHERE
        zz.is_deleted = 0
        and (sq.id = #{params.deptId} :: int8 or sq.ancestors like '%' || #{params.deptId} || '%')
        <if test="params.ryXm != null and params.ryXm != ''">
            and zz.ry_xm = #{params.ryXm}
        </if>
        <if test="params.rySfzh != null and params.rySfzh != ''">
            and zz.ry_sfzh = #{params.rySfzh}
        </if>
        <if test="params.ryTsLb != null and params.ryTsLb != ''">
            and zz.ry_ts_lb like '%' || #{params.ryTsLb} || '%'
        </if>
        <if test="params.bz != null and params.bz != ''">
            and zz.bz like '%' || #{params.bz} || '%'
        </if>
        <if test="params.sssjName != null and params.sssjName != ''">
            and sj.id = #{params.sssjName}
        </if>
        <if test="params.ssfjName != null and params.ssfjName != ''">
            and fj.id = #{params.ssfjName}
        </if>
        <if test="params.sspcsName != null and params.sspcsName != ''">
            and pcs.id = #{params.sspcsName}
        </if>
        <if test="params.sssqName != null and params.sssqName != ''">
            and sq.id = #{params.sssqName}
        </if>
        ) rys
        WHERE 1 = 1
        <if test="params.ryType != null and params.ryType != ''"> and rys.ryType = #{params.ryType}</if>
        <if test="params.dzbm != null and params.dzbm != ''"> and rys.dzbm = #{params.dzbm}</if>
    </select>
</mapper>
