<?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.yonge.cooleshow.biz.dal.dao.ActivityPlanRewardDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.ActivityPlanReward">
            <result column="id_" property="id" />
	        <result column="activity_id_" property="activityId" />
	        <result column="reward_id_" property="rewardId" />
	        <result column="reward_num_" property="rewardNum" />
	        <result column="reward_stock_" property="rewardStock" />
	        <result column="create_time_" property="createTime" />
		</resultMap>  
    
    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as id
        , t.activity_id_ as activityId
        , t.reward_id_ as rewardId
        , t.reward_num_ as rewardNum
        , t.reward_stock_ as rewardStock
        , t.group_ AS `group`
        , t.create_time_ as createTime
        </sql>
    
    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityPlanRewardVo">
        SELECT
            <include refid="baseColumns"/>
        FROM activity_plan_reward t
        where t.id_ = #{id}
    </select>

    <select id="detailByActivityIdAndRewardId"
            resultType="com.yonge.cooleshow.biz.dal.entity.ActivityPlanReward">
        SELECT
            <include refid="baseColumns"/>
        FROM activity_plan_reward t
        where t.activity_id_ = #{activityId} and t.reward_id_ = #{rewardId} AND t.group_ = #{group}
    </select>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityPlanRewardVo">
		SELECT         
        	<include refid="baseColumns" />
		FROM activity_plan_reward t
	</select>

    <!-- 表字段 -->
    <sql id="activityRewardColumns">
        ar.id_ as "activityReward.id"
        , ar.activity_id_ as "activityReward.activityId"
        , ar.reward_client_ as "activityReward.rewardClient"
        , ar.reward_name_ as "activityReward.rewardName"
        , ar.reward_type_ as "activityReward.rewardType"
        , ar.reward_describe_ "activityReward.rewardDescribe"
        , ar.img_url_ as "activityReward.imgUrl"
        , ar.stock_ as "activityReward.stock"
        , ar.num_ as "activityReward.num"
        , ar.unit_ as "activityReward.unit"
        , ar.status_ as "activityReward.status"
        , ar.create_time_ as "activityReward.createTime"
        , ar.create_by_ as "activityReward.createBy"
        , ar.update_time_ as "activityReward.updateTime"
        , ar.update_by_ as "activityReward.updateBy"
        , ar.discount_price_ as "activityReward.discountPrice"
        , ar.vip_card_id_ as "activityReward.vipCardId"
    </sql>

    <select id="queryActivityPlanReward" resultType="com.yonge.cooleshow.biz.dal.dto.ActivityPlanRewardDto">
        SELECT
            <include refid="baseColumns" />,
            <include refid="activityRewardColumns" />
        FROM activity_plan_reward t
        left join activity_reward ar on t.reward_id_ = ar.id_
        where t.activity_id_ = #{activityId}
    </select>

    <select id="getDelRewardList" resultType="com.yonge.cooleshow.biz.dal.entity.ActivityPlanReward">
        SELECT
            <include refid="baseColumns" />
        FROM activity_plan_reward t
        where t.activity_id_ = #{activityId}
        <if test="rewardIds != null and rewardIds.size() != 0">
            and t.reward_id_ not in
            <foreach collection="rewardIds" separator="," close=")" open="(" item="item">
                #{item}
            </foreach>
        </if>
    </select>
    <select id="queryMemberActivityPlanReward"
            resultType="com.yonge.cooleshow.biz.dal.dto.ActivityPlanRewardDto">
        select <include refid="baseColumns" />,
            mps.vip_type_ as "activityReward.rewardType",mps.period_ as "activityReward.unit",mps.id_ as "activityReward.vipCardId"
        from activity_plan_reward t
        left join member_price_settings mps ON mps.id_ = t.reward_id_
        where t.activity_id_ = #{activityId}
    </select>


    <update id="reduceStock">
        update activity_plan_reward set reward_stock_ = reward_stock_ - 1 where id_ = #{activityRewardId} AND activity_id_ = #{activityId} and reward_stock_ > 0
    </update>
</mapper>