<?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.ActivityRewardDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.ActivityReward">
        <result column="id_" property="id" />
        <result column="activity_id_" property="activityId" />
        <result column="reward_client_" property="rewardClient" />
        <result column="reward_name_" property="rewardName" />
        <result column="reward_type_" property="rewardType" />
        <result column="reward_describe_" property="rewardDescribe" />
        <result column="coupon_id_" property="couponId" />
        <result column="img_url_" property="imgUrl" />
        <result column="stock_" property="stock" />
        <result column="num_" property="num" />
        <result column="unit_" property="unit" />
        <result column="status_" property="status" />
        <result column="create_time_" property="createTime" />
        <result column="create_by_" property="createBy" />
        <result column="update_time_" property="updateTime" />
        <result column="update_by_" property="updateBy" />
        <result column="discount_price_" property="discountPrice" />
    </resultMap>

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

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityRewardVo">
        SELECT
            <include refid="baseColumns"/>
        FROM activity_reward t
        where t.id_ = #{id}
    </select>

    <select id="detailByActivityId" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityRewardVo">
        SELECT
            <include refid="baseColumns"/>
        FROM activity_reward t
        where t.activity_id_ = #{id}
    </select>

    <sql id="selectSql">
        SELECT
            <include refid="baseColumns" />,
            u.username_ as updateByName,
            (select count(1) from activity_plan_reward r where t.id_ = r.reward_id_)>0 as useFlag
        FROM activity_reward t
        left join sys_user u on t.update_by_ = u.id_
        <where>
            <if test="param.rewardName != null and param.rewardName != ''">
                and t.reward_name_ like concat('%',#{param.rewardName},'%')
            </if>
            <choose>
                <when test="param.status != null">
                    and t.status_ = #{param.status}
                </when>
                <otherwise>
                    and t.status_ > -1
                </otherwise>
            </choose>
            <!--<if test="param.status != null">
                and t.status_ = #{param.status}
            </if>-->
            <if test="param.rewardClient != null and param.rewardClient != ''">
                and INTE_ARRAY(t.reward_client_,#{param.rewardClient})
            </if>
            <if test="param.hasStock != null and param.hasStock.code == 1">
                and t.stock_>0
            </if>
        </where>
        order by t.create_time_ desc
    </sql>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityRewardVo">
        <include refid="selectSql"/>
	</select>

    <select id="selectAll" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityRewardVo">
        <include refid="selectSql"/>
    </select>


    <resultMap id="ActivityRewardResultMap" type="com.yonge.cooleshow.biz.dal.entity.ActivityReward">
        <result column="id_" property="id" />
        <result column="activity_id_" property="activityId" />
        <result column="reward_client_" property="rewardClient" />
        <result column="reward_name_" property="rewardName" />
        <result column="reward_type_" property="rewardType" />
        <result column="reward_describe_" property="rewardDescribe" />
        <result column="img_url_" property="imgUrl" />
        <result column="stock_" property="stock" />
        <result column="num_" property="num" />
        <result column="unit_" property="unit" />
        <result column="status_" property="status" />
        <result column="create_time_" property="createTime" />
        <result column="create_by_" property="createBy" />
        <result column="update_time_" property="updateTime" />
        <result column="update_by_" property="updateBy" />
        <result column="discount_price_" property="discountPrice" />

        <result column="group_" property="group" />
    </resultMap>
    <select id="getActivityReward" resultMap="ActivityRewardResultMap">
        select ar.*, apr.group_
        from activity_plan_reward apr
        left join activity_reward ar on apr.reward_id_ = ar.id_
        where apr.activity_id_ = #{activityPlanId}
    </select>

    <select id="userRewarded" resultType="java.lang.Boolean">
        select (count(1)>0) as flag from activity_user_reward
        where activity_id_ = #{activityId} and user_id_ = #{userId}
    </select>

    <!--奖品关联活动统计-->
    <select id="selectRewardActivityStatInfo"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper">
        SELECT t1.reward_id_ AS id, COUNT(DISTINCT t1.id_) AS total FROM activity_plan_reward t1
        <where>
            <if test="rewardIds != null">
                AND t1.reward_id_ IN (<foreach collection="rewardIds" separator="," item="item">#{item}</foreach>)
            </if>
        </where>
        GROUP BY t1.reward_id_
    </select>
    <!--奖品关联活动统计-->
</mapper>