<?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.mapper.BzdzMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="blogResultMap" type="org.springblade.modules.entity.BzdzEntity">
        <result column="id" property="id"/>
    </resultMap>

    <select id="getJzwList" resultType="org.springblade.modules.vo.LdBzdzVO">
        SELECT tem.*, mph.jlxmc AS jlxxqzdzrcmc, mph.sqjcwhmc FROM (
        SELECT
        ld.xlh AS ldphxlh,
        ld.mphxlh,
        ld.mlph,
        ld.ldph,
        ld.pointx AS dzjd,
        ld.pointy AS dzwd
        FROM
        sspt.rk_ldphxx ld
        WHERE
        ld.cxbs = 0
        <if test="params.yhjh != null and params.yhjh != ''">
            AND ld.cjrjh = #{params.yhjh}
        </if>
        AND ld.pcsjgdm = #{params.gajgdm}
        ) tem
        LEFT JOIN sspt.rk_mphxx mph ON (mph.xlh = tem.mphxlh AND mph.cxbs = 0)
        ORDER BY COALESCE(NULLIF(regexp_replace( tem.ldph, '[^0-9]', '', 'g' ), '') , '0') :: int8
    </select>

    <select id="getDyList" resultType="org.springblade.modules.vo.DyVO">
        SELECT
            CASE WHEN dyh IS NULL THEN '平房' ELSE dyh END AS dyh
        FROM
            sspt.rk_dzxx
        WHERE
            ldphxlh = #{params.ldphxlh} :: int8
        AND lcnpmh IS NOT NULL
        GROUP BY
            dyh
        ORDER BY
            dyh
    </select>

    <select id="getLcList" resultType="org.springblade.modules.vo.LcVO">
        SELECT
        CASE WHEN lch IS NULL THEN '平房' ELSE lch END AS lch
        FROM
        sspt.rk_dzxx
        WHERE
        ldphxlh = #{params.ldphxlh} :: int8
        AND lcnpmh IS NOT NULL
        <if test="null != params.dyh and '平房' == params.dyh">
            AND dyh IS NULL
        </if>
        <if test="null != params.dyh and '平房' != params.dyh">
            AND dyh = #{params.dyh}
        </if>
        GROUP BY
        lch
        ORDER BY
        regexp_replace( lch, '[^0-9]', '', 'g' ) :: int8
    </select>

    <select id="getFjList" resultType="org.springblade.modules.vo.FjVO">
        SELECT
        bzdz.dzbm,
        bzdz.lcnpmh,
        ( SELECT COUNT ( * ) FROM jdrh.rhzf_fw_ry fwry WHERE fwry.is_deleted = 0 AND fwry.fw_id = fw.id ) AS personNum,
        fw.id,
        fw.fw_syyt,
        ( SELECT COUNT ( 0 ) FROM jdrh.rhzf_fw_info rfi WHERE rfi.is_deleted = 0 AND rfi.dzbm = bzdz.dzbm) AS syfwNum,
        ( SELECT COUNT ( 0 ) FROM jdrh.ybss_sydw sydw WHERE sydw.is_deleted = 0 AND sydw.dzbm = bzdz.dzbm) AS sydwNum
        FROM
        sspt.rk_dzxx bzdz
        LEFT JOIN jdrh.rhzf_fw_info fw ON fw.dzbm = bzdz.dzbm AND fw.is_deleted = 0
        WHERE
        bzdz.ldphxlh = #{params.ldphxlh} :: int8
        AND bzdz.lcnpmh IS NOT NULL
        <if test="null != params.dyh and '平房' == params.dyh">
            AND bzdz.dyh IS NULL
        </if>
        <if test="null != params.dyh and '平房' != params.dyh">
            AND bzdz.dyh = #{params.dyh}
        </if>
        <if test="null != params.lch and '平房' == params.lch">
            AND bzdz.lch IS NULL
        </if>
        <if test="null != params.lch and '平房' != params.lch">
            AND bzdz.lch = #{params.lch}
        </if>
        <if test="params.type == 'syfw'">
            AND ( SELECT COUNT ( 0 ) FROM jdrh.rhzf_fw_info rfi WHERE rfi.is_deleted = 0 AND rfi.dzbm = bzdz.dzbm) != 0
        </if>
        <if test="params.type == 'sydw'">
            AND ( SELECT COUNT ( 0 ) FROM jdrh.ybss_sydw sydw WHERE sydw.is_deleted = 0 AND sydw.dzbm = bzdz.dzbm) != 0
        </if>
        ORDER BY
        COALESCE(NULLIF(regexp_replace( bzdz.lcnpmh, '[^0-9]', '', 'g' ), '') , '0') :: int8
    </select>

    <select id="bzdzDetail" resultType="org.springblade.modules.entity.BzdzEntity">
        select
            rd.xz,
            rfi.id,
            rfi.dzbm,
            rfi.fw_lb || '|' || rfi.fw_yt || '|' || rfi.fw_xz || '|' || rfi.fw_syyt as fwtd,
            rfi.fw_lb,
            rfi.fw_yt,
            rfi.fw_xz,
            rfi.fw_syyt,
            rfi.fw_mj,
            rfi.fw_js,
            rff.id as fzid,
            rff.fz_xm,
            rff.fz_sfzh,
            rff.fz_dh,
            rff.fz_dw,
            rff.fz_jzd,
            rff.fz_zp as fzZp,
            rff.bz,
            rff.hjdz,
            rff.zjzl,
            rff.sfzcjz
        from jdrh.rhzf_fw_info rfi
        left join sspt.rk_dzxx rd on rfi.dzbm = rd.dzbm
        left join jdrh.rhzf_fw_fz rff on rfi.id = rff.fw_id and rff.is_deleted = 0
        where rfi.is_deleted = 0 and rfi.dzbm = #{params.dzbm}
    </select>

    <update id="fzUpdate">
        update jdrh.rhzf_fw_fz
        <set>
            <if test="fzXm != null and fzXm != ''">fz_xm = #{fzXm},</if>
            <if test="fzSfzh != null and fzSfzh != ''">fz_sfzh = #{fzSfzh},</if>
            <if test="fzDh != null and fzDh != ''">fz_dh = #{fzDh},</if>
            <if test="fzDw != null and fzDw != ''">fz_dw = #{fzDw},</if>
            <if test="fzJzd != null and fzJzd != ''">fz_jzd = #{fzJzd},</if>
            <if test="sfzcjz != null">sfzcjz = #{sfzcjz},</if>
            <if test="fzGj != null and fzGj != ''">fz_gj = #{fzGj},</if>
            <if test="fzYwx != null and fzYwx != ''">fz_ywx = #{fzYwx},</if>
            <if test="fzYwm != null and fzYwm != ''">fz_ywm = #{fzYwm},</if>
            <if test="fzZp != null and fzZp != ''">fz_zp = #{fzZp},</if>
            <if test="bz != null and bz != ''">bz = #{bz},</if>
            <if test="hjdz != null and hjdz != ''">hjdz = #{hjdz},</if>
            <if test="updateUser != null and updateUser != ''">update_user = #{updateUser},</if>
            update_time = now()
        </set>
        where id = #{fzid}
    </update>

    <select id="selectByFzId" resultType="org.springblade.modules.entity.FwFzEntity">
        select * from jdrh.rhzf_fw_fz where is_deleted = 0 and id = #{fzid}
    </select>

    <insert id="fzlsSave">
        INSERT INTO jdrh.rhzf_fz_ls ("id", "fz_xm", "fz_sfzh", "fz_dh", "fz_dw", "fz_jzd", "create_user", "create_dept", "create_time")
        VALUES (#{fzid}, #{fzXm}, #{fzSfzh}, #{fzDh}, #{fzDw}, #{fzJzd}, #{createUser}, #{createDept}, #{createTime});
    </insert>

    <select id="selectFzLs" resultType="org.springblade.modules.vo.FzLsVO">
        select * from jdrh.rhzf_fz_ls where is_deleted = 1 and fw_id = #{id}
    </select>

    <select id="syrkDetail" resultType="org.springblade.modules.vo.SyjzryVO">
        SELECT
            *
        FROM
            (
                SELECT
                    cz.id,
                    cz.fw_id AS fwId,
                    cz.ry_xm AS ryXm,
                    cz.ry_xb AS ryXb,
                    cz.dzbm AS dzbm,
                    '常住人口' AS ryType,
                    cz.ry_sfzh AS rySfzh,
                    cz.ry_dh AS ryDh,
                    cz.ry_mz AS ryMz,
                    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.zjxy,
                    cz.ry_ts_lb AS ryTsLb,
                    cz.ry_ts_jtqk AS ryTsJtqk,
                    cz.ry_mz_dm AS ryMzDm,
                    cz.ry_hyzk_dm AS ryHyzkDm,
                    cz.ry_whcd_dm AS ryWhcdDm,
                    cz.ry_zzmm_dm AS ryZzmmDm,
                    cz.ry_byzk_dm AS ryByzkDm,
                    cz.zjxy_dm AS zjxyDm,
                    cz.dw_mc AS dwMc,
                    cz.dw_lb AS dwLb,
                    cz.dw_dz AS dwDz,
                    cz.dw_xz AS dwXz,
                    cz.vx,
                    cz.ry_zp AS ryZp,
                    '' AS ryZzsy,
                    '' AS ryZzsj,
                    '' AS ryLbdrq,
                    '' AS ryHjdlb,
                    '' AS ryZzdlb,
                    '' AS ryLb,
                    '' AS ryXs,
                    '' AS ryYwm,
                    '' AS ryGjdq,
                    '' AS ryFwcs,
                    '' AS ryCsdz,
                    '' AS ryZjlb,
                    '' AS ryZjhm,
                    '' AS ryZjqsrq,
                    '' AS ryZjjzrq,
                    '' AS ryQzlb,
                    '' AS ryQzhm,
                    '' AS ryLhsy,
                    '' AS ryRzrq,
                    '' AS ryLkrq,
                    cz.status,
                    cz.update_dept AS updateDept,
                    cz.new_dzbm AS newDzbm
                FROM
                    jdrh.rhzf_ry_czrk cz
                WHERE
                    cz.is_deleted = 0
                  and cz.dzbm = #{dzbm}
                UNION ALL
                SELECT
                    jw.id,
                    jw.fw_id AS fwId,
                    jw.ry_mz || ' ' || jw.ry_xs AS ryXm,
                    jw.ry_xb AS ryXb,
                    jw.dzbm AS dzbm,
                    '境外人口' AS ryType,
                    jw.ry_zjhm AS rySfzh,
                    jw.ry_dh AS ryDh,
                    '' AS ryMz,
                    '' AS ryHzgx,
                    '' AS ryHyzk,
                    '' AS ryWhcd,
                    '' AS ryZzmm,
                    '' AS ryByzk,
                    jw.zjxy,
                    jw.ry_ts_lb AS ryTsLb,
                    jw.ry_ts_jtqk AS ryTsJtqk,
                    '' AS ryMzDm,
                    '' AS ryHyzkDm,
                    '' AS ryWhcdDm,
                    '' AS ryZzmmDm,
                    '' AS ryByzkDm,
                    jw.zjxy_dm AS zjxyDm,
                    jw.dw_mc AS dwMc,
                    jw.dw_lb AS dwLb,
                    jw.dw_dz AS dwDz,
                    jw.dw_xz AS dwXz,
                    jw.vx,
                    jw.ry_zp AS ryZp,
                    '' AS ryZzsy,
                    '' AS ryZzsj,
                    '' AS ryLbdrq,
                    '' AS ryHjdlb,
                    '' AS ryZzdlb,
                    jw.ry_lb AS ryLb,
                    jw.ry_xs AS ryXs,
                    jw.ry_mz AS ryYwm,
                    jw.ry_gjdq AS ryGjdq,
                    jw.ry_fwcs AS ryFwcs,
                    jw.ry_csdz AS ryCsdz,
                    jw.ry_zjlb AS ryZjlb,
                    jw.ry_zjhm AS ryZjhm,
                    jw.ry_zjqsrq AS ryZjqsrq,
                    jw.ry_zjjzrq AS ryZjjzrq,
                    jw.ry_qzlb AS ryQzlb,
                    jw.ry_qzhm AS ryQzhm,
                    jw.ry_lhsy AS ryLhsy,
                    jw.ry_rzrq AS ryRzrq,
                    jw.ry_lkrq AS ryLkrq,
                    jw.status,
                    jw.update_dept AS updateDept,
                    jw.new_dzbm AS newDzbm
                FROM
                    jdrh.rhzf_ry_jwrk jw
                WHERE
                    jw.is_deleted = 0
                  and jw.dzbm = #{dzbm}
                UNION ALL
                SELECT
                    jz.id,
                    jz.fw_id AS fwId,
                    jz.ry_xm AS ryXm,
                    jz.ry_xb AS ryXb,
                    jz.dzbm AS dzbm,
                    '寄住人口' AS ryType,
                    jz.ry_sfzh AS rySfzh,
                    jz.ry_dh AS ryDh,
                    jz.ry_mz AS ryMz,
                    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.zjxy,
                    jz.ry_ts_lb AS ryTsLb,
                    jz.ry_ts_jtqk AS ryTsJtqk,
                    jz.ry_mz_dm AS ryMzDm,
                    jz.ry_hyzk_dm AS ryHyzkDm,
                    jz.ry_whcd_dm AS ryWhcdDm,
                    jz.ry_zzmm_dm AS ryZzmmDm,
                    jz.ry_byzk_dm AS ryByzkDm,
                    jz.zjxy_dm AS zjxyDm,
                    jz.dw_mc AS dwMc,
                    jz.dw_lb AS dwLb,
                    jz.dw_dz AS dwDz,
                    jz.dw_xz AS dwXz,
                    jz.vx,
                    jz.ry_zp AS ryZp,
                    '' AS ryZzsy,
                    '' AS ryZzsj,
                    '' AS ryLbdrq,
                    '' AS ryHjdlb,
                    '' AS ryZzdlb,
                    '' AS ryLb,
                    '' AS ryXs,
                    '' AS ryYwm,
                    '' AS ryGjdq,
                    '' AS ryFwcs,
                    '' AS ryCsdz,
                    '' AS ryZjlb,
                    '' AS ryZjhm,
                    '' AS ryZjqsrq,
                    '' AS ryZjjzrq,
                    '' AS ryQzlb,
                    '' AS ryQzhm,
                    '' AS ryLhsy,
                    '' AS ryRzrq,
                    '' AS ryLkrq,
                    jz.status,
                    jz.update_dept AS updateDept,
                    jz.new_dzbm AS newDzbm
                FROM
                    jdrh.rhzf_ry_jzrk jz
                WHERE
                    jz.is_deleted = 0
                  and jz.dzbm = #{dzbm}
                UNION ALL
                SELECT
                    wl.id,
                    wl.fw_id AS fwId,
                    wl.ry_xm AS ryXm,
                    wl.ry_xb AS ryXb,
                    wl.dzbm AS dzbm,
                    '未落人口' AS ryType,
                    wl.ry_sfzh AS rySfzh,
                    wl.ry_dh AS ryDh,
                    wl.ry_mz AS ryMz,
                    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.zjxy,
                    wl.ry_ts_lb AS ryTsLb,
                    wl.ry_ts_jtqk AS ryTsJtqk,
                    wl.ry_mz_dm AS ryMzDm,
                    wl.ry_hyzk_dm AS ryHyzkDm,
                    wl.ry_whcd_dm AS ryWhcdDm,
                    wl.ry_zzmm_dm AS ryZzmmDm,
                    wl.ry_byzk_dm AS ryByzkDm,
                    wl.zjxy_dm AS zjxyDm,
                    wl.dw_mc AS dwMc,
                    wl.dw_lb AS dwLb,
                    wl.dw_dz AS dwDz,
                    wl.dw_xz AS dwXz,
                    wl.vx,
                    wl.ry_zp AS ryZp,
                    '' AS ryZzsy,
                    '' AS ryZzsj,
                    '' AS ryLbdrq,
                    '' AS ryHjdlb,
                    '' AS ryZzdlb,
                    '' AS ryLb,
                    '' AS ryXs,
                    '' AS ryYwm,
                    '' AS ryGjdq,
                    '' AS ryFwcs,
                    '' AS ryCsdz,
                    '' AS ryZjlb,
                    '' AS ryZjhm,
                    '' AS ryZjqsrq,
                    '' AS ryZjjzrq,
                    '' AS ryQzlb,
                    '' AS ryQzhm,
                    '' AS ryLhsy,
                    '' AS ryRzrq,
                    '' AS ryLkrq,
                    wl.status,
                    wl.update_dept AS updateDept,
                    wl.new_dzbm AS newDzbm
                FROM
                    jdrh.rhzf_ry_wlrk wl
                WHERE
                    wl.is_deleted = 0
                  and wl.dzbm = #{dzbm}
                UNION ALL
                SELECT
                    zz.id,
                    zz.fw_id AS fwId,
                    zz.ry_xm AS ryXm,
                    zz.ry_xb AS ryXb,
                    zz.dzbm AS dzbm,
                    '暂住人口' AS ryType,
                    zz.ry_sfzh AS rySfzh,
                    zz.ry_dh AS ryDh,
                    zz.ry_mz AS ryMz,
                    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.zjxy,
                    zz.ry_ts_lb AS ryTsLb,
                    zz.ry_ts_jtqk AS ryTsJtqk,
                    zz.ry_mz_dm AS ryMzDm,
                    zz.ry_hyzk_dm AS ryHyzkDm,
                    zz.ry_whcd_dm AS ryWhcdDm,
                    zz.ry_zzmm_dm AS ryZzmmDm,
                    zz.ry_byzk_dm AS ryByzkDm,
                    zz.zjxy_dm AS zjxyDm,
                    zz.dw_mc AS dwMc,
                    zz.dw_lb AS dwLb,
                    zz.dw_dz AS dwDz,
                    zz.dw_xz AS dwXz,
                    zz.vx,
                    zz.ry_zp AS ryZp,
                    zz.ry_zzsy AS ryZzsy,
                    zz.ry_zzsj AS ryZzsj,
                    zz.ry_lbdrq AS ryLbdrq,
                    zz.ry_hjdlb AS ryHjdlb,
                    zz.ry_zzdlb AS ryZzdlb,
                    '' AS ryLb,
                    '' AS ryXs,
                    '' AS ryYwm,
                    '' AS ryGjdq,
                    '' AS ryFwcs,
                    '' AS ryCsdz,
                    '' AS ryZjlb,
                    '' AS ryZjhm,
                    '' AS ryZjqsrq,
                    '' AS ryZjjzrq,
                    '' AS ryQzlb,
                    '' AS ryQzhm,
                    '' AS ryLhsy,
                    '' AS ryRzrq,
                    '' AS ryLkrq,
                    zz.status,
                    zz.update_dept AS updateDept,
                    zz.new_dzbm AS newDzbm
                FROM
                    jdrh.rhzf_ry_zzrk zz
                WHERE
                    zz.is_deleted = 0
                  and zz.dzbm = #{dzbm}
            ) rys
    </select>

    <select id="syrkLsDetail" resultType="org.springblade.modules.vo.SyjzryVO">
        select * from jdrh.rhzf_ry_ls where is_deleted = 1 and dzbm = #{dzbm}
    </select>

    <select id="getFwIsExist" resultType="org.springblade.modules.entity.FwInfoEntity">
        SELECT
            id,
            times
        FROM
            jdrh.rhzf_fw_info
        WHERE
            is_deleted = 0
          AND dzbm = #{dzbm}
    </select>

    <update id="czrkZx" parameterType="org.springblade.modules.vo.SyrkzxVO">
        UPDATE jdrh.rhzf_ry_czrk
        <set>
            is_deleted = 1,
            update_time = now(),
            zxyy = #{zxyy},
            zxrq = #{zxrq},
            bcyy = #{bcyy},
            bcrq = #{bcrq},
            qwdzssxq = #{qwdzssxq},
            qwdzxz = #{qwdzxz},
        </set>
        WHERE id = #{id}
    </update>

    <update id="jwrkZx" parameterType="org.springblade.modules.vo.SyrkzxVO">
        UPDATE jdrh.rhzf_ry_jwrk
        <set>
            is_deleted = 1,
            update_time = now(),
            zxyy = #{zxyy},
            zxrq = #{zxrq},
            qwdzssxq = #{qwdzssxq},
            qwdzxz = #{qwdzxz},
        </set>
        WHERE id = #{id}
    </update>

    <update id="jzrkZx" parameterType="org.springblade.modules.vo.SyrkzxVO">
        UPDATE jdrh.rhzf_ry_jzrk
        <set>
            is_deleted = 1,
            update_time = now(),
            zxyy = #{zxyy},
            zxrq = #{zxrq},
            bcyy = #{bcyy},
            bcrq = #{bcrq},
            qwdzssxq = #{qwdzssxq},
            qwdzxz = #{qwdzxz},
        </set>
        WHERE id = #{id}
    </update>

    <update id="wlrkZx" parameterType="org.springblade.modules.vo.SyrkzxVO">
        UPDATE jdrh.rhzf_ry_wlrk
        <set>
            is_deleted = 1,
            update_time = now(),
            zxyy = #{zxyy},
            zxrq = #{zxrq},
            qwdzssxq = #{qwdzssxq},
            qwdzxz = #{qwdzxz},
        </set>
        WHERE id = #{id}
    </update>

    <update id="zzrkZx" parameterType="org.springblade.modules.vo.SyrkzxVO">
        UPDATE jdrh.rhzf_ry_zzrk
        <set>
            is_deleted = 1,
            update_time = now(),
            zxyy = #{zxyy},
            zxrq = #{zxrq},
            bcrq = #{bcrq},
            qwdzssxq = #{qwdzssxq},
            qwdzxz = #{qwdzxz},
        </set>
        WHERE id = #{id}
    </update>

    <insert id="ryLsSave">
        insert into jdrh.rhzf_ry_ls(id, fw_id, ry_type, dzbm, ry_xm, ry_xb, ry_sfzh, ry_lxdh, ry_ts_lb, create_user, create_dept, create_time)
        values (#{id}, #{fwId}, #{ryType}, #{dzbm}, #{ryXm}, #{ryXb}, #{rySfzh}, #{ryLxdh}, #{ryTsLb}, #{createUser}, #{createDept}, now())
    </insert>

    <select id="selectSydwByDzbm" resultType="org.springblade.modules.entity.SydwEntity">
        select * from jdrh.ybss_sydw where is_deleted = 0 and dzbm = #{dzbm}
    </select>

    <select id="selectFw" resultType="org.springblade.modules.entity.BzdzEntity">
        select id from jdrh.rhzf_fw_info where is_deleted = 0 and dzbm = #{dzbm}
    </select>

    <select id="sydwDetail" resultType="org.springblade.modules.entity.SydwEntity">
        select * from jdrh.ybss_sydw where is_deleted = 0 and dzbm = #{dzbm}
    </select>

    <select id="selectCyryByDzbm" resultType="org.springblade.modules.entity.CyryEntity">
        select * from jdrh.ybss_cyry where is_deleted = 0 and tyshxydm = #{tyshxydm}
    </select>

    <update id="fzzx">
        update jdrh.rhzf_fw_fz set is_deleted = 1, update_time = now() where id = #{id}
    </update>

    <insert id="fzzxlsSave">
        insert into jdrh.rhzf_fz_ls
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != id and '' != id">id,</if>
            <if test="null != fwId and '' != fwId">fw_id,</if>
            <if test="null != fzXm and '' != fzXm">fz_xm,</if>
            <if test="null != fzSfzh and '' != fzSfzh">fz_sfzh,</if>
            <if test="null != fzDh and '' != fzDh">fz_dh,</if>
            <if test="null != fzDw and '' != fzDw">fz_dw,</if>
            <if test="null != fzJzd and '' != fzJzd">fz_jzd,</if>
            <if test="null != sfzcjz and '' != sfzcjz">sfzcjz,</if>
            <if test="null != isDeleted and '' != isDeleted">is_deleted,</if>
            <if test="null != bz and '' != bz">bz,</if>
            <if test="null != hjdz and '' != hjdz">hjdz,</if>
            <if test="null != zjzl and '' != zjzl">zjzl,</if>
            <if test="null != fzGj and '' != fzGj">fz_gj,</if>
            <if test="null != fzYwx and '' != fzYwx">fz_ywx,</if>
            <if test="null != fzYwm and '' != fzYwm">fz_ywm,</if>
            <if test="null != fzZp and '' != fzZp">fz_zp,</if>
            <if test="null != createUser and '' != createUser">create_user,</if>
            <if test="null != createDept and '' != createDept">create_dept,</if>
            create_time,
            update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="null != id and '' != id">#{id},</if>
            <if test="null != fwId and '' != fwId">#{fwId},</if>
            <if test="null != fzXm and '' != fzXm">#{fzXm},</if>
            <if test="null != fzSfzh and '' != fzSfzh">#{fzSfzh},</if>
            <if test="null != fzDh and '' != fzDh">#{fzDh},</if>
            <if test="null != fzDw and '' != fzDw">#{fzDw},</if>
            <if test="null != fzJzd and '' != fzJzd">#{fzJzd},</if>
            <if test="null != sfzcjz and '' != sfzcjz">#{sfzcjz},</if>
            <if test="null != isDeleted and '' != isDeleted">#{isDeleted},</if>
            <if test="null != bz and '' != bz">#{bz},</if>
            <if test="null != hjdz and '' != hjdz">#{hjdz},</if>
            <if test="null != zjzl and '' != zjzl">#{zjzl},</if>
            <if test="null != fzGj and '' != fzGj">#{fzGj},</if>
            <if test="null != fzYwx and '' != fzYwx">#{fzYwx},</if>
            <if test="null != fzYwm and '' != fzYwm">#{fzYwm},</if>
            <if test="null != fzZp and '' != fzZp">#{fzZp},</if>
            <if test="null != createUser and '' != createUser">#{createUser},</if>
            <if test="null != createDept and '' != createDept">#{createDept},</if>
            now(),
            now()
        </trim>
    </insert>

    <insert id="saveCyry">
        insert into jdrh.ybss_cyry
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != id and '' != id">id,</if>
            <if test="null != xm and '' != xm">xm,</if>
            <if test="null != xb and '' != xb">xb,</if>
            <if test="null != xz and '' != xz">xz,</if>
            <if test="null != dwdzbm and '' != dwdzbm">dwdzbm,</if>
            <if test="null != tyshxydm and '' != tyshxydm">tyshxydm,</if>
            <if test="null != zy and '' != zy">zy,</if>
            <if test="null != gmsfhm and '' != gmsfhm">gmsfhm,</if>
            <if test="null != zylb and '' != zylb">zylb,</if>
            <if test="null != lxdh and '' != lxdh">lxdh,</if>
            <if test="null != sjgxbs and '' != sjgxbs">sjgxbs,</if>
            <if test="null != sjgxsj and '' != sjgxsj">sjgxsj,</if>
            <if test="null != sjly and '' != sjly">sjly,</if>
            <if test="null != djrjh and '' != djrjh">djrjh,</if>
            <if test="null != djrxm and '' != djrxm">djrxm,</if>
            <if test="null != djrsfzh and '' != djrsfzh">djrsfzh,</if>
            <if test="null != zxyy and '' != zxyy">zxyy,</if>
            <if test="null != createUser and '' != createUser">create_user,</if>
            <if test="null != createDept and '' != createDept">create_dept,</if>
            <if test="null != createTime and '' != createTime">create_time,</if>
            <if test="null != updateTime and '' != updateTime">update_time,</if>
            <if test="null != isDeleted and '' != isDeleted">is_deleted,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="null != id and '' != id">#{id},</if>
            <if test="null != xm and '' != xm">#{xm},</if>
            <if test="null != xb and '' != xb">#{xb},</if>
            <if test="null != xz and '' != xz">#{xz},</if>
            <if test="null != dwdzbm and '' != dwdzbm">#{dwdzbm},</if>
            <if test="null != tyshxydm and '' != tyshxydm">#{tyshxydm},</if>
            <if test="null != zy and '' != zy">#{zy},</if>
            <if test="null != gmsfhm and '' != gmsfhm">#{gmsfhm},</if>
            <if test="null != zylb and '' != zylb">#{zylb},</if>
            <if test="null != lxdh and '' != lxdh">#{lxdh},</if>
            <if test="null != sjgxbs and '' != sjgxbs">#{sjgxbs},</if>
            <if test="null != sjgxsj and '' != sjgxsj">#{sjgxsj},</if>
            <if test="null != sjly and '' != sjly">#{sjly},</if>
            <if test="null != djrjh and '' != djrjh">#{djrjh},</if>
            <if test="null != djrxm and '' != djrxm">#{djrxm},</if>
            <if test="null != djrsfzh and '' != djrsfzh">#{djrsfzh},</if>
            <if test="null != zxyy and '' != zxyy">#{zxyy},</if>
            <if test="null != createUser and '' != createUser">#{createUser},</if>
            <if test="null != createDept and '' != createDept">#{createDept},</if>
            <if test="null != createTime and '' != createTime">#{createTime},</if>
            <if test="null != updateTime and '' != updateTime">#{updateTime},</if>
            <if test="null != isDeleted and '' != isDeleted">#{isDeleted},</if>
        </trim>
    </insert>

    <update id="updateSydwzx">
        update jdrh.ybss_sydw set zxyy = #{sydw.zxyy}, is_deleted = 1, update_user = #{userId}, update_time = now() where id = #{sydw.id}
    </update>

    <update id="updateCyryzx">
        update jdrh.ybss_cyry set zxyy = #{sydw.zxyy}, is_deleted = 1, update_user = #{userId}, update_time = now() where dwdzbm = #{sydw.dzbm}
    </update>

    <select id="sydwLsjl" resultType="org.springblade.modules.entity.SydwEntity">
        select * from jdrh.ybss_sydw where dzbm = #{dzbm} and is_deleted = 1
    </select>

    <select id="cyryLsjl" resultType="org.springblade.modules.entity.CyryEntity">
        select distinct * from jdrh.ybss_cyry where tyshxydm = #{dzbm} and is_deleted = 1
    </select>

    <select id="selectXxBySfzh" resultType="org.springblade.modules.entity.FwFzEntity">
        select ryxm as fzXm, rydh as fzDh, rydzxz as hjdz, gzdw as fzDw, ryzp as fzZp, ryxb, rydz  from sspt.ybss_xx where rysfzh = #{fzSfzh}
    </select>

    <select id="getFwxzByRkDzxx" resultType="java.lang.String">
        select xz from sspt.rk_dzxx where dzbm = #{dzbm} limit 1
    </select>

    <select id="selectDictBiz" resultType="java.lang.String">
        select dict_value from jdrh.blade_dict_biz where code = 'tsrq' and dict_key :: text = #{tsrq}
    </select>

    <select id="selectCzrkId" resultType="java.lang.Long">
        select id from jdrh.rhzf_ry_czrk where is_deleted = 0 and fw_id = #{fwId} and dzbm = #{dzbm} and ry_sfzh = #{rySfzh}
    </select>

    <select id="selectJwrkId" resultType="java.lang.Long">
        select id from jdrh.rhzf_ry_jwrk where is_deleted = 0 and fw_id = #{fwId} and dzbm = #{dzbm} and ry_zjhm = #{ryZjhm}
    </select>

    <select id="selectJzrkId" resultType="java.lang.Long">
        select id from jdrh.rhzf_ry_jzrk where is_deleted = 0 and fw_id = #{fwId} and dzbm = #{dzbm} and ry_sfzh = #{rySfzh}
    </select>

    <select id="selectWlrkId" resultType="java.lang.Long">
        select id from jdrh.rhzf_ry_wlrk where is_deleted = 0 and fw_id = #{fwId} and dzbm = #{dzbm} and ry_sfzh = #{rySfzh}
    </select>

    <select id="selectZzrkId" resultType="java.lang.Long">
        select id from jdrh.rhzf_ry_zzrk where is_deleted = 0 and fw_id = #{fwId} and dzbm = #{dzbm} and ry_sfzh = #{rySfzh}
    </select>

    <update id="updateFwRyzxById">
        update jdrh.rhzf_fw_ry set is_deleted = 1, update_user = #{updateUser}, update_time = now() where fw_id = #{fwId} and ry_id = #{id}
    </update>

    <select id="djxx" resultType="org.springblade.modules.vo.UserVO">
        select
            bu.sfzh,
            (select dept_name from jdrh.blade_dept where blade_dept.id = bu.dept_id :: int8) as sssq,
            (select dept_name from jdrh.blade_dept where blade_dept.id = bd.parent_id) as sspcs
        from blade_user bu
            left join jdrh.blade_dept bd on bu.dept_id :: int8 = bd.id
        where bu.is_deleted = 0 and bu.id = #{userId}
    </select>

    <select id="selectCyrylsByDzbm" resultType="org.springblade.modules.entity.CyryEntity">
        select * from jdrh.ybss_cyry where is_deleted = 1 and tyshxydm = #{tyshxydm}
    </select>

    <select id="selectFwInfo" resultType="java.lang.String">
        select dzbm from jdrh.rhzf_fw_info where is_deleted = 0 and create_dept = #{deptId} :: int8
    </select>

    <select id="selectDwDz" resultType="org.springblade.modules.vo.DwdzVO">
        SELECT
            rd.xz,
            rd.dzbm
        FROM
            sspt.rk_ldphxx rl
                LEFT JOIN blade_user bu ON bu.dept_id = rl.pcsjgdm
                LEFT JOIN sspt.rk_dzxx rd ON rl.xlh = rd.ldphxlh
        WHERE
            rd.dzbm = #{dz}
    </select>

    <select id="selectSyrkBySfzh" resultType="org.springblade.modules.entity.BzdzEntity">
        SELECT
            *
        FROM
            (
                SELECT
                    cz.fw_id,
                    cz.create_dept
                FROM
                    jdrh.rhzf_ry_czrk cz
                WHERE
                    cz.is_deleted = 0
                and cz.ry_sfzh = #{bzdzEntity.rySfzh}
                UNION ALL
                SELECT
                    jw.fw_id,
                    jw.create_dept
                FROM
                    jdrh.rhzf_ry_jwrk jw
                WHERE
                    jw.is_deleted = 0
                  and jw.ry_zjhm = #{bzdzEntity.rySfzh}
                UNION ALL
                SELECT
                    jz.fw_id,
                    jz.create_dept
                FROM
                    jdrh.rhzf_ry_jzrk jz
                WHERE
                    jz.is_deleted = 0
                  and jz.ry_sfzh = #{bzdzEntity.rySfzh}
                UNION ALL
                SELECT
                    wl.fw_id,
                    wl.create_dept
                FROM
                    jdrh.rhzf_ry_wlrk wl
                WHERE
                    wl.is_deleted = 0
                  and wl.ry_sfzh = #{bzdzEntity.rySfzh}
                UNION ALL
                SELECT
                    zz.fw_id,
                    zz.create_dept
                FROM
                    jdrh.rhzf_ry_zzrk zz
                WHERE
                    zz.is_deleted = 0
                  and zz.ry_sfzh = #{bzdzEntity.rySfzh}
            ) rys
    </select>

    <select id="selectPcsByCreateDept" resultType="org.springblade.modules.entity.BzdzEntity">
        select dept_name as createDeptName from jdrh.blade_dept where is_deleted = 0 and id = #{createDept}
    </select>

    <select id="selectCzrkBySfzh" resultType="org.springblade.modules.entity.BzdzEntity">
        select * from jdrh.rhzf_ry_czrk where is_deleted = 0 and ry_sfzh = #{rySfzh}
    </select>

    <select id="selectJwrkByZjhm" resultType="org.springblade.modules.entity.BzdzEntity">
        select * from jdrh.rhzf_ry_jwrk where is_deleted = 0 and ry_zjhm = #{ryZjhm}
    </select>

    <select id="selectJzrkBySfzh" resultType="org.springblade.modules.entity.BzdzEntity">
        select * from jdrh.rhzf_ry_jzrk where is_deleted = 0 and ry_sfzh = #{rySfzh}
    </select>

    <select id="selectWlrkBySfzh" resultType="org.springblade.modules.entity.BzdzEntity">
        select * from jdrh.rhzf_ry_wlrk where is_deleted = 0 and ry_sfzh = #{rySfzh}
    </select>

    <select id="selectZzrkBySfzh" resultType="org.springblade.modules.entity.BzdzEntity">
        select * from jdrh.rhzf_ry_zzrk where is_deleted = 0 and ry_sfzh = #{rySfzh}
    </select>

    <update id="czrkUpdate">
        update jdrh.rhzf_ry_czrk set status = 2, update_dept = #{updateDept}, new_dzbm = #{dzbm}, new_fw_id = #{fwId}, new_type = #{ryType} where ry_sfzh = #{rySfzh} and is_deleted = 0
    </update>

    <update id="jwrkUpdate">
        update jdrh.rhzf_ry_jwrk set status = 2, update_dept = #{updateDept}, new_dzbm = #{dzbm}, new_fw_id = #{fwId}, new_type = #{ryType} where ry_zjhm = #{rySfzh} and is_deleted = 0
    </update>

    <update id="jzrkUpdate">
        update jdrh.rhzf_ry_jzrk set status = 2, update_dept = #{updateDept}, new_dzbm = #{dzbm}, new_fw_id = #{fwId}, new_type = #{ryType} where ry_sfzh = #{rySfzh} and is_deleted = 0
    </update>

    <update id="wlrkUpdate">
        update jdrh.rhzf_ry_wlrk set status = 2, update_dept = #{updateDept}, new_dzbm = #{dzbm}, new_fw_id = #{fwId}, new_type = #{ryType} where ry_sfzh = #{rySfzh} and is_deleted = 0
    </update>

    <update id="zzrkUpdate">
        update jdrh.rhzf_ry_zzrk set status = 2, update_dept = #{updateDept}, new_dzbm = #{dzbm}, new_fw_id = #{fwId}, new_type = #{ryType} where ry_sfzh = #{rySfzh} and is_deleted = 0
    </update>

    <update id="updatePcsCzrk">
        update jdrh.rhzf_ry_czrk set status = 1, create_dept = #{updateDept} :: int8, dzbm = #{newDzbm}, fw_id = #{newFwId}, update_dept = null, new_dzbm = null, new_fw_id = null where id = #{id}
    </update>

    <update id="updatePcsJwrk">
        update jdrh.rhzf_ry_jwrk set status = 1, create_dept = #{updateDept} :: int8, dzbm = #{newDzbm}, fw_id = #{newFwId}, update_dept = null, new_dzbm = null, new_fw_id = null where id = #{id}
    </update>

    <update id="updatePcsJzrk">
        update jdrh.rhzf_ry_jzrk set status = 1, create_dept = #{updateDept} :: int8, dzbm = #{newDzbm}, fw_id = #{newFwId}, update_dept = null, new_dzbm = null, new_fw_id = null where id = #{id}
    </update>

    <update id="updatePcsWlrk">
        update jdrh.rhzf_ry_wlrk set status = 1, create_dept = #{updateDept} :: int8, dzbm = #{newDzbm}, fw_id = #{newFwId}, update_dept = null, new_dzbm = null, new_fw_id = null where id = #{id}
    </update>

    <update id="updatePcsZzrk">
        update jdrh.rhzf_ry_zzrk set status = 1, create_dept = #{updateDept} :: int8, dzbm = #{newDzbm}, fw_id = #{newFwId}, update_dept = null, new_dzbm = null, new_fw_id = null where id = #{id}
    </update>

    <select id="selectDept" resultType="java.lang.String">
        select dept_name as updateDeptName from jdrh.blade_dept where is_deleted = 0 and id :: text = #{updateDept}
    </select>

    <update id="updateFwRy">
        update jdrh.rhzf_fw_ry set is_deleted = 1 where ry_id = #{ryId}
    </update>

    <select id="selectDeptById" resultType="org.springblade.modules.entity.Dept">
        select
            bd.id as pcsId,
            fj.id as fjId,
            sj.id as sjId,
            bd.dept_category
        from
        jdrh.blade_dept bd
        left join jdrh.blade_dept fj on bd.parent_id = fj.id
        left join jdrh.blade_dept sj on fj.parent_id = sj.id
        where bd.is_deleted = 0 and bd.id = #{deptId} :: int8
    </select>

    <select id="selectRyxxBySfzh" resultType="org.springblade.modules.entity.BzdzEntity">
        select rd.pcsjgdm, rd.dqm from sspt.ybss_xx yx left join sspt.rk_dzxx rd on yx.rydz = rd.dzbm where rysfzh = #{rySfzh}
    </select>

    <select id="cyrycx" resultType="org.springblade.modules.entity.BzdzEntity">
        SELECT
            *
        FROM
            (
                SELECT
                    cz.ry_xm as fzXm,
                    cz.ry_xb,
                    cz.ry_dh as fzDh,
                    cz.dzbm as rydz
                FROM
                    jdrh.rhzf_ry_czrk cz
                WHERE
                    cz.is_deleted = 0
                  and cz.ry_sfzh = #{bzdzEntity.fzSfzh}
                UNION ALL
                SELECT
                    jw.ry_xs || jw.ry_mz as fzXm,
                    jw.ry_xb,
                    jw.ry_dh as fzDh,
                    jw.dzbm as rydz
                FROM
                    jdrh.rhzf_ry_jwrk jw
                WHERE
                    jw.is_deleted = 0
                  and jw.ry_zjhm = #{bzdzEntity.fzSfzh}
                UNION ALL
                SELECT
                    jz.ry_xm as fzXm,
                    jz.ry_xb,
                    jz.ry_dh as fzDh,
                    jz.dzbm as rydz
                FROM
                    jdrh.rhzf_ry_jzrk jz
                WHERE
                    jz.is_deleted = 0
                  and jz.ry_sfzh = #{bzdzEntity.fzSfzh}
                UNION ALL
                SELECT
                    wl.ry_xm as fzXm,
                    wl.ry_xb,
                    wl.ry_dh as fzDh,
                    wl.dzbm as rydz
                FROM
                    jdrh.rhzf_ry_wlrk wl
                WHERE
                    wl.is_deleted = 0
                  and wl.ry_sfzh = #{bzdzEntity.fzSfzh}
                UNION ALL
                SELECT
                    zz.ry_xm as fzXm,
                    zz.ry_xb,
                    zz.ry_dh as fzDh,
                    zz.dzbm as rydz
                FROM
                    jdrh.rhzf_ry_zzrk zz
                WHERE
                    zz.is_deleted = 0
                  and zz.ry_sfzh = #{bzdzEntity.fzSfzh}
            ) rys
    </select>

    <select id="selectRkqy" resultType="org.springblade.modules.entity.BzdzEntity">
        SELECT
            *
        FROM
            (
                SELECT
                    cz.ry_xm,
                    cz.ry_xb,
                    cz.ry_sfzh,
                    cz.ry_dh,
                    bd.dept_name
                FROM
                    jdrh.rhzf_ry_czrk cz
                LEFT JOIN jdrh.blade_dept bd on cz.create_dept = bd.id
                WHERE
                    cz.is_deleted = 0
                and cz.status = 2
                and (bd.id :: text = any(string_to_array(#{deptId},',')) or bd.ancestors like '%' || #{deptId} || '%')
                UNION ALL
                SELECT
                    jw.ry_xs || jw.ry_mz as ryXm,
                    jw.ry_xb,
                    jw.ry_zjhm,
                    jw.ry_dh,
                    bd.dept_name
                FROM
                    jdrh.rhzf_ry_jwrk jw
                LEFT JOIN jdrh.blade_dept bd on jw.create_dept = bd.id
                WHERE
                    jw.is_deleted = 0
                and jw.status = 2
                and (bd.id :: text = any(string_to_array(#{deptId},',')) or bd.ancestors like '%' || #{deptId} || '%')
                UNION ALL
                SELECT
                    jz.ry_xm,
                    jz.ry_xb,
                    jz.ry_sfzh,
                    jz.ry_dh,
                    bd.dept_name
                FROM
                    jdrh.rhzf_ry_jzrk jz
                LEFT JOIN jdrh.blade_dept bd on jz.create_dept = bd.id
                WHERE
                    jz.is_deleted = 0
                and jz.status = 2
                and (bd.id :: text = any(string_to_array(#{deptId},',')) or bd.ancestors like '%' || #{deptId} || '%')
                UNION ALL
                SELECT
                    wl.ry_xm,
                    wl.ry_xb,
                    wl.ry_sfzh,
                    wl.ry_dh,
                    bd.dept_name
                FROM
                    jdrh.rhzf_ry_wlrk wl
                LEFT JOIN jdrh.blade_dept bd on wl.create_dept = bd.id
                WHERE
                    wl.is_deleted = 0
                and wl.status = 2
                and (bd.id :: text = any(string_to_array(#{deptId},',')) or bd.ancestors like '%' || #{deptId} || '%')
                UNION ALL
                SELECT
                    zz.ry_xm,
                    zz.ry_xb,
                    zz.ry_sfzh,
                    zz.ry_dh,
                    bd.dept_name
                FROM
                    jdrh.rhzf_ry_zzrk zz
                LEFT JOIN jdrh.blade_dept bd on zz.create_dept = bd.id
                WHERE
                    zz.is_deleted = 0
                and zz.status = 2
                and (bd.id :: text = any(string_to_array(#{deptId},',')) or bd.ancestors like '%' || #{deptId} || '%')
            ) rys
    </select>

    <update id="detelePcsCzrk">
        update jdrh.rhzf_ry_czrk set is_deleted = 1, status = 1, update_dept = null, new_dzbm = null, new_fw_id = null where id = #{id}
    </update>

    <update id="detelePcsJwrk">
        update jdrh.rhzf_ry_jwrk set is_deleted = 1, status = 1, update_dept = null, new_dzbm = null, new_fw_id = null where id = #{id}
    </update>

    <update id="detelePcsJzrk">
        update jdrh.rhzf_ry_jzrk set is_deleted = 1, status = 1, update_dept = null, new_dzbm = null, new_fw_id = null where id = #{id}
    </update>

    <update id="detelePcsWlrk">
        update jdrh.rhzf_ry_wlrk set is_deleted = 1, status = 1, update_dept = null, new_dzbm = null, new_fw_id = null where id = #{id}
    </update>

    <update id="detelePcsZzrk">
        update jdrh.rhzf_ry_zzrk set is_deleted = 1, status = 1, update_dept = null, new_dzbm = null, new_fw_id = null where id = #{id}
    </update>

    <select id="sydwlsDetail" resultType="org.springblade.modules.entity.SydwEntity">
        select * from jdrh.ybss_sydw where is_deleted = 1 and dzbm = #{dzbm}
    </select>

    <select id="fwIdByDzbm" resultType="java.lang.Long">
        select id from jdrh.rhzf_fw_info where is_deleted = 0 and dzbm = #{dzbm}
    </select>

    <select id="selectParentIdByDeptId" resultType="java.lang.Long">
        select parent_id from jdrh.blade_dept where id = #{deptId} :: int8
    </select>
</mapper>
