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

    <sql id="Base_Column_List">
            a.ID as "id"
            ,a.TENANT_ID as "tenantId"
            ,a.DEVICE_ID as "deviceId"
            ,a.COMPANY_ORG_ID as "companyOrgId"
            ,a.DEPT_ORG_ID as "deptOrgId"
            ,a.ALARM_TYPE as "alarmType"
            ,a.ATTRIBUTE_ID as "attributeId"
            ,a.ALARM_VALUE as "alarmValue"
            ,a.ALARM_CONTENT as "alarmContent"
            ,a.ALARM_START_TIME as "alarmStartTime"
            ,a.ALARM_END_TIME as "alarmEndTime"
            ,a.STATE as "state"
            ,a.OP_STATE as "opState"
            ,a.REMARK as "remark"
            ,a.STATUS as "status"
            ,a.DATE_CREATE as "dateCreate"
            ,a.CREATE_BY as "createBy"
            ,a.DATE_UPDATE as "dateUpdate"
            ,a.UPDATE_BY as "updateBy"
            ,a.MIN_VALUE as "minValue"
            ,a.MAX_VALUE as "maxValue"
            ,timestampdiff(MINUTE,ifnull(a.ALARM_END_TIME,now()),a.ALARM_START_TIME) as "duration"
            /*,(case when ALARM_END_TIME is not null then timestampdiff(MINUTE,a.ALARM_END_TIME,a.ALARM_START_TIME) else null end ) as "duration"*/
            ,att.`NAME` as "attributeName"
            ,a.SCENE_NAME as "sceneName"
            ,d.DEVICE_NAME as "deviceName"
            ,t.DEVICE_TYPE as "deviceType"
            ,d.ADDRESS as "address"
            ,d.POINT_X as "pointX"
            ,d.POINT_Y as "pointY"
            ,ast.id as "alarmSettingId"
            /*,ast.ALARM_CONDITION +" " +ast.ALARM_VALUE as "alermRule"*/
            ,concat(ifnull(ast.ALARM_CONDITION,''),ifnull(ast.ALARM_VALUE,'')) as "alermRule"
            ,att.UNIT as "unit"
    </sql>

    <sql id="Base_Column_List_real_time">
            a.ID as "id"
            ,a.STATE as "state"
            ,a.OP_STATE as "opState"
            ,a.SCENE_NAME as "sceneName"
            ,d.DEVICE_NAME as "deviceName"
            ,t.DEVICE_TYPE as "deviceType"
            ,a.ALARM_TYPE as "alarmType"
            ,att.`NAME` as "attributeName"
            ,a.ALARM_START_TIME as "alarmStartTime"
            ,d.ADDRESS as "address"
            ,d.POINT_X as "pointX"
            ,d.POINT_Y as "pointY"
            ,concat(a.ALARM_VALUE,ifnull(att.unit,'')) as "alarmValue"
            ,a.ALARM_CONTENT as "alarmContent"

    </sql>

    <sql id="Base_Column_List_history">
            a.ID as "id"
            ,a.STATE as "state"
            ,a.OP_STATE as "opState"
            ,a.SCENE_NAME as "sceneName"
            ,d.DEVICE_NAME as "deviceName"
            ,t.DEVICE_TYPE as "deviceType"
            ,a.ALARM_TYPE as "alarmType"
            ,att.`NAME` as "attributeName"
            ,a.ALARM_START_TIME as "alarmStartTime"
            ,d.ADDRESS as "address"
            ,d.POINT_X as "pointX"
            ,d.POINT_Y as "pointY"

            ,concat(a.MIN_VALUE,ifnull(att.unit,'')) as "minValue"
            ,concat(a.MAX_VALUE,ifnull(att.unit,''))as "maxValue"
            ,ast.id as "alarmSettingId"
            ,concat(ifnull(ast.ALARM_CONDITION,''),ifnull(ast.ALARM_VALUE,''),ifnull(att.unit,'')) as "alermRule"
            ,a.ALARM_END_TIME as "alarmEndTime"

            ,concat(concat(timestampdiff(SECOND,a.ALARM_START_TIME,ifnull(a.ALARM_END_TIME,now()))   div 3600,'时'),
						concat(timestampdiff(SECOND,a.ALARM_START_TIME,ifnull(a.ALARM_END_TIME,now()))   mod 3600 div 60,'分'),
						concat(timestampdiff(SECOND,a.ALARM_START_TIME,ifnull(a.ALARM_END_TIME,now()))   mod 60,'秒')) as "duration"


    </sql>

    <sql id="alarmDetailJoins">
        left join sms_device d on a.DEVICE_ID =d.id and d.`STATUS` = 1
        left join sms_device_type t on d.DEVICE_TYPE_ID =t.ID
        left join sms_alarm_setting ast on ast.id= a.alarm_setting_id
        left join sms_device_attribute att on att.id=a.ATTRIBUTE_ID
    </sql>
    <select id="selectById" resultType="com.zcxk.dto.AlarmDetailsDto">
        select
        <include refid="Base_Column_List"/>
        from sms_alarm_details a
        <include refid="alarmDetailJoins"/>
        where a.`STATUS` = 1
            and a.id=#{id}
    </select>

    <select id="selectRealTimeByIds" resultType="com.zcxk.dto.AlarmDetailsRealTimeDto">
        select
        <include refid="Base_Column_List_real_time"/>
        from sms_alarm_details a
        <include refid="alarmDetailJoins"/>
        where a.`STATUS` = 1
        and a.id in
            <foreach collection="ids" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
    </select>

    <select id="selectHistoryByIds" resultType="com.zcxk.dto.AlarmDetailsHistoryDto">
        select
        <include refid="Base_Column_List_history"/>
        from sms_alarm_details a
        <include refid="alarmDetailJoins"/>
        where a.`STATUS` = 1
        and a.id in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>
    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultType="com.zcxk.dto.AlarmDetailsDto">
        select
        <include refid="Base_Column_List"/>
        from sms_alarm_details a
        <include refid="alarmDetailJoins"/>
        where  a.`STATUS` = 1
        <if test="alarmDetailsDto.condition != null and alarmDetailsDto.condition != ''">
            and d.DEVICE_NAME like concat('%', #{alarmDetailsDto.condition},'%')
        </if>

        <if test="alarmDetailsDto.tenantId != null and alarmDetailsDto.tenantId != ''">
            and a.TENANT_ID = #{alarmDetailsDto.tenantId}
        </if>
        <if test="alarmDetailsDto.alarmType != null and alarmDetailsDto.alarmType != ''">
            and a.ALARM_TYPE = #{alarmDetailsDto.alarmType}
        </if>
        <if test="alarmDetailsDto.opState != null">
            and a.OP_STATE =#{alarmDetailsDto.opState}
        </if>
        <if test="alarmDetailsDto.startTime != null">
            and a.ALARM_START_TIME >=#{alarmDetailsDto.startTime}
        </if>
        <if test="alarmDetailsDto.endTime != null">
            and a.ALARM_START_TIME &lt;=DATE_ADD(#{alarmDetailsDto.endTime},INTERVAL 1 DAY)
        </if>
        <if test="alarmDetailsDto.sceneIds != null and alarmDetailsDto.sceneIds.size() > 0">
            and a.SCENE_ID  in
            <foreach collection="alarmDetailsDto.sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="alarmDetailsDto.userType!=null and alarmDetailsDto.userType!=-999 and alarmDetailsDto.userType!=-9999 and  alarmDetailsDto.programItems != null and alarmDetailsDto.programItems.size() > 0">
            <if test="alarmDetailsDto.permissonType == 5 or alarmDetailsDto.permissonType == 2">
                and ( a.DEPT_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                a.COMPANY_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="alarmDetailsDto.permissonType == 4 or alarmDetailsDto.permissonType == 3">
                and a.DEPT_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="alarmDetailsDto.permissonType == 1">
                and a.COMPANY_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
            </if>
        </if>

    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO  sms_alarm_details (  TENANT_ID , PARENT_SCENE_ID, PARENT_SCENE_NAME, SCENE_ID, SCENE_NAME, DEVICE_ID ,  COMPANY_ORG_ID ,  DEPT_ORG_ID ,  ALARM_TYPE ,  ATTRIBUTE_ID ,  ALARM_VALUE ,  ALARM_CONTENT ,  ALARM_START_TIME ,  ALARM_END_TIME ,  STATE ,  OP_STATE ,  REMARK ,  STATUS ,  DATE_CREATE ,  CREATE_BY ,  DATE_UPDATE ,  UPDATE_BY , MIN_VALUE, MAX_VALUE, ALARM_SETTING_ID )
        VALUES ( #{tenantId} , #{parentSceneId} ,#{parentSceneName} ,#{sceneId} ,#{sceneName} , #{deviceId} ,  #{companyOrgId} ,  #{deptOrgId} ,  #{alarmType} ,  #{attributeId} ,  #{alarmValue} ,  #{alarmContent} ,  #{alarmStartTime} ,  #{alarmEndTime} ,  #{state} ,  #{opState} ,  #{remark} ,  #{status} ,  #{dateCreate} ,  #{createBy} ,  #{dateUpdate} ,  #{updateBy},#{minValue} ,#{maxValue}, #{alarmSettingId} )
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        INSERT INTO  sms_alarm_details (  TENANT_ID , PARENT_SCENE_ID, PARENT_SCENE_NAME, SCENE_ID, SCENE_NAME, DEVICE_ID ,  COMPANY_ORG_ID ,  DEPT_ORG_ID ,  ALARM_TYPE ,  ATTRIBUTE_ID ,  ALARM_VALUE ,  ALARM_CONTENT ,  ALARM_START_TIME ,  ALARM_END_TIME ,  STATE ,  OP_STATE ,  REMARK ,  STATUS ,  DATE_CREATE ,  CREATE_BY ,  DATE_UPDATE ,  UPDATE_BY, MIN_VALUE, MAX_VALUE,ALARM_SETTING_ID )
        values
        <foreach collection="alarmDetails" item="item" index="index" separator=",">
            ( #{item.tenantId} , #{item.parentSceneId} ,#{item.parentSceneName} ,#{item.sceneId} ,#{item.sceneName} , #{item.deviceId} ,  #{item.companyOrgId} ,  #{item.deptOrgId} ,  #{item.alarmType} ,  #{item.attributeId} ,  #{item.alarmValue} ,  #{item.alarmContent} ,  #{item.alarmStartTime} ,  #{item.alarmEndTime} ,  #{item.state} ,  #{item.opState} ,  #{item.remark} ,  #{item.status} ,  #{item.dateCreate} ,  #{item.createBy} ,  #{item.dateUpdate} ,  #{item.updateBy} ,#{item.minValue} ,#{item.maxValue},#{item.alarmSettingId})
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        UPDATE  sms_alarm_details
        <set>
            <if test="tenantId != null and tenantId != '' ">
                TENANT_ID  = #{tenantId},
            </if>
            <if test="deviceId != null and deviceId != 0">
                DEVICE_ID  = #{deviceId},
            </if>
            <if test="companyOrgId != null and companyOrgId != 0 ">
                COMPANY_ORG_ID  = #{companyOrgId},
            </if>
            <if test="deptOrgId != null and deptOrgId != 0">
                DEPT_ORG_ID  = #{deptOrgId},
            </if>
            <if test="alarmType != null and alarmType != ''">
                ALARM_TYPE  = #{alarmType},
            </if>
            <if test="attributeId != null and attributeId != 0 ">
                ATTRIBUTE_ID  = #{attributeId},
            </if>
            <if test="alarmValue != null ">
                ALARM_VALUE  = #{alarmValue},
            </if>
            <if test="alarmContent != null and alarmContent != '' ">
                ALARM_CONTENT  = #{alarmContent},
            </if>
            <if test="alarmStartTime != null ">
                ALARM_START_TIME  = #{alarmStartTime},
            </if>
            <if test="alarmStartTime != null ">
                ALARM_START_TIME  = #{alarmStartTime},
            </if>
            <if test="alarmEndTime != null ">
                ALARM_END_TIME  = #{alarmEndTime},
            </if>
            <if test="state != null ">
                STATE  = #{state},
            </if>
            <if test="opState != null ">
                OP_STATE  = #{opState},
            </if>

            <if test="remark != null and remark != '' ">
                REMARK  = #{remark},
            </if>
            <if test="status != null ">
                STATUS  = #{status},
            </if>
            <if test="dateUpdate != null ">
                DATE_UPDATE  = #{dateUpdate},
            </if>
            <if test="updateBy != null and updateBy != ''">
                UPDATE_BY  = #{updateBy},
            </if>
            <if test="minValue != null">
                MIN_VALUE  = #{minValue},
            </if>
            <if test="maxValue != null">
                MAX_VALUE  = #{maxValue},
            </if>
            <if test="alarmSettingId != null  and alarmSettingId != 0 ">
                ALARM_SETTING_ID  = #{alarmSettingId}
            </if>
        </set>
        WHERE ID  = #{id};
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
         UPDATE  sms_alarm_details set STATUS  = 0 where id = #{id}
    </delete>

    <!--删除-->
    <delete id="batchDelete">
        UPDATE  sms_alarm_details set STATUS  = 0
        where id  in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </delete>

    <!-- 报警挂起、恢复 -->
    <delete id="suspendedById">
         UPDATE  sms_alarm_details set op_state  = #{opState} , DATE_UPDATE  = #{dateUpdate}, UPDATE_BY  = #{updateBy} where id = #{id}
    </delete>

    <!-- 分页查询 -->
    <select id="selectPage" resultType="com.zcxk.dto.AlarmDetailsDto">
        select
        <include refid="Base_Column_List"/>
        from sms_alarm_details a
        <include refid="alarmDetailJoins"/>
        where  a.`STATUS` = 1
        <if test="alarmDetailsDto.condition != null and alarmDetailsDto.condition != ''">
            and  d.DEVICE_NAME like concat('%', #{alarmDetailsDto.condition},'%')
        </if>
        <if test="alarmDetailsDto.tenantId != null and alarmDetailsDto.tenantId != ''">
            and a.TENANT_ID = #{alarmDetailsDto.tenantId}
        </if>
        <if test="alarmDetailsDto.alarmType != null and alarmDetailsDto.alarmType != ''">
            and a.ALARM_TYPE = #{alarmDetailsDto.alarmType}
        </if>
        <if test="alarmDetailsDto.state != null">
            and a.STATE =#{alarmDetailsDto.state}
        </if>
        <if test="alarmDetailsDto.opState != null">
            and a.OP_STATE =#{alarmDetailsDto.opState}
        </if>
        <if test="alarmDetailsDto.startTime != null">
            and a.ALARM_START_TIME >=#{alarmDetailsDto.startTime}
        </if>
        <if test="alarmDetailsDto.endTime != null">
            and a.ALARM_START_TIME &lt;=DATE_ADD(#{alarmDetailsDto.endTime},INTERVAL 1 DAY)
        </if>
        <if test="alarmDetailsDto.sceneIds != null and alarmDetailsDto.sceneIds.size() > 0">
            and a.SCENE_ID  in
            <foreach collection="alarmDetailsDto.sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="alarmDetailsDto.userType!=null and alarmDetailsDto.userType!=-999 and alarmDetailsDto.userType!=-9999 and  alarmDetailsDto.programItems != null and alarmDetailsDto.programItems.size() > 0">
            <if test="alarmDetailsDto.permissonType == 5 or alarmDetailsDto.permissonType == 2">
                and ( a.DEPT_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                a.COMPANY_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="alarmDetailsDto.permissonType == 4 or alarmDetailsDto.permissonType == 3">
                and a.DEPT_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="alarmDetailsDto.permissonType == 1">
                and a.COMPANY_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
            </if>
        </if>

    </select>

    <!-- 实时数据分页分页查询 -->
    <select id="selectRealTimePage" resultType="com.zcxk.dto.AlarmDetailsRealTimeDto">
        select
        <include refid="Base_Column_List_real_time"/>
        from sms_alarm_details a
        <include refid="alarmDetailJoins"/>
        where  a.`STATUS` = 1 and a.state = 1
        <if test="alarmDetailsDto.condition != null and alarmDetailsDto.condition != ''">
            and  d.DEVICE_NAME like concat('%', #{alarmDetailsDto.condition},'%')
        </if>
        <if test="alarmDetailsDto.tenantId != null and alarmDetailsDto.tenantId != ''">
            and a.TENANT_ID = #{alarmDetailsDto.tenantId}
        </if>
        <if test="alarmDetailsDto.alarmType != null and alarmDetailsDto.alarmType != ''" >
            and a.ALARM_TYPE = #{alarmDetailsDto.alarmType}
        </if>
        <if test="alarmDetailsDto.opState != null">
            and a.OP_STATE =#{alarmDetailsDto.opState}
        </if>
        <if test="alarmDetailsDto.startTime != null">
            and a.ALARM_START_TIME >=#{alarmDetailsDto.startTime}
        </if>
        <if test="alarmDetailsDto.endTime != null">
            and a.ALARM_START_TIME &lt;=DATE_ADD(#{alarmDetailsDto.endTime},INTERVAL 1 DAY)
        </if>
        <if test="alarmDetailsDto.sceneIds != null and alarmDetailsDto.sceneIds.size() > 0">
            and a.SCENE_ID  in
            <foreach collection="alarmDetailsDto.sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="alarmDetailsDto.userType!=null and alarmDetailsDto.userType!=-999 and alarmDetailsDto.userType!=-9999 and  alarmDetailsDto.programItems != null and alarmDetailsDto.programItems.size() > 0">
            <if test="alarmDetailsDto.permissonType == 5 or alarmDetailsDto.permissonType == 2">
                and ( a.DEPT_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                a.COMPANY_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="alarmDetailsDto.permissonType == 4 or alarmDetailsDto.permissonType == 3">
                and a.DEPT_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="alarmDetailsDto.permissonType == 1">
                and a.COMPANY_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
            </if>
        </if>

    </select>

    <!-- 历史数据分页分页查询 -->
    <select id="selectHistoryPage" resultType="com.zcxk.dto.AlarmDetailsHistoryDto">
        select
        <include refid="Base_Column_List_history"/>
        from sms_alarm_details a
        <include refid="alarmDetailJoins"/>
        where  a.`STATUS` = 1 and a.state = 0
        <if test="alarmDetailsDto.condition != null and alarmDetailsDto.condition != ''">
            and d.DEVICE_NAME like concat('%', #{alarmDetailsDto.condition},'%')
        </if>
        <if test="alarmDetailsDto.tenantId != null and alarmDetailsDto.tenantId != ''">
            and a.TENANT_ID = #{alarmDetailsDto.tenantId}
        </if>
        <if test="alarmDetailsDto.alarmType != null and alarmDetailsDto.alarmType != ''">
            and a.ALARM_TYPE = #{alarmDetailsDto.alarmType}
        </if>
        <if test="alarmDetailsDto.opState != null">
            and a.OP_STATE =#{alarmDetailsDto.opState}
        </if>
        <if test="alarmDetailsDto.startTime != null">
            and a.ALARM_START_TIME >=#{alarmDetailsDto.startTime}
        </if>
        <if test="alarmDetailsDto.endTime != null">
            and a.ALARM_START_TIME &lt;=DATE_ADD(#{alarmDetailsDto.endTime},INTERVAL 1 DAY)
        </if>
        <if test="alarmDetailsDto.sceneIds != null and alarmDetailsDto.sceneIds.size() > 0">
            and a.SCENE_ID  in
            <foreach collection="alarmDetailsDto.sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="alarmDetailsDto.userType!=null and alarmDetailsDto.userType!=-999 and alarmDetailsDto.userType!=-9999 and  alarmDetailsDto.programItems != null and alarmDetailsDto.programItems.size() > 0">
            <if test="alarmDetailsDto.permissonType == 5 or alarmDetailsDto.permissonType == 2">
                and ( a.DEPT_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                a.COMPANY_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="alarmDetailsDto.permissonType == 4 or alarmDetailsDto.permissonType == 3">
                and a.DEPT_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="alarmDetailsDto.permissonType == 1">
                and a.COMPANY_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
            </if>
        </if>

    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectRealTimeList" resultType="com.zcxk.dto.AlarmDetailsRealTimeDto">
        select
        <include refid="Base_Column_List_real_time"/>
        from sms_alarm_details a
        <include refid="alarmDetailJoins"/>
        where  a.`STATUS` = 1 and a.state = 1
        <if test="alarmDetailsDto.condition != null and alarmDetailsDto.condition != ''">
            and  d.DEVICE_NAME like concat('%', #{alarmDetailsDto.condition},'%')
        </if>
        <if test="alarmDetailsDto.tenantId != null and alarmDetailsDto.tenantId != ''">
            and a.TENANT_ID = #{alarmDetailsDto.tenantId}
        </if>
        <if test="alarmDetailsDto.alarmType != null and alarmDetailsDto.alarmType != ''">
            and a.ALARM_TYPE = #{alarmDetailsDto.alarmType}
        </if>
        <if test="alarmDetailsDto.opState != null">
            and a.OP_STATE =#{alarmDetailsDto.opState}
        </if>
        <if test="alarmDetailsDto.startTime != null">
            and a.ALARM_START_TIME >=#{alarmDetailsDto.startTime}
        </if>
        <if test="alarmDetailsDto.endTime != null">
            and a.ALARM_START_TIME &lt;=DATE_ADD(#{alarmDetailsDto.endTime},INTERVAL 1 DAY)
        </if>
        <if test="alarmDetailsDto.sceneIds != null and alarmDetailsDto.sceneIds.size() > 0">
            and a.SCENE_ID  in
            <foreach collection="alarmDetailsDto.sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="alarmDetailsDto.userType!=null and alarmDetailsDto.userType!=-999 and alarmDetailsDto.userType!=-9999 and  alarmDetailsDto.programItems != null and alarmDetailsDto.programItems.size() > 0">
            <if test="alarmDetailsDto.permissonType == 5 or alarmDetailsDto.permissonType == 2">
                and ( a.DEPT_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                a.COMPANY_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="alarmDetailsDto.permissonType == 4 or alarmDetailsDto.permissonType == 3">
                and a.DEPT_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="alarmDetailsDto.permissonType == 1">
                and a.COMPANY_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
            </if>
        </if>
        order by a.ALARM_START_TIME
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectHistoryList" resultType="com.zcxk.dto.AlarmDetailsHistoryDto">
        select
        <include refid="Base_Column_List_history"/>
        from sms_alarm_details a
        <include refid="alarmDetailJoins"/>
        where  a.`STATUS` = 1 and a.state = 0
        <if test="alarmDetailsDto.condition != null and alarmDetailsDto.condition != ''">
            and d.DEVICE_NAME like concat('%', #{alarmDetailsDto.condition},'%')
        </if>
        <if test="alarmDetailsDto.tenantId != null and alarmDetailsDto.tenantId != ''">
            and a.TENANT_ID = #{alarmDetailsDto.tenantId}
        </if>
        <if test="alarmDetailsDto.alarmType != null and alarmDetailsDto.alarmType != ''">
            and a.ALARM_TYPE = #{alarmDetailsDto.alarmType}
        </if>
        <if test="alarmDetailsDto.opState != null">
            and a.OP_STATE =#{alarmDetailsDto.opState}
        </if>
        <if test="alarmDetailsDto.startTime != null">
            and a.ALARM_START_TIME >=#{alarmDetailsDto.startTime}
        </if>
        <if test="alarmDetailsDto.endTime != null">
            and a.ALARM_START_TIME &lt;=DATE_ADD(#{alarmDetailsDto.endTime},INTERVAL 1 DAY)
        </if>
        <if test="alarmDetailsDto.sceneIds != null and alarmDetailsDto.sceneIds.size() > 0">
            and a.SCENE_ID  in
            <foreach collection="alarmDetailsDto.sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="alarmDetailsDto.userType!=null and alarmDetailsDto.userType!=-999 and alarmDetailsDto.userType!=-9999 and  alarmDetailsDto.programItems != null and alarmDetailsDto.programItems.size() > 0">
            <if test="alarmDetailsDto.permissonType == 5 or alarmDetailsDto.permissonType == 2">
                and ( a.DEPT_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                a.COMPANY_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="alarmDetailsDto.permissonType == 4 or alarmDetailsDto.permissonType == 3">
                and a.DEPT_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="alarmDetailsDto.permissonType == 1">
                and a.COMPANY_ORG_ID in
                <foreach collection="alarmDetailsDto.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
            </if>
        </if>
        order by a.ALARM_START_TIME
    </select>


    <select id="selectDeviceForCheckAlarm" resultType="com.zcxk.dto.DeviceCheckAlarmDto">
        select
            d.TENANT_ID as "tenantId"
            ,d.id as "deviceId"
            ,d.DEVICE_NAME as "deviceName"
            ,d.COMPANY_ORG_ID as "companyOrgId"
            ,d.DEPT_ORG_ID as "deptOrgId"
            ,d.address as "address"
            ,a.PARENT_SCENE_ID as "parentSceneId"
            ,a.PARENT_SCENE_NAME as "parentSceneName"
            ,a.SCENE_ID as "sceneId"
            ,a.SCENE_NAME as "sceneName"

            ,a.ALARM_TYPE as "alarmType"
            ,a.ATTRIBUTE_ID as "attributeId"
            ,d.LAST_UPDATE_TIME as "lastUpdateTime"
            ,timestampdiff(MINUTE,ifnull(d.LAST_UPDATE_TIME,d.DATE_CREATE),now()) AS "duration"

            ,a.ALARM_CONDITION as "alarmCondition"
            ,a.ALARM_VALUE as "alarmValue"
            ,a.id as "alarmSettingId"
            ,a.START_TIME as "startTime"
            ,a.END_TIME as "endTime"
        <if test="deviceId != null">
            ,da.id as "attributeId"
            ,da.IDENTIFIER as "identifiter"
		    ,da.`NAME` as "attributeName"
            ,da.unit as "unit"
        </if>
        from sms_device d
        left join sms_alarm_setting a on a.DEVICE_ID =d.ID and a.ALARM_TYPE =#{alarmType} and a.ALARM_VALUE is not null
        <if test="deviceId != null">
            left join sms_device_attribute da on da.id=a.ATTRIBUTE_ID
        </if>
        where  a.ALARM_VALUE is not null and a.ALARM_CONDITION is not null and d.status = 1 and a.status = 1
        and a.status = 1
        <if test="deviceId != null">
            and d.id=#{deviceId}
        </if>
    </select>

    <select id="selectStateAlarm" resultType="com.zcxk.dto.AlarmDetailsDto">
        select
            id as "id"
            ,PARENT_SCENE_ID as "parentSceneId"
            ,PARENT_SCENE_NAME as "parentSceneName"
            ,SCENE_ID as "sceneId"
            ,SCENE_NAME as "sceneName"
            ,DEVICE_ID as "deviceId"
            ,ATTRIBUTE_ID as "attributeId"
            ,ALARM_VALUE as "alarmValue"
            ,ALARM_CONTENT as "alarmContent"
            ,ALARM_START_TIME as "alarmStartTime"
            ,max_value as "maxValue"
            ,min_value as "minValue"
            ,alarm_setting_id as "alarmSettingId"
        from sms_alarm_details
        where STATE = 1 and status = 1
        <if test="alarmType != null and alarmType != ''">
            and ALARM_TYPE=#{alarmType}
        </if>
        <if test="deviceId != null">
            and DEVICE_ID=#{deviceId}
        </if>
        <if test="alarmSettingIds != null and alarmSettingIds.size() > 0">
            and alarm_setting_id IN
            <foreach collection="alarmSettingIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>



    </select>

    <update id="udpateLastUpdateTime">
        update sms_device set LAST_UPDATE_TIME =#{lastUpdateTime} where id=#{deviceId}
    </update>

    <select id="selectByDeviceId" resultType="com.zcxk.dto.AlarmDetailsDto">
        select
        <include refid="Base_Column_List"/>,concat(b.alarm_condition,cast(b.alarm_value as char)+0) alarmRange
        from sms_alarm_details a
        left join sms_alarm_setting b on a.alarm_setting_id=b.id and b.status = 1 and b.alarm_condition!='='
        <include refid="alarmDetailJoins"/>
        where a.`STATUS` = 1
        and a.DEVICE_ID=#{deviceId} and (a.scene_id=#{sceneId} or a.PARENT_SCENE_ID=#{sceneId}) and a.state=1
    </select>

    <select id="selectByDeviceAttributeId" resultType="com.zcxk.dto.AlarmDetailsDto">
        select
        <include refid="Base_Column_List"/>,concat(b.alarm_condition,cast(b.alarm_value as char)+0) alarmRange
        from sms_alarm_details a
        left join sms_alarm_setting b on a.alarm_setting_id=b.id and b.status = 1 and b.alarm_condition!='='
        <include refid="alarmDetailJoins"/>
        where a.`STATUS` = 1
        and a.DEVICE_ID=#{deviceId} and a.ATTRIBUTE_ID=#{attributeId} and (a.scene_id=#{sceneId} or a.PARENT_SCENE_ID=#{sceneId}) and a.state=1
    </select>

    <select id="getAlarmInfoForWorkOrderManage" resultType="com.zcxk.dto.DeviceAlarmInfoForOrder">
        select
            ad.id as "alarmId"
            ,ad.PARENT_SCENE_ID as "parentSceneId"
            ,ad.PARENT_SCENE_NAME as "parentSceneName"
            ,ad.ALARM_TYPE as "alarmType"
            ,d.DEVICE_NAME as "deviceName"
            ,da.NAME as "attributeName"
            ,CONCAT(ast.ALARM_CONDITION,ast.ALARM_VALUE) as "alarmContent"
            ,da.UNIT as "unit"

            ,d.id as "deviceId"
            ,d.TENANT_ID as "tenantId"
            ,d.COMPANY_ORG_ID as "companyOrgId"
            ,d.DEPT_ORG_ID as "departmentOrgId"
            ,d.ADDRESS as "address"
            ,DATE_FORMAT(ad.ALARM_START_TIME, '%Y-%m-%d %H:%i:%s') as "orderTime"
            ,ad.ALARM_CONTENT as "taskDesc"
            ,CONCAT(d.POINT_X,',',d.POINT_Y) as "geo"
        from sms_alarm_details ad
        left join sms_device d on ad.DEVICE_ID =d.ID
        left join sms_device_attribute da on ad.ATTRIBUTE_ID =da.id
        left join sms_alarm_setting ast on ast.id=ad.ALARM_SETTING_ID
        where  ad.OP_STATE=1 and ad.STATE=1 and ad.is_send_message = 0 and d.status = 1 and d.ENABLE_STATE = 1
        and timestampdiff(MINUTE,ad.ALARM_START_TIME,now())>10
    </select>

    <update id="updateAlarmSendMessageState" >
        update sms_alarm_details set is_send_message =1
        where id in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item.alarmId}
        </foreach>
    </update>
    <select id="selectBySceneId" resultType="com.zcxk.dto.AlarmDetailsDto">
        select
        <include refid="Base_Column_List"/>,concat(b.alarm_condition,cast(b.alarm_value as char)+0) alarmRange
        from sms_alarm_details a
        left join sms_alarm_setting b on a.alarm_setting_id=b.id and b.status = 1 and b.alarm_condition!='='
        <include refid="alarmDetailJoins"/>
        where a.`STATUS` = 1
        and (a.parent_scene_id =#{sceneId} or a.scene_id=#{sceneId})and a.state=1
    </select>
</mapper>