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

	<!--  -->
	<sql id="payLogField">
		PAY_ID payId,
		PAY_OPEN_ID payOpenId,
		PAY_MONEY payMoney,
		PAY_STATUS payStatus,
		PAY_TRANSACTION_ID payTransactionId,
		PAY_TRANSACTION_DATE payTransactionDate,
		PAY_GIFT_CARD_NUM payGiftCardNum,
		PAY_CREATE_DATE payCreateDate,
		PAY_REMARK payRemark
	</sql>
	
	<sql id="agentInfoField">
		AGENT_ID agentId,
		AGENT_OPEN_ID agentOpenId,
		AGENT_NICKNAME agentNickname,
		AGENT_HEAD agentHead,
		AGENT_AREA agentArea,
		AGENT_SEX agentSex,
		AGENT_TEL agentTel,
		AGENT_REFERRER_OPENID agentReferrerOpenid,
		AGENT_REFERRER_NAME agentReferrerName,
		AGENT_CREATE_TIME agentCreateTime,
		AGENT_REMARK agentRemark,
		AGENT_CARD_SUM agentCardSum,
		AGENT_IS_HUNDRED agentIsHudredAgent
	</sql>
	
	<insert id="insertAgentInfo" parameterType="AgentInfo"
		keyProperty="agentId" useGeneratedKeys="true">
		INSERT INTO tb_iamberry_agent_info 
		(
			AGENT_OPEN_ID ,
			AGENT_NICKNAME ,
			AGENT_HEAD ,
			AGENT_AREA ,
			AGENT_SEX ,
			AGENT_TEL ,
			AGENT_REFERRER_OPENID ,
			AGENT_REFERRER_NAME ,
			AGENT_CREATE_TIME ,
			AGENT_REMARK ,
			AGENT_CARD_SUM
		)
		VALUES
		(
			#{agentOpenId},#{agentNickname},#{agentHead},#{agentArea},#{agentSex},
			#{agentTel},#{agentReferrerOpenid},#{agentReferrerName},
			NOW(),#{agentRemark},#{agentCardSum}
		)
	</insert>
	<insert id="insertAgentPayLog" parameterType="AgentPayLogs"
		keyProperty="payId" useGeneratedKeys="true">
		INSERT INTO tb_iamberry_agent_pay_logs
			(		
				PAY_OPEN_ID ,
				PAY_MONEY ,
				PAY_STATUS ,
				PAY_TRANSACTION_ID ,
				PAY_TRANSACTION_DATE ,
				PAY_GIFT_CARD_NUM ,
				PAY_CREATE_DATE ,
				PAY_REMARK 
			)
		VALUES
			(
				#{payOpenId},#{payMoney},#{payStatus},#{payTransactionId},
				#{payTransactionDate},#{payGiftCardNum},NOW(),#{payRemark}
			)
	</insert>
	<!-- 分页查询个人所有礼品卡数据 -->
	<select id="beRefereesList" parameterType="AgentInfo" 
		resultType="AgentInfo">
		SELECT 
			<include refid="agentInfoField"></include>,pl.pay_status as payStatus
		FROM 
			tb_iamberry_agent_info ai Left Join tb_iamberry_agent_pay_logs pl on ai.agent_open_id = pl.pay_open_id
		WHERE 
			ai.agent_referrer_openid = #{agentReferrerOpenid} 
		LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<!-- 获取当前用户未支付订单 -->
	<select id="selectPayLogByOpenid" parameterType="AgentPayLogs" resultType="AgentPayLogs">
		SELECT 
			<include refid="payLogField"></include>
		FROM TB_IAMBERRY_AGENT_PAY_LOGS
		WHERE PAY_OPEN_ID = #{payOpenId} 
		<if test="payStatus != null and payStatus !='' ">
			AND PAY_STATUS = #{payStatus}
		</if>
	</select>
	
	<!-- 获取当前用户已支付订单 -->
	<select id="selectPayLogHasByOpenid" parameterType="String" resultType="AgentPayLogs">
		SELECT 
			<include refid="payLogField"></include>
		FROM TB_IAMBERRY_AGENT_PAY_LOGS
		WHERE PAY_OPEN_ID = #{openId} AND PAY_STATUS = 2
	</select>
	
	<!-- 根据ID查询支付记录 -->
	<select id="selectPayLogById" parameterType="Integer" resultType="AgentPayLogs">
		SELECT 
			<include refid="payLogField"></include>
		FROM TB_IAMBERRY_AGENT_PAY_LOGS
		WHERE PAY_ID = #{ID}
	</select>
	
	<!-- 修改支付状态、微信支付订单号、微信支付时间 -->
	<update id="updatePayLogsById" parameterType="AgentPayLogs">
		UPDATE TB_IAMBERRY_AGENT_PAY_LOGS
		<set>
			PAY_STATUS=2
			<if test="payMoney != null">
				,PAY_MONEY = #{payMoney}
			</if>
			<if test="payTransactionId != null and payTransactionId!= ''">
				,PAY_TRANSACTION_ID = #{payTransactionId}
			</if>
			<if test="payTransactionDate != null and payTransactionDate != ''">
				,PAY_TRANSACTION_DATE = #{payTransactionDate}
			</if>
		</set>
		WHERE PAY_ID=#{payId}
	</update>
	
	<!-- 根据openid查询当前付费代理信息 -->
	<select id="selectAgentByOpenId" parameterType="String" resultType="AgentInfo">
		SELECT 
			<include refid="agentInfoField"></include> 
		FROM 
			TB_IAMBERRY_AGENT_INFO 
		WHERE 
			AGENT_OPEN_ID = #{agentOpenId} 
	</select>
	
	<!-- 查询所有付费代理商信息 -->
	<select id="selectAgentList" resultType="AgentInfo">
			SELECT 
				DISTINCT <include refid="agentInfoField" />
			FROM 
				TB_IAMBERRY_AGENT_INFO a 
			JOIN TB_IAMBERRY_AGENT_PAY_LOGS p 
			ON a.AGENT_OPEN_ID = p.PAY_OPEN_ID
	</select>
	
	<!-- 修改付费代理商中统计售出礼品卡总数 -->
	<update id="updateAgentCardSum" parameterType="AgentInfo">
		UPDATE 
			TB_IAMBERRY_AGENT_INFO 
		SET 
			AGENT_CARD_SUM = (AGENT_CARD_SUM + 1) 
		WHERE 
			AGENT_OPEN_ID = #{agentOpenId} 
	</update>
	
	<!-- 将付费代理商中统计售出礼品卡总数置为0 -->
	<update id="updateAgentCardSumZero" parameterType="AgentInfo">
		UPDATE 
			TB_IAMBERRY_AGENT_INFO 
		SET 
			AGENT_CARD_SUM = 0  
		WHERE 
			AGENT_OPEN_ID = #{agentOpenId} 
	</update>
	
	<!-- 修改修改付费代理商的推荐人及姓名电话 -->
	<update id="updateAgentInfoByOpenId" parameterType="AgentInfo">
		UPDATE TB_IAMBERRY_AGENT_INFO
		<set>
			<if test="agentReferrerOpenid != null">
				AGENT_REFERRER_OPENID = #{agentReferrerOpenid},
			</if>
			<if test="agentNickname != null and agentNickname!= ''">
				AGENT_NICKNAME = #{agentNickname},
			</if>
			<if test="agentTel != null and agentTel != ''">
				AGENT_TEL = #{agentTel}
			</if>
		</set>
		WHERE AGENT_OPEN_ID=#{agentOpenId}
	</update>
	
</mapper>
