<?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.fwInfo.mapper.FwInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="fwInfoResultMap" type="org.springblade.modules.rhzf.fwInfo.entity.FwInfoEntity">
        <result column="id" property="id"/>
        <result column="dz_type" property="dzType"/>
        <result column="fw_lb" property="fwLb"/>
        <result column="fw_yt" property="fwYt"/>
        <result column="fw_xz" property="fwXz"/>
        <result column="fw_syyt" property="fwSyyt"/>
        <result column="fw_mj" property="fwMj"/>
        <result column="fw_js" property="fwJs"/>
        <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="getFwxzByRkDzxx" resultType="java.lang.String">
        select xz from sspt.rk_dzxx where dzbm = #{dzbm}  limit 1
    </select>

    <select id="selectFwInfoPage" resultType="org.springblade.modules.rhzf.fwInfo.vo.FwInfoVO">
        SELECT
            fw.*,
            us.NAME userName,
            us1.name updateName,
            fz.update_time as fzUpdateTime,
            dept.dept_name deptName,
            fz.fz_xm fzxm,
            fz.fz_dh fzDh,
            fz.tsqk tsqk,
            fz.fz_sfzh fzSfzh,
            (select count(1) from rhzf_fw_ry ry  where ry.dzbm = fw.dzbm and ry.is_deleted = 0  and ry.fw_id is not null) as fwrycount
        FROM
            rhzf_fw_info fw
        LEFT JOIN blade_dept dept ON dept.ID = fw.create_dept
        LEFT JOIN rhzf_fw_fz fz ON fz.fw_id = fw.ID
        LEFT JOIN blade_user us ON us.ID = fw.create_user and us.is_deleted = 0
        LEFT JOIN blade_user us1 ON us1.ID = fw.update_user and us1.is_deleted = 0
        WHERE
            fw.is_deleted = 0 and fz.is_deleted = 0
            AND ((#{fwInfo.deptId} LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{fwInfo.deptId} || '%'))
            <if test="fwInfo.fzxm != null and fwInfo.fzxm != ''">
                and fz.fz_xm like '%' || #{fwInfo.fzxm} || '%'
            </if>
            <if test="fwInfo.userName != null and fwInfo.userName != ''">
                and us.name like '%' || #{fwInfo.userName} || '%'
            </if>
            <if test="fwInfo.updateName != null and fwInfo.updateName != ''">
                and us1.name like '%' || #{fwInfo.updateName} || '%'
            </if>
            <if test="fwInfo.sssqName != null and fwInfo.sssqName != ''">
                and bz.sssq_bm::text = #{fwInfo.sssqName}
            </if>
            <if test="fwInfo.deptName != null and fwInfo.deptName != ''">
                and dept.id :: text = #{fwInfo.deptName}
            </if>
            <if test="null != fwInfo.ssfjName and '' != fwInfo.ssfjName">
                AND dept.ancestors :: text LIKE '%' || #{fwInfo.ssfjName} || '%'
            </if>
            <if test="null != fwInfo.sspcsName and '' != fwInfo.sspcsName">
                AND dept.ancestors :: text LIKE '%' || #{fwInfo.sspcsName} || '%'
            </if>
            <if test="null != fwInfo.sssjName and '' != fwInfo.sssjName">
                AND dept.ancestors :: text LIKE '%' || #{fwInfo.sssjName} || '%'
            </if>
            <if test="null != fwInfo.fwYt and '' != fwInfo.fwYt">
                AND fw.fw_yt LIKE '%' || #{fwInfo.fwYt} || '%'
            </if>
            <if test="null != fwInfo.fwSyyt and '' != fwInfo.fwSyyt">
                AND fw.fw_syyt LIKE '%' || #{fwInfo.fwSyyt} || '%'
            </if>
            <if test="null != fwInfo.fzDh and '' != fwInfo.fzDh">
                AND fz.fz_dh LIKE '%' || #{fwInfo.fzDh} || '%'
            </if>
            <if test="fwInfo.startTime != null and fwInfo.startTime != ''">
                AND fw.create_time &gt;= #{fwInfo.startTime}::timestamp AND fw.create_time &lt;= #{fwInfo.endTime}::timestamp
            </if>
            <if test="fwInfo.updateStartTime != null and fwInfo.updateStartTime != ''">
                AND fw.update_time &gt;= #{fwInfo.updateStartTime}::timestamp AND fw.update_time &lt;= #{fwInfo.updateEndTime}::timestamp
            </if>
            <if test="fwInfo.sfzh != null and fwInfo.sfzh != ''">
                and fw.id in (SELECT fw_id
                FROM rhzf_ry_czrk cz
                WHERE  cz.ry_sfzh = #{fwInfo.sfzh} And cz.is_deleted = 0
                UNION
                SELECT fw_id
                FROM rhzf_ry_jwrk jw
                WHERE  jw.ry_zjhm = #{fwInfo.sfzh} And jw.is_deleted = 0
                UNION
                SELECT fw_id
                FROM rhzf_ry_jzrk jz
                WHERE  jz.ry_sfzh = #{fwInfo.sfzh} And jz.is_deleted = 0
                UNION
                SELECT fw_id
                FROM rhzf_ry_wlrk wl
                WHERE  wl.ry_sfzh = #{fwInfo.sfzh} And wl.is_deleted = 0
                UNION
                SELECT fw_id
                FROM rhzf_ry_zzrk zz
                WHERE  zz.ry_sfzh = #{fwInfo.sfzh} And zz.is_deleted = 0
                UNION
                SELECT fw_id
                FROM rhzf_fw_fz fz
                WHERE  fz.fz_sfzh = #{fwInfo.sfzh} And fz.is_deleted = 0
                )
            </if>

        <if test="fwInfo.tsrq != null and fwInfo.tsrq != ''">
            and fw.id in (SELECT fw_id
            FROM rhzf_ry_czrk cz
            WHERE  cz.ry_ts_lb like '%' || #{fwInfo.tsrq} || '%'
            UNION
            SELECT fw_id
            FROM rhzf_ry_jwrk jw
            WHERE  jw.ry_ts_lb like '%' || #{fwInfo.tsrq} || '%'
            UNION
            SELECT fw_id
            FROM rhzf_ry_jzrk jz
            WHERE  jz.ry_ts_lb like '%' || #{fwInfo.tsrq} || '%'
            UNION
            SELECT fw_id
            FROM rhzf_ry_wlrk wl
            WHERE  wl.ry_ts_lb like '%' || #{fwInfo.tsrq} || '%'
            UNION
            SELECT fw_id
            FROM rhzf_ry_zzrk zz
            WHERE  zz.ry_ts_lb like '%' || #{fwInfo.tsrq} || '%'
            )
        </if>
    </select>

    <select id="getFwByRysfzh" resultType="org.springblade.modules.rhzf.fwInfo.vo.FwInfoVO">
        SELECT fw_id fwId
        FROM rhzf_ry_czrk cz
        WHERE  cz.ry_sfzh = #{sfzh}
        UNION
        SELECT fw_id
        FROM rhzf_ry_jwrk jw
        WHERE  jw.ry_zjhm = #{sfzh}
        UNION
        SELECT fw_id
        FROM rhzf_ry_jzrk jz
        WHERE  jz.ry_sfzh = #{sfzh}
        UNION
        SELECT fw_id
        FROM rhzf_ry_wlrk wl
        WHERE  wl.ry_sfzh = #{sfzh}
        UNION
        SELECT fw_id
        FROM rhzf_ry_zzrk zz
        WHERE  zz.ry_sfzh = #{sfzh}
    </select>

    <select id="exportFwInfo" resultType="org.springblade.modules.rhzf.fwInfo.excel.FwInfoExcel">
        SELECT
            dz.xz AS dzXz,
            dept.dept_name AS deptName,
            dept1.dept_name AS pcsName,
            fz.fz_xm,
            fz.fz_dh,
            fz.fz_sfzh,
            (select count(1) from rhzf_fw_ry ry  where ry.dzbm = fw.dzbm and ry.is_deleted = 0 and ry.fw_id is not null) as fwrycount,
            fw.fw_xz,
            fz.tsqk,
            fw.fw_syyt,
            fw.fw_yt,
            us.NAME AS userName,
            fw.create_time,
            us1.NAME AS updateName,
            fw.update_time,
            fw.ID AS fwid,
            fw.dzbm
        FROM
            rhzf_fw_info fw
        LEFT JOIN sspt.rk_dzxx dz ON dz.dzbm = fw.dzbm
        LEFT JOIN blade_dept dept ON dept.ID = fw.create_dept
        LEFT JOIN blade_dept dept1 ON dept1.ID = dept.parent_id
        LEFT JOIN rhzf_fw_fz fz ON fz.fw_id = fw.ID
        LEFT JOIN blade_user us ON us.ID = fw.create_user and us.is_deleted = 0
        LEFT JOIN blade_user us1 ON us1.ID = fw.update_user and us1.is_deleted = 0
        WHERE
            fw.is_deleted = 0
        AND ((#{fwInfo.deptId} LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{fwInfo.deptId} || '%'))
        <if test="fwInfo.fzxm != null and fwInfo.fzxm != ''">
            and fz.fz_xm like '%' || #{fwInfo.fzxm} || '%'
        </if>
        <if test="fwInfo.dzXz != null and fwInfo.dzXz != ''">
            and bz.wzxxdz like '%' || #{fwInfo.dzXz} || '%'
        </if>
        <if test="fwInfo.userName != null and fwInfo.userName != ''">
            and us.name like '%' || #{fwInfo.userName} || '%'
        </if>
        <if test="fwInfo.updateName != null and fwInfo.updateName != ''">
            and us1.name like '%' || #{fwInfo.updateName} || '%'
        </if>
        <if test="fwInfo.sssqName != null and fwInfo.sssqName != ''">
            and bz.sssq_bm::text = #{fwInfo.sssqName}
        </if>
        <if test="fwInfo.deptName != null and fwInfo.deptName != ''">
            and dept.id :: text = #{fwInfo.deptName}
        </if>
        <if test="null != fwInfo.ssfjName and '' != fwInfo.ssfjName">
            AND dept.ancestors :: text LIKE '%' || #{fwInfo.ssfjName} || '%'
        </if>
        <if test="null != fwInfo.sspcsName and '' != fwInfo.sspcsName">
            AND dept.ancestors :: text LIKE '%' || #{fwInfo.sspcsName} || '%'
        </if>
        <if test="null != fwInfo.sssjName and '' != fwInfo.sssjName">
            AND dept.ancestors :: text LIKE '%' || #{fwInfo.sssjName} || '%'
        </if>
        <if test="null != fwInfo.fwYt and '' != fwInfo.fwYt">
            AND fw.fw_yt LIKE '%' || #{fwInfo.fwYt} || '%'
        </if>
        <if test="null != fwInfo.fwSyyt and '' != fwInfo.fwSyyt">
            AND fw.fw_syyt LIKE '%' || #{fwInfo.fwSyyt} || '%'
        </if>
        <if test="fwInfo.startTime != null and fwInfo.startTime != ''">
            AND fw.create_time &gt;= #{fwInfo.startTime}::timestamp AND fw.create_time &lt;= #{fwInfo.endTime}::timestamp
        </if>
        <if test="fwInfo.updateStartTime != null and fwInfo.updateStartTime != ''">
            AND fw.update_time &gt;= #{fwInfo.updateStartTime}::timestamp AND fw.update_time &lt;= #{fwInfo.updateEndTime}::timestamp
        </if>
    </select>

<!--    <select id="getLdList" resultType="org.springblade.modules.rhzf.fwInfo.vo.LdBzdzVO">-->
<!--        SELECT-->
<!--            dzbm,-->
<!--            sqjcwhmc,-->
<!--            jlxxqzdzrcmc,-->
<!--            mlph,-->
<!--            ldph,-->
<!--            dzjd,-->
<!--            dzwd-->
<!--        FROM-->
<!--            PUBLIC.fw_bzdz,-->
<!--            string_to_array( #{params.deptId} , ',' ) AS sqbm-->
<!--        WHERE-->
<!--            pcsjgdm = #{params.pcsId} :: text-->
<!--          AND sssq_bm :: text = ANY(sqbm)-->
<!--          AND sssq_bm IS NOT NULL-->
<!--          AND dyh = ''-->
<!--          AND lch = ''-->
<!--          AND lcnpmh = ''-->
<!--        <if test="null != params.ldMc and '' != params.ldMc">-->
<!--          AND sqjcwhmc || jlxxqzdzrcmc || mlph || ldph LIKE '%' || #{params.ldMc} || '%'-->
<!--        </if>-->
<!--        <if test="null != params.sssqBm and '' != params.sssqBm">-->
<!--            AND sssq_bm :: text = #{params.sssqBm}-->
<!--        </if>-->
<!--        ORDER BY-->
<!--            jlxxqzdzrcmc,-->
<!--            mlph,-->
<!--            ldph-->
<!--    </select>-->
    <select id="getLdList" resultType="org.springblade.modules.rhzf.fwInfo.vo.LdBzdzVO">
        SELECT
        dzbm,
        sqjcwhmc,
        jlxxqzdzrcmc,
        mlph,
        ldph,
        dzjd,
        dzwd
        FROM
        PUBLIC.fw_bzdz,
        string_to_array( #{params.deptId} , ',' ) AS sqbm
        WHERE
        pcsjgdm = #{params.pcsId} :: text
        AND sssq_bm IS NOT NULL
        AND dyh = ''
        AND lch = ''
        AND lcnpmh = ''
        <if test="null != params.ldMc and '' != params.ldMc">
            AND sqjcwhmc || jlxxqzdzrcmc || mlph || ldph LIKE '%' || #{params.ldMc} || '%'
        </if>
        <if test="null != params.sssqBm and '' != params.sssqBm">
            AND sssq_bm :: text = #{params.sssqBm}
        </if>
        <if test="null == params.sssqBm or '' == params.sssqBm">
            AND sssq_bm :: text = ANY(sqbm)
        </if>
        ORDER BY
        jlxxqzdzrcmc,
        mlph,
        ldph
    </select>
    <!--<select id="getLdList" resultType="org.springblade.modules.bzdz.vo.BzdzVO">
        SELECT
            ld.id,
            ld.dzbm,
            ld.dzmc,
            ld.dzmc_dt,
            ld.xqmc || ld.ldph || ld.ldph_suffix AS ldMc,
            ld.dzjd,
            ld.dzwd,
            ld.ld_type
        FROM
            UNNEST ( string_to_array( #{params.deptId}, ',' ) ) AS sqbm,
            ld_bzdz ld
        LEFT JOIN blade_dept sq ON sq.id = ld.sqjcwhdm
        WHERE
            ld.is_deleted = 0
          AND ld.sqjcwhdm :: TEXT IN ( sqbm )
    </select>-->

<!--    <select id="getDyList" resultType="org.springblade.modules.rhzf.fwInfo.vo.DyVO">-->
<!--        SELECT-->
<!--            dyh-->
<!--        FROM-->
<!--            rhzf_fw_info-->
<!--        WHERE-->
<!--            is_deleted = 0-->
<!--          AND dzbm = #{params.dzbm}-->
<!--        GROUP BY-->
<!--            dyh-->
<!--        ORDER BY-->
<!--            dyh-->
<!--    </select>-->

    <select id="getDyList" resultType="org.springblade.modules.rhzf.fwInfo.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.rhzf.fwInfo.vo.LcVO">-->
<!--        SELECT-->
<!--            lch-->
<!--        FROM-->
<!--            rhzf_fw_info-->
<!--        WHERE-->
<!--            is_deleted = 0-->
<!--          AND dzbm = #{params.dzbm}-->
<!--          AND dyh = #{params.dyh}-->
<!--        GROUP BY-->
<!--            lch-->
<!--        ORDER BY-->
<!--            lch-->
<!--    </select>-->

    <select id="getLcList" resultType="org.springblade.modules.rhzf.fwInfo.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.rhzf.fwInfo.vo.FjVO">-->
<!--        SELECT-->
<!--            fw.id,-->
<!--            fw.dzbm,-->
<!--            fw.lcnpmh,-->
<!--            fw.lcnpmh_suffix,-->
<!--            fw.fw_syyt,-->
<!--            ( SELECT COUNT ( * ) FROM rhzf_fw_ry fwry WHERE fwry.is_deleted = 0 AND fwry.fw_id = fw.ID ) AS personNum-->
<!--        FROM-->
<!--            rhzf_fw_info fw-->
<!--        WHERE-->
<!--            fw.is_deleted = 0-->
<!--          AND fw.dzbm = #{params.dzbm}-->
<!--          AND fw.dyh = #{params.dyh}-->
<!--          AND fw.lch = #{params.lch}-->
<!--        ORDER BY-->
<!--            fw.lcnpmh-->
<!--    </select>-->

    <select id="getFjList" resultType="org.springblade.modules.rhzf.fwInfo.vo.FjVO">
        SELECT
            bzdz.dzbm,
            bzdz.lcnpmh,
            ( SELECT COUNT ( * ) FROM rhzf_fw_ry fwry WHERE fwry.is_deleted = 0 AND fwry.fw_id = fw.id ) AS personNum,
            fw.id,
            fw.fw_syyt,
            fw.times
        FROM
            sspt.rk_dzxx bzdz
        LEFT JOIN 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>
        ORDER BY
            COALESCE(NULLIF(regexp_replace( bzdz.lcnpmh, '[^0-9]', '', 'g' ), '') , '0') :: int8
    </select>

    <select id="getFjDetail" resultType="org.springblade.modules.rhzf.fwInfo.vo.RhzfSaveVO">
        SELECT
            fw.*,
            bzdz.dzmc,
            bzdz.xqmc || bzdz.ldph || bzdz.ldph_suffix AS ldMc
        FROM
            rhzf_fw_info fw
        LEFT JOIN ld_bzdz bzdz ON bzdz.dzbm = fw.dzbm
        WHERE
            fw.is_deleted = 0
          AND fw.dzbm = #{params.dzbm}
    </select>
    <select id="getFjDetailByFwIdAndDzbm" resultType="org.springblade.modules.rhzf.fwInfo.vo.RhzfSaveVO">
        SELECT
            fw.*,
            bzdz.dzmc,
            bzdz.xqmc || bzdz.ldph || bzdz.ldph_suffix AS ldMc
        FROM
            rhzf_fw_info fw
                LEFT JOIN ld_bzdz bzdz ON bzdz.dzbm = fw.dzbm
        WHERE
            fw.is_deleted = 0
          AND fw.dzbm = #{params.dzbm} and fw.id = #{params.fwid}
    </select>
    <select id="getRyList" resultType="org.springblade.modules.rhzf.fwRy.vo.FwRyVO">
        SELECT
            fwry.ry_id,
            fwry.ry_type
        FROM
            rhzf_fw_ry fwry
        WHERE
            fwry.is_deleted = 0
          AND fwry.dzbm = #{params.dzbm}
    </select>

    <select id="getCzrk" resultType="org.springblade.modules.rhzf.fwInfo.vo.SyjzryVO">
        SELECT
            *,
            'czrk' AS ryType
        FROM
            rhzf_ry_czrk
        WHERE
                is_deleted = 0
            AND id = #{params.ryId}
    </select>

    <select id="getJwrk" resultType="org.springblade.modules.rhzf.fwInfo.vo.SyjzryVO">
        SELECT
            *,
            'jwrk' AS ryType
        FROM
            rhzf_ry_jwrk
        WHERE
                is_deleted = 0
            AND id = #{params.ryId}
    </select>

    <select id="getJzrk" resultType="org.springblade.modules.rhzf.fwInfo.vo.SyjzryVO">
        SELECT
            *,
            'jzrk' AS ryType
        FROM
            rhzf_ry_jzrk
        WHERE
                is_deleted = 0
            AND id = #{params.ryId}
    </select>

    <select id="getWlrk" resultType="org.springblade.modules.rhzf.fwInfo.vo.SyjzryVO">
        SELECT
            *,
            'wlrk' AS ryType
        FROM
            rhzf_ry_wlrk
        WHERE
                is_deleted = 0
            AND id = #{params.ryId}
    </select>

    <select id="getZzrk" resultType="org.springblade.modules.rhzf.fwInfo.vo.SyjzryVO">
        SELECT
            *,
            'zzrk' AS ryType
        FROM
            rhzf_ry_zzrk
        WHERE
                is_deleted = 0
            AND id = #{params.ryId}
    </select>

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

    <select id="getFjNum" resultType="int">
        SELECT
            COUNT(*)
        FROM
            rhzf_fw_info
        WHERE
            is_deleted = 0
          AND dzbm = #{dzbm}
          AND dyh = #{dyh}
          AND lch = #{lch}
          AND lcnpmh = #{lcnpmh}
    </select>

    <select id="getYwxxParams" resultType="org.springblade.common.controller.vo.webService.YwxxVO">
        SELECT
            bu.jh AS yhjh,
            bu.dept_id,
            "substring" ( dept."id市局" :: TEXT, 0, 7 ) AS dqm,
            dept.ID派出所 AS gajgdm,
            dept."dept_name派出所名" AS gajgmc,
            jy.jh
        FROM
            jdrh.blade_user bu
        LEFT JOIN jdrh.v_dept_all dept ON dept.ID社区 = ( string_to_array( bu.dept_id, ',' ) ) [ 1 ] :: int8
        LEFT JOIN sspt.jy jy on jy.jh = bu.jh AND (jy.gwmc IN ('社区民警', '社区民警（专职）', '一警多能（社区警务方向）', '兼任社区警务队队长'))
        WHERE
            bu.ID = #{userId}
    </select>

    <select id="getTimes" resultType="integer">
        SELECT
            times
        FROM
            rhzf_fw_info
        WHERE
            dzbm = #{dzbm}
    </select>

    <select id="getJzwList" resultType="org.springblade.modules.rhzf.fwInfo.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="getDzList" resultType="org.springblade.modules.rhzf.fwInfo.vo.LdBzdzVO">
        SELECT
            xlh AS ldphxlh,
            sqjcwhmc,
            jlxmc AS jlxxqzdzrcmc,
            mlph,
            ldph,
            pointx AS dzjd,
            pointy AS dzwd
        FROM
            sspt.rk_ldphxx
        WHERE
            cxbs = 0
          AND pcsjgdm = #{params.gajgdm}
        ORDER BY COALESCE(NULLIF(regexp_replace( ldph, '[^0-9]', '', 'g' ), '') , '0') :: int8
    </select>

    <select id="getFjJzwList" resultType="org.springblade.modules.rhzf.fwInfo.vo.LdBzdzVO">
        SELECT
            xlh AS ldphxlh,
            jlxmc AS jlxxqzdzrcmc,
            mlph,
            ldph,
            sqjcwhmc,
            pointx AS dzjd,
            pointy AS dzwd
        FROM
            sspt.rk_ldphxx
        WHERE
            cxbs = 0
        AND fjgmsfhm = #{params.account}
        AND pcsjgdm = #{params.gajgdm}
        ORDER BY COALESCE(NULLIF(regexp_replace( ldph, '[^0-9]', '', 'g' ), '') , '0') :: int8
    </select>

    <select id="getRhzfCs" resultType="int">
        SELECT
            COUNT(id)
        FROM
            jdrh.rhzf_fw_info
        WHERE
            create_user = #{userId}
        AND fw_syyt != '空房'
        AND create_time :: date = now( ) :: date
    </select>


    <select id="getFwRyAll" resultType="org.springblade.modules.rhzf.fwInfo.excel.FwInfoExcel">
        SELECT
            cz.ry_xm ryxm,
            cz.ry_sfzh ryzjhm,
            cz.ry_dh rydh,
            '常住' rylb,
            cz.ry_hzgx ryHzgx
        FROM
            jdrh.rhzf_ry_czrk cz
        where cz.is_deleted = 0 and cz.fw_id = #{fwid}
        UNION
        SELECT
            zz.ry_xm ryxm,
            zz.ry_sfzh ryzjhm,
            zz.ry_dh rydh,
            '暂住' rylb,
            zz.ry_hzgx ryHzgx
        FROM
            jdrh.rhzf_ry_zzrk zz
        where zz.is_deleted = 0 and zz.fw_id = #{fwid}
        UNION
        SELECT
            jz.ry_xm ryxm,
            jz.ry_sfzh ryzjhm,
            jz.ry_dh rydh,
            '寄住' rylb,
            jz.ry_hzgx ryHzgx
        FROM
            jdrh.rhzf_ry_jzrk jz
        where jz.is_deleted = 0 and jz.fw_id = #{fwid}
        UNION
        SELECT
            wl.ry_xm ryxm,
            wl.ry_sfzh ryzjhm,
            wl.ry_dh rydh,
            '未落户' rylb,
            wl.ry_hzgx ryHzgx
        FROM
            jdrh.rhzf_ry_wlrk wl
        where wl.is_deleted = 0 and wl.fw_id = #{fwid}
        union
        SELECT
            jw.ry_xs||jw.ry_mz ryxm,
            jw.ry_zjhm ryzjhm,
            jw.ry_dh rydh,
            '境外' rylb,
            '' ryHzgx
        FROM
            jdrh.rhzf_ry_jwrk jw
        where jw.is_deleted = 0 and jw.fw_id = #{fwid}
    </select>

    <select id="getCsLdList" resultType="org.springblade.modules.rhzf.fwInfo.vo.LdBzdzVO">
        SELECT
            xlh AS ldphxlh,
            jlxmc AS jlxxqzdzrcmc,
            mlph,
            ldph,
            sqjcwhmc,
            pointx AS dzjd,
            pointy AS dzwd
        FROM
            sspt.rk_ldphxx
        WHERE
            cxbs = 0
        AND xlh IN
        <foreach collection="ldphxlhList" item="ldphxlh" index="index" open="(" close=")" separator=",">
            #{ldphxlh}
        </foreach>
        ORDER BY COALESCE(NULLIF(regexp_replace( ldph, '[^0-9]', '', 'g' ), '') , '0') :: int8
    </select>

    <select id="getFwSl" parameterType="java.lang.Long" resultType="int">
        SELECT
            COUNT(id)
        FROM
            jdrh.rhzf_fw_info
        WHERE
            create_user = #{userId}
          AND is_deleted = 0
    </select>

    <select id="getRyxxBySfzh" resultType="org.springblade.modules.rhzf.fwInfo.vo.RkxxVO">
        SELECT
            rk.ryxm AS xm,
            rk.ryxb AS xb,
            rk.rymz AS mz,
            rk.rycsrq AS csrq,
            rk.rydh AS lxdh,
            rk.rydzxz AS hjdz,
            zp.zp
        FROM
            sspt.app_syrk rk
        LEFT JOIN sspt.jy_zp zp ON zp.sfzh = rk.rysfzh
        WHERE
            rk.rysfzh = #{params.sfzh}
    </select>

    <select id="getJcdList" resultType="org.springblade.modules.rhzf.fwInfo.entity.Fwccjgb">
        select fw.id,sj.dept_name sjName,fj.dept_name fjName,fw.pcsname,fw.sqmj,dz.xz ,fw.dzbm from public.fwccjgb fw
        left join public.blade_dept fj on fj.id = fw.fjid
        left join public.blade_dept sj on sj.id = fj.parent_id
        left join sspt.rk_dzxx dz on dz.dzbm = fw.dzbm
        where pc = #{pc}
    </select>

    <select id="getKfsl" resultType="int">
        SELECT
            COUNT(1)
        FROM
            jdrh.rhzf_fw_info
        WHERE
            create_user = #{userId}
          AND fw_syyt = '空房'
          AND create_time :: DATE = now() :: DATE
    </select>

    <select id="getTarget" resultType="java.lang.String">
        SELECT
            target_jh
        FROM
            jdrh.rhzf_tem_duizhao
        WHERE
            is_deleted = 0
          AND source_jh = #{userAccount}
    </select>

    <select id="getMphxlhByLdphxlh" resultType="org.springblade.modules.rhzf.fwInfo.vo.FwInfoVO">
        select dzbm from sspt.rk_dzxx where ldphxlh = coalesce (nullif (#{ldphxlh}, ''), '0'):: int8
    </select>

    <select id="selectFwInfo" resultType="org.springblade.modules.rhzf.fwInfo.vo.FwInfoVO">
        SELECT
        fw.*,
        us.NAME userName,
        us1.name updateName,
        fz.update_time as fzUpdateTime,
        dept.dept_name deptName,
        fz.fz_xm fzxm,
        fz.fz_dh fzDh,
        fz.tsqk tsqk,
        fz.fz_sfzh fzSfzh,
        (select count(1) from rhzf_fw_ry ry  where ry.dzbm = fw.dzbm and ry.is_deleted = 0  and ry.fw_id is not null) as fwrycount
        FROM
        rhzf_fw_info fw
        LEFT JOIN blade_dept dept ON dept.ID = fw.create_dept
        LEFT JOIN rhzf_fw_fz fz ON fz.fw_id = fw.ID
        LEFT JOIN blade_user us ON us.ID = fz.create_user
        LEFT JOIN blade_user us1 ON us1.ID = fz.update_user
        WHERE
        fw.is_deleted = 0 and fz.is_deleted = 0
        AND ((#{fwInfo.deptId} LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{fwInfo.deptId} || '%'))
        AND fw.dzbm = #{dzbm}
        <if test="fwInfo.fzxm != null and fwInfo.fzxm != ''">
            and fz.fz_xm like '%' || #{fwInfo.fzxm} || '%'
        </if>
        <if test="fwInfo.userName != null and fwInfo.userName != ''">
            and us.name like '%' || #{fwInfo.userName} || '%'
        </if>
        <if test="fwInfo.updateName != null and fwInfo.updateName != ''">
            and us1.name like '%' || #{fwInfo.updateName} || '%'
        </if>
        <if test="fwInfo.sssqName != null and fwInfo.sssqName != ''">
            and bz.sssq_bm::text = #{fwInfo.sssqName}
        </if>
        <if test="fwInfo.deptName != null and fwInfo.deptName != ''">
            and dept.id :: text = #{fwInfo.deptName}
        </if>
        <if test="null != fwInfo.ssfjName and '' != fwInfo.ssfjName">
            AND dept.ancestors :: text LIKE '%' || #{fwInfo.ssfjName} || '%'
        </if>
        <if test="null != fwInfo.sspcsName and '' != fwInfo.sspcsName">
            AND dept.ancestors :: text LIKE '%' || #{fwInfo.sspcsName} || '%'
        </if>
        <if test="null != fwInfo.sssjName and '' != fwInfo.sssjName">
            AND dept.ancestors :: text LIKE '%' || #{fwInfo.sssjName} || '%'
        </if>
        <if test="null != fwInfo.fwYt and '' != fwInfo.fwYt">
            AND fw.fw_yt LIKE '%' || #{fwInfo.fwYt} || '%'
        </if>
        <if test="null != fwInfo.fwSyyt and '' != fwInfo.fwSyyt">
            AND fw.fw_syyt LIKE '%' || #{fwInfo.fwSyyt} || '%'
        </if>
        <if test="null != fwInfo.fzDh and '' != fwInfo.fzDh">
            AND fz.fz_dh LIKE '%' || #{fwInfo.fzDh} || '%'
        </if>
        <if test="fwInfo.startTime != null and fwInfo.startTime != ''">
            AND fw.create_time &gt;= #{fwInfo.startTime}::timestamp AND fw.create_time &lt;= #{fwInfo.endTime}::timestamp
        </if>

        <if test="fwInfo.sfzh != null and fwInfo.sfzh != ''">
            and fw.id in (SELECT fw_id
            FROM rhzf_ry_czrk cz
            WHERE  cz.ry_sfzh = #{fwInfo.sfzh} And cz.is_deleted = 0
            UNION
            SELECT fw_id
            FROM rhzf_ry_jwrk jw
            WHERE  jw.ry_zjhm = #{fwInfo.sfzh} And jw.is_deleted = 0
            UNION
            SELECT fw_id
            FROM rhzf_ry_jzrk jz
            WHERE  jz.ry_sfzh = #{fwInfo.sfzh} And jz.is_deleted = 0
            UNION
            SELECT fw_id
            FROM rhzf_ry_wlrk wl
            WHERE  wl.ry_sfzh = #{fwInfo.sfzh} And wl.is_deleted = 0
            UNION
            SELECT fw_id
            FROM rhzf_ry_zzrk zz
            WHERE  zz.ry_sfzh = #{fwInfo.sfzh} And zz.is_deleted = 0
            UNION
            SELECT fw_id
            FROM rhzf_fw_fz fz
            WHERE  fz.fz_sfzh = #{fwInfo.sfzh} And fz.is_deleted = 0
            )
        </if>

        <if test="fwInfo.tsrq != null and fwInfo.tsrq != ''">
            and fw.id in (SELECT fw_id
            FROM rhzf_ry_czrk cz
            WHERE  cz.ry_ts_lb like '%' || #{fwInfo.tsrq} || '%'
            UNION
            SELECT fw_id
            FROM rhzf_ry_jwrk jw
            WHERE  jw.ry_ts_lb like '%' || #{fwInfo.tsrq} || '%'
            UNION
            SELECT fw_id
            FROM rhzf_ry_jzrk jz
            WHERE  jz.ry_ts_lb like '%' || #{fwInfo.tsrq} || '%'
            UNION
            SELECT fw_id
            FROM rhzf_ry_wlrk wl
            WHERE  wl.ry_ts_lb like '%' || #{fwInfo.tsrq} || '%'
            UNION
            SELECT fw_id
            FROM rhzf_ry_zzrk zz
            WHERE  zz.ry_ts_lb like '%' || #{fwInfo.tsrq} || '%'
            )
        </if>
        limit 1
    </select>

    <select id="getTargetDm" resultType="java.lang.String">
        SELECT
            target_dm
        FROM
            jdrh.gajgdm_comparison
        WHERE
            dept_id = #{gajgdm}
    </select>

    <select id="getLdByJg" resultType="org.springblade.modules.rhzf.fwInfo.vo.LdBzdzVO">
        SELECT
            ld.id AS ldphxlh,
            ld.mphxlh,
            ld.mlph,
            ld.ldph,
            ld.jlxmc AS jlxxqzdzrcmc,
            ld.sqjcwhmc,
            ld.pointx AS dzjd,
            ld.pointy AS dzwd
        FROM
            sspt.rk_ldphxx ld
        LEFT JOIN sspt.ldgl gl ON gl.ldphxlh = ld.id
        WHERE
            ld.cxbs = 0
          AND gl.ssjg_bm IN
        <foreach collection="list" item="jgId" index="index" open="(" close=")" separator=",">
            #{jgId} :: int8
        </foreach>
    </select>

    <update id="gxUpdateTime" parameterType="java.lang.Long">
        UPDATE jdrh.rhzf_fw_info
        SET update_time = now()
        WHERE id = #{fwId} AND is_deleted = 0
    </update>

    <select id="countByType" resultType="int">
        SELECT
            COUNT(*)
        FROM
            jdrh.rhzf_fw_info fw
        LEFT JOIN jdrh.blade_dept dept ON dept.id = fw.create_dept
        WHERE
            fw.is_deleted = 0
          AND (#{params.deptId} LIKE '%' || dept.id || '%' OR dept.ancestors LIKE '%' || #{params.deptId} || '%')
        <if test="1 == type">
            AND fw.update_time BETWEEN DATE_TRUNC( 'year', CURRENT_DATE ) AND DATE_TRUNC( 'year', CURRENT_DATE ) + INTERVAL '1 year'
        </if>
        <if test="2 == type">
            AND fw.update_time BETWEEN (CURRENT_DATE - EXTRACT(DOW FROM CURRENT_DATE)::INT + (CASE WHEN EXTRACT(DOW FROM CURRENT_DATE) = 0 THEN 7 ELSE 1 END)) AND (CURRENT_DATE - EXTRACT(DOW FROM CURRENT_DATE)::INT + (CASE WHEN EXTRACT(DOW FROM CURRENT_DATE) = 0 THEN 7 ELSE 1 END) + INTERVAL '7 days')
        </if>
    </select>

    <update id="updateMdId">
        UPDATE jdrh.rhzf_fw_info
        SET md_id =
                CASE
                    WHEN md_id IS NULL THEN
                        #{mdId} :: TEXT ELSE md_id || ',' || #{mdId} :: TEXT
                    END
        WHERE
            id = #{fwId}
    </update>

    <select id="history" resultType="org.springblade.modules.rhzf.fwInfo.vo.RhzfHistoryVO">
        SELECT
            dz.xz,
            fz.fz_xm,
            ( SELECT COUNT(*) FROM jdrh.rhzf_fw_ry ry WHERE ry.fw_id = fw.id AND ry.is_deleted = 0 ) AS syrks,
            fw.update_time
        FROM
            jdrh.rhzf_fw_info fw
        LEFT JOIN sspt.rk_dzxx dz ON dz.dzbm = fw.dzbm
        LEFT JOIN jdrh.rhzf_fw_fz fz ON fz.fw_id = fw.id
        LEFT JOIN jdrh.blade_dept dept ON dept.id = fw.create_dept
        WHERE
            (#{params.deptId} LIKE '%' || dept.id || '%' OR dept.ancestors LIKE '%' || #{params.deptId} || '%')
        ORDER BY
            fw.update_time DESC
    </select>
</mapper>
