<?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.jxiop.biz.mapper2.IdxBizFanWarningRecordMapper">
    
<select id="getEquipWarningInfoByPage"
            resultType="com.yeejoin.amos.boot.module.jxiop.biz.entity.IdxBizFanWarningRecord">
    SELECT
        a.*
    FROM
        (
            SELECT
                REC_DATE AS recDate,
                STATION AS station,
                EQUIPMENT_NAME AS equipmentName,
                CONTENT AS content,
                WARNING_NAME AS warningName,
                DISPOSOTION_STATE AS disposotionState,
                ARAE AS arae,
                'pv' AS stationType,
                GATEWAY_ID,
                concat(POINT_NAME, '异常') AS pointName,
                DISPOSOTION_DATE as disposotionDate,
                (case when WARNING_NAME = '注意' then 1
                     when WARNING_NAME = '警告' then 2
                     when WARNING_NAME = '危险' then 3
                end) as sort
            FROM
                idx_biz_pv_warning_record UNION ALL
            SELECT
                REC_DATE AS recDate,
                STATION AS station,
                EQUIPMENT_NAME AS equipmentName,
                CONTENT AS content,
                WARNING_NAME AS warningName,
                DISPOSOTION_STATE AS disposotionState,
                ARAE AS arae,
                'fan' AS stationType,
                GATEWAY_ID,
                concat(POINT_NAME, '异常') AS pointName,
                DISPOSOTION_DATE as disposotionDate,
                (case when WARNING_NAME = '注意' then 1
                when WARNING_NAME = '警告' then 2
                when WARNING_NAME = '危险' then 3
                end) as sort
            FROM
                idx_biz_fan_warning_record
        ) a
    <where>
        ((a.disposotionState = '待确认') or (a.disposotionState = '已确认' AND a.disposotionDate > DATE_ADD( now(), INTERVAL - 3 DAY )))
        <if test="arae != '' and arae != null">
            AND a.arae like concat('%', #{arae}, '%')
        </if>
        <if test="station != '' and station != null">
            AND a.station = #{station}
        </if>
        <if test="stationType != '' and stationType != null">
            AND a.stationType = #{stationType}
        </if>
        <if test="warningName != '' and warningName != null">
            AND a.warningName = #{warningName}
        </if>
        <if test="stationId != '' and stationId != null">
            AND a.GATEWAY_ID = #{stationId}
        </if>
        <if test="startDate != '' and startDate != null">
            AND a.recDate >= concat(#{startDate}, ' 00:00:00')
        </if>
        <if test="endDate != '' and endDate != null">
            AND concat(#{endDate}, ' 23:59:59') >= a.recDate
        </if>
    </where>
    order by sort DESC, recDate DESC
    limit #{current}, #{size}
</select>

<select id="getEquipWarningInfoByPageCount" resultType="java.lang.Integer">
    select count(1) from
    (SELECT
    a.*
    FROM
    (
    SELECT
    REC_DATE AS recDate,
    STATION AS station,
    EQUIPMENT_NAME AS equipmentName,
    CONTENT AS content,
    WARNING_NAME AS warningName,
    DISPOSOTION_STATE AS disposotionState,
    ARAE AS arae,
    'pv' AS stationType,
    GATEWAY_ID,
    DISPOSOTION_DATE as disposotionDate
    FROM
    idx_biz_pv_warning_record UNION ALL
    SELECT
    REC_DATE AS recDate,
    STATION AS station,
    EQUIPMENT_NAME AS equipmentName,
    CONTENT AS content,
    WARNING_NAME AS warningName,
    DISPOSOTION_STATE AS disposotionState,
    ARAE AS arae,
    'fan' AS stationType,
    GATEWAY_ID,
    DISPOSOTION_DATE as disposotionDate
    FROM
    idx_biz_fan_warning_record
    ) a
    <where>
        ((a.disposotionState = '待确认') or (a.disposotionState = '已确认' AND a.disposotionDate > DATE_ADD( now(), INTERVAL - 3 DAY )))
        <if test="arae != '' and arae != null">
            AND a.arae like concat('%', #{arae}, '%')
        </if>
        <if test="station != '' and station != null">
            AND a.station = #{station}
        </if>
        <if test="stationType != '' and stationType != null">
            AND a.stationType = #{stationType}
        </if>
        <if test="warningName != '' and warningName != null">
            AND a.warningName = #{warningName}
        </if>
        <if test="stationId != '' and stationId != null">
            AND a.GATEWAY_ID = #{stationId}
        </if>
        <if test="startDate != '' and startDate != null">
            AND a.recDate >= concat(#{startDate}, ' 00:00:00')
        </if>
        <if test="endDate != '' and endDate != null">
            AND concat(#{endDate}, ' 23:59:59') >= a.recDate
        </if>
    </where>) b
    </select>
</mapper>
