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

    <resultMap id="DeviceMonitorInfo" type="com.zcxk.charge.core.entity.MonitorDataEntity">
        <result property="tenantId" column="tenantId" jdbcType="INTEGER"/>
        <result property="deviceId" column="deviceId" jdbcType="INTEGER"/>
        <result property="deviceName" column="deviceName" jdbcType="INTEGER"/>
        <result property="deviceCode" column="deviceCode" jdbcType="INTEGER"/>
        <collection property="dataValues" ofType="com.zcxk.charge.core.entity.MonitorDataValueEntity" >
            <result property="attributeId" column="attributeId" jdbcType="INTEGER"/>
            <result property="attributeName" column="attributeName" jdbcType="INTEGER"/>
            <result property="identifier" column="identifier" jdbcType="INTEGER"/>
            <result property="unit" column="unit" jdbcType="INTEGER"/>
        </collection>
    </resultMap>
    <select id="getDeviceMonitorInfoByDeviceCode" resultMap="DeviceMonitorInfo">

        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"
            ,da.IDENTIFIER as "identifier"
            ,da.UNIT as "unit"
        from sms_device d
        left join sms_device_type dt on dt.id=d.DEVICE_TYPE_ID and dt.STATUS = 1
        left join sms_device_attribute da on dt.id=da.DEVICE_TYPE_ID and da.STATUS = 1
        where d.STATUS =1 and d.DEVICE_CODE=#{deviceCode}

    </select>

    <insert id="batchInsertDayReport">
        INSERT INTO sms_day_report(
        TENANT_ID, YEAR, MONTH, DAY, HOUR, DEVICE_ID, DEVICE_NAME, DEVICE_CODE, ATTRIBUTE_ID,
        ATTRIBUTE_NAME, MIN_VALUE, MAX_VALUE, AVG_VALUE, SUM_VALUE, LATEST_VALUE, COLLECT_DATE)
        VALUES
        <foreach collection="dayReportEntities" item="item" index="index" separator=",">
            (#{item.tenantId},#{item.year},#{item.month},#{item.day},#{item.hour},#{item.deviceId},#{item.deviceName},#{item.deviceCode},#{item.attributeId},
            #{item.attributeName},#{item.minValue},#{item.maxValue},#{item.avgValue},#{item.sumValue},#{item.latestValue},#{item.collectDate})
        </foreach>
    </insert>

    <insert id="batchInsertYearReport">
        INSERT INTO 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 month =#{month}
        group by TENANT_ID, DEVICE_ID, DEVICE_NAME, DEVICE_CODE, ATTRIBUTE_ID, ATTRIBUTE_NAME,  YEAR, MONTH
    </insert>

    <insert id="batchInsertMonthReport">
        INSERT INTO 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 day=#{day}
        group by TENANT_ID, DEVICE_ID, DEVICE_NAME, DEVICE_CODE, ATTRIBUTE_ID, ATTRIBUTE_NAME,  YEAR, MONTH, DAY
    </insert>


    <select id="checkReportDataExit" resultType="java.lang.Integer">
        select
            distinct device_id
        from sms_day_report
        where year=#{year} and month = #{month} and day =#{day} and hour =#{hour}
        and device_id in
        <foreach collection="deviceIds" item="item" index="index" open="(" separator="," close=")">
             #{item}
        </foreach>

    </select>
</mapper>