<?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">
<!-- 命名，每一个映射对象不一样 namespace:必须与对应的接口全类名一致 -->
<mapper namespace="com.iamberry.wechat.service.mapper.LeaseMachineMapper">

	<sql id="machineInfo">
		MACHINE_ID machineId,
		MACHINE_ORDER_ID machineOrderId,
		MACHINE_OPENID machineOpenid,
		MACHINE_DEVICE_ID machineDeviceId,
		MACHINE_SURPLUS_WATER machineSurplusWater,
		MACHINE_UPDATE_TIME machineUpdateTime,
		MACHINE_NEXT_COMMAND machineNextCommand,
		MACHINE_CONNECT_STATE machineConnectState,
		MACHINE_INIT_STATE machineInitState,
		MACHINE_STATE machineState,
		MACHINE_INIT_CODE machineInitCode,
		MACHINE_CREATE_TIME machineCreateTime 
	</sql>

	<sql id="rechargeInfo">
		RECHARGE_ID rechargeId,
		RECHARGE_OPENID rechargeOpenId,
		RECHARGE_MACHINE_ID rechargeMachineId,
		RECHARGE_MAOUNT rechargeMaount,
		RECHARGE_TIME rechargeTime,
		RECHARGE_WATER rechargeWater,
		RECHARGE_STATE rechargeState,
		RECHARGE_ORDERID rechargeOrderId,
		RECHARGE_PAY_TIME rechargePayTime
	</sql>

	<sql id="shareInfo">
		SHARE_ID shareId,
		SHARE_USER_OPENID shareUserOpenid,
		CREATE_TIME createTime,
		SHARE_CONTENT shareContent,
		SHATE_TYPE shateType
	</sql>
	<!-- 新增机器信息 -->
	<insert id="insertMachineInfo" parameterType="Machine" useGeneratedKeys="true" keyProperty="machineId">
		INSERT INTO TB_IAMBERRY_MACHINE_INFO
		(
			MACHINE_ORDER_ID,MACHINE_OPENID,MACHINE_DEVICE_ID,MACHINE_SURPLUS_WATER,
			MACHINE_UPDATE_TIME,MACHINE_NEXT_COMMAND,MACHINE_CONNECT_STATE,
			MACHINE_INIT_STATE,MACHINE_STATE,MACHINE_INIT_CODE,MACHINE_CREATE_TIME
		) 
		VALUES 
		(
			#{machineOrderId},#{machineOpenid},#{machineDeviceId},#{machineSurplusWater},
			NOW(),#{machineNextCommand},#{machineConnectState},
			#{machineInitState},#{machineState},#{machineInitCode},NOW()
		)
	</insert>
	
	<!-- 新增充值记录信息 -->
	<insert id="insertMachineRecharge" parameterType="com.iamberry.wechat.core.entity.machine.MachineRechargeDto" 
		useGeneratedKeys="true" keyProperty="rechargeId">
		INSERT INTO TB_IAMBERRY_MACHINE_RECHARGE
		(
			RECHARGE_OPENID,RECHARGE_MACHINE_ID,RECHARGE_ORDERID,RECHARGE_MAOUNT,
			RECHARGE_TIME,RECHARGE_WATER,RECHARGE_STATE,RECHARGE_PAY_TIME
		) 
		VALUES 
		(
			#{rechargeOpenId},#{rechargeMachineId},#{rechargeOrderId},#{rechargeMaount},
			NOW(),#{rechargeWater},#{rechargeState},#{rechargePayTime}
		)
	</insert>
	
	<!-- 根据openId查询机器列表信息 -->
	<select id="selectMachineList" parameterType="com.iamberry.wechat.core.entity.machine.MachineDto" 
		resultType="com.iamberry.wechat.core.entity.machine.MachineDto">
		SELECT 
			<include refid="machineInfo" />
		FROM 
			TB_IAMBERRY_MACHINE_INFO 
		WHERE 
			MACHINE_OPENID = #{machineOpenid} 
		LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<!-- 按机器id查询机器充值列表 -->
	<select id="selectMachineRechargeList" parameterType="com.iamberry.wechat.core.entity.machine.MachineRechargeDto" 
		resultType="com.iamberry.wechat.core.entity.machine.MachineRechargeDto">
		SELECT 
			<include refid="rechargeInfo" /> 
		FROM 
			TB_IAMBERRY_MACHINE_RECHARGE 
		WHERE 
			RECHARGE_MACHINE_ID = #{rechargeMachineId} 
		LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<!-- 新增分享记录 -->
	<insert id="insertShareRecord" parameterType="com.iamberry.wechat.core.entity.machine.MachineShareRecord">
		INSERT INTO TB_IAMBERRY_MACHINE_SHARE
		(
			SHARE_USER_OPENID,CREATE_TIME,SHARE_CONTENT,SHATE_TYPE
		) 
		VALUES 
		(
			#{shareUserOpenid},NOW(),#{shareContent},#{shateType}
		)
	</insert>
</mapper>