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

    <resultMap id="perceptionMonitorAlarmInfoMap" type="com.lutao.entity.PerceptionMonitorAlarmInfo">
        <id property="id" column="id"/>
        <result property="originId" column="origin_id"/>
        <result property="pfId" column="pf_id"/>
        <result property="appId" column="app_id"/>
        <result property="platformCode" column="platform_code"/>
        <result property="platformName" column="platform_name"/>
        <result property="platformId" column="platform_id"/>
        <result property="equipId" column="equip_id"/>
        <result property="equipCode" column="equip_code"/>
        <result property="equipName" column="equip_name"/>
        <result property="tagId" column="tag_id"/>
        <result property="tagCode" column="tag_code"/>
        <result property="tagName" column="tag_name"/>
        <result property="alarmValue" column="alarm_value"/>
        <result property="collectTime" column="collect_time"/>
        <result property="lastUpdateTime" column="last_update_time"/>
        <result property="alarmState" column="alarm_state"/>
        <result property="alarmType" column="alarm_type"/>
        <result property="handleBy" column="handle_by"/>
        <result property="handleState" column="handle_state"/>
        <result property="alarmDuration" column="alarm_duration"/>
        <result property="createDate" column="create_date"/>
        <result property="createBy" column="create_by"/>
        <result property="updateDate" column="update_date"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>
    <select id="selectAlarmList" resultType="com.lutao.cloud.oil.pojo.vo.AlarmVO">
        SELECT pmai.equip_name,
        pmai.equip_code,
        pmai.alarm_value,
        pmai.collect_time,
        pmai.alarm_state,
        pmai.alarm_type,
        pmai.alarm_duration
        --CONCAT(t.limit_up,'-',t.limit_lo) AS limits
        FROM perception_monitor_alarm_info pmai
        -- LEFT JOIN equipment_tag t ON pmai.tag_id = t.origin_id
        <where>
            <if test="platformId != null and platformId != ''">
                pmai.platform_code = #{platformId}
            </if>
            <!--            <if test="tagType != null">-->
            <!--                 and t.tag_type = #{tagType}-->
            <!--            </if>-->
        </where>
    </select>

    <select id="selectDetectionAlarmList" resultType="com.lutao.cloud.oil.pojo.vo.DetectionAlarmVO">
        SELECT
        pi.platform_name,
        pi.oil_gas_name,
        pi.operating_company,
        pmai.tag_code AS equipCode,
        pmai.alarm_value,
        pmai.alarm_timestamp AS collectTime,
        pmai.state AS alarmState,
        pmai.alarm_type,
        pmai.alarm_duration AS alarmDuration,
        ai.app_name AS limits
        FROM tag_alarm_clm pmai
        LEFT JOIN app_info ai ON pmai.app_id = ai.id
        LEFT JOIN platform_info pi ON pmai.platform_code = pi.platform_code
        <where>
            pi.delete_flag = '0'
            <if test="platformId != null and platformId != ''">
              and  pmai.platform_code = #{platformId}
            </if>
            <if test="oilGasName != null and oilGasName != ''">
                and pi.oil_gas_name = #{oilGasName}
            </if>
            <if test="operatingCompany != null and operatingCompany != ''">
                and pi.operating_company = #{operatingCompany}
            </if>
            <if test="tagType != null">
                and pmai.tag_type = #{tagType}
            </if>
            <if test="appId != null and appId != ''">
                and pmai.app_id = #{appId}
            </if>
            <if test="startDate != null">
                and DATE(pmai.alarm_timestamp) &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                and DATE(pmai.alarm_timestamp) &lt;= #{endDate}
            </if>
            <if test="startDate == null and endDate == null">
                -- and DATE(pmai.alarm_timestamp) = DATE(NOW())
            </if>
        </where>
        ORDER BY pmai.alarm_timestamp DESC
    </select>
    <select id="getEquipmentTagAlarm" resultType="com.lutao.cloud.oil.pojo.vo.EquipmentTagAlarmVO">

        SELECT
        pi.platform_name AS facilityName,
        pi.oil_gas_name AS oilGasName,
        pi.operating_company AS operatingCompany,
        ai.app_name AS appName,
        tac.tag_name AS equipName,
        tac.tag_code AS equipmentCode,
        tac.alarm_value AS monitorValue,
        tac.tag_type AS tagType,
        tac.alarm_timestamp AS collectTime,
        tac.alarm_duration AS alarmDuration,
        tac.alarm_type AS alarmType,
        tac.state AS alarmStatus,
        ppt.unit AS unit,
        CONCAT(CONCAT(CONCAT( IFNULL(threshold_lo,'-'), '~', IFNULL(threshold_up,'-') ),'(',CONCAT( IFNULL(threshold_los,'-'), '~', IFNULL(threshold_ups,'-') )),')') AS limits
        FROM tag_alarm_clm tac
        LEFT JOIN platform_info pi ON tac.platform_code = pi.platform_code
        LEFT JOIN process_parameters_tag ppt ON tac.tag_code = ppt.tag_code
        LEFT JOIN app_info ai ON ai.id = tac.app_id
        <where>
            pi.delete_flag = '0' and ppt.del_flag = '0'
            <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.oilGasName != null and param.oilGasName">
                and pi.oil_gas_name = #{param.oilGasName}
            </if>
            <if test="param.operatingCompany != null and param.operatingCompany != ''">
                and pi.operating_company = #{param.operatingCompany}
            </if>
            <if test="param.paramType != null and param.paramType != ''">
                and tac.tag_type = #{param.paramType}
            </if>
            <if test="true">
                and tac.tag_type IN (2,3,5)
            </if>
            <if test="param.startDate != null">
                and DATE(tac.alarm_timestamp) &gt;= #{param.startDate}
            </if>
            <if test="param.endDate != null">
                and DATE(tac.alarm_timestamp) &lt;= #{param.endDate}
            </if>
            <if test="param.alarmType != null and param.alarmType != ''">
                and tac.alarm_type = #{param.alarmType}
            </if>
            <if test="param.alarmStatus != null and param.alarmStatus != ''">
                and tac.state = #{param.alarmStatus}
            </if>
        </where>
        ORDER BY collectTime DESC
    </select>
    <select id="getAlarmTrend" resultType="com.lutao.cloud.oil.pojo.vo.TagAlarmTrendVO">
        SELECT DATE(alarm_timestamp) AS alarmDate, COUNT(*) AS alarmCount
        FROM tag_alarm_clm
        <where>
            <if test="true">
                YEAR(alarm_timestamp) = YEAR(CURDATE()) AND MONTH(alarm_timestamp) = MONTH(CURDATE())
            </if>
            <if test="param.platformId != null and param.platformId != ''">
                and platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and app_id = #{param.appId}
            </if>
            <if test="param.paramType != null">
                and tag_type = #{param.paramType}
            </if>

        </where>
        GROUP BY alarmDate
        ORDER BY alarmDate ASC
    </select>
    <select id="getScreenTagAlarm" resultType="com.lutao.cloud.oil.pojo.vo.ScreenTagAlarmVO">
        SELECT tac.tag_name        AS equipName,
               tac.alarm_value     AS monitorValue,
               tac.tag_type        AS tagType,
               tac.alarm_timestamp AS alarmTime,
               ppt.unit            AS unit,
               ai.app_name         AS appName
        FROM tag_alarm_clm tac
                 LEFT JOIN app_info ai ON ai.id = tac.app_id
                 LEFT JOIN process_parameters_tag ppt ON tac.tag_code = ppt.tag_code
        WHERE tac.tag_type IN (2, 3, 5)
          AND DATE (tac.alarm_timestamp) = DATE (NOW()) and ppt.del_flag = '0'
    </select>
    <select id="getDetectAlarmChart" resultType="com.lutao.cloud.oil.pojo.vo.DetectAlarmChartVO">

        SELECT
        DATE(alarm_timestamp) AS alarmDate,
        COUNT(*) AS alarmCount
        FROM
        tag_alarm_clm
        <where>
            <if test="param.platformId != null and param.platformId != ''">
                and platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and app_id = #{param.appId}
            </if>
            <if test="param.tagType != null and param.tagType != ''">
                and tag_type = #{param.tagType}
            </if>
            <choose>
                <when test="param.startDate != null">
                    and DATE(alarm_timestamp) &gt;= #{param.startDate}
                </when>
                <when test="param.endDate != null">
                    and DATE(alarm_timestamp) &lt;= #{param.endDate}
                </when>
                <otherwise>
                    and DATE(alarm_timestamp) &gt; DATE_SUB(CURDATE(), INTERVAL 1 MONTH) -- 筛选最近一周的数据
                </otherwise>
            </choose>
        </where>
        GROUP BY
        alarmDate
        ORDER BY
        alarmDate

    </select>
</mapper>
