<?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.WdRelationMapper">

	<sql id="allField">
		WD_ID wdId,
		WD_USER_OPENID openid,
		WD_FIRST wdFirst,
		WD_SECOND wdSecond,
		WD_THIRD wdThird,
		WD_FOURTH wdFourth,
		WD_COUNT wdCount,
		WD_CREATE_DATE createDate,
		WD_UPDATE_DATE updateDate,
		WD_STATUS status
	</sql>

	<select id="getWdRelationByOpenid" parameterType="String"
		resultType="WdRelation">
		SELECT
		<include refid="allField"></include>
		FROM TB_IAMBERRY_WD
		WHERE WD_USER_OPENID=#{openid} AND WD_STATUS = 1
	</select>
	<select id="selectWdRelationByOpenid" parameterType="String"
		resultType="WdRelation">
		SELECT
		<include refid="allField"></include>
		FROM TB_IAMBERRY_WD
		WHERE WD_USER_OPENID=#{openid}
	</select>

	<insert id="insertWdRelation" parameterType="WdRelation"
		useGeneratedKeys="true" keyProperty="wdId">
		INSERT 
			INTO TB_IAMBERRY_WD
				(
					WD_USER_OPENID ,
					WD_FIRST ,
					WD_SECOND ,
					WD_THIRD ,
					WD_FOURTH ,
					WD_COUNT ,
					WD_CREATE_DATE,
					WD_UPDATE_DATE,
					WD_STATUS
				)
		VALUES
			(
				#{openid},#{wdFirst},#{wdSecond},#{wdThird},
				#{wdFourth},#{wdCount},NOW(),#{updateDate},1
			)
	</insert>
	
	<!-- 修改微代理层级状态 -->
	<update id="updateStatus" parameterType="String">
		UPDATE 
			TB_IAMBERRY_WD 
		SET 
			WD_STATUS = 2
		WHERE 
			WD_USER_OPENID = #{openid} AND WD_STATUS = 1
	</update>
	
	<!-- 修改微代理层级信息 -->
	<update id="updateWdRelation" parameterType="WdRelation">
		UPDATE 
			TB_IAMBERRY_WD 
		SET 
			WD_FIRST = #{wdFirst},
			WD_SECOND = #{wdSecond},
			WD_THIRD = #{wdThird},
			WD_FOURTH = #{wdFourth},
			WD_COUNT = #{wdCount},
			WD_UPDATE_DATE = #{updateDate},
			WD_STATUS = #{status}
		WHERE 
			WD_USER_OPENID = #{openid}
	</update>
</mapper>