<?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.yeejoin.amos.boot.module.ccs.api.mapper.FireStationInfoMapper">

    <select id="queryStationListWithStatistics"
            resultType="com.yeejoin.amos.boot.module.ccs.api.dto.FireStationInfoDto">
        SELECT
        i.*,
        IFNULL((SELECT sum(s.report_times) from asf_fire_signal_day_statistics s where s.collect_date = CURRENT_DATE and
        s.station_code = i.`code`),0) as todayAlarmTimes,
        IFNULL((SELECT count(1) from asf_fire_latent_danger d where d.deal_state = 1 and d.station_code = i.`code`),0)
        as existDangerNumber
        FROM
        `asf_fire_station_info` i
        where i.status = false
        <if test="orderBy != null and orderBy != ''">
            ${orderBy}
        </if>
    </select>
    <select id="queryStationEquipInfo"
            resultType="java.util.Map">
        SELECT
        si.id,
        si.code,
        si.`name`,
        (select count(1) from ast_fire_equipment e where e.station_code = si.code and e.is_iot = true) as
        iotEquipmentNumber,
        (select count(DISTINCT(l.fire_equipment_mrid)) from asf_fire_equipment_signal_log l where l.station_code = si.code and
        l.signal_report_date =CURRENT_DATE and l.is_alarm = true) as todayAlarmEquipmentNumber
        FROM `asf_fire_station_info` si
        <where>
            <if test="stationCode != null and stationCode != ''">
                si.code = #{stationCode}
            </if>
            <if test="stationName != null and stationName != ''">
                si.name like concat('%', #{stationName},'%')
            </if>
        </where>
        order by iotEquipmentNumber desc
    </select>
    <select id="lastYearAlarmCount" resultType="java.util.Map">
        SELECT
            s.code,
            s.name,
        (select
        count(DISTINCT(l.fire_equipment_mrid))
        from asf_fire_equipment_signal_log l
        where l.is_alarm =true and
        l.signal_report_date >= DATE_sub(CURDATE(),INTERVAL 1 year)
        and l.signal_report_date <![CDATA[ <= ]]> CURDATE() and l.station_code = s.code) as alarmEquipNumber,
        ifnull((SELECT sum(ds.report_times) from asf_fire_signal_day_statistics ds
        where ds.is_alarm =true and
        ds.collect_date >= DATE_sub(CURDATE(),INTERVAL 1 year)
        and ds.collect_date <![CDATA[ <= ]]> CURDATE()
        and ds.station_code = s.code),0) as alarmTimes
        FROM
        `asf_fire_station_info` s
    </select>

    <select id="queryStationAlarmListByCode" resultType="com.yeejoin.amos.boot.module.ccs.api.dto.FireStationInfoDto">
        SELECT
        i.*
        FROM
        `asf_fire_station_info` i
        where i.status = false
        <if test="stationCode != null and stationCode != ''">
            and i.code = #{stationCode,jdbcType=VARCHAR}
        </if>
    </select>

    <select id="queryAlarmCountByCode" resultType="com.yeejoin.amos.boot.module.ccs.api.vo.AlarmCountVO">
        SELECT
            station_code,
            sum( is_alarm ) as alarmNum,
            sum( confirm_state ) as confirmTrueNum,
            count(*) - sum( confirm_state ) as confirmfalseNum
        FROM
            asf_fire_equipment_signal_log
        GROUP BY
            station_code
    </select>

    <select id="queryAllAlarmCountByCode" resultType="com.yeejoin.amos.boot.module.ccs.api.vo.AlarmCountVO">
        SELECT
            station_code,
            count(*) as alarmNum
        FROM
            asf_fire_equipment_signal_log
        <where>
            <if test="beginDateTime != null and beginDateTime != '' and endDateTime != null and endDateTime != ''">
                and confirm_date between #{beginDateTime} and #{endDateTime}
            </if>
        </where>
        GROUP BY
            station_code
    </select>

    <select id="queryAlarmCountByDateAndCode" resultType="java.lang.Integer">
        select count(esl.id) from asf_fire_station_info asi
        left join asf_fire_equipment_signal_log esl on asi.code = esl.station_code
        <where>
            1 = 1
            <if test="stationCode != null and stationCode != ''">
                and esl.station_code = #{stationCode,jdbcType=VARCHAR}
            </if>
            <if test="date != null and date != ''">
                and esl.confirm_date like concat(#{date,jdbcType=VARCHAR},'%')
            </if>
        </where>
    </select>

    <select id="getAlarmTrendByDatePeriod" resultType="com.yeejoin.amos.boot.module.ccs.api.vo.DateAlarmCountVO">
        select
        (SELECT
        IFNULL(count(s.id),0)
        FROM  asf_fire_station_info asi
        LEFT JOIN `asf_fire_equipment_signal_log` s ON asi.code = s.station_code
        <where>
            <if test="stationCode != null and stationCode != ''">
                and s.station_code = #{stationCode}
            </if>
            and s.confirm_date like concat(t.date,'%')
        </where>
        ) as alarmNums,
        date
        from
        (SELECT
        DATE_FORMAT(DATE( DATE_ADD( #{beginDate}, INTERVAL @s DAY )),'%Y-%m-%d') AS date,
        @s := @s + 1 AS `index`
        FROM
        mysql.help_topic,
        ( SELECT @s := 0 ) temp
        WHERE
        @s <![CDATA[<=]]>
        DATEDIFF(#{endDate},#{beginDate})) t
        GROUP BY t.date
    </select>

    <select id="getAlarmNumByDateTimePeriodByStationCode"
            resultType="com.yeejoin.amos.boot.module.ccs.api.vo.DateAlarmCountVO">
        select
        (SELECT
        IFNULL(count(s.id),0)
        FROM `asf_fire_equipment_signal_log` s
        <where>
            <if test="stationCode != null and stationCode != ''">
                and s.station_code = #{stationCode}
            </if>
            and s.confirm_date like concat(date ,'%')
        </where>
        ) as alarmNums,
        concat(date ,'时') as date
        from
        (SELECT
        DATE_FORMAT( DATE_ADD( #{beginDateTime}, INTERVAL @s HOUR ),'%Y-%m-%d %H') AS date,
        @s := @s + 1 AS `index`
        FROM
        mysql.help_topic,
        ( SELECT @s := 1 ) temp
        WHERE
        @s <![CDATA[<=]]>
        hour(TIMEDIFF(#{endDateTime},#{beginDateTime}))) t
        GROUP BY t.date
    </select>
</mapper>
