<?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.yjxt.business.resident.mapper.DangerAreaResidentInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="desidentResultMap" type="org.springblade.modules.yjxt.business.resident.entity.DangerAreaResidentInfoEntity">
        <result column="person_id" property="personId"/>
        <result column="person_name" property="personName"/>
        <result column="is_householder" property="isHouseholder"/>
        <result column="householder_id" property="householderId"/>
        <result column="danger_area_pid" property="dangerAreaPid"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="address" property="address"/>
        <result column="register_type" property="registerType"/>
    </resultMap>

    <resultMap id="residentVOResultMap" type="org.springblade.modules.yjxt.business.resident.vo.DangerAreaResidentInfoVO">
        <result column="person_id" property="personId"/>
        <result column="person_name" property="personName"/>
        <result column="is_householder" property="isHouseholder"/>
        <result column="householder_id" property="householderId"/>
        <result column="danger_area_pid" property="dangerAreaPid"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="address" property="address"/>
        <result column="danger_area_name" property="dangerAreaName"/>
        <result column="adnm" property="adName"/>
        <result column="adcd" property="adCode"/>
        <result column="register_type" property="registerType"/>
    </resultMap>

    <select id="selectPage" resultMap="residentVOResultMap">

        SELECT
        n.*,a.adcd,a.adnm,d.danger_area_name
        FROM
        yjxt_danger_area_resident_info n
        LEFT JOIN map_danger_area_info d ON d.danger_area_pid = n.danger_area_pid and d.is_deleted=0
        LEFT JOIN base_data_ad_cd a ON a.adcd = d.adcd and a.is_deleted=0
        WHERE
        n.is_deleted = 0
        <if test="dto.isHouseholder!=null">
            and n.is_householder = #{dto.isHouseholder}
        </if>
        <if test="dto.householderId!=null">
            and (n.householder_id = #{dto.householderId} or n.person_id = #{dto.householderId})
        </if>
        <if test="dto.personName!=null">
            and n.person_name like concat(concat('%', #{dto.personName}), '%')
        </if>
        <if test="dto.dangerAreaName!=null">
            and d.danger_area_name like concat(concat('%', #{dto.dangerAreaName}), '%')
        </if>
        <if test="dto.adName!=null">
            and a.adnm like concat(concat('%', #{dto.adName}), '%')
        </if>
        <if test="dto.dangerAreaPid!=null">
            and n.danger_area_pid = #{dto.dangerAreaPid}
        </if>
        <if test="dto.dangerAreaCode!=null and dto.dangerAreaCode!=''">
            and (a.adcd =#{dto.dangerAreaCode} or  FIND_IN_SET(#{dto.dangerAreaCode} ,a.ancestors) >0 )
        </if>
        ORDER BY
        n.is_householder desc
    </select>

    <select id="residentCount" resultType="java.lang.Long">
        SELECT
        count(n.person_id)
        FROM
        yjxt_danger_area_resident_info n
        LEFT JOIN map_danger_area_info d ON d.danger_area_pid = n.danger_area_pid and d.is_deleted = 0
        WHERE
        n.is_deleted = 0
        <if test="dto.householderId != null">
            and (n.householder_id = #{dto.householderId} || n.person_id = #{dto.householderId})
        </if>
        <if test="dto.isHouseholder!=null">
            and n.is_householder = #{dto.isHouseholder}
        </if>
        <if test="dto.dangerAreaCode!=null">
            and d.adcd = #{dto.dangerAreaCode}
        </if>
        <if test="dto.dangerAreaPid!=null">
            and n.danger_area_pid = #{dto.dangerAreaPid}
        </if>
    </select>

    <select id="getFamilyMembers" resultMap="desidentResultMap">
        SELECT
        n.*
        FROM
        yjxt_danger_area_resident_info n
        WHERE
        n.is_deleted = 0
        <if test="dto.personId!=null">
            and (n.person_id =  #{dto.personId} or n.householder_id = #{dto.personId})
        </if>
        <if test="dto.isHouseholder!=null">
            and n.is_householder = #{dto.isHouseholder}
        </if>
        <if test="dto.dangerAreaPid!=null">
            and n.danger_area_pid = #{dto.dangerAreaPid}
        </if>
        ORDER BY
        n.is_householder desc
    </select>

    <select id="getFamilyPoints" resultMap="desidentResultMap">
        SELECT
        n.*
        FROM
        yjxt_danger_area_resident_info n
        WHERE
        n.is_deleted = 0
        and n.is_householder = 1
        and n.longitude is not null
        and n.latitude is not null
        <if test="dto.dangerAreaPid!=null">
            and n.danger_area_pid = #{dto.dangerAreaPid}
        </if>
    </select>
</mapper>
