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


 	<sql id="allField">
		user_id userId,
		user_openid userOpenid,
		user_head userHead,
		user_nickname userNickName,
		user_addr userAddr,
		
		user_sex userSex,
		user_integral userIntegral,
		user_account_num userAccountNum,
		user_create_date userCreateDate,
		user_birth_date userBirthDate,
		
		user_identity userIdentity,
		user_is_flag userIsFlag,
		user_pwd userPwd,
		user_tel userTel,
		user_name userName,
		
		user_qrcode userQrcode,
		user_res_type userResType,
		user_status userStatus,
		user_surplus_integral userSurplusIntegral,
		user_sub_date userSubDate,
		user_stay_integral userStayIntegral,
		user_use_integral userUseIntegral,
		user_income userIncome,
		user_template_id templateId,
		user_dealers userDealers,
		user_is_bought userIsBought,

		user_email userEmail,
		user_qrcode_type userQrcodeType,
		user_age_stage userAgeStage
	</sql>
	<!-- 获取个人信息-->
	<select id="getMyMemberInfo"  parameterType="String" resultType="Member" >
		SELECT
			user_head,user_nickname,user_sex,user_name,user_birth_date,user_tel,user_age_stage,user_identity,
			user_integral,user_stay_integral,user_use_integral,user_surplus_integral
		FROM TB_IAMBERRY_USER_USERINFO
		WHERE user_openid = #{userOpenid}
	</select>
	<!--修改个人信息-->
	<update id="updateMyMember" parameterType="Member" >
		UPDATE TB_IAMBERRY_USER_USERINFO
		<set>
			<if test="userName!=null">
				user_name=#{userName},
			</if>
			<if test="userTel!=null">
				user_tel=#{userTel},
			</if>
			<if test="userSex!=null">
				user_sex=#{userSex},
			</if>
			<if test="userAgeStage!=null">
				user_age_stage=#{userAgeStage}
			</if>
		</set>
		WHERE user_openid = #{userOpenid}
	</update>
	<!-- 匹配查询会员信息 -->
	<select id="getMemberByFuzzy" parameterType="Member" resultType="Member">
		select 
		<include refid="allField"/>
		from TB_IAMBERRY_USER_USERINFO 
		where 1 = 1 
		<if test="user_create_date!=null">
			and user_create_date between #{userCreateDate} and #{userCreateDate} 
		</if>
		<if test="user_name!=null">
			and user_name like '%${value}%' 
		</if>
		ORDER BY USER_ID DESC 
	</select>
	<!-- 	根据id查询会员信息 -->
	<select id="getMemberByUserId" resultType="Member"
		parameterType="int">
		SELECT
		<include refid="allField"/>
		FROM TB_IAMBERRY_USER_USERINFO
		WHERE USER_ID = #{userId}
	</select>
	
	<!-- 	根据openid查询会员信息 -->
	<select id="getMemberByUserOpenId" resultType="Member"
		parameterType="String">
		SELECT
		<include refid="allField"/>
		FROM TB_IAMBERRY_USER_USERINFO
		WHERE user_openid = #{userOpenid}
	</select>
	<!-- 查询会员数量 -->
	<select id="getCount" resultType="int">
		SELECT 
			COUNT(USER_ID) MEMBERNUM
		FROM 
			TB_IAMBERRY_USER_USERINFO
	</select>
	<!-- 分页显示会员信息 -->
	<select id="getMemberByPage" parameterType="Page"
		resultType="Member">
		SELECT
			<include refid="allField" />
		FROM 
			TB_IAMBERRY_USER_USERINFO where 1=1
			<if test="params.userInput != null and params.userInput != ''">
				 AND user_nickname LIKE CONCAT('%',#{params.userInput},'%')
				OR user_tel LIKE  CONCAT('%',#{params.userInput},'%')
			</if>
			<if test="params.beginDate != null and params.beginDate != ''">
				<![CDATA[AND DATE(user_sub_date) >= #{params.beginDate}]]>
			</if>
			<if test="params.endDate != null and params.endDate != ''">
				<![CDATA[AND DATE(user_sub_date) <= #{params.endDate}]]>
			</if>
		ORDER BY USER_ID DESC 
		<if test="pageNo>=0 and pageSize>0 ">
		LIMIT 
			${pageNo},${pageSize}
		</if>	
	</select>
	<!-- 删除一条记录 -->
	<delete id="deleteMemById" parameterType="int">
		DELETE FROM
			TB_IAMBERRY_USER_USERINFO
		WHERE 
			USER_ID = #{userId}
	</delete>

	<!-- 修改 -->
	<update id="updateMemberByUserId" parameterType="Member">
		update tb_iamberry_user_userinfo
		<set>
			<if test="userOpenid!=null">
				user_openid = #{userOpenid},
			</if>
			<if test="userHead!=null">
				user_head = #{userHead},
			</if>
			<if test="userNickname!=null">
				user_nickname = #{userNickname},
			</if>
			<if test="userAddr!=null">
				user_addr = #{userAddr},
			</if>
			<if test="userSex!=null">
				user_sex = #{userSex},
			</if>
			<if test="userIntegral!=null">
				user_integral = #{userIntegral},
			</if>
			<if test="userAccountNum!=null">
				user_account_num = #{userAccountNum},
			</if>
			<if test="userPwd!=null">
				user_pwd = #{userPwd},
			</if>
			<if test="userTel!=null">
				user_tel = #{userTel},
			</if>
			<if test="userName!=null">
				user_name = #{userName},
			</if>
			<if test="userBirthDate!=null">
				user_birth_date = #{userBirthDate},
			</if>
			<if test="userSurplusIntegral!=null">
				user_surplus_integral = #{userSurplusIntegral},
			</if>
			<if test="userUseIntegral!=null">
				user_use_integral = #{userUseIntegral},
			</if>
			<if test="userResType!=null">
				user_res_type = #{userResType},
			</if>
			<if test="userStatus!=null">
				user_status = #{userStatus},
			</if>
			<if test="userSubDate!=null">
				user_sub_date = #{userSubDate},
			</if>
			<if test="userCreateDate!=null">
				user_create_date = #{userCreateDate},
			</if>
			<if test="userIdentity!=null">
				user_identity = #{userIdentity},
			</if>
			<if test="userQrcode!=null">
				user_qrcode = #{userQrcode},
			</if>
			<if test="userIsFlag!=null">
				user_is_flag = #{userIsFlag},
			</if>
			<if test="userEmail!=null">
				user_email = #{userEmail},
			</if>
			<if test="userQrcodeType != null">
				user_qrcode_type = #{userQrcodeType}
			</if>
			<if test="userStayIntegral != null">
				user_stay_integral = #{userStayIntegral}
			</if>
			<if test="userAgeStage != null">
				user_age_stage = #{userAgeStage}
			</if>
		</set>
		<where>
			<if test="userId != null">
				user_id = #{userId}
			</if>
			<if test="userOpenid != null">
				user_openid = #{userOpenid}
			</if>
		</where>
	</update>

	<!-- 修改用户已用积分 -->
	<update id="updateUserUseIntegral" parameterType="Member">
		UPDATE
			TB_IAMBERRY_USER_USERINFO
		SET
			USER_USE_INTEGRAL = (USER_USE_INTEGRAL + #{userUseIntegral})
		WHERE
			USER_OPENID = #{userOpenid}
	</update>

	<!-- 修改用户可用积分 -->
	<update id="updateUserIntegral" parameterType="Member">
		UPDATE
			TB_IAMBERRY_USER_USERINFO
		SET
			USER_SURPLUS_INTEGRAL = (USER_SURPLUS_INTEGRAL - #{userSurplusIntegral})
		WHERE
			USER_OPENID = #{userOpenid}
	</update>
	
	<!-- 修改用户可用金额 -->
	<update id="updateUserIncomeByOpenId" parameterType="Member">
		UPDATE
		TB_IAMBERRY_USER_USERINFO
		SET
		USER_INCOME = (USER_INCOME + #{userIncome})
		WHERE
		USER_OPENID = #{userOpenid}
	</update>

	<!-- 修改用户待入账积分 -->
	<update id="updateUserStayIntegralByOpenId" parameterType="Member">
		UPDATE
		TB_IAMBERRY_USER_USERINFO
		SET
		user_stay_integral = (user_stay_integral + #{userStayIntegral})
		WHERE
		USER_OPENID = #{userOpenid}
	</update>

	<!-- 根据当前用户Id，下线信息 -->
	<select id="selectUserIntegralByWechatProxyId" parameterType="Page"
		resultType="Member">
		
		SELECT u.USER_ID userId,u.USER_HEAD userHead, u.USER_NICKNAME userNickname,
		 u.USER_ADDR userAddr, u.USER_SEX userSex,
		 u.USER_SUB_DATE userSubDate
		FROM TB_IAMBERRY_USER_USERINFO u 
		JOIN TB_IAMBERRY_WD w
		ON u.USER_OPENID=w.WD_USER_OPENID
		WHERE w.WD_FIRST=#{totalRecord}
		AND u.USER_RES_TYPE = 2
		<if test="params!=null and params.endNo!=null and params.endNo!=''">
		<![CDATA[ and u.USER_ID <  #{params.endNo} ]]>
		</if>
		<if test="params!=null and params.beginNo!=null and params.beginNo!=''">
		<![CDATA[ and u.USER_ID > #{params.begindNo} ]]>
		</if>
		ORDER BY u.USER_SUB_DATE DESC
		LIMIT 0, #{pageSize}
	</select> 
	
	<!-- 根据用户openid，获取用户来源、用户身份 -->
	<select id="selectUserResAndIdSByOpenId" parameterType="String" resultType="Member">
		SELECT
			USER_RES_TYPE userResType, USER_IDENTITY userIdentity, USER_ID userId
		FROM
			TB_IAMBERRY_USER_USERINFO
		WHERE
			USER_OPENID = #{userOpenid}
	</select>
	
	<!-- 根据openid获取用户的绑定状态 -->
	<update id="updateUserFlagByOpenId" parameterType="String">
		UPDATE 
			TB_IAMBERRY_USER_USERINFO
		SET 
			USER_IS_FLAG = 2
		WHERE
			USER_OPENID = #{openid} AND USER_IS_FLAG = 1
	</update>
	
	<!-- 根据 userid 查询下线人数 和 已购买人数-->
	<select id="selectDownCountByUserId" parameterType="Integer" resultType="Integer">
		(
			SELECT COUNT(u.USER_ID) count
			FROM TB_IAMBERRY_USER_USERINFO u 
			JOIN TB_IAMBERRY_WD w
			ON u.USER_OPENID=w.WD_USER_OPENID
			WHERE w.WD_FIRST=#{userid}
			AND u.USER_RES_TYPE = 2
		)
		UNION ALL
		(
			SELECT COUNT(userOpenid) count
			FROM
			(
			SELECT DISTINCT r.REBACK_FROM_OPENID userOpenid
			FROM TB_IAMBERRY_REBACK r
			JOIN TB_IAMBERRY_WD w
			ON r.REBACK_FROM_OPENID=w.WD_USER_OPENID
			JOIN TB_IAMBERRY_USER_USERINFO u
			ON w.WD_FIRST=u.USER_ID
			WHERE r.REBACK_TO_OPENID IN 
				(SELECT us.USER_OPENID FROM TB_IAMBERRY_USER_USERINFO us WHERE us.USER_ID=#{userid})
			AND u.USER_ID=#{userid}
			) s
		)
	</select>
	
	<!-- 根据openid 修改用户的是否购买 -->
	<update id="updateIsBaughtByOpenid" parameterType="Member">
		UPDATE TB_IAMBERRY_USER_USERINFO
		SET USER_IS_BOUGHT = #{userIsBought}
		WHERE USER_OPENID = #{userOpenid}
	</update>

	<!-- 个人分销下线用户 - 普通用户个数 -->
	<select id="getOrdinaryNumber"  resultType="integer" parameterType="Member">
		SELECT count(*)
		FROM tb_iamberry_user_userinfo TIUU
		WHERE TIUU.user_dealers = #{userDealers}
		<if test="userNickname!=null">
			and user_nickname like '%${userNickname}%'
		</if>
	</select>

	<!-- 公司分销下线用户 - 个人分销列表 -->
	<select id="getOrdinary"  resultType="Member" parameterType="Member">
		SELECT
		<include refid="allField"/>
		FROM tb_iamberry_user_userinfo
		WHERE user_dealers = #{userDealers}
		<if test="userNickname!=null">
			and user_nickname like '%${userNickname}%'
		</if>
		ORDER BY user_id DESC
		<if test="pageBean!=null and pageBean.recordBegin>0 and pageBean.pageSize>0 ">
			limit ${pageBean.recordBegin},${pageBean.pageSize}
		</if>
		<if test="pageBean!=null and pageBean.recordBegin==0 and pageBean.pageSize>0 ">
			limit ${pageBean.pageSize}
		</if>
	</select>

	<insert id="insertStayIntegral" parameterType="StayIntegral"  useGeneratedKeys="true" keyProperty="stayId">
        INSERT INTO tb_iamberry_stay_integral (user_openid,config_id,sales_orderid,stay_number,stay_recorded_time,stay_status,stay_remark)
        VALUES
         (#{userOpenId},#{configId},#{orderId},#{stayNumber},#{stayRecordedTime},#{stayStatus},#{stayRemark})
	</insert>

	<!-- 获取个人信息-->
	<select id="getRegisteredNumber" resultType="Integer" >
		SELECT
			COUNT(user_id)
		FROM
			TB_IAMBERRY_USER_USERINFO
		WHERE user_identity = 1;
	</select>

	<!-- 获取个人信息-->
	<select id="getBirthday" resultType="Member" parameterType="Member" >
		select
		<include refid="allField"/>
		from TB_IAMBERRY_USER_USERINFO
		where
			date_format(user_birth_date,'%m-%d') = date_format(#{userBirthDate},'%m-%d')
		AND
			user_identity = 1
	</select>

	<update id="update" parameterType="Member" >
		update
			tb_iamberry_user_userinfo
		<set >
			<if test="userId != null ">
				user_id = #{userId},
			</if >
			<if test="userOpenid != null and userOpenid != ''">
				user_openid = #{userOpenid},
			</if >
			<if test="userHead != null and userHead != ''">
				user_head = #{userHead},
			</if >
			<if test="userNickname != null and userNickname != ''">
				user_nickname = #{userNickname},
			</if >
			<if test="userAddr != null and userAddr != ''">
				user_addr = #{userAddr},
			</if >
			<if test="userSex != null ">
				user_sex = #{userSex},
			</if >
			<if test="userIntegral != null ">
				user_integral = #{userIntegral},
			</if >
			<if test="userAccountNum != null and userAccountNum != ''">
				user_account_num = #{userAccountNum},
			</if >
			<if test="userPwd != null and userPwd != ''">
				user_pwd = #{userPwd},
			</if >
			<if test="userTel != null and userTel != ''">
				user_tel = #{userTel},
			</if >
			<if test="userName != null and userName != ''">
				user_name = #{userName},
			</if >
			<if test="userBirthDate != null and userBirthDate != ''">
				user_birth_date = #{userBirthDate},
			</if >
			<if test="userSurplusIntegral != null ">
				user_surplus_integral = #{userSurplusIntegral},
			</if >
			<if test="userUseIntegral != null ">
				user_use_integral = #{userUseIntegral},
			</if >
			<if test="userResType != null ">
				user_res_type = #{userResType},
			</if >
			<if test="userStatus != null ">
				user_status = #{userStatus},
			</if >
			<if test="userSubDate != null and userSubDate != ''">
				user_sub_date = #{userSubDate},
			</if >
			<if test="userIdentity != null ">
				user_identity = #{userIdentity},
			</if >
			<if test="userQrcode != null and userQrcode != ''">
				user_qrcode = #{userQrcode},
			</if >
			<if test="userIsFlag != null ">
				user_is_flag = #{userIsFlag},
			</if >
			<if test="userIncome != null ">
				user_income = #{userIncome},
			</if >
			<if test="userDealers != null ">
				user_dealers = #{userDealers},
			</if >
			<if test="userIsBought != null ">
				user_is_bought = #{userIsBought},
			</if >
			<if test="userEmail != null and userEmail != ''">
				user_email = #{userEmail},
			</if >
			<if test="userQrcodeType != null ">
				user_qrcode_type = #{userQrcodeType},
			</if >
			<if test="userAgeStage != null ">
				user_age_stage = #{userAgeStage},
			</if >
			<if test="userStayIntegral != null ">
				user_stay_integral = #{userStayIntegral}
			</if >
		</set >
		where user_id= #{userId}
	</update>


</mapper>
