<?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.charge.core.dao.alarm.DataAlarmManageMapper">

    <sql id="alarmManage_result_list" >
            a.id,
            a.alarm_name,
            a.be_scene_id,
            a.be_device_id,
            a.be_attribute_id,
            a.be_type,
            a.compute_rules,
            a.to_scene_id,
            a.to_device_id,
            a.to_attribute_id,
            a.to_type,
            a.alarm_rules,
            a.alarm_value,
            a.tenant_id,
            a.company_org_id,
            a.dept_org_id
    </sql>

    <insert id="saveDataAlarmManage" useGeneratedKeys="true"  keyProperty="id" parameterType="com.zcxk.charge.core.entity.DataAlarmManageEntity">
        insert sms_data_alarm_manage (alarm_name,company_org_id,dept_org_id,tenant_id,be_scene_id,be_device_id,be_attribute_id,be_type,compute_rules,
               to_scene_id,to_device_id,to_attribute_id,to_type,alarm_rules,alarm_value,date_create,create_by,status)
        values
            (#{alarmName}, #{companyOrgId}, #{deptOrgId}, #{tenantId}, #{beSceneId},#{beDeviceId},
            #{beAttributeId}, #{beType}, #{computeRules}, #{toSceneId},
            #{toDeviceId}, #{toAttributeId}, #{toType}, #{alarmRules},
            #{alarmValue}, #{dateCreate},#{createBy},#{status})
    </insert>

    <select id="findPage" resultType="com.zcxk.charge.api.vo.DataAlarmManageVo">
        select
            a.id,a.alarm_name, t2.name as be_attribute_name,s.SCENE_NAME as be_scene_name,
            a.compute_rules,a.alarm_rules,a.alarm_value,mm.to_attribute_name,mm.to_scene_name
        from
           sms_data_alarm_manage  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, t2.name as to_attribute_name,s.SCENE_NAME as to_scene_name
            from
            sms_data_alarm_manage  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>
            and a.status = 1
        </where>
        order by id desc
    </select>

    <select id="findAlarmManage" resultType="com.zcxk.charge.api.vo.DataAlarmManageInfoVo">
        select
        <include refid="alarmManage_result_list"></include>
        from sms_data_alarm_manage a
        where
            a.id = #{alarId} and a.status = 1
    </select>

    <update id="updateDataAlarmManage">
    update sms_data_alarm_manage
    set
        alarm_name = #{alarmName},
        be_scene_id =#{beSceneId},
        be_device_id = #{beDeviceId},
        be_attribute_id = #{beAttributeId},
        be_type = #{beType},
        compute_rules = #{computeRules},
        to_scene_id = #{toSceneId},
        to_device_id= #{toDeviceId},
        to_attribute_id = #{toAttributeId},
        to_type = #{toType},
        alarm_rules = #{alarmRules},
        alarm_value = #{alarmValue},
        date_update = #{dateUpdate},
        update_by = #{updateBy}
    where
        id =#{id}
    </update>

    <update id="delDataAlarmManage">
        update sms_data_alarm_manage
        set
            status = 0
        where
            id in
        <foreach collection="alarmIds" item="alarmId" open="(" close=")" separator=",">
            #{alarmId}
        </foreach>
    </update>

    <select id="listByType" resultType="com.zcxk.charge.api.vo.DataAlarmManageVo">
        select
            a.id,a.alarm_name,a.be_scene_id,a.be_device_id,
            a.be_attribute_id,a.be_type,a.compute_rules,a.to_scene_id,a.to_device_id,a.to_attribute_id,a.to_type,
            a.alarm_rules,a.alarm_value,a.tenant_id,a.company_org_id,a.dept_org_id,
            t2.name as be_attribute_name,s.SCENE_NAME as be_scene_name,
            a.compute_rules,a.alarm_rules,a.alarm_value,mm.to_attribute_name,mm.to_scene_name
        from
            sms_data_alarm_manage  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, t2.name as to_attribute_name,s.SCENE_NAME as to_scene_name
            from
            sms_data_alarm_manage  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.be_type = #{type} and a.to_type =#{type}
        and a.status = 1
    </select>

</mapper>