<?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.lutao.cloud.oil.mapper.TagAlarmClmMapper">


    <select id="selectTagAlarmClmPoint" resultType="com.lutao.cloud.oil.pojo.vo.TagAlarmClmPointVO">

        SELECT COUNT(DISTINCT tag_code) AS alarmPointNumber, tag_type
        FROM tag_alarm_clm
        WHERE DATE (alarm_timestamp)= CURRENT_DATE
        GROUP BY tag_type
    </select>
    <select id="selectTagProcessAlarmByUnit" resultType="com.lutao.cloud.oil.pojo.vo.TagProcessAlarmByUnit">
        SELECT
        ai.app_name,
        COALESCE(COUNT(tac.app_id), 0) AS alarmNumber
        FROM
        app_info ai
        LEFT JOIN tag_alarm_clm tac ON ai.id = tac.app_id AND tac.tag_type IN (2, 3, 5)
        <where>
            <if test="appId != null and appId != ''">
                AND tac.app_id = #{appId}
            </if>
            <if test="platformCode != null and platformCode != ''">
                AND tac.platform_code = #{platformCode}
            </if>
        </where>
        GROUP BY
        ai.app_name
    </select>
    <select id="selectTagAlarmClmNumber" resultType="com.lutao.cloud.oil.pojo.vo.TagAlarmClmPointVO">
        SELECT COUNT(1) AS alarmNumber, tag_type
        FROM tag_alarm_clm
        WHERE DATE (alarm_timestamp)= CURRENT_DATE
        GROUP BY tag_type
    </select>
    <select id="selectTagAlarmClmCancelNumber" resultType="com.lutao.cloud.oil.pojo.vo.TagAlarmClmPointVO">
        select tag_type,
               count(1)
        from tag_alarm_clm tac
        where tac.state = 00
          and DATE (alarm_timestamp)= CURRENT_DATE
        group by
            tag_type
    </select>
    <select id="getTagAlarmQoqList" resultType="com.lutao.cloud.oil.pojo.vo.TagAlarmQoqListVO">
        SELECT
        CASE
        WHEN tac.tag_type = 2 THEN '温度'
        WHEN tac.tag_type = 3 THEN '压力'
        WHEN tac.tag_type = 5 THEN '液位'
        END AS tagType,
        COUNT(1) as thisCycleAlarmNumber
        FROM
        tag_alarm_clm tac
        LEFT JOIN platform_info pi ON tac.platform_code = pi.platform_code
        <where>
            pi.delete_flag = '0'
            and tac.tag_type IN (2, 3, 5)
            <if test="param.startDate != null and param.endDate != null">
                and alarm_timestamp &gt;= #{param.startDate}
                and alarm_timestamp &lt;= #{param.endDate}
            </if>
            <if test="param.platformId != null and param.platformId != ''">
                and tac.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and tac.app_id = #{param.appId}
            </if>
            <if test="param.operatingCompany != null and param.operatingCompany != ''">
                and pi.operating_company = #{param.operatingCompany}
            </if>
            <if test="param.oilGasName != null and param.oilGasName != ''">
                and pi.oil_gas_name = #{param.oilGasName}
            </if>
        </where>

        GROUP BY
        tagType

    </select>
    <select id="alarmStatisticByAppIdByTagType"
            resultType="com.lutao.cloud.oil.pojo.vo.AlarmStatisticByAppIdByTagType">
        SELECT CASE
                   WHEN
                       tac.tag_type = 2 THEN
                       '温度'
                   WHEN tac.tag_type = 3 THEN
                       '压力'
                   WHEN tac.tag_type = 5 THEN
                       '液位'
                   END  AS tagType,
               count(1) AS alarmNumber,
               ai.app_name
        FROM tag_alarm_clm tac
                 LEFT JOIN platform_info pi ON tac.platform_code = pi.platform_code
                 LEFT JOIN app_info ai ON tac.app_id = ai.id
        --WHERE pi.delete_flag = '0' and tac.tag_type IN (2, 3, 5)
            <where>
                pi.delete_flag = '0'
                and tac.tag_type IN (2, 3, 5)
                <if test="param.platformId != null and param.platformId != ''">
                    and tac.platform_code = #{param.platformId}
                </if>
                <if test="param.appId != null and param.appId != ''">
                    and tac.app_id = #{param.appId}
                </if>
            </where>
        GROUP BY tagType, ai.app_name
    </select>
</mapper>