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

    <select id="selectDeviceInfo" resultType="com.zcxk.entity.DayReportEntity">
        select
            d.TENANT_ID as "tenantId"
            ,d.id as "deviceId"
            ,d.DEVICE_NAME as "deviceName"
            ,d.DEVICE_CODE as "deviceCode"
            ,da.id as "attributeId"
            ,da.`NAME` as "attributeName"
        from sms_device d
        left join sms_device_type dt on dt.id=d.DEVICE_TYPE_ID
        left join sms_device_attribute da on da.DEVICE_TYPE_ID =dt.id
        where d.ID=#{id}
    </select>

    <insert id="batchInsertYearReport">
        INSERT INTO `sms`.`sms_year_report`( `TENANT_ID`, `DEVICE_ID`, `DEVICE_NAME`, `DEVICE_CODE`, `ATTRIBUTE_ID`, `ATTRIBUTE_NAME`, `YEAR`, `MONTH`, `MIN_VALUE`, `MAX_VALUE`, `AVG_VALUE`, `SUM_VALUE`, `LATEST_VALUE`, `COLLECT_DATE`)
        select TENANT_ID,  DEVICE_ID, DEVICE_NAME, DEVICE_CODE, ATTRIBUTE_ID, ATTRIBUTE_NAME,  YEAR, MONTH
            ,min(MIN_VALUE) MIN_VALUE
            ,max(MAX_VALUE) MAX_VALUE
            ,avg(AVG_VALUE) AVG_VALUE
            ,sum(SUM_VALUE) SUM_VALUE
            ,(select LATEST_VALUE from sms_month_report where id = max(a.id)) LATEST_VALUE
            ,max(COLLECT_DATE) COLLECT_DATE
        from sms_month_report a
        where year =#{year} and DEVICE_ID=#{id}
        group by TENANT_ID,  DEVICE_ID, DEVICE_NAME, DEVICE_CODE, ATTRIBUTE_ID, ATTRIBUTE_NAME,  YEAR, MONTH
    </insert>

    <insert id="batchInsertMonthReport">
        INSERT INTO `sms`.`sms_month_report`( `TENANT_ID`, `DEVICE_ID`, `DEVICE_NAME`, `DEVICE_CODE`, `ATTRIBUTE_ID`, `ATTRIBUTE_NAME`, `YEAR`, `MONTH`, `DAY`, `MIN_VALUE`, `MAX_VALUE`, `AVG_VALUE`, `SUM_VALUE`, `LATEST_VALUE`, `COLLECT_DATE`)

        select TENANT_ID,  DEVICE_ID, DEVICE_NAME, DEVICE_CODE, ATTRIBUTE_ID, ATTRIBUTE_NAME,  YEAR, MONTH, DAY
            ,min(MIN_VALUE) MIN_VALUE
            ,max(MAX_VALUE) MAX_VALUE
            ,avg(AVG_VALUE) AVG_VALUE
            ,sum(SUM_VALUE) SUM_VALUE
            ,(select LATEST_VALUE from sms_day_report where id = max(a.id)) LATEST_VALUE
            ,max(COLLECT_DATE) COLLECT_DATE
        from sms_day_report a
        where year = #{year} and month =#{month} and DEVICE_ID=#{id}
        group by TENANT_ID,  DEVICE_ID, DEVICE_NAME, DEVICE_CODE, ATTRIBUTE_ID, ATTRIBUTE_NAME,  YEAR, MONTH, DAY
    </insert>

</mapper>