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

    <insert id="saveDataAlarmAnalysis" useGeneratedKeys="true"  keyProperty="id" parameterType="com.zcxk.charge.core.entity.DataAlarmAnalysisEntity">
        insert sms_data_alarm_analysis (alarm_name,manage_id,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
               ,result_alarm_value,date_create,status)
        values
            (#{alarmName}, #{manageId},#{companyOrgId}, #{deptOrgId}, #{tenantId}, #{beSceneId},#{beDeviceId},
        #{beAttributeId}, #{beAlarmValue},#{beType}, #{computeRules}, #{toSceneId},
        #{toDeviceId}, #{toAttributeId},#{toAlarmValue}, #{toType}, #{alarmRules},
        #{alarmValue},#{resultAlarmValue},#{dateCreate},#{status})
    </insert>

    <select id="findPage" resultType="com.zcxk.charge.api.vo.DataAlarmAnalysisVo">
        select
            a.id,a.alarm_name,a.compute_rules,a.alarm_rules,a.date_create,
            a.be_alarm_value,a.to_alarm_value,a.result_alarm_value,a.alarm_value,
            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_analysis  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_analysis  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="dto.beginTime != null and dto.endTime != null and dto.beginTime != '' and dto.endTime != ''">
                and a.date_create  between #{dto.beginTime} and #{dto.endTime}
            </if>
            and a.status = 1
        </where>
        order by id desc
    </select>
</mapper>