<?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.KwMeterMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.KwMeter">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
		<result column="address" jdbcType="BIGINT" property="address"/>
		<result column="brand" jdbcType="VARCHAR" property="brand"/>
		<result column="place" jdbcType="VARCHAR" property="place"/>
		<result column="model" jdbcType="VARCHAR" property="model"/>
		<result column="last_online_time" jdbcType="TIMESTAMP" property="lastOnlineTime"/>
		<result column="total_power" jdbcType="INTEGER" property="totalPower"/>
		<result column="online_status" jdbcType="INTEGER" property="onlineStatus"/>
		<result column="config" jdbcType="VARCHAR" property="config"/>
		<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
		<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
		<result column="box_id" jdbcType="BIGINT" property="boxId"/>
		<result column="status" jdbcType="INTEGER" property="status"/>
		<result column="bind_status" jdbcType="INTEGER" property="bindStatus"/>

		<result column="merchant_name" jdbcType="VARCHAR" property="merchantName"/>
		<result column="merchant_id" jdbcType="BIGINT" property="merchantId"/>
	</resultMap>
	
	<sql id="allColumns">
        `kw_meter`.`id`,`kw_meter`.`tenant_id`,`kw_meter`.`address`,`kw_meter`.`brand`,`kw_meter`.`model`,
        `kw_meter`.`place`,`kw_meter`.`last_online_time`,`kw_meter`.`total_power`,`kw_meter`.`online_status`,
        `kw_meter`.`config`,`kw_meter`.`create_time`,`kw_meter`.`update_time`,`kw_meter`.`box_id`,`kw_meter`.`status`,`kw_meter`.`bind_status`
	</sql>
	
	<sql id="dynamicWhereConditions">
		where 1 = 1
		
		<if test=" null != id ">
			and `kw_meter`.`id` = #{id}
		</if>
		
		<if test=" null != tenantId ">
			and `kw_meter`.`tenant_id` = #{tenantId}
		</if>
		
		<if test=" null != address">
			and `kw_meter`.`address` like concat('%', #{address},'%')
		</if>
		
		<if test=" null != brand and '' != brand ">
			and `kw_meter`.`brand` like concat('%', #{brand},'%')
		</if>
		
		<if test=" null != place and '' != place ">
			and `kw_meter`.`place` like concat('%', #{place},'%')
		</if>
		
		<if test=" null != model ">
			and `kw_meter`.`model` = #{model}
		</if>
		
		<if test=" null != onlineStatus ">
			and `kw_meter`.`online_status` = #{onlineStatus}
		</if>

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

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

		<if test=" null != bindStatus ">
			and `kw_meter`.`bind_status` = #{bindStatus}
		</if>

		<if test=" null != merchantMeterStatus and 1 == merchantMeterStatus">
			and `kw_meter`.`id` not in (select meter_id from kw_merchant_meter where status = 0)
		</if>
		<if test=" null != merchantMeterStatus and 0 == merchantMeterStatus">
			and `kw_meter`.`id` in (select meter_id from kw_merchant_meter where status = 0)
		</if>

		<if test=" null != ids ">
			and `kw_meter`.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.KwMeter" resultMap="BaseResultMap">
		select
		<include refid="allColumns"/>
		from kw_meter
		<include refid="dynamicWhereConditions"/>
	</select>

	<select id="findVoList" parameterType="com.iformall.domain.po.KwMeter" resultMap="BaseResultMap">
		select
		<include refid="allColumns"/>
		,mm.merchant_id as merchant_id, m.name as merchant_name
		from kw_meter
		left join kw_merchant_meter mm on kw_meter.id = mm.meter_id and mm.status = 0
		left join wx_merchant m on m.id = mm.merchant_id
		<include refid="dynamicWhereConditions"/>
	</select>


	<update id="onlineBatch" parameterType="java.util.List" >
		update kw_meter
		set
		total_power=
		case
			<foreach collection="list" item="item" index="index">
				<if test="item.totalPower!=null">
					when id = #{item.id} then #{item.totalPower}
				</if>
			</foreach>
		end,
		online_status = 1,
		last_online_time = now()
		where id in
		<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
			#{item.id}
		</foreach>
	</update>

	<update id="offlineBatch" parameterType="com.iformall.domain.po.KwMeter" >
		update kw_meter
		set
		online_status = 0
		where id in
		<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
			  #{idItem}
		</foreach>
		and online_status = 1
	</update>

	<update id="unbindBatch" parameterType="java.lang.Long">
		update kw_meter set bind_status = 1, box_id = 0
		where bind_status = 0
		and `box_id` = #{boxId}
	</update>


	<update id="unbind" parameterType="java.lang.Long">
		update kw_meter set bind_status = 1, box_id = 0
		where bind_status = 0
		and `id` = #{meterId}
	</update>

	<update id="bindBatch" parameterType="map">
		update kw_meter set bind_status = 0, box_id = #{boxId}
		where id in
		<foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
			#{idItem}
		</foreach>
	</update>

	<update id="bind" parameterType="map">
		update kw_meter set bind_status = 0, box_id = #{boxId}
		where `id` = #{id}
	</update>

	<update id="offlineScan" parameterType="java.util.Date">
		update kw_meter set `online_status` = 0
		where `status` = 0
		and   `online_status` = 1
		and   `last_online_time` &lt; #{expiredTime}
	</update>

</mapper>
