<?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.iformall.mapper.KwMeterDataMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.KwMeterData">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
		<result column="meter_id" jdbcType="BIGINT" property="meterId"/>
		<result column="box_id" jdbcType="BIGINT" property="boxId"/>
		<result column="address" jdbcType="VARCHAR" property="address"/>
		<result column="total_power" jdbcType="INTEGER" property="totalPower"/>
		<result column="device_time" jdbcType="TIMESTAMP" property="deviceTime"/>
		<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
		<result column="used_power" jdbcType="INTEGER" property="usedPower"/>
		<result column="last_device_time" jdbcType="TIMESTAMP" property="lastDeviceTime"/>
		<result column="last_total_power" jdbcType="INTEGER" property="lastTotalPower"/>
	</resultMap>
	
	<sql id="allColumns">
        `id`,`tenant_id`,`meter_id`,`box_id`,`address`,`total_power`,`device_time`,`create_time`,`used_power`,`last_device_time`,`last_total_power`
	</sql>
	
	<sql id="dynamicWhereConditions">
		where 1 = 1
		
		<if test=" null != id ">
			and `id` = #{id}
		</if>

		<if test=" null != tenantId ">
			and `tenant_id` = #{tenantId}
		</if>

		<if test=" null != boxId ">
			and `box_id` = #{boxId}
		</if>

		<if test=" null != meterId ">
			and `meter_id` = #{meterId}
		</if>

		<if test=" null != address ">
			and `address` = #{address}
		</if>

		<if test=" null != totalPower ">
			and `total_power` is not null
		</if>

		<if test=" null != ids ">
			and id in
			<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
				#{idItem}
			</foreach>
		</if>
		<if test=" null != sortColumns">order by ${sortColumns}</if>
	</sql>
	
	<select id="findList" parameterType="com.iformall.domain.po.KwMeterData" resultMap="BaseResultMap">
		select
		<include refid="allColumns"/>
		from kw_meter_data
		<include refid="dynamicWhereConditions"/>
	</select>

	<select id="findLastRecordList" parameterType="com.iformall.domain.po.KwMeterData" resultMap="BaseResultMap">
		select
		<include refid="allColumns"/>
		from kw_meter_data
		join
		(
			select
			`meter_id` as maxMeterId,
			max(`create_time`) as maxTime
			from kw_meter_data
			where
			`total_power` is not null
			<if test=" null != meterId ">
				and `meter_id` = #{meterId}
			</if>

			<if test=" null != ids ">
				and `meter_id` in
				<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
					#{idItem}
				</foreach>
			</if>
			group by `meter_id`
		) temp
		on create_time = temp.maxTime
	  	and meter_id = temp.maxMeterId
	</select>


	<resultMap id="VoResultMap" type="com.iformall.domain.vo.KwMeterDataVo">
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
		<result column="totalPower" jdbcType="INTEGER" property="totalPower"/>
		<result column="lastTotalPower" jdbcType="INTEGER" property="lastTotalPower"/>
		<result column="usedPower" jdbcType="INTEGER" property="usedPower"/>
		<result column="reportTime" jdbcType="VARCHAR" property="reportTime"/>
		<result column="recordStartTime" jdbcType="TIMESTAMP" property="recordStartTime"/>
		<result column="recordEndTime" jdbcType="TIMESTAMP" property="recordEndTime"/>
	</resultMap>

	<sql id="dynamicWhereConditionsVo">
		where 1 = 1

		<if test=" null != tenantId ">
			and `tenant_id` = #{tenantId}
		</if>

		<if test=" null != boxId ">
			and `box_id` = #{boxId}
		</if>

		<if test=" null != meterId ">
			and `meter_id` = #{meterId}
		</if>

		<if test=" null != address ">
			and `address` = #{address}
		</if>

		<if test=" null != startTime and null != endTime ">
			and `create_time` between #{startTime} and #{endTime}
		</if>
		<if test=" null == reportType and null != reportTime">
			and  date_format(`create_time`, '%Y-%m-%d %H:00') = #{reportTime}
		</if>
		<if test=" 1 == reportType and null != reportTime">
			and  date_format(`create_time`, '%Y-%m-%d') = #{reportTime}
		</if>
		<if test=" 2 == reportType and null != reportTime">
			and  date_format(`create_time`, '%Y-%m') = #{reportTime}
		</if>
		<if test=" 3 == reportType and null != reportTime">
			and  date_format(`create_time`, '%Y') = #{reportTime}
		</if>
		<if test=" 4 == reportType and null != reportTime">
			and  date_format(`create_time`, '%Y-%m') = #{reportTime}
		</if>

	</sql>

	<select id="findVoList" parameterType="com.iformall.domain.dto.KwMeterDataDto" resultMap="VoResultMap">
		select
		temp.tenant_id,
		temp.reportTime,
		<if test="4 != reportType ">
			min(temp.recordStartTime) as recordStartTime,
			max(temp.recordEndTime) as recordEndTime,
			sum(temp.totalPower) as totalPower,
			sum(temp.lastTotalPower) as lastTotalPower,
		</if>
		sum(temp.usedPower) as usedPower
		from
			(SELECT
				tenant_id,box_id,meter_id,
				<if test=" null == reportType">
					create_time as reportTime,
				</if>
				<if test="1 == reportType ">
					date_format(create_time, '%Y-%m-%d %H:00') as reportTime,
				</if>
				<if test="2 == reportType ">
					date_format(create_time, '%Y-%m-%d') as reportTime,
				</if>
				<if test="3 == reportType ">
					date_format(create_time, '%Y-%m') as reportTime,
				</if>
				<if test="4 == reportType ">
					date_format(create_time, '%H:00') as reportTime,
				</if>
				<if test="4 != reportType ">
					min(last_device_time) as recordStartTime,
					min(last_total_power) as lastTotalPower,
					max(device_time) as recordEndTime,
					max(total_power) as totalPower,
				</if>
				sum(used_power) as usedPower
				FROM kw_meter_data
				<include refid="dynamicWhereConditionsVo"/>
				group by reportTime,meter_id,box_id
			) as temp join kw_meter on id = temp.meter_id
				<if test=" null != status ">
					and `status` = #{status}
				</if>
		group by
		temp.reportTime,
		temp.tenant_id
		<if test=" null != boxId ">
		,temp.box_id
		</if>
		<if test=" null != meterId ">
		,temp.meter_id
		</if>
		<if test=" null != sortColumns">order by ${sortColumns}</if>
	</select>

	<select id="getPower" parameterType="com.iformall.domain.dto.KwMeterDataDto" resultMap="VoResultMap">
		SELECT
		<if test=" null == reportType">
			#{reportTime} as reportTime,
		</if>
		<if test=" 1 == reportType ">
			date_format(create_time, '%Y-%m-%d %H:00') as reportTime,
		</if>
		<if test=" 2 == reportType ">
			date_format(create_time, '%Y-%m-%d') as reportTime,
		</if>
		<if test=" 3 == reportType ">
			date_format(create_time, '%Y-%m') as reportTime,
		</if>
		<if test=" null != startTime">
			min(device_time) as recordStartTime,
			min(total_power) as totalPower,
		</if>
		<if test=" null != endTime">
			max(device_time) as recordEndTime,
			max(total_power) as totalPower,
		</if>
		<if test=" null != endTime and null != endTime">
			sum(used_power) as usedPower,
		</if>
		<if test=" null != meterId ">
			meter_id,
		</if>
		<if test=" null != boxId ">
			box_id,
		</if>
		tenant_id
		FROM kw_meter_data
		where 1 = 1
		<if test=" null != tenantId ">
			and `tenant_id` = #{tenantId}
		</if>
		<if test=" null != meterId ">
			and `meter_id` = #{meterId}
		</if>
		<if test=" null != address ">
			and `address` = #{address}
		</if>

		<if test=" null != startTime">
			<if test=" null == reportType">
				and create_time &gt; #{startTime}
			</if>
			<if test=" 1 == reportType ">
				and date_format(create_time, '%Y-%m-%d %H:00') = date_format(#{startTime}, '%Y-%m-%d %H:00')
			</if>
			<if test=" 2 == reportType ">
				and date_format(create_time, '%Y-%m-%d') = date_format(#{startTime}, '%Y-%m-%d')
			</if>
			<if test=" 3 == reportType ">
				and date_format(create_time, '%Y-%m') = date_format(#{startTime}, '%Y-%m')
			</if>
		</if>

		<if test=" null != endTime">
			<if test=" null == reportType">
				and create_time &lt; #{endTime}
			</if>
			<if test=" 1 == reportType ">
				and date_format(create_time, '%Y-%m-%d %H:00') = date_format(#{endTime}, '%Y-%m-%d %H:00')
			</if>
			<if test=" 2 == reportType ">
				and date_format(create_time, '%Y-%m-%d') = date_format(#{endTime}, '%Y-%m-%d')
			</if>
			<if test=" 3 == reportType ">
				and date_format(create_time, '%Y-%m') = date_format(#{endTime}, '%Y-%m')
			</if>
		</if>

		group by reportTime
		<if test=" null != boxId ">
		,box_id
		</if>
		<if test=" null != meterId ">
		,meter_id
		</if>
	</select>

</mapper>
