<?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.FireDeviceMapper">
        <select id="countByFireRegionIdIn" resultType="com.zcloud.fire.check.persistence.dataobject.FireDeviceStatisticDO">
            select fire_point_id as firePointId, count(*) as fireDeviceCount
            from fire_device where delete_enum='FALSE' and fire_point_id in
            <foreach collection="firePointIds" item="firePointId" open="(" close=")" separator=",">
                #{firePointId}
            </foreach>
            group by fire_point_id
        </select>

    <select id="getFireDevicePage" resultType="com.zcloud.fire.check.persistence.dataobject.FireDeviceDO">
        SELECT
            d.*,
            r.department_id as departmentId,
            p.fire_point_name as firePointName,
            r.fire_region_name as fireRegionName,
            r.fire_region_code as fireRegionCode,
            p.fire_point_code as firePointCode,
            p.longitude ,
             p.latitude,
            dp.name as departmentName,
            u.name as userName
        FROM fire_device d
            LEFT JOIN fire_point p ON d.fire_point_id = p.fire_point_id
            LEFT JOIN fire_region r ON p.fire_region_id = r.fire_region_id
            LEFT JOIN department dp ON r.department_id = dp.id
            LEFT JOIN user u ON u.id = p.user_id
        WHERE d.delete_enum = 'false'
            AND d.corpinfo_id = #{params.corpinfoId}
        <if test="params.ids != null and params.ids.size > 0">
            AND d.id IN
            <foreach collection="params.ids" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
          <if test="params.firePointId != null and params.firePointId != ''">
                AND d.fire_point_id = #{params.firePointId}
            </if>
           <if test="params.fireDeviceType != null and params.fireDeviceType != ''">
                AND d.fire_device_type = #{params.fireDeviceType}
            </if>
            <if test="params.fireRegionId != null and params.fireRegionId != ''">
                AND d.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.fireDeviceCode != null and params.fireDeviceCode != ''">
                AND d.fire_device_code LIKE CONCAT('%', #{params.fireDeviceCode}, '%')
            </if>
            <if test="params.departmentId != null ">
              AND r.department_id = #{params.departmentId}
            </if>

            <if test="params.departmentName != null and params.departmentName != ''">
                AND dp.name LIKE CONCAT('%', #{params.departmentName}, '%')
            </if>
            <if test="params.userName != null and params.userName != ''">
                AND u.name LIKE CONCAT('%', #{params.userName}, '%')
            </if>
        order by  d.create_time desc

    </select>
    <select id="getAllList" resultType="com.zcloud.fire.check.persistence.dataobject.FireDeviceDO">
        SELECT
        d.*,
        r.department_id as departmentId,
        p.fire_point_name as firePointName,
        r.fire_region_name as fireRegionName,
        r.fire_region_code as fireRegionCode,
        p.fire_point_code as firePointCode,
        dp.name as departmentName,
        u.name as userName,
        po.post_name as userPostName
        FROM fire_device d
        LEFT JOIN fire_point p ON d.fire_point_id = p.fire_point_id
        LEFT JOIN fire_region r ON p.fire_region_id = r.fire_region_id
        LEFT JOIN department dp ON r.department_id = dp.id
        LEFT JOIN user u ON u.id = p.user_id
        left join post po on u.post_id = po.id
        WHERE d.delete_enum = 'false'
        AND d.corpinfo_id = #{params.corpinfoId}
        <if test="params.ids != null and params.ids.size > 0">
            AND d.id IN
            <foreach collection="params.ids" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
          <if test="params.fireDeviceIdList != null and !params.fireDeviceIdList.isEmpty()">
              AND d.fire_device_id IN
              <foreach collection="params.fireDeviceIdList" item="id" open="(" close=")" separator=",">
                  #{id}
              </foreach>
            </if>
        <if test="params.firePointId != null and params.firePointId != ''">
            AND d.fire_point_id = #{params.firePointId}
        </if>
        <if test="params.fireDeviceType != null and params.fireDeviceType != ''">
            AND d.fire_device_type = #{params.fireDeviceType}
        </if>
        <if test="params.fireRegionId != null and params.fireRegionId != ''">
            AND d.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.fireDeviceCode != null and params.fireDeviceCode != ''">
            AND d.fire_device_code LIKE CONCAT('%', #{params.fireDeviceCode}, '%')
        </if>
        <if test="params.departmentIdList != null and !params.departmentIdList.isEmpty()">
            AND r.department_id IN
            <foreach collection="params.departmentIdList" item="departmentId" open="(" close=")" separator=",">
                #{departmentId}
            </foreach>
        </if>

        <if test="params.departmentName != null and params.departmentName != ''">
            AND dp.name LIKE CONCAT('%', #{params.departmentName}, '%')
        </if>
        <if test="params.userName != null and params.userName != ''">
            AND u.name LIKE CONCAT('%', #{params.userName}, '%')
        </if>
        order by  d.create_time desc

    </select>
    <select id="getInfoById" resultType="com.zcloud.fire.check.persistence.dataobject.FireDeviceDO">
        SELECT
            d.*,
            r.department_id as departmentId,
            p.fire_point_name as firePointName,
            r.fire_region_name as fireRegionName,
            r.fire_region_code as fireRegionCode,
            p.fire_point_code as firePointCode,
            dp.name as departmentName,
            u.name as userName
        FROM fire_device d
                 LEFT JOIN fire_point p ON d.fire_point_id = p.fire_point_id
                 LEFT JOIN fire_region r ON p.fire_region_id = r.fire_region_id
                 LEFT JOIN department dp ON r.department_id = dp.id
                 LEFT JOIN user u ON u.id = p.user_id
        WHERE d.delete_enum = 'false'
          AND d.id = #{id}
    </select>
    <select id="getDeviceListByFirePointId" resultType="com.zcloud.fire.check.persistence.dataobject.FireDeviceDO">
        SELECT
            d.*,
            r.department_id as departmentId,
            p.fire_point_name as firePointName,
            r.fire_region_name as fireRegionName,
            r.fire_region_code as fireRegionCode,
            p.fire_point_code as firePointCode,
            dp.name as departmentName,
            u.name as userName
        FROM fire_device d
                 LEFT JOIN fire_point p ON d.fire_point_id = p.fire_point_id
                 LEFT JOIN fire_region r ON p.fire_region_id = r.fire_region_id
                 LEFT JOIN department dp ON r.department_id = dp.id
                 LEFT JOIN user u ON u.id = p.user_id
        WHERE d.delete_enum = 'false' and d.fire_point_id = #{firePointId}
    </select>
    <select id="countByCorpinfoIds" resultType="com.zcloud.fire.check.persistence.dataobject.DataStatisticDO">
        select d.corpinfo_id as corpinfoId,count(1) as totalCount
        from fire_device d
        where d.delete_enum = 'FALSE'
        and d.corpinfo_id in
        <foreach collection="corpinfoIds" item="corpinfoId" open="(" close=")" separator=",">
            #{corpinfoId}
        </foreach>
        group by d.corpinfo_id
    </select>
    <select id="getFireDeviceCount" resultType="long">
        select count(1)
        from fire_device 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.delete_enum = 'FALSE'
    </select>

    <select id="pageByPointId" resultType="com.zcloud.fire.check.persistence.dataobject.FireDeviceDO">
        SELECT
        d.*,
        r.department_id as departmentId,
        p.fire_point_name as firePointName,
        r.fire_region_name as fireRegionName,
        r.fire_region_code as fireRegionCode,
        p.fire_point_code as firePointCode,
        dp.name as departmentName,
        u.name as userName,
        c.corp_name as corpName
        FROM fire_device d
        LEFT JOIN fire_point p ON d.fire_point_id = p.fire_point_id
        LEFT JOIN fire_region r ON p.fire_region_id = r.fire_region_id
        LEFT JOIN department dp ON r.department_id = dp.id
        LEFT JOIN user u ON u.id = p.user_id
        left join corp_info c on d.corpinfo_id = c.id
        WHERE d.delete_enum = 'false'
        <if test="params.ids != null and params.ids.size > 0">
            AND d.id IN
            <foreach collection="params.ids" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="params.firePointId != null and params.firePointId != ''">
            AND d.fire_point_id = #{params.firePointId}
        </if>
        <if test="params.fireDeviceType != null and params.fireDeviceType != ''">
            AND d.fire_device_type = #{params.fireDeviceType}
        </if>
        <if test="params.fireRegionId != null and params.fireRegionId != ''">
            AND d.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.fireDeviceCode != null and params.fireDeviceCode != ''">
            AND d.fire_device_code LIKE CONCAT('%', #{params.fireDeviceCode}, '%')
        </if>
        <if test="params.departmentId != null ">
            AND r.department_id = #{params.departmentId}
        </if>

        <if test="params.departmentName != null and params.departmentName != ''">
            AND dp.name LIKE CONCAT('%', #{params.departmentName}, '%')
        </if>
        <if test="params.userName != null and params.userName != ''">
            AND u.name LIKE CONCAT('%', #{params.userName}, '%')
        </if>
        order by  d.create_time desc

    </select>
</mapper>

