<?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.ActivityRegistrationDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.ActivityRegistration">
            <result column="id_" property="id" />
	        <result column="activity_id_" property="activityId" />
	        <result column="user_id_" property="userId" />
	        <result column="order_no_" property="orderNo" />
	        <result column="sub_order_no_" property="subOrderNo" />
	        <result column="create_time_" property="createTime" />
		</resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as id
        , t.activity_id_ as activityId
        , t.user_id_ as userId
        , t.order_no_ as orderNo
        , t.sub_order_no_ as subOrderNo
        , t.create_time_ as createTime
        </sql>

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

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityRegistrationVo">
		SELECT
        	<include refid="baseColumns" />,
		    u.id_ as studentId,u.username_ as username,u.phone_ as phone,u.real_name_ as realName,
            <if test="param.activityType != null and param.activityType.code == 'EVALUATION'">
                b.music_sheet_name_ as musicSheetName,b.max_score_ as maxScore,b.num_ as num,
            </if>
            if(a.num>0, true, false) as rewardFlag
		FROM activity_registration t
		left join sys_user u on t.user_id_ = u.id_
		left join (
            select
		        aur.activity_id_,aur.user_id_,count(1) as num
		    from activity_user_reward aur
		    <where>
                <if test="param.activityId != null">
                    and aur.activity_id_ = #{param.activityId}
                </if>
            </where>
		    group by aur.activity_id_,aur.user_id_
        ) a on t.activity_id_ = a.activity_id_ and t.user_id_ = a.user_id_
        <if test="param.activityType != null and param.activityType.code == 'EVALUATION'">
            left join (
                select
                    ar.activity_id_,aer.user_id_,
                    group_concat(DISTINCT ms.music_sheet_name_) as music_sheet_name_,
                    sum(if(aer.score_ is null, 0, 1)) as num_,
                    max(aer.score_) as max_score_
                from activity_registration ar
		        left join activity_evaluation_record aer on ar.activity_id_ = aer.activity_id_ and ar.user_id_ = aer.user_id_
                left join activity_evaluation ae on aer.evaluation_id_ = ae.id_
                left join music_sheet ms on ae.music_sheet_id_ = ms.id_ and find_in_set('PLATFORM',ms.provider_type_)
                <where>
                    <if test="param.activityId != null">
                        and aer.activity_id_ = #{param.activityId}
                    </if>
		            <if test="param.subjectIds != null and param.subjectIds.size() > 0">
                        AND aer.resource_id_ IN (<foreach collection="param.subjectIds" separator="," item="item">#{item}</foreach>)
                    </if>
		            <if test="param.musicSheetIds != null and param.musicSheetIds.size()>0">
                        and ae.music_sheet_id_ IN
                        <foreach collection="param.musicSheetIds" item="item" separator="," open="(" close=")">
                            #{item}
                        </foreach>
                    </if>
                </where>
                group by ar.activity_id_,aer.user_id_
            ) b on t.activity_id_ = b.activity_id_ and t.user_id_ = b.user_id_
        </if>
        <where>
            <if test="param.activityType != null and param.activityType.code == 'EVALUATION'">
		        and b.activity_id_ is not null
            </if>
            <if test="param.activityId != null">
                and t.activity_id_ = #{param.activityId}
            </if>
            <if test="null != param.search and '' != param.search">
                AND (
                    t.user_id_ LIKE CONCAT('%', #{param.search}, '%') or
                    u.username_ LIKE CONCAT('%', #{param.search}, '%') or
                    u.phone_ LIKE CONCAT('%', #{param.search}, '%')
                )
            </if>
            <if test="null != param.rewardFlag and param.rewardFlag.code == 0">
                and (a.num is null or a.num = 0)
            </if>
            <if test="null != param.rewardFlag and param.rewardFlag.code == 1">
                and a.num > 0
            </if>
        </where>
        order by t.create_time_ desc
	</select>
</mapper>