<?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.AlarmSettingMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.dto.AlarmSettingDto" id="alarmSettingMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>
        <result property="companyOrgId" column="company_org_id" jdbcType="INTEGER"/>
        <result property="deptOrgId" column="dept_org_id" jdbcType="INTEGER"/>
        <result property="sceneId" column="scene_id" jdbcType="INTEGER"/>
        <result property="sceneName" column="scene_name1" jdbcType="VARCHAR"/>
        <result property="deviceId" column="device_id" jdbcType="INTEGER"/>
        <result property="deviceName" column="device_name" jdbcType="VARCHAR"/>
        <result property="deviceType" column="device_type" jdbcType="VARCHAR"/>
        <result property="attributeId" column="attribute_id" jdbcType="INTEGER"/>
        <result property="attributeName" column="attribute_name" jdbcType="VARCHAR"/>
        <result property="alarmType" column="alarm_type" jdbcType="VARCHAR"/>
        <result property="alarmCondition" column="alarm_condition" jdbcType="VARCHAR"/>
        <result property="alarmValue" column="alarm_value" jdbcType="VARCHAR"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="dateCreate" column="date_create" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="dateUpdate" column="date_update" jdbcType="TIMESTAMP"/>
        <result property="startTime" column="start_time" jdbcType="VARCHAR"/>
        <result property="endTime" column="end_time" jdbcType="VARCHAR"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
         t1.*,t2.company_org_id,t2.dept_org_id,t3.scene_name scene_name1,t2.device_name,t2.device_type_id,t5.device_type,t4.name attribute_name
     </sql>

    <!-- 外联表  -->
    <sql id="leftJoins">
        left join sms_device t2 on t1.device_id=t2.id and t2.status =1
        left join sms_scene t3 on t1.scene_id=t3.id and t3.status =1
        left join sms_device_attribute t4 on t1.attribute_id=t4.id and t4.status =1
        left join sms_device_type t5 on t2.device_type_id=t5.id and t5.status =1
     </sql>

    <select id="findPage" resultMap="alarmSettingMap">
        select
        <include refid="Base_Column_List"/>
        from sms_alarm_setting t1
        <include refid="leftJoins"/>
        <where>
            <if test="alarm.id != null">
                and t1.id = #{alarm.id}
            </if>
            <if test="alarm.tenantId != null and alarm.tenantId != ''">
                and t1.tenant_id = #{alarm.tenantId}
            </if>
            <if test="alarm.attributeName != null and alarm.attributeName != ''">
                and t4.name like CONCAT('%',#{alarm.attributeName},'%')
            </if>
            <if test="alarm.sceneId != null ">
                and t1.scene_id = #{alarm.sceneId}
            </if>
            <if test="alarm.sceneIds != null and alarm.sceneIds.size()>0">
                and t1.scene_id in
                <foreach item="item" index="index" collection="alarm.sceneIds" open="("
                         separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="alarm.userType!=null and alarm.userType!=-999 and alarm.userType!=-9999 and  alarm.programItems != null and alarm.programItems.size() > 0">
                <if test="alarm.permissonType == 5 or alarm.permissonType == 2">
                    and ( t2.DEPT_ORG_ID in
                    <foreach collection="alarm.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    t2.COMPANY_ORG_ID in
                    <foreach collection="alarm.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="alarm.permissonType == 4 or alarm.permissonType == 3">
                    and t2.DEPT_ORG_ID in
                    <foreach collection="alarm.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="alarm.permissonType == 1">
                    and t2.COMPANY_ORG_ID in
                    <foreach collection="alarm.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (t2.DEPT_ORG_ID is null or t2.DEPT_ORG_ID =0)
                </if>
            </if>
            and t1.status =1
        </where>
        order by t1.device_id,t1.date_create
    </select>

    <select id="selectAttributes" resultMap="alarmSettingMap">
        select t2.id attribute_id,t2.name attribute_name
        from  sms_device t1
        inner join sms_device_attribute t2 on t1.device_type_id=t2.device_type_id and t2.status =1
        inner join sms_device_parm t3 on t3.device_id=t1.id and t3.attribute_id=t2.id and t3.status=1
        where t3.device_id=#{deviceId} and t3.scene_id=#{sceneId} and t1.status=1 and t3.is_alarm=1
        order by t3.seq
    </select>



    <!-- 修改实时报警数据 -->
    <update id="updateRealtimeAlarm">
        update  sms_alarm_details
        set state  = 0,alarm_end_time  = now(),date_update  = now(),update_by  = #{updateBy}
        where  state  = 1 and alarm_setting_id IN
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>

</mapper>