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

    <select id="selectSyfwPage" resultType="org.springblade.modules.entity.SyfwEntity">
        select
            rd.xz,
            rfi.id,
            rfi.fw_lb || '|' || rfi.fw_yt || '|' || rfi.fw_xz || '|' || rfi.fw_syyt as fwtd,
            rfi.fw_mj,
            rfi.fw_js,
            rff.fz_xm,
            rff.fz_dh,
            rfi.dzbm,
            case when rff.sfzcjz = '0' then '不在此居住' when rff.sfzcjz = '1' then '在此居住' end as sfzcjz
        from jdrh.rhzf_fw_info rfi
        left join sspt.rk_dzxx rd on rd.dzbm = rfi.dzbm
        left join jdrh.rhzf_fw_fz rff on rfi.id = rff.fw_id
        left join jdrh.blade_dept bd on rfi.create_dept = bd.id
        where rfi.is_deleted = 0 and (bd.id :: text = #{params.deptId} or bd.ancestors like '%' || #{params.deptId} || '%')
        <if test="params.sfzcjz != null and params.sfzcjz != ''">
            and rff.sfzcjz :: text = #{params.sfzcjz}
        </if>
        <if test="params.fzXm != null and params.fzXm != ''">
            and rff.fz_xm = #{params.fzXm}
        </if>
        <if test="params.fzDh != null and params.fzDh != ''">
            and rff.fz_dh = #{params.fzDh}
        </if>
        <if test="params.fwLb != null and params.fwLb != ''">
            and rfi.fw_lb = #{params.fwLb}
        </if>
        <if test="params.fwYt != null and params.fwYt != ''">
            and rfi.fw_yt = #{params.fwYt}
        </if>
        <if test="params.fwXz != null and params.fwXz != ''">
            and rfi.fw_xz = #{params.fwXz}
        </if>
        <if test="params.fwSyyt != null and params.fwSyyt != ''">
            and rfi.fw_syyt = #{params.fwSyyt}
        </if>
    </select>

    <select id="bzdzDetail" resultType="org.springblade.modules.entity.SyfwEntity">
        select
            rd.xz,
            rfi.fw_lb || '|' || rfi.fw_yt || '|' || rfi.fw_xz || '|' || rfi.fw_syyt as fwtd,
            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
        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
        where rfi.is_deleted = 0 and rfi.dzbm = #{params.dzbm}
    </select>

    <select id="selectFzLs" resultType="org.springblade.modules.vo.FzLsVO">
        select * from jdrh.rhzf_fz_ls where is_deleted = 0 and id = #{fzid}
    </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="updateUser != null and updateUser != ''">update_user = #{updateUser},</if>
            updateTime = now()
        </set>
        where id = #{fzid}
    </update>

    <select id="selectByFzId" resultType="org.springblade.modules.entity.SyfwEntity">
        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="exportSyfwInfo" resultType="org.springblade.modules.excel.SyfwExcel">
        select
            rd.xz,
            rfi.id,
            rfi.fw_lb || '|' || rfi.fw_yt || '|' || rfi.fw_xz || '|' || rfi.fw_syyt as fwtd,
            rfi.fw_mj as fwMj,
            rfi.fw_js as fwJs,
            rff.fz_xm as fzXm,
            rff.fz_dh as fzDh,
            case when rff.sfzcjz = '0' then '不在此居住' when rff.sfzcjz = '1' then '在此居住' end as sfzcjz
        from jdrh.rhzf_fw_info rfi
        left join sspt.rk_dzxx rd on rd.dzbm = rfi.dzbm
        left join jdrh.rhzf_fw_fz rff on rfi.id = rff.fw_id
        where rfi.is_deleted = 0 and rfi.id = #{params.id}
    </select>

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

    <select id="selectFz" resultType="org.springblade.modules.entity.FzLsEntity">
        select * from jdrh.rhzf_fw_fz where is_deleted = 1 and fw_id = #{id}
    </select>

    <select id="selectCzrk" resultType="org.springblade.modules.entity.RyCzrkEntity">
        select * from jdrh.rhzf_ry_czrk where is_deleted = 1 and fw_id = #{id}
    </select>

    <select id="selectJwrk" resultType="org.springblade.modules.entity.RyJwrkEntity">
        select * from jdrh.rhzf_ry_jwrk where is_deleted = 1 and fw_id = #{id}
    </select>

    <select id="selectJzrk" resultType="org.springblade.modules.entity.RyJzrkEntity">
        select * from jdrh.rhzf_ry_jzrk where is_deleted = 1 and fw_id = #{id}
    </select>

    <select id="selectWlrk" resultType="org.springblade.modules.entity.RyWlrkEntity">
        select * from jdrh.rhzf_ry_wlrk where is_deleted = 1 and fw_id = #{id}
    </select>

    <select id="selectZzrk" resultType="org.springblade.modules.entity.RyZzrkEntity">
        select * from jdrh.rhzf_ry_zzrk where is_deleted = 1 and fw_id = #{id}
    </select>

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

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

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

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

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

    <update id="updateFwRyzx">
        update jdrh.rhzf_fw_ry set is_deleted = 1, update_user = #{userId}, update_time = now() where fw_id = #{id}
    </update>

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

    <select id="selectFw" resultType="org.springblade.modules.entity.FwLsEntity">
        select * from jdrh.rhzf_fw_info where id = #{id}
    </select>

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

    <select id="selectFwInfo" resultType="org.springblade.modules.entity.SyfwEntity">
        select
        rd.xz,
        rfi.id,
        rfi.fw_lb || '|' || rfi.fw_yt || '|' || rfi.fw_xz || '|' || rfi.fw_syyt as fwtd,
        rfi.fw_mj,
        rfi.fw_js,
        rff.fz_xm,
        rff.fz_dh,
        rfi.dzbm,
        case when rff.sfzcjz = '0' then '不在此居住' when rff.sfzcjz = '1' then '在此居住' end as sfzcjz
        from jdrh.rhzf_fw_info rfi
        left join sspt.rk_dzxx rd on rd.dzbm = rfi.dzbm
        left join jdrh.rhzf_fw_fz rff on rfi.id = rff.fw_id
        left join jdrh.blade_dept bd on rfi.create_dept = bd.id
        where rfi.is_deleted = 0 and (bd.id :: text = #{params.deptId} or bd.ancestors like '%' || #{params.deptId} || '%')
        and rfi.dzbm = #{dzbm}
        <if test="params.sfzcjz != null and params.sfzcjz != ''">
            and rff.sfzcjz :: text = #{params.sfzcjz}
        </if>
        <if test="params.fzXm != null and params.fzXm != ''">
            and rff.fz_xm = #{params.fzXm}
        </if>
        <if test="params.fzDh != null and params.fzDh != ''">
            and rff.fz_dh = #{params.fzDh}
        </if>
        <if test="params.fwLb != null and params.fwLb != ''">
            and rfi.fw_lb = #{params.fwLb}
        </if>
        <if test="params.fwYt != null and params.fwYt != ''">
            and rfi.fw_yt = #{params.fwYt}
        </if>
        <if test="params.fwXz != null and params.fwXz != ''">
            and rfi.fw_xz = #{params.fwXz}
        </if>
        <if test="params.fwSyyt != null and params.fwSyyt != ''">
            and rfi.fw_syyt = #{params.fwSyyt}
        </if>
    </select>
</mapper>
