<?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.ActivityPlanDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.ActivityPlan">
            <result column="id_" property="id" />
	        <result column="activity_type_" property="activityType" />
            <result column="activity_client_" property="activityClient" />
	        <result column="activity_name_" property="activityName" />
	        <result column="describe_" property="describe" />
	        <result column="rule_describe_" property="ruleDescribe" />
	        <result column="subject_url_" property="subjectUrl" />
	        <result column="background_url_" property="backgroundUrl" />
            <result column="activity_url_" property="activityUrl" />
            <result column="share_url_" property="shareUrl" />
            <result column="activity_start_" property="activityStart" />
	        <result column="activity_end_" property="activityEnd" />
	        <result column="registration_method_" property="registrationMethod" />
	        <result column="registration_price_" property="registrationPrice" />
            <result column="share_rate_" property="shareRate" />
            <result column="share_type_" property="shareType" />
            <result column="ranking_" property="ranking" />
            <result column="ranking_rule_" property="rankingRule" />
            <result column="ranking_method_" property="rankingMethod" />
            <result column="ranking_score_" property="rankingScore" />
	        <result column="draft_flag_" property="draftFlag" />
	        <result column="activity_state_" property="activityState" />
	        <result column="vip_card_id_" property="vipCardId" />
	        <result column="ext_config_" property="extConfig" />
	        <result column="buy_count_" property="buyCount" />
	        <result column="create_time_" property="createTime" />
	        <result column="create_by_" property="createBy" />
	        <result column="update_time_" property="updateTime" />
	        <result column="update_by_" property="updateBy" />
		</resultMap>  
    
    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as id
        , t.activity_type_ as activityType
        , t.activity_client_ as activityClient
        , t.activity_name_ as activityName
        , t.describe_ as `describe`
        , t.rule_describe_ as ruleDescribe
        , t.subject_url_ as subjectUrl
        , t.background_url_ as backgroundUrl
        , t.share_url_ as shareUrl
        , t.activity_url_ as activityUrl
        , t.activity_start_ as activityStart
        , t.activity_end_ as activityEnd
        , t.registration_method_ as registrationMethod
        , t.registration_price_ as registrationPrice
        , t.share_rate_ as shareRate
        , t.share_type_ as shareType
        , t.ranking_ as ranking
        , t.ranking_rule_ as rankingRule
        , t.ranking_method_ as rankingMethod
        , t.ranking_score_ as rankingScore
        , t.draft_flag_ as draftFlag
        , if(t.activity_state_ = 1, 1, 0) as activityState
        , t.vip_card_id_ as vipCardId
        , t.ext_config_ as extConfig
        , t.buy_count_ as buyCount
        , t.create_time_ as createTime
        , t.create_by_ as createBy
        , t.update_time_ as updateTime
        , t.update_by_ as updateBy
        </sql>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityPlanVo">
        SELECT
            <include refid="baseColumns"/>
        FROM activity_plan t
        where t.id_ = #{id}
    </select>
    
    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityPlanVo">
		SELECT         
        	<include refid="baseColumns" />
		    ,   u.username_ as updateByName
		FROM activity_plan t
        LEFT JOIN  sys_user u on t.update_by_ = u.id_
        <where>
            <if test="param.activityName != null and param.activityName != ''">
                and (t.activity_name_ like concat('%',#{param.activityName},'%') <if test="param.activityId != null"> OR t.id_ = #{param.activityId}</if> )
            </if>
            <if test="param.draftFlag != null">
                and t.draft_flag_ = #{param.draftFlag}
            </if>
            <choose>
                <when test="param.activityState != null">
                    <if test="param.activityState == 0">
                        and (t.activity_state_ = 0 or t.activity_state_ = 2)
                    </if>
                    <if test="param.activityState == 1">
                        and t.activity_state_ = 1
                    </if>
                </when>
                <otherwise> AND t.activity_start_ &gt;= -1 </otherwise>
            </choose>
            <if test="param.startTime !=null">
                and t.activity_start_ &gt;= #{param.startTime}
            </if>
            <if test="param.endTime !=null">
                and t.activity_end_ &lt;= #{param.endTime}
            </if>
		    <if test="param.activityClient != null">
                and t.activity_client_ = #{param.activityClient}
            </if>
            <if test="param.activityType != null">
                and t.activity_type_ = #{param.activityType}
            </if>
        </where>
        order by t.id_ desc
	</select>

	<select id="selectActivityPlanEvaluation" resultType="java.lang.String">
        select evaluation_difficulty_
        from activity_plan_evaluation
        where id_ = #{activityPlanId}
    </select>

    <select id="activityState" resultType="com.yonge.cooleshow.biz.dal.entity.ActivityPlan">
        select <include refid="baseColumns"/>
        from activity_plan t where draft_flag_ = 0
        and (
            (t.activity_start_ &lt;= now() and t.activity_end_ &gt; now() and activity_state_ = 0)
            or (t.activity_end_ &lt;= now() and activity_state_ = 1)
        )
    </select>

	<select id="selectActivityShare" resultType="com.yonge.cooleshow.biz.dal.vo.ActivityPlanVo">
        select
        <include refid="baseColumns"/>
        from activity_plan t
        left join activity_evaluation ae on t.id_ = ae.activity_id_
        <where>
            activity_state_ = 1 and activity_start_ &lt;= now() and activity_end_ &gt;= now() and activity_type_ = 'SHARE'
            <if test="type != null and type != ''">
                and #{type} = t.share_type_
            </if>
            <if test="userId != null">
                and ae.music_sheet_id_ = #{userId}
                and ae.resource_type_ = 'TEACHER'
            </if>
        </where>
    </select>

    <!--活动参与人数、获奖人数统计-->
    <select id="selectActivityParticipateStatInfo"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper">
        SELECT t1.activity_id_ AS id, COUNT(DISTINCT t1.id_) AS total FROM activity_registration t1
        <where>
            <if test="activityIds != null">
                AND t1.activity_id_ IN (<foreach collection="activityIds" separator="," item="item">#{item}</foreach>)
            </if>
        </where>
        GROUP BY t1.activity_id_
    </select>

    <select id="selectActivityWinnerStatInfo"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper">
        SELECT t1.activity_id_ AS id, COUNT(DISTINCT t1.user_id_) AS total FROM activity_user_reward t1
        <where>
            <if test="activityIds != null">
                AND t1.activity_id_ IN (<foreach collection="activityIds" separator="," item="item">#{item}</foreach>)
            </if>
        </where>
        GROUP BY t1.activity_id_
    </select>
    <!--活动参与人数、获奖人数统计-->

    <!--活动总分用户排名-->
    <select id="selectActivityHighestScoreRankingInfo"
            resultType="com.yonge.cooleshow.biz.dal.vo.ActivityRankingVo">
        SELECT
            t1.score_, t1.user_id_, t1.resource_id_, t1.create_time_ AS joinDate, t1.times_, t2.avatar_ AS userAvatar, t2.username_ AS username
        FROM
            activity_evaluation_record t1 LEFT JOIN sys_user t2 ON t1.user_id_ = t2.id_
        WHERE
            t1.activity_id_ = #{activityId} AND t1.ranking_method_ = 'TOTAL_SCORE' AND t1.score_ >= #{rankingScore}
            <if test="subjectId != null">
                AND t1.resource_id_ = #{subjectId}
            </if>
            <if test="subjectId == null">
                AND t1.user_id_ NOT IN (SELECT t3.user_id_ FROM activity_user_reward t3 WHERE t3.activity_id_ = #{activityId})
            </if>
        ORDER BY
            t1.score_ DESC,
            t1.registration_time_ ASC
        LIMIT #{limit}
    </select>

    <select id="selectActivityHighestScoreUserRanking"
            resultType="com.yonge.cooleshow.biz.dal.vo.ActivityRankingVo">
        SELECT
            t1.score_, t1.user_id_, t1.resource_id_, t1.create_time_ AS joinDate, t1.times_, t2.avatar_ AS userAvatar, t2.username_ AS username
        FROM
            activity_evaluation_record t1 LEFT JOIN sys_user t2 ON t1.user_id_ = t2.id_
        WHERE
            t1.activity_id_ = #{activityId} AND t1.ranking_method_ = 'TOTAL_SCORE' AND t1.score_ >= #{rankingScore}
            <if test="subjectId != null">
                AND t1.resource_id_ = #{subjectId}
            </if>
            <if test="userId != null">
                AND t1.user_id_ = #{userId}
            </if>
        ORDER BY
            t1.score_ DESC,
            t1.registration_time_ ASC
        LIMIT 1
    </select>
    <!--活动总分用户排名-->
</mapper>