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

	<!-- 根据用户Openid获取用户信息 -->
	<select id="selectMemberInfoByOpenId" parameterType="String" resultType="Member">
		SELECT
			USER_ID userId,
			USER_OPENID userOpenid, 
			USER_IS_FLAG userIsFlag, 
			USER_DEALERS userDealers,
			USER_STATUS userStatus,
			USER_RES_TYPE userResType,
			USER_TEMPLATE_ID templateId,
			USER_IS_BOUGHT userIsBought
		FROM
			TB_IAMBERRY_USER_USERINFO
		WHERE
			USER_OPENID = #{openid}
	</select>

	<!-- 根据用户Openid,更新用户微信信息 -->
	<update id="updateWechatMemberInfoByByOpenid" parameterType="Member">
		UPDATE 
			TB_IAMBERRY_USER_USERINFO 
		SET  
			USER_HEAD = #{userHead},
			USER_SEX = #{userSex},
			USER_NICKNAME = #{userNickname},
			USER_ADDR = #{userAddr},
			USER_STATUS = #{userStatus}, 
			USER_SUB_DATE = NOW()
		WHERE 
			USER_OPENID  = #{userOpenid}
	</update>
	
	<!-- 插入用户信息 -->
	<insert id="insertMemberInfo" parameterType="Member" useGeneratedKeys="true" keyProperty="userId">
		 INSERT INTO 
		 	TB_IAMBERRY_USER_USERINFO
		 		(
			 		USER_OPENID, USER_HEAD, USER_SEX, USER_NICKNAME, USER_ADDR,
					USER_STATUS, USER_SUB_DATE, USER_CREATE_DATE, USER_IS_FLAG,USER_IDENTITY,user_is_bought
				)
		 VALUES 
		  	( 
		  	  #{userOpenid},
			  #{userHead},  
			  #{userSex}, 
			  #{userNickname},
			  #{userAddr},
			  #{userStatus},
			  NOW(),
			  NOW(),
			  1,
			  2,
			  1
		  	)
	</insert>
	
	<!-- 根据用户Openid,更新用户来源和来源类型  -->
	<update id="updateUserResAndTypeByOpenid" parameterType="Member">
		UPDATE 
			TB_IAMBERRY_USER_USERINFO 
		SET  
			<if test="userResType != null">
				USER_RES_TYPE = #{userResType},
			</if>
			<if test="templateId != null and templateId != ''">
				USER_TEMPLATE_ID = #{templateId},
			</if>
			<if test="userIsFlag != null and userIsFlag != ''">
				USER_IS_FLAG = #{userIsFlag},
			</if>
			USER_DEALERS = #{userDealers}
		WHERE 
			USER_OPENID = #{userOpenid} AND USER_IS_FLAG = 1
	</update>
	
	<!-- 根据Openid，更新用户状态为取消关注状态 -->
	<update id="updateMemberStatusByOpenId" parameterType="String">
		UPDATE 
			TB_IAMBERRY_USER_USERINFO 
		SET  
			USER_STATUS = 0, 
			<!-- USER_QRCODE = null, -->
			USER_SUB_DATE = NOW()
		WHERE 
			USER_OPENID  = #{userOpenid}
	</update>
	
	<!-- 根据用户Openid获取用户二维码(不一定有)-->
	<select id="selectMemberQrcodeUrlByOpenid" parameterType="String" resultType="Member">
		SELECT
			USER_QRCODE userQrcode,USER_OPENID userOpenid, USER_ID userId, USER_IS_FLAG userIsFlag
		FROM
			TB_IAMBERRY_USER_USERINFO
		WHERE
			USER_OPENID = #{openid}
	</select>
	
	<!-- 根据Openid，更新用户二维码 -->
	<update id="updateMemberQrcodeByOpenid" parameterType="Member">
		UPDATE 
			TB_IAMBERRY_USER_USERINFO 
		SET  
			USER_QRCODE = #{userQrcode}
		WHERE 
			USER_OPENID  = #{userOpenid}
	</update>
	
	<!-- 根据用户Openid获取用户二可用积分-->
	<select id="selectUserIntegralByOpenid" parameterType="String" resultType="Integer">
		SELECT
			USER_SURPLUS_INTEGRAL
		FROM
			TB_IAMBERRY_USER_USERINFO
		WHERE
			USER_OPENID = #{openid}
	</select>
	
	<!-- 根据placeID，获取当前激活用户的信息 -->
	<select id="selectUserInfoByPlaceId" parameterType="Integer" resultType="Member">
		SELECT
			USER_HEAD userHead, USER_OPENID userOpenid, USER_NICKNAME userNickname
		FROM
			TB_IAMBERRY_USER_USERINFO
		WHERE
			USER_OPENID = (
				SELECT
					PLACE_OPENID
				FROM
					TB_IAMBERRY_PLACE_INFO
				WHERE 
					PLACE_ID = #{placeId}
			)
	</select>
	
	<!-- 根据Openid，更新绑定状态 -->
	<update id="updateMemberIsFlagByOpenid" parameterType="Member">
		UPDATE 
			TB_IAMBERRY_USER_USERINFO 
		SET  
			user_is_flag = #{userIsFlag}
		WHERE 
			USER_OPENID  = #{userOpenid}
	</update>
</mapper>