<?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.FireRegionMapper">

    <select id="getFireRegionPage" resultType="com.zcloud.fire.check.persistence.dataobject.FireRegionDO">
        select fr.*,d.name AS departmentName
        from fire_region fr
        left join department d on fr.department_id=d.id
        left join corp_info c on fr.corpinfo_id = c.id
        <where>
            fr.delete_enum = 'FALSE'
            <if test="params.corpinfoId != null ">
                AND fr.corpinfo_id = #{params.corpinfoId}
            </if>

            <if test="params.fireRegionName != null and params.fireRegionName != ''">
                and fr.fire_region_name like concat('%',#{params.fireRegionName},'%')
            </if>
            <if test="params.fireRegionCode != null and params.fireRegionCode != ''">
                and fr.fire_region_code like concat('%',#{params.fireRegionCode},'%')
            </if>
            <if test="params.departmentId != null">
                and fr.department_id = #{params.departmentId}
            </if>
            <if test="params.state != null">
                and fr.state = #{params.state}
            </if>
           order by fr.create_time desc
        </where>
    </select>
    <select id="countByCorpinfoIds" resultType="com.zcloud.fire.check.persistence.dataobject.DataStatisticDO">
        select fr.corpinfo_id as corpinfoId,count(1) as totalCount
        from fire_region fr
        where fr.delete_enum = 'FALSE'
        and fr.state = 0
        and fr.corpinfo_id in
        <foreach collection="corpinfoIds" item="corpinfoId" open="(" close=")" separator=",">
            #{corpinfoId}
        </foreach>
        group by fr.corpinfo_id
    </select>
    <select id="getFireRegionCount" resultType="long">
        select count(1)
        from fire_region r
         inner join corp_info c on r.corpinfo_id = c.id
        where c.type in
        <foreach collection="corpTypeList" item="corpType" open="(" close=")" separator=",">
            #{corpType}
        </foreach>
        and c.use_flag = 1
          and r.state = #{state}
         and r.delete_enum = 'FALSE'
    </select>
    <select id="listByCorpinfoId" resultType="com.zcloud.fire.check.persistence.dataobject.FireRegionDO">
        select fr.*,d.name AS departmentName
        from fire_region fr
        left join department d on fr.department_id=d.id
        where
        fr.delete_enum = 'FALSE'

            AND fr.corpinfo_id = #{corpinfoId}
        and fr.state = 0


        order by fr.create_time desc
    </select>
</mapper>

