<?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.wechat.service.mapper.UserMachineInfoMapper">
	
	<!-- 添加一个兑换记录 -->
	<insert id="add" parameterType="UserMachineInfo" useGeneratedKeys="true" keyProperty="userMachineId">
		 INSERT INTO 
		 	TB_IAMBERRY_USER_MACHINE
		 		(
			 		USER_MACHINE_OPEN_ID, USER_MACHINE_TEL, USER_MACHINE_CARD_ID, USER_MACHINE_STATUS, USER_MACHINE_PRODUCT_ID, 
			 		USER_MACHINE_PRODUCT_NAME, USER_MACHINE_COLOR_ID, USER_MACHINE_COLOR_NAME, USER_MACHINE_69CODE, 
			 		USER_MACHINE_ADDRESS_INFO, USER_MACHINE_ADDRESS_NAME, USER_MACHINE_ADDRESS_TEL, USER_MACHINE_CREATE_DATE
				)
		 VALUES 
		  	( 
			  #{userMachineOpenId}, #{userMachineTel}, #{userMachineCardId}, #{userMachineStatus}, #{userMachineProductId}, 
			  #{userMachineProductName}, #{userMachineColorId},  #{userMachineColorName}, #{userMachine69code}, 
			  #{userMachineAddressInfo}, #{userMachineAddressName}, #{userMachineAddressTel}, NOW()
		  	)
	</insert>
	
	<!-- 根据用户openid获取对应的兑换信息 -->
	<select id="list" parameterType="String" resultType="UserMachineInfo">
		SELECT 
			m.*, i.CARD_PASSWORD cardPwd
		FROM
			TB_IAMBERRY_USER_MACHINE m
		LEFT JOIN
			TB_IAMBERRY_GIFT_CARD_INFO i
		ON
			m.USER_MACHINE_CARD_ID = i.CARD_ID
		WHERE 
			USER_MACHINE_OPEN_ID = #{openid} 
		ORDER BY m.USER_MACHINE_CREATE_DATE DESC 
	</select>
	
	<!-- 查询一页的待发货兑换记录 -->
	<select id="page" parameterType="java.util.Map" resultType="UserMachineInfo">
		SELECT
			*,AGENT_NICKNAME userNickName 
		FROM
			TB_IAMBERRY_USER_MACHINE 
		LEFT JOIN TB_IAMBERRY_AGENT_INFO ON USER_MACHINE_OPEN_ID = AGENT_OPEN_ID 
		WHERE
			USER_MACHINE_STATUS = 1 
		ORDER BY
			USER_MACHINE_CREATE_DATE DESC
		<!-- 分页基本语句 -->
		LIMIT #{min}, #{max}
	</select>
	
	<!-- 获取总数量 -->
	<select id="count" resultType="int">
		SELECT
			COUNT(USER_MACHINE_ID)
		FROM
			TB_IAMBERRY_USER_MACHINE
		WHERE
			USER_MACHINE_STATUS = 1
	</select>
	
	<!-- 修改兑换状态 -->
	<update id="update" parameterType="UserMachineInfo">
		UPDATE 
			TB_IAMBERRY_USER_MACHINE 
		<set>
			<if test="userMachinePostName != null">
				USER_MACHINE_POST_NAME = #{userMachinePostName}, 
			</if>
			<if test="userMachineStatus != null">
				USER_MACHINE_STATUS = #{userMachineStatus}, 
			</if>
			<if test="userMachinePostNum != null">
				USER_MACHINE_POST_NUM = #{userMachinePostNum}
			</if>
		</set>
		<where>
			USER_MACHINE_ID = #{userMachineId}
		</where>
	</update>
</mapper>
