<?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.iamberry.rent.service.machine.mapper.MachineMapper">
	<sql id="rentMachineInfo">
		machine_id machineId,
		machine_order_id  machineOrderId,
		<!-- machine_openid machineOpenid, -->
		machine_device_id machineDeviceId,
		machine_qrcode machineQrcode,
		machine_mac machineMac,
		machine_surplus_water machineSurplusWater,
		machine_have_water machineHaveWater,
		machine_update_time machineUpdateTime,
		machine_stop_date machineStopDate,
		machine_next_command machineNextCommand,
		machine_connect_state machineConnectState,
		machine_init_state machineInitState,
		machine_state machineState,
		machine_init_code  machineInitCode,
		machine_create_time machineCreateTime,
		machine_TDS machineTDS,
		machine_activation_time machineActivationTime
	</sql>
	<sql id="CompeteWater" >
		share_one_openid shareOneOpenId,
		share_machine_id shareMachineId,
		share_nickname shareNickname,
		machine_water_TDS machineWaterTDS,
		machine_amount_water machineAmountWater,
		share_creation_time shareCreationTime,
		share_user_head shareUserHead
	</sql>

	<select id="list" parameterType="MachineInfo" resultType="MachineInfo">
		SELECT
			*
		FROM
			TB_IAMBERRY_RENT_MACHINE_INFO	
		<where>
			<!-- 机器id -->
			<if test="machineId!=null and machineId!=''">
				MACHINE_ID = #{machineId}
			</if>
			<!-- 机器号 -->
			<if test="machineDeviceId!=null and machineDeviceId!=''">
				MACHINE_DEVICE_ID = #{machineDeviceId}
			</if>
			<!-- openid -->
			<if test="machineOpenid!=null and machineOpenid!=''">
				AND MACHINE_OPENID = #{machineOpenid}
			</if>
			<!-- 订单id -->
			<if test="machineOrderId!=null and machineOrderId!=''">
				AND MACHINE_ORDER_ID = #{machineOrderId}
			</if>
			<!-- 机器MAC -->
			<if test="machineMac!=null and machineMac!=''">
				AND MACHINE_MAC = #{machineMac}
			</if>
			<!-- 链接状态 -->
			<if test="machineConnectState!=null and machineConnectState!=''">
				AND MACHINE_CONNECT_STATE = #{machineConnectState}
			</if>
			<!-- 激活状态 -->
			<if test="machineInitState!=null and machineInitState!=''">
				AND MACHINE_INIT_STATE = #{machineInitState}
			</if>
			<!-- 机器状态 -->
			<if test="machineState!=null and machineState!=''">
				AND MACHINE_STATE = #{machineState}
			</if>
		</where>
		<if test="machineDeviceId!=null and machineDeviceId!=''">
			LIMIT 0, 1
		</if>
	</select>
	
	<!-- 添加一台机器 -->
	<insert id="add" parameterType="MachineInfo" 
		keyProperty="machineId" useGeneratedKeys="true">
		 INSERT INTO 
		 	TB_IAMBERRY_RENT_MACHINE_INFO
	 		(
		 		MACHINE_ORDER_ID, MACHINE_OPENID, MACHINE_DEVICE_ID, MACHINE_QRCODE, MACHINE_MAC, MACHINE_SURPLUS_WATER,
		 		MACHINE_UPDATE_TIME, MACHINE_STOP_DATE, MACHINE_NEXT_COMMAND, MACHINE_CONNECT_STATE, MACHINE_INIT_STATE,
		 		MACHINE_STATE, MACHINE_INIT_CODE, MACHINE_CREATE_TIME,MACHINE_HAVE_WATER
			)
		 VALUES 
		  	(
		  		#{machineOrderId}, #{machineOpenid}, #{machineDeviceId}, #{machineQrcode}, #{machineMac}, 0,
		  		NOW(), NOW(), null, 2, 2, 2, null, NOW(),#{machineHaveWater}
		  	)
	</insert>
	
	<!-- 修改机器信息 -->
	<update id="update" parameterType="MachineInfo">
		UPDATE
			TB_IAMBERRY_RENT_MACHINE_INFO 
		<set>
			<if test="machineOrderId != null">
				MACHINE_ORDER_ID = #{machineOrderId}, 
			</if>
			<if test="machineOpenid != null">
				MACHINE_OPENID = #{machineOpenid}, 
			</if>
			<if test="machineSurplusWater != null">
				MACHINE_SURPLUS_WATER = #{machineSurplusWater}, 
			</if>
			<if test="machineStopDate != null">
				MACHINE_STOP_DATE = #{machineStopDate}, 
			</if>
			<if test="machineNextCommand != null">
				MACHINE_NEXT_COMMAND = #{machineNextCommand},
			</if>
			<if test="machineConnectState != null">
				MACHINE_CONNECT_STATE = #{machineConnectState}, 
			</if>
			<if test="machineInitState != null">
				MACHINE_INIT_STATE = #{machineInitState}, 
			</if>
			<if test="machineState != null">
				MACHINE_STATE = #{machineState}, 
			</if>
			<if test="machineInitCode != null and machineInitCode != ''">
				MACHINE_INIT_CODE = #{machineInitCode},
			</if>
			<if test="machineActivationTime != null">
				machine_activation_time = #{machineActivationTime},
			</if>
			<if test="machineHaveWater != null">
				machine_have_water = #{machineHaveWater},
			</if>
			<if test="machineTDS != null">
				machine_TDS = #{machineTDS},
			</if>
			<if test="machineTotalWater != null and machineTotalWater != 0">
				machine_total_water = #{machineTotalWater}
			</if>
		</set>
		<where>
			<if test="machineId != null and machineId != 0">
				MACHINE_ID = #{machineId}
			</if>
			<if test="machineDeviceId != null">
				AND MACHINE_DEVICE_ID = #{machineDeviceId}
			</if>
			<!-- 根据机器的状态修改 -->
			<if test="machineOldState != null">
				AND MACHINE_STATE = #{machineOldState}
			</if>
		</where>
	</update>
	
	<!-- 添加续费记录 -->
	<insert id="addRecharge" parameterType="MachineRecharge">
		 INSERT INTO 
		 	TB_IAMBERRY_RENT_MACHINE_RECHARGE
	 		(
		 		RECHARGE_OPENID, RECHARGE_MACHINE_ID, RECHARGE_AMOUNT, RECHARGE_TIME, RECHARGE_WATER, RECHARGE_COUPONID,
		 		RECHARGE_DURATION, RECHARGE_STATE, RECHARGE_ORDERID, RECHARGE_PAY_TIME, RECHARGE_MESSAGE_STATE, RECHARGE_END_TIME
			)
		 VALUES 
		  	(
		  		#{rechargeOpenId}, #{rechargeMachineId}, #{rechargeAmount}, NOW(), #{rechargeWater}, #{rechargeCouponId},
		  		#{rechargeDuration}, 0, #{rechargeOrderId}, null, 0, #{rechargeEndTime}
		  	)
	</insert>
	
	<!-- 获取续费的信息 -->
	<select id="listRecharge" parameterType="MachineRecharge" resultType="MachineRecharge">
		SELECT
			*
		FROM
			TB_IAMBERRY_RENT_MACHINE_RECHARGE
		<where>
			<if test="rechargeId != null">
				AND RECHARGE_ID = #{rechargeId}
			</if>
			<if test="rechargeOrderId != null">
				AND RECHARGE_ORDERID = #{rechargeOrderId}
			</if>
			<if test="rechargeMachineId != null">
				AND RECHARGE_MACHINE_ID = #{rechargeMachineId}
			</if>
			<if test="rechargeOpenId != null">
				AND RECHARGE_OPENID = #{rechargeOpenId}
			</if>
			<if test="rechargeState != null">
				AND RECHARGE_STATE = #{rechargeState}
			</if>
		</where>
	</select>
	
	<!-- 修改状态 -->
	<update id="updateRecharge" parameterType="MachineRecharge">
		UPDATE
			TB_IAMBERRY_RENT_MACHINE_RECHARGE
		SET
			RECHARGE_STATE = #{rechargeState},
			RECHARGE_MESSAGE_STATE = #{rechargeMessageState},
			RECHARGE_WX_ORDERID = #{rechargeWxOrderId},
			RECHARGE_PAY_TIME = #{rechargePayTime}
		WHERE
			RECHARGE_ID = #{rechargeId}
	</update>
	
	<!-- 后台设备列表页面 -->
	<select id="selectEquipmentList" parameterType="MachineInfo" resultType="MachineInfo">
		SELECT 
			<include refid="rentMachineInfo"/>
			,tb_iamberry_user_userinfo.user_nickname machineOpenid
		FROM
			tb_iamberry_rent_machine_info left join tb_iamberry_user_userinfo 
			on tb_iamberry_rent_machine_info.machine_openid = tb_iamberry_user_userinfo.user_openid
		<where>
			<if test="machineDeviceId != null and machineDeviceId != ''">
				and machine_device_id = #{machineDeviceId}
			</if>
			<if test="machineMac != null and machineMac != ''">
				and machine_mac = #{machineMac}
			</if>
			<if test="machineInitState != null and machineInitState != '' ">
				and machine_init_state = #{machineInitState}
			</if>
			<if test="beginWater != null">
				and machine_surplus_water &gt;  #{beginWater}
			</if>
			<if test="endWater != null">
				and machine_surplus_water &lt; #{endWater}
			</if>
			<if test="beginDate != null">
				and machine_stop_date &gt; #{beginDate}
			</if>
			<if test="endDate != null">
				and machine_stop_date &lt; #{endDate}
			</if>
		</where>
		 order by machine_create_time desc
	    <if test="page!=null and page.pageNo>0 and page.pageSize>0 ">
	     limit ${page.pageNo},${page.pageSize}
	    </if>
	     <if test="page!=null and page.pageNo==0 and page.pageSize>0 ">
	     limit ${page.pageSize}
	    </if>  
	</select>
	<!-- 后台设备列表页面总数 -->
	<select id="selectEquipmentCount" parameterType="MachineInfo" resultType="Integer">
		SELECT 
			count(1)
		FROM
			tb_iamberry_rent_machine_info
		<where>
			<if test="machineDeviceId != null and machineDeviceId != ''">
				and machine_device_id = #{machineDeviceId}
			</if>
			<if test="machineMac != null and machineMac != ''">
				and machine_mac = #{machineMac}
			</if>
			<if test="machineInitState != null and machineInitState != '' ">
				and machine_init_state = #{machineInitState}
			</if>
			<if test="beginWater != null">
				and machine_surplus_water &gt;  #{beginWater}
			</if>
			<if test="endWater != null">
				and machine_surplus_water &lt; #{endWater}
			</if>
			<if test="beginDate != null">
				and machine_stop_date &gt; #{beginDate}
			</if>
			<if test="endDate != null">
				and machine_stop_date &lt; #{endDate}
			</if>
		</where>
	</select>
	
	<!--  -->
	<select id="getRechargeByIdOrOrderId" parameterType="MachineRecharge" resultType="MachineRecharge">
		SELECT * FROM TB_IAMBERRY_RENT_MACHINE_RECHARGE
		<where>
			<if test="rechargeId != null and rechargeId != ''">
				RECHARGE_ID = #{rechargeId}
			</if>
			<if test="rechargeOrderId != null and rechargeOrderId != ''">
				RECHARGE_ORDERID = #{rechargeOrderId}
			</if>
		</where>
	</select>
	<!-- 添加分享机器记录 -->
	<insert id="insertMachineShare" parameterType="MachineShare"
		keyProperty="shareId" useGeneratedKeys="true">
		INSERT INTO TB_IAMBERRY_RENT_MACHINE_SHARE
		(
			SHARE_OPENID,
			SHARE_RE_OPENID,
			SHARE_TIME,
			SHARE_CODE,
			SHARE_BIND_TIME
		)
		VALUES
		(
			#{shareOpenId},#{shareReOpenid},NOW(),#{shareCode},#{shareBindTime}
		)
	</insert>
	
	<!-- 根据shareId查询一个机器分享记录 -->
	<select id="getMachineShareById" parameterType="String" resultType="MachineShare" >
		SELECT * FROM TB_IAMBERRY_RENT_MACHINE_SHARE
		WHERE SHARE_ID = #{shareId}
	</select>
	
	<!-- 根据shareid 修改机器分享记录 -->
	<update id="updateMachineShare" parameterType="MachineShare">
		UPDATE TB_IAMBERRY_RENT_MACHINE_SHARE
		<set>
			SHARE_BIND_TIME=NOW()
			<if test="shareReOpenid != null and shareReOpenid != ''">
				,share_re_openid=#{shareReOpenid}
			</if>
		</set>
		WHERE SHARE_ID = #{shareId}
	</update>
	
	<!-- 根据设备id查询单个设备 -->
	<select id="selectMachineById" parameterType="machineInfo" resultType="MachineInfo">
		SELECT 
			<include refid="rentMachineInfo"/>
		FROM
			tb_iamberry_rent_machine_info
		<where>
			<if test="machineId != null and machineId != ''">
				and machine_id = #{machineId}
			</if>
			<if test="machineDeviceId != null and machineDeviceId != ''">
				and machine_device_id = #{machineDeviceId}
			</if>
		</where>
		
	</select>
	
	<!-- 分页查询所有分享机器记录 -->
	<select id="listMachineShareAdmin" parameterType="MachineShare" resultType="MachineShare">
		SELECT 
			S.*,
			U1.USER_NICKNAME SHAREUSERNICKNAME,
			U2.USER_NICKNAME SHAREREUSERNICKNAME
		FROM TB_IAMBERRY_RENT_MACHINE_SHARE S
		LEFT JOIN TB_IAMBERRY_USER_USERINFO U1 ON S.SHARE_OPENID=U1.USER_OPENID
		LEFT JOIN TB_IAMBERRY_USER_USERINFO U2 ON S.SHARE_RE_OPENID=U2.USER_OPENID
		ORDER BY SHARE_BIND_TIME DESC,SHARE_TIME DESC
		<if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
	     LIMIT ${page.recordBegin},${page.pageSize}
	    </if>
	     <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
	     LIMIT ${page.pageSize} 
	    </if>
	</select>
	
	<!-- 查询所有分享机器记录总数 -->
	<select id="countMachineShare"  parameterType="MachineShare" resultType="Integer">
		SELECT COUNT(SHARE_ID) FROM TB_IAMBERRY_RENT_MACHINE_SHARE
	</select>
	
	<!-- 获取还需要执行命令的机器 -->
	<select id="listNextCommandMachine" resultType="MachineInfo">
		SELECT
			*
		FROM
			TB_IAMBERRY_RENT_MACHINE_INFO
		WHERE
			!(MACHINE_NEXT_COMMAND IS NULL) AND MACHINE_NEXT_COMMAND != ""
	</select>
	
	<!-- 根据条件查询滤芯更换记录 -->
	<select id="listSelectChangeLogs" parameterType="RentChangeLogs" resultType="RentChangeLogs">
		SELECT 
			* 
		FROM 
			TB_IAMBERRY_RENT_CHANGE_LOGS 
		<where>
			<if test="logsOrderId != null and logsOrderId != ''">
				LOGS_ORDER_ID = #{logsOrderId} 
			</if>
			<if test="logsOpenid != null and logsOpenid != ''">
				AND LOGS_OPENID = #{logsOpenid} 
			</if>
			<if test="logsId != null and logsId != ''">
				AND LOGS_ID = #{logsId} 
			</if>
			<if test="logsStatus != null and logsStatus != ''">
				AND LOGS_STATUS = #{logsStatus} 
			</if>
		</where> 
	</select>
	
	<!-- 批量添加滤芯更换记录 -->
	<insert id="saveChangeLogs" parameterType="RentChangeLogs">
		INSERT INTO TB_IAMBERRY_RENT_CHANGE_LOGS
		(
			LOGS_MACHINE_DEVICE_ID,LOGS_ORDER_ID,LOGS_OPENID,LOGS_RENT_TYPE,LOGS_FILTER_TYPE,
			LOGS_STATUS,LOGS_CREATE_TIME,LOGS_ADDRESS_INFO,LOGS_ADDRESS_TEL,LOGS_ADDRESS_NAME 
		) 
		VALUES 
		<foreach collection="list" item="item" separator=",">
			(
				#{item.logsMachineDeviceId},#{item.logsOrderId},#{item.logsOpenid},#{item.logsRentType},#{item.logsFilterType},
				#{item.logsStatus},NOW(),#{item.logsAddressInfo},#{item.logsAddressTel},#{item.logsAddressName} 
			)
		</foreach>
	</insert>

	<!-- 单个添加更换滤芯记录 -->
	<insert id="insertFilterLogs" parameterType="RentChangeLogs" keyProperty="logsId" useGeneratedKeys="true">
		INSERT INTO TB_IAMBERRY_RENT_CHANGE_LOGS
		(
			LOGS_MACHINE_DEVICE_ID,LOGS_ORDER_ID,LOGS_OPENID,LOGS_RENT_TYPE,LOGS_FILTER_TYPE,
			LOGS_STATUS,LOGS_CREATE_TIME,LOGS_ADDRESS_INFO,LOGS_ADDRESS_TEL,LOGS_ADDRESS_NAME
		)
		VALUES
		(
			#{logsMachineDeviceId},#{logsOrderId},#{logsOpenid},#{logsRentType},#{logsFilterType},
			#{logsStatus},NOW(),#{logsAddressInfo},#{logsAddressTel},#{logsAddressName}
		)
	</insert>
	
	<!-- 查询更换滤芯集合 -->
	<select id="listChangeFilterLogs" parameterType="RentChangeLogs" resultType="RentChangeLogs">
		SELECT
			l.*,u.USER_NICKNAME rentUserNickName
		FROM 
			TB_IAMBERRY_RENT_CHANGE_LOGS l LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON l.LOGS_OPENID = u.USER_OPENID 
		<where>
			<if test="logsStatus != null and logsStatus != -1">
				LOGS_STATUS = #{logsStatus} 
			</if>
			<if test="logsAddressInfo != null">
				AND LOGS_ADDRESS_INFO LIKE CONCAT('%',#{logsAddressInfo},'%')
			</if>
			<if test="logsAddressName != null">
				AND LOGS_ADDRESS_NAME LIKE CONCAT('%',#{logsAddressName},'%')
			</if>
			<if test="logsAddressTel != null">
				AND LOGS_ADDRESS_TEL LIKE CONCAT('%',#{logsAddressTel},'%')
			</if>
			<if test="logsOrderId != null">
				AND LOGS_ORDER_ID LIKE CONCAT('%',#{logsOrderId},'%')
			</if>
			<if test="logsId != null">
				AND LOGS_ID = #{logsId}
			</if>
			<if test="logsMachineDeviceId != null">
				AND LOGS_MACHINE_DEVICE_ID = #{logsMachineDeviceId}
			</if>
			<if test="logsOpenid != null">
				AND LOGS_OPENID = #{logsOpenid}
			</if>
		</where>
		ORDER BY LOGS_CREATE_TIME ASC
		<if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
	     LIMIT ${page.recordBegin},${page.pageSize} 
	    </if>
	     <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
	     LIMIT ${page.pageSize} 
	    </if>
	</select>
	
	<!-- 根据条件获取更换滤芯的数量 -->
	<select id="getChangeFilterCount" parameterType="RentChangeLogs" resultType="Integer">
		SELECT
			COUNT(LOGS_ID) 
		FROM 
			TB_IAMBERRY_RENT_CHANGE_LOGS l LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON l.LOGS_OPENID = u.USER_OPENID
		<where>
			<if test="logsStatus != null and logsStatus != -1">
				LOGS_STATUS = #{logsStatus}
			</if>
			<if test="logsAddressInfo != null">
				AND LOGS_ADDRESS_INFO LIKE CONCAT('%',#{logsAddressInfo},'%')
			</if>
			<if test="logsAddressName != null">
				AND LOGS_ADDRESS_NAME LIKE CONCAT('%',#{logsAddressName},'%')
			</if>
			<if test="logsAddressTel != null">
				AND LOGS_ADDRESS_TEL LIKE CONCAT('%',#{logsAddressTel},'%')
			</if>
			<if test="logsOrderId != null">
				AND LOGS_ORDER_ID LIKE CONCAT('%',#{logsOrderId},'%')
			</if>
		</where>
	</select>

	<insert id="addCompeteWater" parameterType="CompeteWaterInfo"
			keyProperty="shareId" useGeneratedKeys="true">
		INSERT INTO tb_iamberry_compete_water
		(
			share_one_openid,
			share_machine_id,
			share_nickname,
			machine_water_TDS,
			machine_amount_water,
			share_creation_time,
			share_user_head
		)
		VALUES
		(
			#{shareOneOpenId},#{shareMachineId},#{shareNickname},#{machineWaterTDS},#{machineAmountWater},NOW(),#{shareUserHead}
		)
	</insert>

	<select id="selectCompeteWater" parameterType="String" resultType="CompeteWaterInfo">
		SELECT <include refid="CompeteWater"/> FROM  tb_iamberry_compete_water
		WHERE share_id = #{shareId}
	</select>

	<!-- 修改滤芯更换记录表 -->
	<update id="updateChangeLogsById" parameterType="RentChangeLogs">
		UPDATE
			TB_IAMBERRY_RENT_CHANGE_LOGS
		<set>
			<if test="logsStatus != null">
				LOGS_STATUS = #{logsStatus},
			</if>
			<if test="logsPostNum != null and logsPostNum != ''">
				LOGS_POST_NUM = #{logsPostNum},
			</if>
			<if test="logsPostFirm != null and logsPostFirm != ''">
				LOGS_POST_FIRM = #{logsPostFirm},
			</if>
			<if test="logsSendTime != null and logsSendTime != ''">
				LOGS_SEND_TIME = #{logsSendTime},
			</if>
			<if test="logsAccomplishTime != null and logsAccomplishTime != ''">
				LOGS_ACCOMPLISH_TIME = #{logsAccomplishTime}
			</if>
		</set>
		<where>
			LOGS_ID = #{logsId}
			<if test="logsOldStatus != null and logsOldStatus != ''">
				AND LOGS_STATUS = #{logsOldStatus}
			</if>
		</where>
	</update>
</mapper>
