<?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.ActivityEvaluationDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.ActivityEvaluation">
        <result column="id_" property="id" />
        <result column="activity_id_" property="activityId" />
        <result column="music_sheet_id_" property="musicSheetId" />
        <result column="resource_type_" property="resourceType" />
        <result column="create_time_" property="createTime" />
        <result column="create_by_" property="createBy" />
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t.id_ as id
        , t.activity_id_ as activityId
        , t.music_sheet_id_ as musicSheetId
        , t.resource_type_ as resourceType
        , t.evaluation_difficulty_ as evaluationDifficulty
        , t.create_time_ as createTime
        , t.create_by_ as createBy
    </sql>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityEvaluationVo">
        SELECT
        <include refid="baseColumns"/>
        FROM activity_evaluation t
        left join music_sheet ms on t.music_sheet_id_ = ms.id_
        where t.id_ = #{id}
    </select>

    <sql id="selectSql">
        SELECT
            <include refid="baseColumns" />
            , ms.music_sheet_name_ as musicSheetName
            , ms.music_subject_ AS subjectId
            , ms.title_img_ AS musicImage
            , ms.composer_ AS composer
            , (select group_concat(mt.name_) from music_tag mt where find_in_set(mt.id_,ms.music_tag_) and mt.del_flag_ = 0  and mt.state_ = 1) as musicTagNames
            , (select group_concat(s.name_) from subject s where find_in_set(s.id_,ms.music_subject_) and s.del_flag_ = 0 ) as subjectNames
            , ms.source_type_ as sourceType
            , ms.music_price_ as musicPrice
        FROM activity_evaluation t
        left join music_sheet ms on t.music_sheet_id_ = ms.id_
        <where>
            <if test="param.activityId != null">
                and t.activity_id_ = #{param.activityId}
            </if>
        </where>
    </sql>

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

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

    <insert id="addBatch">
        insert into activity_evaluation (activity_id_,create_by_, music_sheet_id_, resource_type_) values
        <foreach collection="musicSheetIds" item="item" separator="," index="index">
            (#{activityId,jdbcType=BIGINT},#{userId,jdbcType=BIGINT},#{item,jdbcType=BIGINT}, #{resourceType,jdbcType=VARCHAR})
        </foreach>
    </insert>


    <select id="selectActivityMusic" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityMusicVo">

        select ms.id_ as musicSheetId
        ,ms.music_sheet_name_ as musicSheetName
        , ms.music_subject_ AS subjectId
        , ms.title_img_ AS musicImage
        ,s.name_ as musicSubject
        , ms.composer_ AS composer
        ,ae.id_ as evaluationId
        , ae.evaluation_difficulty_ AS evaluationDifficulty
        ,(select if(count(1)>0,1,0) as `join` from activity_evaluation_record aer where aer.evaluation_id_ = ae.id_ and aer.user_id_ = #{userId}) as `join`
        from activity_evaluation ae
        left join music_sheet ms on ms.id_ = ae.music_sheet_id_
        left join subject s on s.id_ = ms.music_subject_
        <where>
            ms.del_flag_ = 0
            <if test="activityPlanId != null">
                and ae.activity_id_ = #{activityPlanId}
            </if>
        </where>
    </select>

    <select id="selectActivityUser" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityMusicVo">
        select t2.score
        ,t2.username_ as username
        ,t2.avatar_ as userAvatar
        ,s.name_ as userSubject
        ,t2.evaluationId
        ,t2.userId
        from
        (select aem2.evaluation_id_ as evaluationId
            ,su.id_ as userId
            ,su.username_
            ,su.avatar_
            ,t.score
        from (select
            max(aem.score_) as score,
            aem.evaluation_id_
            from  activity_evaluation_record aem
            where
                aem.score_ is not null   and aem.activity_id_ = #{activityPlanId}
            group by aem.evaluation_id_
                       ) t
            left join activity_evaluation_record aem2 on aem2.score_ = t.score and t.evaluation_id_ = aem2.evaluation_id_
            left join sys_user su on su.id_ = aem2.user_id_
            where aem2.activity_id_ = #{activityPlanId} and su.del_flag_ = 0 group by t.score,t.evaluation_id_

        ) t2
        left join student stu on stu.user_id_ = t2.userId
        left join subject s on s.id_ = stu.subject_id_

    </select>

    <select id="musicPage" resultType="com.yonge.cooleshow.biz.dal.vo.MusicSheetVo">
        select
            <include refid="com.yonge.cooleshow.biz.dal.dao.MusicSheetDao.Base_Column_List"/>,
            su.username_ as addName,
            su.avatar_ as addUserAvatar,
            (select group_concat(mt.name_) from music_tag mt
                where find_in_set(mt.id_,t.music_tag_) and mt.del_flag_ = 0  and mt.state_ = 1) as musicTagNames,
            (select group_concat(s.name_) from subject s
                where find_in_set(s.id_,t.music_subject_) and s.del_flag_ = 0 ) as subjectNames
        from music_sheet t
        left join sys_user su on t.create_by_ = su.id_
        <where>
            su.del_flag_ = 0
            and not exists(
                select 1 from activity_evaluation ae where ae.activity_id_ = ${param.activityId} and ae.music_sheet_id_ = t.id_
            )
            <include refid="com.yonge.cooleshow.biz.dal.dao.MusicSheetDao.QueryInfo"/>
        </where>
        order by t.id_ desc
    </select>

    <!--活动老师信息-->
    <resultMap id="ActivityTeacherPageInfoResultMap" type="com.yonge.cooleshow.biz.dal.vo.activity.ActivityTeacherWrapper">
        <result column="id_" jdbcType="BIGINT" property="id" />
        <result column="real_name_" jdbcType="BIGINT" property="realName" />
        <result column="username_" jdbcType="VARCHAR" property="username" />
        <result column="phone_" jdbcType="VARCHAR" property="phone" />
        <result column="subject_id_" jdbcType="VARCHAR" property="subjectId" />
        <result column="teacher_id" jdbcType="BIGINT" property="teacherId" />
    </resultMap>
    <sql id="ActivityTeacherPageInfo_Column_List">
        t1.id_ AS teacher_id, t1.real_name_, t1.username_, t1.phone_, t2.subject_id_
        <if test="record.activityId != null and record.addType == 0">, t3.id_</if>
    </sql>
    <sql id="ActivityTeacherPageInfo_Condition_List">
        <if test="record.id != null">
            AND t1.id_ = #{record.id}
        </if>
        <if test="record.mobile != null">
            AND t1.phone_ = #{record.mobile}
        </if>
        <if test="record.name != null">
            AND (t1.real_name_ LIKE '%${record.name}%' OR t1.username_ LIKE '%${record.name}%')
        </if>
        <if test="record.lockFlag != null">
            AND t1.lock_flag_ = #{record.lockFlag}
        </if>
        <if test="record.deleteFlag != null">
            AND t1.del_flag_ = #{record.deleteFlag}
        </if>
        <if test="record.addType == 1">
            AND t4.user_id IS NULL
        </if>
    </sql>
    <sql id="ActivityTeacherNotIn_Condition_List">
        <if test="record.activityType != null">
            AND t1.activity_type_ = #{record.activityType}
        </if>
        <if test="record.resourceType != null">
            AND t2.resource_type_ = #{record.resourceType}
        </if>
        <choose>
            <when test="record.activityId != null">
                AND (t2.activity_id_ = #{record.activityId} <if test="record.startTime != null and record.endTime != null"> OR (<include refid="ActivityTeacherNotIn_ActivityTime_Condition_List" />) </if>)
            </when>
            <otherwise>
                AND <include refid="ActivityTeacherNotIn_ActivityTime_Condition_List" />
            </otherwise>
        </choose>
    </sql>
    <sql id="ActivityTeacherNotIn_ActivityTime_Condition_List">
        <if test="record.startTime != null and record.endTime != null">
            t1.activity_state_ = #{record.activityState} AND ((unix_timestamp(t1.activity_start_) &lt;= #{record.startTime} AND #{record.startTime} &lt;= unix_timestamp(t1.activity_end_)) OR (unix_timestamp(t1.activity_start_) &lt;= #{record.endTime} AND #{record.endTime} &lt;= unix_timestamp(t1.activity_end_)))
        </if>
    </sql>
    <select id="selectActivityTeacherPageInfo" resultMap="ActivityTeacherPageInfoResultMap">
        SELECT <include refid="ActivityTeacherPageInfo_Column_List" />
        FROM sys_user t1 JOIN teacher t2 ON (t1.id_ = t2.user_id_ <if test="record.lockFlag !=null"> AND t2.lock_flag_ = #{record.lockFlag} </if> )
        <choose>
            <when test="record.addType == 1">
                LEFT JOIN (SELECT t2.music_sheet_id_ AS user_id FROM activity_plan t1 JOIN activity_evaluation t2 ON (t1.id_ = t2.activity_id_ <include refid="ActivityTeacherNotIn_Condition_List"/> ) )t4 ON (t4.user_id = t1.id_)
            </when>
            <otherwise>
                <if test="record.activityId != null and record.addType == 0"> JOIN activity_evaluation t3 ON (t1.id_ = t3.music_sheet_id_ AND t3.activity_id_ = #{record.activityId}) </if>
            </otherwise>
        </choose>
        <where>
            <include refid="ActivityTeacherPageInfo_Condition_List" />
            <if test="record.subjectIdList != null">
                AND <foreach collection="record.subjectIdList" separator="or" item="item" open="(" close=")"> FIND_IN_SET(#{item}, t2.subject_id_) </foreach>
            </if>
        </where>
    </select>
    <!--活动老师信息-->

    <!--时间匹配活动老师信息-->
    <resultMap id="ActivityTeacherByTimePageInfoResultMap" type="com.yonge.cooleshow.biz.dal.vo.activity.ActivityTeacherWrapper">
        <result column="real_name_" jdbcType="BIGINT" property="realName" />
        <result column="username_" jdbcType="VARCHAR" property="username" />
        <result column="teacher_id" jdbcType="BIGINT" property="teacherId" />
    </resultMap>
    <sql id="ActivityTeacherByTimePageInfo_Column_List">
        t4.id_ AS teacher_id, t4.real_name_, t4.username_
    </sql>
    <select id="selectActivityTeacherByTimePageInfo" resultMap="ActivityTeacherByTimePageInfoResultMap">
        SELECT <include refid="ActivityTeacherByTimePageInfo_Column_List" />
        FROM activity_plan t1 JOIN activity_evaluation t2 ON t1.id_ = t2.activity_id_ LEFT JOIN activity_evaluation t3 ON (t3.music_sheet_id_ = t2.music_sheet_id_ AND t3.activity_id_ = #{record.activityId}) JOIN sys_user t4 ON (t2.music_sheet_id_ = t4.id_)
        <where>
            t3.id_ IS NOT NULL
            <if test="record.activityId != null">
                AND t2.activity_id_ != #{record.activityId}
            </if>
            <if test="record.lockFlag != null">
                AND t4.lock_flag_ = #{record.lockFlag}
            </if>
            <if test="record.deleteFlag != null">
                AND t4.del_flag_ = #{record.deleteFlag}
            </if>
            <if test="record.activityState != null">
                AND t1.activity_state_ = #{record.activityState}
            </if>
            <if test="record.startTime != null and record.endTime != null">
                AND ((unix_timestamp(t1.activity_start_) &lt;= #{record.startTime} AND #{record.startTime} &lt;= unix_timestamp(t1.activity_end_)) OR (unix_timestamp(t1.activity_start_) &lt;= #{record.endTime} AND #{record.endTime} &lt;= unix_timestamp(t1.activity_end_)))
            </if>
        </where>
    </select>
    <!--时间匹配活动老师信息-->

    <!--评测活动声部曲目最多分-->
    <select id="selectSubjectMusicHighestScoreStat"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper">
        SELECT t1.evaluation_id_ AS id, MAX(t1.score_) AS number FROM activity_evaluation_record t1
        <where>
            t1.score_ IS NOT NULL
            <if test="activityId != null">
                AND t1.activity_id_ = #{activityId}
            </if>
            <if test="userId != null">
                AND t1.user_id_ = #{userId}
            </if>
        </where>
        GROUP BY t1.evaluation_id_
    </select>
    <!--评测活动声部曲目最多分-->
</mapper>