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

    <sql id="Base_Column_List">
             p.ID as "id"
            ,p.TENANT_ID as "tenantId"
            ,p.parent_scene_id as "parentSceneId"
            ,p.scene_id as "sceneId"
            ,p.DEVICE_ID as "deviceId"
            ,p.ATTRIBUTE_ID as "attributeId"
            ,p.IS_SUSPENSION as "isSuspension"
            ,p.IS_REPORT as "isReport"
            ,p.IS_CHART as "isChart"
            ,p.IS_MAP as "isMap"
            ,p.IS_ALARM as "isAlarm"
            ,p.IS_ARTWORK as "isArtwork"
            ,p.SEQ as "seq"
            ,p.REMARK as "remark"
            ,p.STATUS as "status"
            ,p.DATE_CREATE as "dateCreate"
            ,p.CREATE_BY as "createBy"
            ,p.DATE_UPDATE as "dateUpdate"
            ,p.UPDATE_BY as "updateBy"
            ,p.parm_type as "parmType"
            ,t.`NAME` as "attributeName"
    </sql>

    <sql id="deviceParmJoins">
        left join sms_device_attribute t on t.id= p.ATTRIBUTE_ID
    </sql>

    <select id="selectByDeviceId" resultType="com.zcxk.entity.DeviceParmEntity">
        select      p.ID as "id"
                    ,p.TENANT_ID as "tenantId"
                    ,p.parent_scene_id as "parentSceneId"
                    ,p.scene_id as "sceneId"
                    ,p.DEVICE_ID as "deviceId"
                    ,b.id  as "attributeId"
                    ,p.IS_SUSPENSION as "isSuspension"
                    ,p.IS_REPORT as "isReport"
                    ,p.IS_CHART as "isChart"
                    ,p.IS_MAP as "isMap"
                    ,p.IS_ALARM as "isAlarm"
                    ,p.IS_ARTWORK as "isArtwork"
                    ,p.SEQ as "seq"
                    ,p.REMARK as "remark"
                    ,p.STATUS as "status"
                    ,p.DATE_CREATE as "dateCreate"
                    ,p.CREATE_BY as "createBy"
                    ,p.DATE_UPDATE as "dateUpdate"
                    ,p.UPDATE_BY as "updateBy"
                    ,b.`NAME` as "attributeName"
                    ,p.PARM_TYPE as "parmType"
        from sms_device  a
        inner join sms_device_parm p on p.DEVICE_ID=a.id and p.scene_id = #{sceneId} and p.`STATUS`=1
        inner join sms_device_attribute b on p.ATTRIBUTE_ID=b.id and b.`STATUS`=1
        where a.id=#{id}
    </select>
    <select id="selectBySceneId" resultType="com.zcxk.entity.DeviceParmEntity">
        select       p.ID as "id"
                    ,p.TENANT_ID as "tenantId"
                    ,p.parent_scene_id as "parentSceneId"
                    ,p.scene_id as "sceneId"
                    ,p.DEVICE_ID as "deviceId"
                    ,b.id  as "attributeId"
                    ,p.IS_SUSPENSION as "isSuspension"
                    ,p.IS_REPORT as "isReport"
                    ,p.IS_CHART as "isChart"
                    ,p.IS_MAP as "isMap"
                    ,p.IS_ALARM as "isAlarm"
                    ,p.IS_ARTWORK as "isArtwork"
                    ,p.SEQ as "seq"
                    ,p.REMARK as "remark"
                    ,p.STATUS as "status"
                    ,p.DATE_CREATE as "dateCreate"
                    ,p.CREATE_BY as "createBy"
                    ,p.DATE_UPDATE as "dateUpdate"
                    ,p.UPDATE_BY as "updateBy"
                    ,ifnull(p.REMARK,b.`NAME`) as "attributeName"
                    ,p.PARM_TYPE as "parmType"
                    ,a.device_code as "deviceCode"
        from sms_device_parm p
        inner join sms_device_attribute b on p.ATTRIBUTE_ID=b.id and b.`STATUS`=1
        inner join sms_device  a on a.id=p.DEVICE_ID
        where p.IS_ARTWORK=1 and (p.scene_id = #{sceneId} or p.parent_scene_id = #{sceneId}) and p.`STATUS`=1
    </select>
    <select id="selectByDeviceIdForGis" resultType="com.zcxk.entity.DeviceParmEntity">
        select      p.ID as "id"
                    ,p.TENANT_ID as "tenantId"
                    ,p.DEVICE_ID as "deviceId"
                    ,b.id  as "attributeId"
                    ,p.IS_SUSPENSION as "isSuspension"
                    ,p.IS_REPORT as "isReport"
                    ,p.IS_CHART as "isChart"
                    ,p.IS_MAP as "isMap"
                    ,p.IS_ALARM as "isAlarm"
                    ,p.IS_ARTWORK as "isArtwork"
                    ,p.SEQ as "seq"
                    ,p.REMARK as "remark"
                    ,p.STATUS as "status"
                    ,p.DATE_CREATE as "dateCreate"
                    ,p.CREATE_BY as "createBy"
                    ,p.DATE_UPDATE as "dateUpdate"
                    ,p.UPDATE_BY as "updateBy"
                    ,p.parm_type as "parmType"
                    ,b.`NAME` as "attributeName"
        from sms_device  a
         inner join sms_device_parm p on p.DEVICE_ID=a.id and (p.parent_scene_id = #{sceneId} or  p.SCENE_ID= #{sceneId})  and p.`STATUS`=1
        inner join sms_device_attribute b on p.ATTRIBUTE_ID=b.id and b.`STATUS`=1
        where a.id=#{id} and p.IS_ARTWORK=1  order by  p.seq is null,p.seq asc
    </select>

    <select id="selectById" resultType="com.zcxk.entity.DeviceParmEntity">
        select
        <include refid="Base_Column_List"/>
        from sms_device_parm p
        <include refid="deviceParmJoins"/>
        where p.ID = #{id} and p.status = 1
    </select>


    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultType="com.zcxk.entity.DeviceParmEntity">
        select
        <include refid="Base_Column_List"/>
        from sms_device_parm p
        <include refid="deviceParmJoins"/>
        where p.status = 1
        <if test="id != null">
            and p.ID = #{id}
        </if>
        <if test="deviceId != null">
            and p.device_id = #{deviceId}
        </if>
        <if test="attributeId != null">
            and p.attribute_id = #{attributeId}
        </if>
        <if test="tenantId != null">
            and p.TENANT_ID = #{tenantId}
        </if>
        <if test="parentSceneId != null">
            and p.parent_scene_id = #{parentSceneId}
        </if>
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO sms_device_parm (TENANT_ID ,parent_scene_id,scene_id,DEVICE_ID ,ATTRIBUTE_ID ,IS_SUSPENSION ,IS_REPORT ,IS_CHART ,IS_MAP ,IS_ALARM ,IS_ARTWORK,PARM_TYPE ,REMARK ,SEQ,STATUS ,DATE_CREATE ,CREATE_BY ,DATE_UPDATE ,UPDATE_BY )
        VALUES(#{tenantId},#{parentSceneId},#{sceneId},#{deviceId},#{attributeId},#{isSuspension},#{isReport},#{isChart},#{isMap},#{isAlarm},#{isArtwork},#{parmType},#{remark},#{seq},#{status},#{dateCreate},#{createBy},#{dateUpdate},#{updateBy})
   </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        INSERT INTO sms_device_parm (TENANT_ID ,parent_scene_id,scene_id,DEVICE_ID ,ATTRIBUTE_ID ,IS_SUSPENSION ,IS_REPORT ,IS_CHART ,IS_MAP
        ,IS_ALARM ,IS_ARTWORK ,PARM_TYPE,REMARK ,SEQ,STATUS ,DATE_CREATE ,CREATE_BY ,DATE_UPDATE ,UPDATE_BY )
        values
        <foreach collection="deviceParms" item="item" index="index" separator=",">
            (#{item.tenantId},#{item.parentSceneId},#{item.sceneId},#{item.deviceId},#{item.attributeId},#{item.isSuspension},#{item.isReport},#{item.isChart},#{item.isMap},#{item.isAlarm},#{item.isArtwork},#{item.parmType},#{item.remark},#{seq},#{item.status},#{item.dateCreate},#{item.createBy},#{item.dateUpdate},#{item.updateBy})
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        UPDATE sms_device_parm
        <set>
            <if test="tenantId != null and tenantId != '' ">
                TENANT_ID = #{tenantId},
            </if>
            <if test="deviceId != null ">
                DEVICE_ID = #{deviceId},
            </if>
            <if test="attributeId != null ">
                ATTRIBUTE_ID = #{attributeId},
            </if>
            <if test="isSuspension != null ">
                IS_SUSPENSION = #{isSuspension},
            </if>
            <if test="isReport != null ">
                IS_REPORT = #{isReport},
            </if>
            <if test="isChart != null ">
                IS_CHART = #{isChart},
            </if>
            <if test="isMap != null ">
                IS_MAP = #{isMap},
            </if>
            <if test="isAlarm != null ">
                IS_ALARM = #{isAlarm},
            </if>
            <if test="isArtwork != null ">
                IS_ARTWORK = #{isArtwork},
            </if>
            REMARK = #{remark},
            parm_type = #{parmType},
            <if test="seq != null and seq != '' ">
                SEQ = #{seq},
            </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>
        </set>
        WHERE
        ID = #{id};
    </update>

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

    <delete id="deleteByDeviceId">
         UPDATE  sms_device_parm set STATUS  = 0 where device_id = #{deviceId}
    </delete>


    <!-- 分页查询 -->
    <select id="selectPage" resultType="com.zcxk.entity.DeviceParmEntity">
        select
        <include refid="Base_Column_List"/>
        from sms_device_parm p
        <include refid="deviceParmJoins"/>
        where p.status = 1

    </select>
    <select id="findAttributeNameList" resultType="com.zcxk.dto.ReportAttributeDto">
        SELECT DISTINCT tab.attributeName,tab.attributeType,tab.SEQ,tab.showMinValue,tab.showAvgValue,tab.showMaxValue,
        tab.showSumValue,tab.showLatestValue,tab.classify,tab.showOnOff,tab.deviceName,tab.deviceParmId,tab.dimensionId,
        tab.unit
        from
        (SELECT
        IFNULL(a.REMARK,b.`NAME`) AS "attributeName",
        a.parm_type as "attributeType",a.SEQ,
        s.SHOW_MIN_VALUE AS "showMinValue",
        s.SHOW_AVG_VALUE AS "showAvgValue",
        s.SHOW_MAX_VALUE AS "showMaxValue",
        s.SHOW_SUM_VALUE AS "showSumValue",
        s.SHOW_LATEST_VALUE AS "showLatestValue",
        s.id as "dimensionId",
        s.CLASSIFY AS "classify",
        s.SHOW_ON_OFF AS "showOnOff",
        d.DEVICE_NAME AS "deviceName",
        a.id AS "deviceParmId",
        b.UNIT as "unit"
        FROM sms_device_parm a
        INNER JOIN sms_device d on d.ID = a.DEVICE_ID
        INNER JOIN sms_device_attribute b on a.ATTRIBUTE_ID=b.ID
        LEFT JOIN sms_report_dimension_setting s on s.device_parm_id=a.ID and s.type is null
        <if test ="report.userId != null">
            and s.user_id =#{report.userId}
        </if>
        where
        a.IS_REPORT=1 and a.status=1
        <if test="report.deviceIds != null and report.deviceIds.size() > 0">
            and a.DEVICE_ID in
            <foreach collection="report.deviceIds" item="dramaId" open="(" close=")" separator=",">
                #{dramaId.id}
            </foreach>
        </if>
        <if test="report.parentSceneIds != null and report.parentSceneIds.length > 0">
            and a.PARENT_SCENE_ID in
            <foreach collection="report.parentSceneIds" item="dramaId" open="(" close=")" separator=",">
                #{dramaId}
            </foreach>
        </if>
        <if test="report.sceneId != null ">
            and a.scene_id = #{report.sceneId}
        </if>
        <if test="report.parmType != null">
            and a.PARM_TYPE in
            <foreach collection="report.parmType" item="typeId" open="(" close=")" separator=",">
                #{typeId}
            </foreach>
        </if>
        ORDER BY a.SEQ asc
        ) as tab
        ORDER BY tab.SEQ asc
    </select>
    <select id="findHistoryDataAttributeNameList" resultType="com.zcxk.dto.ReportAttributeDto">
        SELECT DISTINCT tab.attributeName,tab.attributeType,tab.SEQ,tab.showMinValue,tab.showAvgValue,tab.showMaxValue,
        tab.showSumValue,tab.showLatestValue,tab.classify,tab.showOnOff,tab.deviceName,tab.deviceParmId,tab.dimensionId,
        tab.unit,tab.type,tab.attributeId,tab.deviceId
        from
        (SELECT
        IFNULL(a.REMARK,b.`NAME`) AS "attributeName",
        a.parm_type as "attributeType",a.SEQ,
        s.SHOW_MIN_VALUE AS "showMinValue",
        s.SHOW_AVG_VALUE AS "showAvgValue",
        s.SHOW_MAX_VALUE AS "showMaxValue",
        s.SHOW_SUM_VALUE AS "showSumValue",
        s.SHOW_LATEST_VALUE AS "showLatestValue",
        s.id as "dimensionId",
        s.CLASSIFY AS "classify",
        s.SHOW_ON_OFF AS "showOnOff",
        d.DEVICE_NAME AS "deviceName",
        a.id AS "deviceParmId",
        b.UNIT as "unit",
        1 as "type",
        a.ATTRIBUTE_ID as "attributeId",
        a.DEVICE_ID as "deviceId"
        FROM sms_device_parm a
        INNER JOIN sms_device d on d.ID = a.DEVICE_ID
        INNER JOIN sms_device_attribute b on a.ATTRIBUTE_ID=b.ID
        LEFT JOIN sms_report_dimension_setting s on s.device_parm_id=a.ID and s.type=1
        <if test ="report.userId != null">
            and s.user_id =#{report.userId}
        </if>
        where
        a.IS_REPORT=1 and a.status=1
        <if test="report.deviceIds != null and report.deviceIds.size() > 0">
            and a.DEVICE_ID in
            <foreach collection="report.deviceIds" item="dramaId" open="(" close=")" separator=",">
                #{dramaId.id}
            </foreach>
        </if>
        <if test="report.parentSceneIds != null and report.parentSceneIds.length > 0">
            and a.PARENT_SCENE_ID in
            <foreach collection="report.parentSceneIds" item="dramaId" open="(" close=")" separator=",">
                #{dramaId}
            </foreach>
        </if>
        <if test="report.sceneId != null ">
            and a.scene_id = #{report.sceneId}
        </if>
        <if test="report.parmType != null">
            and a.PARM_TYPE in
            <foreach collection="report.parmType" item="typeId" open="(" close=")" separator=",">
                #{typeId}
            </foreach>
        </if>
        ORDER BY a.SEQ asc
        ) as tab
        ORDER BY tab.SEQ asc
    </select>
    <select id="findBySceneDeviceId" resultType="com.zcxk.dto.DeviceParmInfoDto">
            select a.id as "deviceId",b.id as "attributeId",b.`NAME` as "attributeName"
            ,case when p.ID is null then 0 else 1 end as "isCheck"
            from sms_device a
            inner join sms_device_attribute b on a.DEVICE_TYPE_ID= b.DEVICE_TYPE_ID  and b.`STATUS` =1
            left join sms_device_parm p on p.DEVICE_ID=a.ID and p.ATTRIBUTE_ID=b.ID and p.`STATUS`=1 and p.SCENE_ID=#{info.sceneId}
            where a.id=#{info.deviceId}
        <if test="info.sceneIds != null and info.sceneIds.size() > 0">
            and b.id not in (
                  select d.ATTRIBUTE_ID
                     from sms_device_parm d
                  where  d.`STATUS` =1 and d.DEVICE_ID = #{info.deviceId}  and d.scene_id in
               <foreach collection="info.sceneIds" item="dramaId" open="(" close=")" separator=",">
                   #{dramaId}
               </foreach>
               )
        </if>
    </select>
    <select id="selectBindByDeviceId" resultType="com.zcxk.dto.DeviceParmInfoDto">
        select p.id,a.id as "deviceId",p.attribute_id as "attributeId"
        from sms_device a
        inner join sms_device_parm p on p.DEVICE_ID=a.ID and p.`STATUS`=1 and p.SCENE_ID=#{sceneId}
        where a.ID=#{deviceId}
    </select>
    <select id="selectDeviceBySceneIdAndType"  resultType="com.zcxk.entity.DeviceParmEntity">
        select  a.DEVICE_ID as "deviceId",b.DEVICE_CODE as "deviceCode",a.ATTRIBUTE_ID  as "attributeId"
        from sms_device_parm a
        inner join sms_device b on a.DEVICE_ID=b.id
        where a.PARM_TYPE=#{info.parmType} and b.`STATUS`=1 and a.`STATUS`=1
        and a.PARENT_SCENE_ID in
        <foreach collection="info.sceneEntities" item="dramaId" open="(" close=")" separator=",">
            #{dramaId.id}
        </foreach>
    </select>
    <select id="findAlarmCount" resultType="com.zcxk.dto.ParmTypeCountDto">
        select tab.parmType,sum(ifnull(tab.nbnormalCount,0)) as "nbnormalCount" from(
        select  b.PARENT_SCENE_ID,b.PARM_TYPE as "parmType",
        case when count(c.id)>0 then 1 else 0 end  as "nbnormalCount"
        from sms_device_scene a
        INNER JOIN sms_device_parm b on a.DEVICE_ID=b.DEVICE_ID
               and a.PARENT_SCENE_ID=b.PARENT_SCENE_ID and b.PARM_TYPE in (15,8,10,11,18,19) and b.`STATUS`=1
        LEFT JOIN  sms_alarm_details c on c.PARENT_SCENE_ID=b.PARENT_SCENE_ID
               and c.ATTRIBUTE_ID=b.ATTRIBUTE_ID and c.STATE=1
        where a.PARENT_SCENE_ID in
        <foreach collection="info.sceneEntities" item="dramaId" open="(" close=")" separator=",">
            #{dramaId.id}
        </foreach>
        and
        a.`STATUS`=1
        group by b.PARENT_SCENE_ID,b.PARM_TYPE )
        as tab
        GROUP BY tab.parmType
    </select>
    <select id="findAlarmTotalCount" resultType="com.zcxk.dto.ParmTypeCountDto">
        select count(1) as "totalCount" ,sum(tab.nbnormalCount) as "nbnormalCount" from(
        select a.PARENT_SCENE_ID,case when count(c.id)>0 then 1 else 0 end  as "nbnormalCount"
        from sms_device_scene a
        INNER JOIN sms_device_parm b on a.DEVICE_ID=b.DEVICE_ID
        and a.PARENT_SCENE_ID=b.PARENT_SCENE_ID  and b.`STATUS`=1
        LEFT JOIN  sms_alarm_details c on c.PARENT_SCENE_ID=b.PARENT_SCENE_ID
        and c.ATTRIBUTE_ID=b.ATTRIBUTE_ID and c.STATE=1
        where a.PARENT_SCENE_ID in
        <foreach collection="info.sceneEntities" item="dramaId" open="(" close=")" separator=",">
            #{dramaId.id}
        </foreach>
        and
        a.`STATUS`=1
        group by a.PARENT_SCENE_ID
        ) as tab
    </select>
    <select id="findAlarmCountTotalCount" resultType="com.zcxk.dto.ParmTypeCountDto">
        select count(1) as "totalCount" ,ifnull(sum(tab.nbnormalCount),0) as "nbnormalCount" from(
        select a.PARENT_SCENE_ID,case when count(c.id)>0 then 1 else 0 end  as "nbnormalCount"
        from sms_device_scene a
        INNER JOIN sms_device_parm b on a.DEVICE_ID=b.DEVICE_ID and b.PARM_TYPE in (15,8,9,11,18,19)
        and a.PARENT_SCENE_ID=b.PARENT_SCENE_ID  and b.`STATUS`=1
        LEFT JOIN  sms_alarm_details c on c.PARENT_SCENE_ID=b.PARENT_SCENE_ID
        and c.ATTRIBUTE_ID=b.ATTRIBUTE_ID and c.STATE=1
        where a.PARENT_SCENE_ID in
        <foreach collection="info.sceneEntities" item="dramaId" open="(" close=")" separator=",">
            #{dramaId.id}
        </foreach>
        and
        a.`STATUS`=1
        group by a.PARENT_SCENE_ID
        ) as tab
    </select>
    <update id="updateStatisticalDimension" >
        UPDATE sms_device_parm
        <set>
            CLASSIFY = #{classify},
            SHOW_ON_OFF = #{showOnOff},
            SHOW_MIN_VALUE = #{showMinValue},
            SHOW_AVG_VALUE = #{showAvgValue},
            SHOW_MAX_VALUE = #{showMaxValue},
            SHOW_SUM_VALUE = #{showSumValue},
            SHOW_LATEST_VALUE = #{showLatestValue}
        </set>
        WHERE
        ID = #{deviceParmId};
    </update>
</mapper>