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

    <resultMap id="workStausMap" type="com.lutao.entity.WorkStaus">
        <id property="id" column="id"/>
        <result property="originId" column="origin_id"/>
        <result property="equipId" column="equip_id"/>
        <result property="equipCode" column="equip_code"/>
        <result property="workStatus" column="work_status"/>
        <result property="collectTime" column="collect_time"/>
        <result property="toLevle" column="to_levle"/>
        <result property="changeReason" column="change_reason"/>
        <result property="valveState" column="valve_state"/>
        <result property="createDate" column="create_date"/>
        <result property="createBy" column="create_by"/>
        <result property="updateDate" column="update_date"/>
        <result property="updateBy" column="update_by"/>
        <result property="pfId" column="pf_id"/>
        <result property="appId" column="app_id"/>
        <result property="platformId" column="platform_id"/>
        <result property="platformCode" column="platform_code"/>
        <result property="platformName" column="platform_name"/>
    </resultMap>
    <select id="getEquipmentWorkStatusAlarm"
            resultType="com.lutao.cloud.oil.pojo.vo.EquipmentWorkStatusAlarmVO">
        SELECT
        pi.platform_name AS platformName,
        ws.equip_name AS point,
        -- ws.`status` AS workStatus,
        ws.collect_time AS collectTime,
        ai.app_name AS appName,
        pi.oil_gas_name AS oilGasName,
        pi.operating_company AS operatingCompany,
        ws.to_levle AS toLevle,
        ws.change_reason AS changeReason,
        t.work_status AS workStatus,
        ws.descs AS `desc`
        FROM
        work_status_clm ws
        LEFT JOIN app_info ai ON ws.app_id = ai.id
        LEFT JOIN platform_info pi ON ws.platform_code = pi.platform_code
        LEFT JOIN (
        SELECT
        *
        FROM
        work_status_monitor AS wsm
        WHERE
        ( wsm.equip_code, wsm.collect_time ) IN ( SELECT equip_code, MAX( collect_time ) AS latest_created_at FROM work_status_monitor GROUP BY equip_code )
        ORDER BY
        wsm.equip_code,
        wsm.collect_time DESC
        ) t ON ws.equip_code = t.equip_code
        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and ws.platform_code = #{param.platformId}
            </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.appId != null and param.appId != ''">
                and ws.app_id = #{param.appId}
            </if>
            <if test="param.workStatus != null">
                and t.work_status = #{param.workStatus}
            </if>
            <if test="param.showUnitClose == 1">
                and ws.to_levle != 2
            </if>
            <if test = "true">
                and t.work_status  is NOT null
            </if>
            <if test="param.collectStartTime != null">
                and ws.collect_time &gt;= #{param.collectStartTime}
            </if>
            <if test="param.collectEndTime != null">
                and ws.collect_time &lt;= #{param.collectEndTime}
            </if>
            <if test="param.toLevle != null and param.toLevle != ''">
                and ws.to_levle = #{param.toLevle}
            </if>
        </where>
        ORDER BY
        collectTime DESC
    </select>
</mapper>
