<?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.ewarning.dao.EwarningDao">
  <!-- Result Map -->
  <resultMap id="BaseResultMap" type="com.devplatform.equipment.modules.ewarning.bean.Ewarning">
    <result column="id" property="id"/>
    <result column="equ_id" property="equId"/>
    <result column="equ_code" property="equCode"/>
    <result column="camera_code" property="cameraCode"/>
    <result column="type" property="type"/>
    <result column="ip" property="ip"/>
    <result column="remark" property="remark"/>
    <result column="warning_time" property="warningTime"/>
    <result column="area" property="area"/>
    <result column="area_code" property="areaCode"/>
    <result column="isvalid" property="isvalid"/>
    <result column="operate_man" property="operateMan"/>
    <result column="operate_man_name" property="operateManName"/>
    <result column="operate_time" property="operateTime"/>
    <result column="communication_data" property="communicationData"/>
    <result column="state" property="state"/>
    <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"/>
    <result column="byx4" property="byx4"/>
    <result column="byx5" property="byx5"/>
  </resultMap>

  <!-- e_warning table all fields -->
  <sql id="Base_Column_List">
		id,equ_id,equ_code,camera_code,type,ip,remark,warning_time,area,area_code,isvalid,
		operate_man,operate_man_name,operate_time,communication_data,state,deleted,create_user_id,
		create_time,update_user_id,update_time,byx1,byx2,byx3,byx4,byx5
	</sql>

  <!-- 公共查询条件 -->
  <sql id="Example_Where_Clause">
    where ew.deleted=0
    <if test="model.id!=null and model.id!=''">and ew.id = #{model.id}</if>
    <if test="model.equId!=null and model.equId!=''">and ew.equ_id = #{model.equId}</if>
    <if test="model.equCode!=null and model.equCode!=''">and ew.equ_code = #{model.equCode}</if>
    <if test="model.cameraCode!=null and model.cameraCode!=''">and ew.camera_code = #{model.cameraCode}</if>
    <if test="model.type!=null ">and ew.type = #{model.type}</if>
    <if test="model.ip!=null and model.ip!=''">and ew.ip = #{model.ip}</if>
    <if test="model.remark!=null and model.remark!=''">and ew.remark = #{model.remark}</if>
    <if test="model.warningTime!=null ">and ew.warning_time = #{model.warningTime}</if>
    <if test="model.area!=null and model.area!=''">and INSTR(ew.area ,#{model.area})</if>
    <if test="model.areaCode!=null and model.areaCode!=''">and ew.area_code = #{model.areaCode}</if>
    <if test="model.isvalid!=null ">and ew.isvalid = #{model.isvalid}</if>
    <if test="model.operateMan!=null and model.operateMan!=''">and ew.operate_man = #{model.operateMan}</if>
    <if test="model.operateManName!=null and model.operateManName!=''">and ew.operate_man_name =
      #{model.operateManName}
    </if>
    <if test="model.operateTime!=null ">and ew.operate_time = #{model.operateTime}</if>
    <if test="model.communicationData!=null ">and ew.communication_data = #{model.communicationData}</if>
    <if test="model.state!=null ">and ew.state = #{model.state}</if>
    <if test="model.deleted!=null ">and ew.deleted = #{model.deleted}</if>
    <if test="model.createUserId!=null and model.createUserId!=''">and ew.create_user_id = #{model.createUserId}
    </if>
    <if test="model.createTime!=null ">and ew.create_time = #{model.createTime}</if>
    <if test="model.updateUserId!=null and model.updateUserId!=''">and ew.update_user_id = #{model.updateUserId}
    </if>
    <if test="model.updateTime!=null ">and ew.update_time = #{model.updateTime}</if>
    <if test="model.byx1!=null and model.byx1!=''">and ew.byx1 = #{model.byx1}</if>
    <if test="model.byx2!=null and model.byx2!=''">and ew.byx2 = #{model.byx2}</if>
    <if test="model.byx3!=null and model.byx3!=''">and ew.byx3 = #{model.byx3}</if>
  </sql>

  <select id="queryPageList" parameterType="Object" resultMap="BaseResultMap">
    select ew.*,eq.equ_name
    from e_warning ew
    left join e_equipment eq on eq.id=ew.equ_id and eq.deleted=0
    <include refid="Example_Where_Clause"></include>
    <if test="model.equName!=null and model.equName!=''">
      and INSTR(eq.equ_name, #{model.equName})
    </if>
    <if test="model.warningTimeStr!=null ">
      and ew.warning_time &gt;= #{model.warningTimeStr}
    </if>
    <if test="model.warningTimeEnd!=null ">
      and ew.warning_time &lt;= date_add(#{model.warningTimeEnd}, INTERVAL 1 DAY)
    </if>
    order by ew.warning_time desc
  </select>

  <select id="getActionByType" resultMap="arAlarmsTypeList">
    select
    aat.*
    from ar_alarms_type aat
    where aat.deleted = 0 and aat.type = 2
    <if test="subCode!=null and subCode!=''">and aat.sub_code = #{subCode}</if>
    <if test="resourceType!=null and resourceType!=''">and aat.resource_type = #{resourceType}</if>
    <if test="brand!=null and brand!=''">and aat.brand = #{brand}</if>
    <if test="stationId!=null and stationId!=''">and aat.station_id = #{stationId}</if>
  </select>

  <resultMap id="arAlarmsTypeList" type="java.util.Map">
    <result column="id" property="id"/>
    <result column="sub_code" property="subCode"/>
    <result column="resource_type" property="resourceType"/>
    <result column="brand" property="brand"/>
    <result column="value_type" property="valueType"/>
    <result column="type" property="type"/>
    <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="station_id" property="stationId"/>
    <result column="byx1" property="byx1"/>
    <result column="byx2" property="byx2"/>
    <result column="byx3" property="byx3"/>
    <collection column="{id=id,stationId=station_id}" property="actions" select="getAction"/>
  </resultMap>

  <select id="getAction" resultMap="actionList">
    SELECT
    aeti.*,
    la.action_info as actionName,la.byx3 as qxan
    from ar_eq_type_info aeti
    left join link_action la on la.id = aeti.action_id and aeti.station_id = la.station_id
    WHERE aeti.ar_alarms_type_id = #{id}
    <if test="stationId!=null and stationId!=''">and aeti.station_id = #{stationId}</if>
  </select>

  <resultMap id="actionList" type="java.util.Map">
    <result column="id" property="id"/>
    <result column="ar_alarms_type_id" property="arAlarmsTypeId"/>
    <result column="action_id" property="actionId"/>
    <result column="value_info" property="valueInfo"/>
    <result column="send_type" property="sendType"/>
    <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="station_id" property="stationId"/>
    <result column="byx1" property="byx1"/>
    <result column="byx2" property="byx2"/>
    <result column="byx3" property="byx3"/>
    <result column="actionName" property="actionName"/>
    <result column="qxan" property="qxan"/>
  </resultMap>

  <select id="getOneEqTypeInfo" resultType="java.util.Map">
    select
    aeti.*,la.action_info as actionName
    from ar_eq_type_info aeti
    left join ar_alarms_type aat on aat.id = aeti.ar_alarms_type_id and aeti.station_id = aat.station_id
    left join link_action la on la.id = aeti.action_id and aeti.station_id = la.station_id
    where aat.deleted = 0 and aat.type = 2
    <if test="subCode!=null and subCode!=''">and aat.sub_code = #{subCode}</if>
    <if test="resourceType!=null and resourceType!=''">and aat.resource_type = #{resourceType}</if>
    <if test="brand!=null and brand!=''">and aat.brand = #{brand}</if>
    <if test="stationId!=null and stationId!=''">and aat.station_id = #{stationId}</if>
    <if test="actionId!=null and actionId!=''">and aeti.action_id = #{actionId}</if>
  </select>
  <select id="queryLevel" resultType="java.lang.Integer">
        select level
        from ar_up_config
        where deleted = 0 and priority = 1
        <if test="stationId!=null and stationId!=''">and station_id = #{stationId}</if>
    </select>

    <select id="getOneArUpConfig" resultType="java.util.Map">
      select *
      from ar_up_config
      where deleted = 0 and priority = 1 and level != 1
      <if test="stationId!=null and stationId!=''">and station_id = #{stationId}</if>
    </select>

    <update id="deleteBatch">
    update e_warning
    set deleted=1
    where id in
    <foreach close=")" collection="ids" index="index" item="item" open="(" separator=",">
      #{item}
    </foreach>
  </update>

  <update id="dealWithBatch">
    update e_warning
    set state=1
    where id in
    <foreach close=")" collection="ids" index="index" item="item" open="(" separator=",">
      #{item}
    </foreach>
  </update>

</mapper>


