<?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.KwMerchantMeterMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.KwMerchantMeter">
		<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="merchant_id" jdbcType="BIGINT" property="merchantId"/>
		<result column="status" jdbcType="INTEGER" property="status"/>
		<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
		<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>

	</resultMap>
	
	<sql id="allColumns">
        `id`,`tenant_id`,`meter_id`,`merchant_id`,`status`,`create_time`,`update_time`
	</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 != merchantId ">
			and `merchant_id` = #{merchantId}
		</if>

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

		<if test=" null != status ">
			and `status` = #{status}
		</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.KwMerchantMeter" resultMap="BaseResultMap">
		select
		<include refid="allColumns"/>
		from kw_merchant_meter
		<include refid="dynamicWhereConditions"/>
	</select>

	<select id="findSimpleMeterList" parameterType="java.util.Map" resultType="com.iformall.domain.po.KwMeter">
		select m.`id`, m.`total_power` from kw_merchant_meter mm
		left join kw_meter m on mm.`meter_id` = m.id
		where mm.`status` = 0
		<if test=" null != merchantId ">
			and mm.`merchant_id` = #{merchantId}
		</if>
	</select>

	<resultMap id="BaseResultMapListVo" type="com.iformall.domain.vo.KwMerchantMeterVo">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
		<result column="status" jdbcType="INTEGER" property="status"/>

		<result column="name" jdbcType="VARCHAR" property="merchantName"/>
		<result column="link_phone" jdbcType="VARCHAR" property="merchantLinkPhone"/>

		<collection column="{merchantId=id}" property="meterList"
		            ofType="com.iformall.domain.po.KwMeter"
		            javaType="java.util.List"
		            select="com.iformall.mapper.KwMerchantMeterMapper.findSimpleMeterList">
		</collection>
	</resultMap>

	<sql id="allColumnsVo">
        `id`, `tenant_id`, `merchant_id`, `status`, `name`, `link_phone`
	</sql>

	<select id="findVoList" parameterType="com.iformall.domain.dto.KwMerchantMeterDto" resultMap="BaseResultMapListVo">
		select <include refid="allColumnsVo"/>
		from (
			select merchant_id from kw_merchant_meter
			where `status` = 0
			<if test=" null != tenantId ">
				and `tenant_id` = #{tenantId}
			</if>

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

			group by merchant_id
			) temp
		, wx_merchant
		where temp.merchant_id = id

		<if test=" null != merchantName and ''!=merchantName ">
			and `name` like concat('%', #{merchantName},'%')
		</if>

		<if test=" null != merchantLinkPhone and ''!=merchantLinkPhone ">
			and `link_phone` like concat('%', #{merchantLinkPhone},'%')
		</if>

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

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

	</select>

	<select id="findMeterList" parameterType="java.util.Map" resultType="com.iformall.domain.po.KwMeter">
		select m.* from kw_merchant_meter mm
		left join kw_meter m on mm.`meter_id` = m.id
		where mm.`status` = 0
		<if test=" null != merchantId ">
			and mm.`merchant_id` = #{merchantId}
		</if>
	</select>


	<resultMap id="BaseResultMapVo" type="com.iformall.domain.vo.KwMerchantMeterVo">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
		<result column="status" jdbcType="INTEGER" property="status"/>

		<result column="name" jdbcType="VARCHAR" property="merchantName"/>
		<result column="link_phone" jdbcType="VARCHAR" property="merchantLinkPhone"/>

		<collection column="{merchantId=id}" property="shopVoList"
					ofType="com.iformall.domain.vo.WxShopVo"
					javaType="java.util.List"
					select="com.iformall.mapper.WxShopMapper.findShopVoList">
		</collection>

		<collection column="{merchantId=id}" property="meterList"
					ofType="com.iformall.domain.po.KwMeter"
					javaType="java.util.List"
					select="com.iformall.mapper.KwMerchantMeterMapper.findMeterList">
		</collection>
	</resultMap>

	<select id="getById" parameterType="Long" resultMap="BaseResultMapVo">
		select <include refid="allColumnsVo"/>
		from (
			select merchant_id from kw_merchant_meter
			where `status` = 0
			and `merchant_id` = #{merchantId}
			group by merchant_id
		) temp
		, wx_merchant
		where temp.merchant_id = id
	</select>


	<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>

	</sql>

	<resultMap id="DataVoResultMap" type="com.iformall.domain.vo.KwMerchantMeterDataVo">
		<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"/>
		<result column="merchantId" jdbcType="BIGINT" property="merchantId"/>
		<result column="merchantName" jdbcType="VARCHAR" property="merchantName"/>
		<result column="merchantLinkPhone" jdbcType="VARCHAR" property="merchantLinkPhone"/>
		<result column="businessId" jdbcType="INTEGER" property="businessId"/>
		<result column="businessTitle" jdbcType="VARCHAR" property="businessTitle"/>
	</resultMap>

	<select id="findDataVoList" parameterType="com.iformall.domain.dto.KwMerchantMeterDataDto" resultMap="DataVoResultMap">
		select
		temp.tenant_id,
        <if test=" null != merchantId ">
            mm.merchant_id as merchantId,
            m.name as merchantName,
            m.link_phone as merchantLinkPhone,
        </if>
        <if test=" null != businessId ">
            m.business_id as businessId,
        </if>
		min(temp.recordStartTime) as recordStartTime,
		max(temp.recordEndTime) as recordEndTime,
		sum(temp.totalPower) as totalPower,
		sum(temp.lastTotalPower) as lastTotalPower,
		sum(temp.usedPower) as usedPower
		from
		kw_merchant_meter mm,
		(SELECT
		tenant_id,box_id,meter_id,
		min(last_device_time) as recordStartTime,
		min(last_total_power) as lastTotalPower,
		max(device_time) as recordEndTime,
		max(total_power) as totalPower,
		sum(used_power) as usedPower
		FROM kw_meter_data
		<include refid="dynamicWhereConditionsVo"/>
		group by tenant_id,box_id,meter_id
		) as temp,
		wx_merchant m
		where mm.status = 0
		and mm.meter_id = temp.meter_id
		and mm.merchant_id = m.id
		<if test=" null != merchantStatus ">
			and m`status` = #{merchantStatus}
		</if>
		group by
		temp.tenant_id
		<if test=" null != merchantId ">
			,merchantId
		</if>
		<if test=" null != businessId ">
			,businessId
		</if>

		<if test=" null != sortColumns">order by ${sortColumns}</if>

		<if test=" null != limitCount">limit #{limitCount}</if>
	</select>



	<update id="unbindBatch" parameterType="java.lang.Long">
		update kw_merchant_meter set status = 1
		where status = 0
		and `merchant_id` = #{merchantId}
	</update>


	<update id="unbind" parameterType="java.lang.Long">
		update kw_merchant_meter set status = 1
		where status = 0
		and `meter_id` = #{meterId}
	</update>
	
	
	<resultMap id="MerchantMeterMap" type="com.iformall.domain.vo.MerchantMeterVo">
		<result column="merchant_id" property="merchantId"/>
		<result column="merchant_name" property="merchantName"/>
		<result column="has" property="has"/>
	</resultMap>
	
	<select id="getMerchantMeter" resultMap="MerchantMeterMap">
		select m.id as merchant_id,m.name as merchant_name,ifnull(mm.status,0) has  from wx_merchant m
		left join (select merchant_id,1 as status from kw_merchant_meter where status=0 group by merchant_id) mm on m.id=mm.merchant_id
		where m.status=1 and m.id not in(select merchant_id from wx_merchant_power_bill_config where tenant_id=#{tenantId})
	</select>

</mapper>
