<?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="com.zcloud.fire.check.persistence.mapper.FirePointMapper">

    <select id="countByFireRegionIdIn" resultType="com.zcloud.fire.check.persistence.dataobject.FirePointStatisticDO">
        select fire_region_id as fireRegionId, count(*) as firePointCount
        from fire_point where delete_enum='FALSE' and fire_region_id in
        <foreach collection="fireRegionIds" item="fireRegionId" open="(" close=")" separator=",">
            #{fireRegionId}
        </foreach>
        group by fire_region_id
    </select>

    <select id="getFirePointPage" resultType="com.zcloud.fire.check.persistence.dataobject.FirePointDO">
        SELECT
        p.*,
        r.department_id as departmentId,
        r.fire_region_name as fireRegionName,
        d.name as departmentName,
        u.name as userName
        FROM fire_point p
        LEFT JOIN fire_region r ON r.fire_region_id = p.fire_region_id
        LEFT JOIN department d ON r.department_id = d.id
        LEFT JOIN user u ON p.user_id = u.id
        WHERE p.delete_enum = 'FALSE'
        <if test="params.ids != null and params.ids.length > 0">
            AND p.id IN
            <foreach collection="params.ids" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="params.corpinfoId != null and params.corpinfoId != ''">
            AND p.corpinfo_id = #{params.corpinfoId}
        </if>
        <if test="params.fireRegionId != null and params.fireRegionId != ''">
            AND p.fire_region_id = #{params.fireRegionId}
        </if>
        <if test="params.firePointName != null and params.firePointName != ''">
            AND p.fire_point_name LIKE CONCAT('%', #{params.firePointName}, '%')
        </if>
        <if test="params.firePointCode != null and params.firePointCode != ''">
            AND p.fire_point_code LIKE CONCAT('%', #{params.firePointCode}, '%')
        </if>
        <if test="params.departmentId != null">
            AND r.department_id = #{params.departmentId}
        </if>
          <if test="params.nfcFlag != null">
            AND p.nfc_flag = #{params.nfcFlag}
          </if>
        <!-- 经纬度判断：punctuationFlag 1-有经纬度，0-无经纬度 -->
        <choose>
            <when test="params.punctuationFlag != null and params.punctuationFlag == 1">
                AND p.latitude IS NOT NULL AND p.longitude IS NOT NULL
            </when>
            <when test="params.punctuationFlag != null and params.punctuationFlag == 0">
                AND (p.latitude IS NULL OR p.longitude IS NULL)
            </when>
        </choose>
        ORDER BY p.create_time DESC
    </select>
    <select id="getInfoById" resultType="com.zcloud.fire.check.persistence.dataobject.FirePointDO">
        SELECT
            p.*,
            r.department_id as departmentId,
            r.fire_region_name as fireRegionName,
            d.name as departmentName,
            u.name as userName
        FROM fire_point p
                 LEFT JOIN fire_region r ON r.fire_region_id = p.fire_region_id
                 LEFT JOIN department d ON r.department_id = d.id
                 LEFT JOIN user u ON p.user_id = u.id
        WHERE p.delete_enum = 'FALSE' and p.id = #{id}
    </select>
    <select id="getInfoByUUId" resultType="com.zcloud.fire.check.persistence.dataobject.FirePointDO">
        select p.*, r.fire_region_name as fireRegionName, r.fire_region_code as fireRegionCode
        from fire_point p
                 left join fire_region r on p.fire_region_id = r.fire_region_id
        where p.fire_point_id = #{firePointId}
          and p.delete_enum = 'FALSE'
    </select>
    <select id="countByCorpinfoIds" resultType="com.zcloud.fire.check.persistence.dataobject.DataStatisticDO">
        select p.corpinfo_id as corpinfoId,count(1) as totalCount
        from fire_point p
        where p.delete_enum = 'FALSE'
        and p.corpinfo_id in
        <foreach collection="corpinfoIds" item="corpinfoId" open="(" close=")" separator=",">
            #{corpinfoId}
        </foreach>
        group by p.corpinfo_id
    </select>
    <select id="getFirePointCount" resultType="long">
        select count(1)
        from fire_point p
        inner join corp_info c on p.corpinfo_id = c.id
        where c.type in
        <foreach collection="corpTypeList" item="corpType" open="(" close=")" separator=",">
            #{corpType}
        </foreach>
        and c.use_flag = 1
        and p.delete_enum = 'FALSE'
    </select>
</mapper>

