<?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.zcxk.dao.alarm.DataAlarmDetailMapper">

    <insert id="saveDataAlarmDetail" useGeneratedKeys="true"  keyProperty="id" parameterType="com.zcxk.entity.alarm.DataAlarmDetailEntity">
        insert sms_data_alarm_detail (manage_id,alarm_name,company_org_id,dept_org_id,tenant_id,be_scene_id,be_device_id,be_attribute_id,be_alarm_value,
               be_type,compute_rules,to_scene_id,to_device_id,to_attribute_id,to_alarm_value,to_type,alarm_rules,alarm_value,alarm_start_time
               ,result_alarm_value,is_history,date_create,status)
        values
            (#{manageId},#{alarmName}, #{companyOrgId}, #{deptOrgId}, #{tenantId}, #{beSceneId},#{beDeviceId},
            #{beAttributeId}, #{beAlarmValue},#{beType}, #{computeRules}, #{toSceneId},
            #{toDeviceId}, #{toAttributeId},#{toAlarmValue}, #{toType}, #{alarmRules},
            #{alarmValue},#{alarmStartTime},#{resultAlarmValue},#{isHistory},#{dateCreate},#{status})
    </insert>

    <select id="findPage" resultType="com.zcxk.vo.DataAlarmDetailsVo">
        select
            a.id,a.alarm_name,a.manage_id,a.compute_rules,a.alarm_rules,a.alarm_value,a.is_hangup,
            a.be_alarm_value,a.to_alarm_value,a.result_alarm_value,a.alarm_start_time,a.alarm_end_time,
            s.SCENE_NAME as be_scene_name, t1.DEVICE_NAME as be_device_name, t2.name as be_attribute_name,
            s.ADDRESS as "beAddress",s.POINT_X as "bePointX",s.POINT_Y as "bePointY",t2.UNIT as beUnit,
            mm.to_scene_name,mm.to_device_name,mm.to_attribute_name,mm.toAddress,mm.toPointX,mm.toPointY,mm.toUnit
        from
            sms_data_alarm_detail  a
        INNER JOIN sms_scene s on s.id = a.be_scene_id and s.status = 1
        INNER JOIN sms_device t1 on a.be_device_id = t1.id and t1.status = 1
        INNER JOIN sms_device_attribute t2 on a.be_attribute_id = t2.id  and t2.status = 1
        LEFT JOIN  (
            select m.id, s.SCENE_NAME as to_scene_name, t1.DEVICE_NAME as to_device_name, t2.name as to_attribute_name,
            s.ADDRESS as "toAddress",s.POINT_X as "toPointX",s.POINT_Y as "toPointY",t2.UNIT as toUnit
            from
            sms_data_alarm_detail  m
            INNER JOIN sms_scene s on s.id = m.to_scene_id and s.status = 1
            INNER JOIN sms_device t1 on m.to_device_id = t1.id and t1.status = 1
            INNER JOIN sms_device_attribute t2 on m.to_attribute_id=t2.id  and t2.status =1
        ) mm on mm.id = a.id
      <where>
          a.tenant_id = #{dto.tenantId}
          <if test="dto.userType != null and dto.userType != -999 and dto.userType!=-9999 and  dto.programItems != null and dto.programItems.size() > 0">
              <if test="dto.permissonType == 5 or dto.permissonType == 2">
                  and ( a.DEPT_ORG_ID in
                  <foreach collection="dto.programItems" item="item" open="(" close=")" separator=",">
                      #{item.orgId}
                  </foreach>
                  or
                  a.COMPANY_ORG_ID in
                  <foreach collection="dto.programItems" item="item" open="(" close=")" separator=",">
                      #{item.orgId}
                  </foreach>
                  )
              </if>
              <if test="dto.permissonType == 4 or dto.permissonType == 3">
                  and a.DEPT_ORG_ID in
                  <foreach collection="dto.programItems" item="item" open="(" close=")" separator=",">
                      #{item.orgId}
                  </foreach>
              </if>
              <if test="dto.permissonType == 1">
                  and a.COMPANY_ORG_ID in
                  <foreach collection="dto.programItems" item="item" open="(" close=")" separator=",">
                      #{item.orgId}
                  </foreach>
                  and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
              </if>
          </if>
        <if test="dto.alarmName != null and dto.alarmName != ''">
            and a.alarm_name like CONCAT('%',#{dto.alarmName,jdbcType=VARCHAR},'%')
        </if>
        <if test="isHistory = 0  and dto.beginTime != null and dto.endTime != null and dto.beginTime != '' and dto.endTime != ''">
            and a.alarm_start_time  between #{dto.beginTime} and #{dto.endTime}
        </if>
        <if test="isHistory = 1  and dto.beginTime != null and dto.endTime != null and dto.beginTime != '' and dto.endTime != ''">
            and a.alarm_start_time  >= #{dto.beginTime} and  #{dto.endTime} >= a.alarm_start_time
        </if>
        and a.is_history = #{history}
        and a.status = 1
        order by id desc
    </where>
    </select>

    <update id="hangUpAlarmDetails">
        update sms_data_alarm_detail set is_hangup = #{status} where id =#{alarmId}
    </update>

    <update id="updateHistoryData">
        update sms_data_alarm_detail set is_history = 1, alarm_end_time = now() where id =#{alarmId}
    </update>

    <select id="listByManageId" resultType="com.zcxk.entity.alarm.DataAlarmDetailEntity">
        select
            a.id,a.manage_id,a.alarm_name,a.compute_rules,a.alarm_rules,a.alarm_value,
            a.be_alarm_value,a.to_alarm_value,a.result_alarm_value,
            a.alarm_start_time,a.alarm_end_time,a.be_scene_id,
            a.be_device_id,a.be_attribute_id,a.is_hangup,
            a.be_type,a.compute_rules,a.to_scene_id,a.to_device_id,a.to_attribute_id,a.to_type
        from sms_data_alarm_detail a
        where a.manage_id in
        <foreach collection="manageIds" item="manageId" open="(" close=")" separator=",">
            #{manageId}
        </foreach>
         and a.is_history = 0
    </select>

    <update id="updateAlrarmData">
        update sms_data_alarm_detail
        set
            be_alarm_value= #{beAlarmValue},
            to_alarm_value= #{toAlarmValue}
            <if test="alarmEndTime != null">
                ,alarm_end_time= #{alarmEndTime}
            </if>
            <if test="resultAlarmValue != null">
                ,result_alarm_value= #{resultAlarmValue}
            </if>
        where
            id =#{id}
    </update>
</mapper>