<?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.KwBoxMapper">
	<resultMap id="BaseResultMap" type="com.iformall.domain.po.KwBox">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
		<result column="imei" jdbcType="BIGINT" property="imei"/>
		<result column="iccid" jdbcType="VARCHAR" property="iccid"/>
		<result column="place" jdbcType="VARCHAR" property="place"/>
		<result column="fw_version" jdbcType="VARCHAR" property="fwVersion"/>
		<result column="hd_version" jdbcType="VARCHAR" property="hdVersion"/>
		<result column="register_status" jdbcType="INTEGER" property="registerStatus"/>
		<result column="online_status" jdbcType="INTEGER" property="onlineStatus"/>
		<result column="register_time" jdbcType="TIMESTAMP" property="registerTime"/>
		<result column="last_online_time" jdbcType="TIMESTAMP" property="lastOnlineTime"/>
		<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="com_key" jdbcType="VARCHAR" property="comKey"/>
		<result column="status" jdbcType="INTEGER" property="status"/>
		<result column="max_tag_num" jdbcType="INTEGER" property="maxTagNum"/>
		<result column="meterCount" jdbcType="INTEGER" property="meterCount"/>
	</resultMap>
	
	<sql id="allColumns">
        `id`,`tenant_id`,`imei`,`iccid`,`place`,`fw_version`,`hd_version`,`register_status`,`online_status`,`register_time`,
        `last_online_time`,`config`,`create_time`,`update_time`,`com_key`,`status`,`max_tag_num`
	</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 != imei ">
			and `imei` like concat('%', #{imei},'%')
		</if>
		
		<if test=" null != iccid ">
			and `iccid` = #{iccid}
		</if>
		
		<if test=" null != place and '' != place ">
			and `place` like concat('%', #{place},'%')
		</if>
		
		<if test=" null != registerStatus ">
			and `register_status` = #{registerStatus}
		</if>
		
		<if test=" null != onlineStatus ">
			and `online_status` = #{onlineStatus}
		</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.KwBox" resultMap="BaseResultMap">
		select
		<include refid="allColumns"/>
		,IFNULL(mc,0) as meterCount
		from kw_box left join (
			select box_id, count(box_id) as mc from kw_meter
			where `status` = 0
			and `bind_status` = 0
			group by box_id
		) meter on meter.box_id = id
		<include refid="dynamicWhereConditions"/>
	</select>


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

</mapper>
