<?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.ActivityUserRewardDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.ActivityUserReward">
        <result column="id_" property="id" />
        <result column="activity_id_" property="activityId" />
        <result column="biz_id_" property="bizId" />
        <result column="user_id_" property="userId" />
        <result column="reward_id_" property="rewardId" />
        <result column="unit_" property="unit" />
        <result column="grant_flag_" property="grantFlag" />
        <result column="receive_flag_" property="receiveFlag" />
        <result column="receive_time_" property="receiveTime" />
        <result column="create_time_" property="createTime" />
        <result column="update_time_" property="updateTime" />
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t.id_ as id
        , t.activity_id_ as activityId
        , t.biz_id_ as bizId
        , t.user_id_ as userId
        , t.reward_id_ as rewardId
        , t.unit_ as unit
        , t.grant_flag_ as grantFlag
        , t.receive_flag_ as receiveFlag
        , t.create_time_ as createTime
        , t.update_time_ as updateTime
        , t.receive_time_ as receiveTime
    </sql>

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

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.UserRewardVo">
        SELECT
            su.id_ as userId,
            su.real_name_ as realName,
            su.username_ as username,
            su.phone_ as phone,
            IFNULL(ar.reward_type_, "VIP") AS rewardType,
            <choose>
                <when test="param.activityGroupBy == 1">
                    /*按用户、活动分组统计*/
                    IFNULL(t.winning_time_, t.create_time_) AS winningTime,
                    MIN(t.create_time_) as createTime,
                    COUNT(DISTINCT t.id_) AS amount,
                    group_concat(if(t.grant_flag_ = 1,ar.reward_name_,null)) as rewardNames,
                </when>
                <otherwise>
                    /*分享活动，直接查询用户购买记录*/
                    t.create_time_ as createTime,
                    IF(t.grant_flag_ = 1,ar.reward_name_,null) AS rewardNames,
                </otherwise>
            </choose>
            IFNULL(ar.unit_, t.unit_) AS unit
        FROM activity_user_reward t
        left join activity_reward ar on ar.id_ = t.reward_id_
        left join sys_user su on t.user_id_ = su.id_
        <where>
            <if test="param.activityId != null">
                and t.activity_id_ = #{param.activityId}
            </if>
            <if test="param.search !=null and param.search != ''">
                and (su.real_name_ like concat('%',#{param.search},'%')
                    or su.phone_ like concat('%',#{param.search},'%')
                    or su.id_ like concat('%',#{param.search},'%'))
            </if>
            <if test="param.activityRewardId != null">
                and t.reward_id_ = #{param.activityRewardId}
            </if>
            <if test="param.startTime != null">
                and t.create_time_ &gt;= #{param.startTime}
            </if>
            <if test="param.endTime != null">
                and  t.create_time_ &lt;= #{param.endTime}
            </if>
        </where>
        <if test="param.activityGroupBy == 1">GROUP BY t.user_id_ , t.activity_id_ ORDER BY t.create_time_ ASC </if>
    </select>

    <!--分享活动关联老师-->
    <select id="selectActivityShareTeacher" resultType="com.yonge.cooleshow.biz.dal.vo.UserRewardVo">
        SELECT t1.id_ AS teacherId, t1.username_ AS username, t1.real_name_ AS realName, t3.user_id_ AS userId
        FROM sys_user t1 JOIN activity_evaluation t2 ON t1.id_ = t2.music_sheet_id_ JOIN activity_evaluation_record t3 ON t2.id_ = t3.evaluation_id_
        WHERE t3.activity_id_ = #{activityId} AND t3.user_id_ IN (<foreach collection="userIds" separator="," item="item">#{item}</foreach>)
    </select>

    <select id="selectReceiveRewardCount" resultType="int">

        select count(1) from
        activity_user_reward aur
        left join activity_plan ap on ap.id_ = aur.activity_id_
        left join activity_reward ar on ar.id_ = aur.reward_id_
        <include refid="selectReceiveRewardSql"/>
    </select>

    <update id="receiveReward">
        update  activity_user_reward set receive_flag_ = 1 ,receive_time_ = #{receiveTime} where grant_flag_=1 and receive_flag_ = 0 and id_ = #{receiveRewardId} and user_id_ = #{userId}

    </update>
    <sql id="selectReceiveRewardSql">
        <where>
            ar.reward_type_ != 'ACTUAL' and grant_flag_=1
            <if test="query.userId != null">
                and aur.user_id_ = #{query.userId}
            </if>
            <if test="query.client != null">
                and ap.activity_client_ = #{query.client}
            </if>
            <if test="query.receive != null">
                and aur.receive_flag_ = #{query.receive}
            </if>
        </where>
    </sql>

    <select id="selectReceiveRewardList" resultType="com.yonge.cooleshow.biz.dal.vo.activity.UserRewardWrapper">
        select
            aur.id_ as receiveRewardId,
            ar.reward_name_ as rewardName,
            ar.reward_type_ as rewardType,
            ar.reward_describe_ as rewardDescribe,
            ar.img_url_ as imgUrl,
            aur.create_time_ as winningTime
        from
        activity_user_reward aur
        left join activity_plan ap on ap.id_ = aur.activity_id_
        left join activity_reward ar on ar.id_ = aur.reward_id_
        <include refid="selectReceiveRewardSql"/>
    </select>
    <!--分享活动关联老师-->

</mapper>