<?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.MonitorInfoMapper">
    <resultMap type="MonitorInfoEntity" id="MonitorInfoResult">
        <result property="id" column="id"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="sceneId" column="scene_id"/>
        <result property="deviceId" column="device_id"/>
        <result property="attributeId" column="attribute_id"/>
        <result property="monitorName" column="monitor_name"/>
        <result property="imageType" column="image_type"/>
        <result property="monitorType" column="monitor_type"/>
        <result property="pointLeft" column="point_left"/>
        <result property="pointTop" column="point_top"/>
        <result property="pointX" column="point_x"/>
        <result property="pointY" column="point_y"/>
        <result property="seq" column="seq"/>
        <result property="status" column="status"/>
        <result property="dateCreate" column="date_create"/>
        <result property="createBy" column="create_by"/>
        <result property="dateUpdate" column="date_update"/>
        <result property="updateBy" column="update_by"/>
        <result property="monitorSceneId" column="monitor_scene_id"/>
    </resultMap>
    <!--  实体栏位  -->
    <sql id="monitorInfoColumns">
         a.id as "id" ,
         a.tenant_id as "tenantId" ,
         a.scene_id as "sceneId" ,
         a.device_id as "deviceId" ,
         a.attribute_id as "attributeId" ,
         case when a.monitor_type=0 then IFNULL(d.remark,a.monitor_name) else  a.monitor_name end as "monitorName" ,
         a.image_type as "imageType" ,
         a.monitor_type as "monitorType" ,
         a.point_left as "pointLeft" ,
         a.point_top as "pointTop" ,
         a.point_x as "pointX" ,
         a.point_y as "pointY" ,
         a.seq as "seq" ,
         a.status as "status" ,
         a.date_create as "dateCreate" ,
         a.create_by as "createBy" ,
         a.date_update as "dateUpdate" ,
         a.update_by as "updateBy" ,
         b.DEVICE_CODE as "deviceCode",
         a.monitor_scene_id as "monitorSceneId"
     </sql>
    <sql id="leftJoins">
          LEFT JOIN sms_device b on a.DEVICE_ID=b.ID
          left join sms_device_parm d on a.SCENE_ID=d.SCENE_ID and d.DEVICE_ID=a.DEVICE_ID and d.ATTRIBUTE_ID = a.ATTRIBUTE_ID and  d.`STATUS`=1
    </sql>
    <!--  根据主键获取实体   -->
    <select id="findMonitorInfoById" resultType="com.zcxk.entity.MonitorInfoEntity">
        SELECT
        <include refid="monitorInfoColumns"/>
        FROM sms_monitor_info a
        <include refid="leftJoins"/>
        WHERE a.id = #{id}
    </select>

    <!--  根据获取实体List   -->
    <select id="findList" resultType="com.zcxk.entity.MonitorInfoEntity">
        SELECT
        <include refid="monitorInfoColumns"/>
        FROM sms_monitor_info a
        <include refid="leftJoins"/>
        <where>
            a.status=1
            <if test="tenantId != null  and tenantId != ''">and a.tenant_id = #{tenantId}</if>
            <if test="sceneId != null ">and a.scene_id = #{sceneId}</if>
            <if test="deviceId != null ">and a.device_id = #{deviceId}</if>
            <if test="attributeId != null ">and a.attribute_id = #{attributeId}</if>
            <if test="imageType != null ">and a.image_type = #{imageType}</if>
            <if test="type != null ">and a.type = #{type}</if>
            <if test="type == null ">and a.type is null</if>
            <if test="monitorType != null ">and a.monitor_type = #{monitorType}</if>
        </where>
    </select>

    <!--  根据获取实体 page   -->
    <select id="findPage" resultType="com.zcxk.entity.MonitorInfoEntity">
        SELECT
        <include refid="monitorInfoColumns"/>
        FROM sms_monitor_info a
        <include refid="leftJoins"/>
        <where>
            a.status=1
            <if test="monitorInfo.tenantId != null  and monitorInfo.tenantId != ''">and a.tenant_id =
                #{monitorInfo.tenantId}
            </if>
            <if test="monitorInfo.sceneId != null ">and a.scene_id = #{monitorInfo.sceneId}</if>
            <if test="monitorInfo.deviceId != null ">and a.device_id = #{monitorInfo.deviceId}</if>
            <if test="monitorInfo.attributeId != null ">and a.attribute_id = #{monitorInfo.attributeId}</if>
            <if test="monitorInfo.imageType != null ">and a.image_type = #{monitorInfo.imageType}</if>
            <if test="monitorInfo.monitorType != null ">and a.monitor_type = #{monitorInfo.monitorType}</if>
        </where>
    </select>
    <select id="findAttributeList" resultType="com.zcxk.dto.SceneDeviceAttributeDto">
       SELECT  IFNULL(a.REMARK,b.`NAME`) AS "attributeName",a.ATTRIBUTE_ID as "attributeId",
        c.min_value as "minValue" ,c.max_value as "maxValue" ,c.avg_value as "avgValue" ,
        c.sum_value as "sumValue" ,c.latest_value as "latestValue",
        a.PARM_TYPE as "attributeType"
        FROM sms_device_parm a
        INNER JOIN sms_device_attribute b on a.ATTRIBUTE_ID=b.ID
        INNER JOIN sms_day_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
        INNER JOIN (select b.PARM_TYPE,max(d.collect_date) collect_date
        from sms_day_report d
        INNER JOIN sms_device_parm b on d.DEVICE_ID=b.DEVICE_ID and d.ATTRIBUTE_ID = b.ATTRIBUTE_ID and  b.`STATUS`=1
        where d.DEVICE_ID = #{report.deviceId}
        <if test="report.year != null ">and d.`YEAR`=#{report.year}</if>
        <if test="report.month != null ">and d.`MONTH`=#{report.month}</if>
        <if test="report.day != null ">and d.`DAY`=#{report.day}</if>
        and (b.PARM_TYPE =3 or b.PARM_TYPE =4 or b.PARM_TYPE =5 or b.PARM_TYPE =6)
        GROUP BY b.PARM_TYPE) as tab on a.PARM_TYPE=tab.PARM_TYPE and c.collect_date=tab.collect_date
        where
        a.DEVICE_ID = #{report.deviceId} and  a.`STATUS`=1
        <if test="report.sceneId != null ">and a.PARENT_SCENE_ID = #{report.sceneId}</if>
        <if test="report.year != null ">and c.`YEAR`=#{report.year}</if>
        <if test="report.month != null ">and c.`MONTH`=#{report.month}</if>
        <if test="report.day != null ">and c.`DAY`=#{report.day}</if>
        and (a.PARM_TYPE =3 or a.PARM_TYPE =4 or a.PARM_TYPE =5 or a.PARM_TYPE =6)
        ORDER BY c.collect_date desc
    </select>
    <select id="findAttributeListForLastDay" resultType="com.zcxk.dto.SceneDeviceAttributeDto">
        SELECT  IFNULL(a.REMARK,b.`NAME`) AS "attributeName",a.ATTRIBUTE_ID as "attributeId",
        c.min_value as "minValue" ,c.max_value as "maxValue" ,c.avg_value as "avgValue" ,
        c.sum_value as "sumValue" ,c.latest_value as "latestValue",
        a.PARM_TYPE as "attributeType"
        FROM sms_device_parm a
        INNER JOIN sms_device_attribute b on a.ATTRIBUTE_ID=b.ID
        INNER JOIN sms_month_report c on a.ATTRIBUTE_ID=c.ATTRIBUTE_ID and a.DEVICE_ID=c.DEVICE_ID
        where
        a.DEVICE_ID = #{report.deviceId} and  a.`STATUS`=1
        <if test="report.sceneId != null ">and a.PARENT_SCENE_ID = #{report.sceneId}</if>
        <if test="report.year != null ">and c.`YEAR`=#{report.year}</if>
        <if test="report.month != null ">and c.`MONTH`=#{report.month}</if>
        <if test="report.day != null ">and c.`DAY`=#{report.day}</if>
        and (a.PARM_TYPE =3 or a.PARM_TYPE =4 or a.PARM_TYPE =5 or a.PARM_TYPE =6)
        ORDER BY c.collect_date desc
    </select>
</mapper>