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

	<!-- 根据产品类型查询会员等级表 -->
	<select id="getRankByRankProductType" parameterType="RewardsRank" resultType="RewardsRank">
		SELECT 
			RANK_ID rankId,
			RANK_MONEY rankMoney,
			RANK_NUM rankNum,
			RANK_PRODUCT_TYPE rankProductType 
		FROM 
			TB_IAMBERRY_MEMBER_RANK 
		WHERE 
			RANK_PRODUCT_TYPE = #{productType.typeId} 
		AND 
			RANK_NUM &gt; #{rankNum} 
		ORDER BY RANK_NUM ASC 
	</select>
	
	<!-- 获取最大销售任务奖励内容 -->
	<select id="getMaxRankByProductType" parameterType="RewardsRank" resultType="RewardsRank">
		SELECT 
			RANK_ID rankId,
			RANK_MONEY rankMoney,
			RANK_NUM rankNum,
			RANK_PRODUCT_TYPE rankProductType 
		FROM 
			TB_IAMBERRY_MEMBER_RANK 
		WHERE 
			RANK_PRODUCT_TYPE = #{productType.typeId} 
		AND 
			<![CDATA[RANK_NUM <= #{rankNum} ]]>
		ORDER BY RANK_NUM DESC
		LIMIT 0,1
	</select>
	
	<!-- 插入会员销售奖励记录表数据 -->
	<insert id="insertRewardsLog" parameterType="RewardsLog">
		INSERT INTO TB_IAMBERRY_SALES_REWARDS_LOGS
			(
				LOGS_RANK_ID,
				LOGS_USER_OPENID,
				LOGS_PRODUCT_TYPE,
				LOGS_NUM,
				LOGS_MONEY,
				LOGS_DESC,
				LOGS_CREATE_TIME
			) 
			VALUES 
			(
				#{logsRankId},
				#{logsUserOpenId},
				#{logsProductType},
				#{logsNum},
				#{logsMoney},
				#{logsDesc},
				NOW()
			)
	</insert>
	<sql id="allRewardsField">
		RANK_ID rankId,
		RANK_NAME rankName,
		RANK_DESC rankDesc,
		RANK_NUM rankNum,
		RANK_MONEY rankMoney,
		RANK_CREATE_DATE rankCreateDate,
		RANK_UPDATE_DATE rankUpdateDate
	</sql>
	<!-- 【查询所有 分销奖励】 -->
	<select id="selectRewardsList" resultType="RewardsRank">
		SELECT 
			<include refid="allRewardsField"></include>,
			child_type_id "productType.typeId",
			child_type_name "productType.typeName"
		FROM TB_IAMBERRY_MEMBER_RANK
		JOIN tb_iamberry_product_child_type ON RANK_PRODUCT_TYPE = child_type_id
	</select>
	<!-- 【添加一个分销奖励规则】 -->
	<insert id="insertRewardsRank" parameterType="RewardsRank"
			useGeneratedKeys="true" keyProperty="rankId">
		INSERT INTO TB_IAMBERRY_MEMBER_RANK
			(RANK_NAME,RANK_DESC,RANK_NUM,RANK_MONEY,RANK_PRODUCT_TYPE,RANK_CREATE_DATE,RANK_UPDATE_DATE)
		VALUES
			(#{rankName},#{rankDesc},#{rankNum},#{rankMoney},#{productType.typeId},NOW(),NOW())
	</insert>
	
	<select id="selectRewardsRankById" parameterType="Integer" resultType="RewardsRank">
		SELECT 
			<include refid="allRewardsField"></include>,
			child_type_id "productType.typeId",
			child_type_name "productType.typeName"
		FROM TB_IAMBERRY_MEMBER_RANK
		JOIN tb_iamberry_product_child_type ON RANK_PRODUCT_TYPE = child_type_id
		WHERE RANK_ID = #{rankId}
	</select>
	
	<update id="updateRewardsRank" parameterType="RewardsRank">
		UPDATE TB_IAMBERRY_MEMBER_RANK
		SET 
			RANK_UPDATE_DATE = NOW(),
		<if test="rankName != null and rankName != ''">
			RANK_NAME = #{rankName},
		</if>
		<if test="rankDesc != null and rankDesc != ''">
			RANK_DESC = #{rankDesc},
		</if>
		<if test="rankNum != null and rankNum != ''">
			RANK_NUM = #{rankNum},
		</if>
		<if test="rankMoney != null and rankMoney != ''">
			RANK_MONEY = #{rankMoney},
		</if>
		<if test="productType.typeId !=null and productType.typeId !=''">
			RANK_PRODUCT_TYPE=#{productType.typeId}
		</if>
		WHERE RANK_ID = #{rankId}
	</update>
	
	<delete id="deleteRewardsRankById" parameterType="RewardsRank">
		DELETE FROM TB_IAMBERRY_MEMBER_RANK
		WHERE  RANK_ID = #{rankId}
	</delete>
</mapper>