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


    <resultMap type="MonthReportEntity" id="MonthReportResult">
        <result property="id" column="id"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="year" column="year"/>
        <result property="month" column="month"/>
        <result property="day" column="day"/>
        <result property="hour" column="hour"/>
        <result property="parentSceneId" column="parent_scene_id"/>
        <result property="parentSceneName" column="parent_scene_name"/>
        <result property="sceneId" column="scene_id"/>
        <result property="sceneName" column="scene_name"/>
        <result property="deviceId" column="device_id"/>
        <result property="deviceName" column="device_name"/>
        <result property="deviceCode" column="device_code"/>
        <result property="attributeId" column="attribute_id"/>
        <result property="attributeName" column="attribute_name"/>
        <result property="minValue" column="min_value"/>
        <result property="maxValue" column="max_value"/>
        <result property="avgValue" column="avg_value"/>
        <result property="sumValue" column="sum_value"/>
        <result property="latestValue" column="latest_value"/>
        <result property="collectDate" column="collect_date"/>
        <result property="dateCreate" column="date_create"/>
        <result property="createBy" column="create_by"/>
        <result property="dateUpdate" column="date_update"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>

    <!--  实体栏位  -->
    <sql id="monthReportColumns">
         a.id as "id" ,
         a.tenant_id as "tenantId" ,
         a.year as "year" ,
         a.month as "month" ,
         a.day as "day" ,
         a.hour as "hour" ,
         a.parent_scene_id as "parentSceneId" ,
         a.parent_scene_name as "parentSceneName" ,
         a.scene_id as "sceneId" ,
         a.scene_name as "sceneName" ,
         a.device_id as "deviceId" ,
         a.device_name as "deviceName" ,
         a.device_code as "deviceCode" ,
         a.attribute_id as "attributeId" ,
         a.attribute_name as "attributeName" ,
         a.min_value as "minValue" ,
         a.max_value as "maxValue" ,
         a.avg_value as "avgValue" ,
         a.sum_value as "sumValue" ,
         a.latest_value as "latestValue" ,
         a.collect_date as "collectDate" ,
         a.date_create as "dateCreate" ,
         a.create_by as "createBy" ,
         a.date_update as "dateUpdate" ,
         a.update_by as "updateBy" 
     </sql>


    <!--  根据主键获取实体   -->
    <select id="findMonthReportById" resultType="com.zcxk.entity.MonthReportEntity">
        SELECT
        <include refid="monthReportColumns"/>
        FROM sms_month_report a
        WHERE a.id = #{id}
    </select>

    <!--  根据获取实体List   -->
    <select id="findList" resultType="com.zcxk.entity.MonthReportEntity">
        SELECT
        <include refid="monthReportColumns"/>
        FROM sms_month_report a
        <where>
            <if test="tenantId != null  and tenantId != ''">and a.tenant_id = #{tenantId}</if>
            <if test="year != null ">and a.year = #{year}</if>
            <if test="month != null ">and a.month = #{month}</if>
            <if test="day != null ">and a.day = #{day}</if>
            <if test="hour != null ">and a.hour = #{hour}</if>
            <if test="parentSceneId != null ">and a.parent_scene_id = #{parentSceneId}</if>
            <if test="sceneId != null ">and a.scene_id = #{sceneId}</if>
            <if test="deviceId != null ">and a.device_id = #{deviceId}</if>
            <if test="deviceCode != null  and deviceCode != ''">and a.device_code = #{deviceCode}</if>
            <if test="attributeId != null ">and a.attribute_id = #{attributeId}</if>
            <if test="collectDate != null ">and a.collect_date = #{collectDate}</if>
        </where>
    </select>

    <!--  根据获取实体 page   -->
    <select id="findPage" resultType="com.zcxk.dto.ReportDto">
        SELECT
        distinct
        a.tenant_id as "tenantId" ,
        a.year as "year" ,
        a.month as "month" ,
        a.day as "day" ,
        a.DEVICE_NAME as "deviceName",
        b.parent_scene_id as "parentSceneId",
        c.SCENE_NAME as "parentSceneName"
        FROM sms_month_report a
        inner join sms_device_parm b on a.DEVICE_ID = b.DEVICE_ID and a.ATTRIBUTE_ID=b.ATTRIBUTE_ID
        <if test="monthReport.typeIds != null">
            and b.PARM_TYPE in
            <foreach collection="monthReport.typeIds" item="typeId" open="(" close=")" separator=",">
                #{typeId}
            </foreach>
        </if>
        and b.`STATUS`=1
        inner join sms_scene c on b.PARENT_SCENE_ID=c.id
        <where>
            b.IS_REPORT=1
            <if test="monthReport.tenantId != null  and monthReport.tenantId != ''">and a.tenant_id =
                #{monthReport.tenantId}
            </if>
            <if test="monthReport.deviceIds != null and monthReport.deviceIds.size() > 0">
                and b.DEVICE_ID in
                <foreach collection="monthReport.deviceIds" item="dramaId" open="(" close=")" separator=",">
                    #{dramaId.id}
                </foreach>
            </if>
            <if test="monthReport.year != null ">and a.year = #{monthReport.year}</if>
            <if test="monthReport.month != null ">and a.month = #{monthReport.month}</if>
            <if test="monthReport.parentSceneIds != null and monthReport.parentSceneIds.length > 0">
            and b.parent_scene_id in
            <foreach collection = "monthReport.parentSceneIds" item = "dramaId" open = "(" close = ")" separator = "," >
                #{dramaId}
            </foreach>
            </if>
        </where>
        order by  b.parent_scene_id,a.year,a.month,a.day
    </select>
    <!--  根据获取实体 page   -->
    <select id="findPageReprot" resultType="com.zcxk.dto.ReportDto">
        SELECT
        distinct
        a.tenant_id as "tenantId" ,
        a.year as "year" ,
        a.month as "month" ,
        a.day as "day" ,
        <if test="monthReport.type != null">
            a.DEVICE_NAME as "deviceName"
        </if>
        <if test="monthReport.type == null">
            b.parent_scene_id as "parentSceneId",
            c.SCENE_NAME as "parentSceneName",
            t.SCENE_TYPE_NAME as "sceneTypeName"
        </if>
        FROM sms_month_report a
        inner join sms_device_parm b on a.DEVICE_ID = b.DEVICE_ID and a.ATTRIBUTE_ID=b.ATTRIBUTE_ID
        <if test="monthReport.typeIds != null">
            and b.PARM_TYPE in
            <foreach collection="monthReport.typeIds" item="typeId" open="(" close=")" separator=",">
                #{typeId}
            </foreach>
        </if>
        and b.`STATUS`=1
        inner join sms_scene c on b.PARENT_SCENE_ID=c.id
        inner join sms_scene_type t on c.SCENE_TYPE_ID  = t.id
        <where>
            b.IS_REPORT=1
            <if test="monthReport.tenantId != null  and monthReport.tenantId != ''">and a.tenant_id =
                #{monthReport.tenantId}
            </if>
            <if test="monthReport.deviceIds != null and monthReport.deviceIds.size() > 0">
                and b.DEVICE_ID in
                <foreach collection="monthReport.deviceIds" item="dramaId" open="(" close=")" separator=",">
                    #{dramaId.id}
                </foreach>
            </if>
            <if test="monthReport.startTime != null and monthReport.endTime != null">
                and a.COLLECT_DATE BETWEEN #{monthReport.startTime} AND #{monthReport.endTime}
            </if>
            <if test="monthReport.year != null ">and a.year = #{monthReport.year}</if>
            <if test="monthReport.month != null ">and a.month = #{monthReport.month}</if>
            <if test="monthReport.parentSceneIds != null and monthReport.parentSceneIds.length > 0">
                and b.parent_scene_id in
                <foreach collection = "monthReport.parentSceneIds" item = "dramaId" open = "(" close = ")" separator = "," >
                    #{dramaId}
                </foreach>
            </if>
            <if test="monthReport.sceneId != null ">
                and b.scene_id = #{monthReport.sceneId}
            </if>
        </where>
        <if test="monthReport.type == null">
            order by b.parent_scene_id,a.year,a.month,a.day
        </if>
        <if test="monthReport.type != null">
            order by a.year,a.month,a.day
        </if>
    </select>
    <select id="findAttributeList" resultType="com.zcxk.dto.ReportAttributeDto">
        SELECT tab.deviceParmId,tab.attributeName,tab.ATTRIBUTE_TYPE,min(tab.minValue) as "minValue",
        max(tab.maxValue) as "maxValue",AVG(tab.avgValue)as "avgValue",
        AVG(tab.sumValue)as "sumValue",AVG(tab.latestValue) as "latestValue"
        from
        (SELECT a.id as "deviceParmId",IFNULL(a.REMARK,b.`NAME`) AS "attributeName",
        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 ATTRIBUTE_TYPE
        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.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.startTime != null and report.endTime != null ">
            and c.COLLECT_DATE BETWEEN #{report.startTime} AND #{report.endTime}
        </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>
        <if test="report.parentSceneId != null ">and a.PARENT_SCENE_ID=#{report.parentSceneId}</if>
        <if test="report.sceneId != null ">and a.scene_id = #{report.sceneId} </if>
        ORDER BY a.SEQ
        ) as tab
        group by tab.deviceParmId,tab.attributeName,tab.ATTRIBUTE_TYPE
    </select>

    <select id="findReport" resultType="com.zcxk.dto.ReportDto">
        select
        tab.year,tab.month,tab.day,
        sum(ifnull(intakeWaterUsage,0))as "intakeWaterUsage",
        sum(ifnull(yieldWaterUsage,0))as "yieldWaterUsage",
        sum(ifnull(powerUsage,0))as "powerUsage",
        sum(ifnull(drugUsage,0))as "drugUsage"
        from
        (SELECT
        c.year,c.month,c.day,
        case when a.PARM_TYPE=4 then
        c.SUM_VALUE end as "intakeWaterUsage",
        case when a.PARM_TYPE=3 then
        c.SUM_VALUE end as "yieldWaterUsage",
        case when a.PARM_TYPE=5 then
        c.SUM_VALUE end as "powerUsage",
        case when a.PARM_TYPE=6 then
        c.SUM_VALUE end as "drugUsage"
        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
        <if test="report.deviceIds != null and report.deviceIds.size() > 0">
            a.DEVICE_ID in
            <foreach collection="report.deviceIds" item="dramaId" open="(" close=")" separator=",">
                #{dramaId.id}
            </foreach>
        </if>
        <if test="report.parentSceneIds != null">
            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.startTime != null and report.endTime != null ">
            and c.COLLECT_DATE BETWEEN #{report.startTime} AND #{report.endTime}
        </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.parentSceneId != null ">and c.PARENT_SCENE_ID=#{report.parentSceneId}</if>
        <if test="report.parmType != null ">and a.PARM_TYPE in
            <foreach collection="report.parmType" item="ptype" open="(" close=")" separator=",">
                #{ptype}
            </foreach>
         and a.`STATUS`=1) tab
        </if>
        <if test="report.parmType == null ">
            and (a.PARM_TYPE =3 or a.PARM_TYPE =4 or a.PARM_TYPE =5 or a.PARM_TYPE =6) and a.`STATUS`=1) tab
        </if>
        group by tab.year,tab.month,tab.day
        order by tab.year desc,tab.month desc,tab.day desc
    </select>
    <!--  根据获取实体 page   -->
    <select id="findDevicePressPage" resultType="com.zcxk.dto.DevicePressReportAttributeDto">
        SELECT
        a.DEVICE_CODE as  "deviceCode",
        a.DEVICE_NAME as "deviceName",
        a.min_value as "minValue" ,a.max_value as "maxValue" ,a.avg_value as "avgValue" ,
        a.sum_value as "sumValue" ,a.latest_value as "latestValue",
        a.COLLECT_DATE as "collectDate"
        FROM sms_month_report a
        inner join sms_device d on d.id = a.DEVICE_ID
        inner join sms_device_parm b on a.DEVICE_ID = b.DEVICE_ID and a.ATTRIBUTE_ID=b.ATTRIBUTE_ID and b.`STATUS`=1
        <if test="monthReport.type == 1">
            and b.PARM_TYPE = 13
        </if>
        <if test="monthReport.type == 2">
            and b.PARM_TYPE = 14
        </if>
        <where>
            <if test="monthReport.tenantId != null  and monthReport.tenantId != ''">and a.tenant_id =
                #{monthReport.tenantId}
            </if>
            <if test="monthReport.year != null ">and a.year = #{monthReport.year}</if>
            <if test="monthReport.month != null ">and a.month = #{monthReport.month}</if>
            and a.DEVICE_ID in
            <foreach collection="monthReport.deviceIds" item="dramaId" open="(" close=")" separator=",">
                #{dramaId.id}
            </foreach>
        </where>
        order by a.DEVICE_ID,a.year,a.month,a.day
    </select>
    <!--  根据获取实体 page   -->
    <select id="findDeviceWaterPage" resultType="com.zcxk.dto.DeviceWaterReportAttributeDto">
        select tab.deviceCode,tab.deviceName,tab.collectDate,
        sum(tab.minPHValue) as minPHValue,sum(tab.maxPHValue) as maxPHValue,sum(tab.avgPHValue) as avgPHValue,sum(tab.sumPHValue) as sumPHValue,sum(tab.latestPHValue) as latestPHValue,
        sum(tab.minQuValue) as minQuValue,sum(tab.maxQuValue) as maxQuValue,sum(tab.avgQuValue) as avgQuValue,sum(tab.sumQuValue) as sumQuValue,sum(tab.latestQuValue) as latestQuValue,
        sum(tab.minCLValue) as minCLValue,sum(tab.maxCLValue) as maxCLValue,sum(tab.avgCLValue) as avgCLValue,sum(tab.sumCLValue) as sumCLValue,sum(tab.latestCLValue) as latestCLValue
        from (
        SELECT a.DEVICE_ID,a.id,a.DEVICE_CODE as "deviceCode", a.DEVICE_NAME as "deviceName",
        case when b.PARM_TYPE=7 then a.min_value end as "minPHValue" ,
        case when b.PARM_TYPE=7 then a.max_value end as "maxPHValue" ,
        case when b.PARM_TYPE=7 then a.avg_value end as "avgPHValue" ,
        case when b.PARM_TYPE=7 then a.sum_value end as "sumPHValue" ,
        case when b.PARM_TYPE=7 then a.latest_value end as "latestPHValue",
        case when b.PARM_TYPE=9 then a.min_value end as "minQuValue" ,
        case when b.PARM_TYPE=9 then a.max_value end as "maxQuValue" ,
        case when b.PARM_TYPE=9 then a.avg_value end as "avgQuValue" ,
        case when b.PARM_TYPE=9 then a.sum_value end as "sumQuValue" ,
        case when b.PARM_TYPE=9 then a.latest_value end as "latestQuValue",
        case when b.PARM_TYPE=11 then a.min_value end as "minCLValue" ,
        case when b.PARM_TYPE=11 then a.max_value end as "maxCLValue" ,
        case when b.PARM_TYPE=11 then a.avg_value end as "avgCLValue" ,
        case when b.PARM_TYPE=11 then a.sum_value end as "sumCLValue" ,
        case when b.PARM_TYPE=11 then a.latest_value end as "latestCLValue",
        a.COLLECT_DATE as "collectDate" ,
        a.year,a.month,a.day
        FROM sms_month_report a
        inner join sms_device d on d.id = a.DEVICE_ID
        inner join sms_device_parm b on a.DEVICE_ID = b.DEVICE_ID and a.ATTRIBUTE_ID=b.ATTRIBUTE_ID and b.`STATUS`=1
        and (b.PARM_TYPE = 7 or b.PARM_TYPE = 9  or b.PARM_TYPE = 11)
        <where>
            <if test="monthReport.tenantId != null  and monthReport.tenantId != ''">and a.tenant_id =
                #{monthReport.tenantId}
            </if>
            <if test="monthReport.year != null ">and a.year = #{monthReport.year}</if>
            <if test="monthReport.month != null ">and a.month = #{monthReport.month}</if>
            and a.DEVICE_ID in
            <foreach collection="monthReport.deviceIds" item="dramaId" open="(" close=")" separator=",">
                #{dramaId.id}
            </foreach>
        </where>) as tab
        group by tab.DEVICE_ID,tab.year,tab.month,tab.day,tab.deviceCode,tab.deviceName,tab.collectDate
        order by tab.DEVICE_ID,tab.year,tab.month,tab.day
    </select>
    <select id="findAmountBySceneIds" resultType="com.zcxk.entity.MonthReportEntity">
        SELECT a.year as "year",a.`MONTH` as "month",a.`DAY` as "day",sum(a.LATEST_VALUE) as "latestValue",
        sum(a.SUM_VALUE) as "sumValue"
        FROM  sms_month_report a
        INNER JOIN sms_device_parm b on a.DEVICE_ID=b.DEVICE_ID and a.ATTRIBUTE_ID=b.ATTRIBUTE_ID and b.`STATUS`=1
        where DATE_SUB(CURDATE(), INTERVAL 15 DAY) &lt;= date(a.COLLECT_DATE)
        and b.PARM_TYPE=3 and b.PARENT_SCENE_ID in
        <foreach collection="monthReport.parentSceneLists" item="dramaId" open="(" close=")" separator=",">
            #{dramaId.id}
        </foreach>
        group by a.year,a.`MONTH`,a.`DAY`
        order by a.year desc,a.`MONTH` desc,a.`DAY` desc
    </select>
    <select id="findAmountTotalBySceneIds" resultType="com.zcxk.entity.MonthReportEntity">
        select sum(a.LATEST_VALUE) as "latestValue"
        from sms_month_report a
        inner join sms_device_parm b on a.DEVICE_ID=b.DEVICE_ID and b.ATTRIBUTE_ID=a.ATTRIBUTE_ID and b.`STATUS`=1
        inner join (
        select max(a.`DAY`) as day,a.DEVICE_ID
        from sms_month_report a
        inner join sms_device_parm b on a.DEVICE_ID=b.DEVICE_ID and b.ATTRIBUTE_ID=a.ATTRIBUTE_ID and b.`STATUS`=1
        where  b.PARM_TYPE=3 and b.PARENT_SCENE_ID in
        <foreach collection="monthReport.parentSceneLists" item="dramaId" open="(" close=")" separator=",">
            #{dramaId.id}
        </foreach>
        <if test="monthReport.year != null ">and a.year = #{monthReport.year}</if>
        <if test="monthReport.month != null ">and a.month = #{monthReport.month}</if>
        GROUP BY a.DEVICE_ID
        )as tab on tab.DEVICE_ID=a.DEVICE_ID and a.`DAY`=tab.`day`
        where  b.PARM_TYPE=3 and b.PARENT_SCENE_ID in
        <foreach collection="monthReport.parentSceneLists" item="dramaId" open="(" close=")" separator=",">
            #{dramaId.id}
        </foreach>
        <if test="monthReport.year != null ">and a.year = #{monthReport.year}</if>
        <if test="monthReport.month != null ">and a.month = #{monthReport.month}</if>
    </select>
    <select id="findAttributeListForMonth" resultType="com.zcxk.dto.ReportAttributeDto">
        SELECT tab.attributeName,tab.ATTRIBUTE_TYPE,sum(tab.sumValue)as "sumValue"
        from
        (SELECT IFNULL(a.REMARK,b.`NAME`) AS "attributeName",
        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 ATTRIBUTE_TYPE
        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.`STATUS`=1 and (a.PARM_TYPE=3 or a.PARM_TYPE=4)
        <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.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>
        <if test="report.parentSceneId != null ">and a.PARENT_SCENE_ID=#{report.parentSceneId}</if>
        ) as tab
        group by tab.attributeName,tab.ATTRIBUTE_TYPE
    </select>

    <select id="findSumValue" resultType="com.zcxk.dto.ReportAttributeDto">
        SELECT ROUND(a.SUM_VALUE, 2) as sumValue FROM sms_month_report a
        <where>
            <if test="report.tenantId != null  and report.tenantId != ''">and a.tenant_id = #{report.tenantId}</if>
            <if test="report.year != null ">and a.year = #{report.year}</if>
            <if test="report.month != null ">and a.month = #{report.month}</if>
            <if test="report.day != null ">and a.day = #{report.day}</if>
            <if test="report.deviceId != null ">and a.device_id = #{report.deviceId}</if>
            <if test="report.attributeId != null ">and a.attribute_id = #{report.attributeId}</if>
        </where>
        limit 1;
    </select>
</mapper>