<?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.devplatform.equipment.modules.alarms.dao.AlarmsLogDao">
  <!-- Result Map -->
  <resultMap id="BaseResultMap" type="com.devplatform.equipment.modules.alarms.bean.AlarmsLog">
    <result column="id" property="id"/>
    <result column="alarms_id" property="alarmsId"/>
    <result column="process_mode" property="processMode"/>
    <result column="plan_id" property="planId"/>
    <result column="sys_sign" property="sysSign"/>
    <result column="deleted" property="deleted"/>
    <result column="create_user_id" property="createUserId"/>
    <result column="create_time" property="createTime"/>
    <result column="update_user_id" property="updateUserId"/>
    <result column="update_time" property="updateTime"/>
    <result column="byx1" property="byx1"/>
    <result column="byx2" property="byx2"/>
    <result column="byx3" property="byx3"/>
  </resultMap>

  <!-- alarms_log table all fields -->
  <sql id="Base_Column_List">
        id,alarms_id,process_mode,plan_id,sys_sign,deleted,create_user_id,create_time,update_user_id,update_time,byx1,byx2,byx3
    </sql>

  <!-- 公共查询条件 -->
  <sql id="Example_Where_Clause">
    where deleted=0
    <if test="id!=null and id!=''">and id = #{id}</if>
    <if test="alarmsId!=null and alarmsId!=''">and alarms_id = #{alarmsId}</if>
    <if test="processMode!=null ">and process_mode = #{processMode}</if>
    <if test="planId!=null and planId!=''">and plan_id = #{planId}</if>
    <if test="sysSign!=null and sysSign!=''">and sys_sign = #{sysSign}</if>
    <if test="deleted!=null ">and deleted = #{deleted}</if>
    <if test="createUserId!=null and createUserId!=''">and create_user_id = #{createUserId}</if>
    <if test="createTime!=null ">and create_time = #{createTime}</if>
    <if test="updateUserId!=null and updateUserId!=''">and update_user_id = #{updateUserId}</if>
    <if test="updateTime!=null ">and update_time = #{updateTime}</if>
    <if test="byx1!=null and byx1!=''">and byx1 = #{byx1}</if>
    <if test="byx2!=null and byx2!=''">and byx2 = #{byx2}</if>
    <if test="byx3!=null and byx3!=''">and byx3 = #{byx3}</if>
  </sql>
  <select id="instructList" resultType="java.util.HashMap">
    SELECT aa.instruction,
    aa.result,
    CASE aa.result
    WHEN 1 THEN '成功'
    WHEN 2 THEN '失败'
    ELSE aa.result
    END AS resultName,
    aa.line_station_name AS lineStation,
    aa.action_name AS actionName,
    aa.source,
    CASE aa.source
    WHEN 1 THEN '手动执行'
    WHEN 2 THEN '联动执行'
    WHEN 3 THEN '预案执行'
    ELSE aa.source
    END AS 'sourceName',
    aa.alarms_code AS alarmsCode,
    aa.create_time AS createTime,
    sd.name AS resourceType,
    lr.id,
    lr.name AS resourceName,
    lr.code AS resourceCode,
    lr.type,
    lr.line_id AS lineId,
    lr.line_name AS lineName,
    lr.station_id AS stationId,
    lr.station_name AS stationName,
    lr.tier_id AS tierId,
    lr.sub_system AS subSystem,
    lr.sub_code AS subCode,
    lr.address_code AS addressCode,
    lr.port,
    CONCAT(lr.address_code, ':', lr.port) AS IPPORT,
    lr.xpoint,
    lr.ypoint,
    lr.deploy_location AS deployLocation,
    lr.attached_info AS attachedInfo,
    lr.status,
    CASE lr.status
    WHEN 1 THEN '正常'
    WHEN 0 THEN '不可用'
    ELSE lr.status END AS resourceStatus,
    lr.brand,
    '系统' AS createUser,
    '自动' AS triggerMode
    FROM (SELECT *
    FROM or_video_monitor
    UNION ALL
    SELECT *
    FROM or_gate
    UNION ALL
    SELECT *
    FROM or_intrude
    UNION ALL
    SELECT *
    FROM or_safety_check) aa
    LEFT JOIN li_resource lr ON lr.id = aa.resource_id AND lr.deleted = 0
    LEFT JOIN sys_dictionary sd ON sd.id = lr.type AND sd.deleted = 0
    WHERE aa.source = '3'
    <if test="par != null and par != ''">
      AND (INSTR(aa.alarms_code, #{par}) OR INSTR(aa.instruction, #{par}))
    </if>
    <if test="startTime != null and startTime != ''">
      AND aa.create_time &gt;= #{startTime}
    </if>
    <if test="endTime != null and endTime != ''">
      AND aa.create_time &lt;= #{endTime}
    </if>
    <choose>
      <when test="sort != null and sort != '' and order != null and order != ''">
        ORDER BY ${sort} ${order}
      </when>
      <otherwise>
        ORDER BY aa.create_time DESC
      </otherwise>
    </choose>
  </select>
</mapper>