<?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.ym.mec.biz.dal.dao.TempLittleArtistTrainingCampDao">
    <resultMap id="BaseResultMap" type="com.ym.mec.biz.dal.entity.TempLittleArtistTrainingCamp">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="name_" jdbcType="VARCHAR" property="name"/>
        <result column="num_" jdbcType="INTEGER" property="num"/>
        <result column="apply_start_date_" jdbcType="TIMESTAMP" property="applyStartDate"/>
        <result column="apply_end_date_" jdbcType="TIMESTAMP" property="applyEndDate"/>
        <result column="train_start_date_" jdbcType="TIMESTAMP" property="trainStartDate"/>
        <result column="train_end_date_" jdbcType="TIMESTAMP" property="trainEndDate"/>
        <result column="state_" jdbcType="VARCHAR" property="state"/>
        <result column="im_group_ids_" jdbcType="VARCHAR" property="imGroupIds"/>
        <result column="pic_url_" jdbcType="VARCHAR" property="picUrl"/>
        <result column="del_flag_" jdbcType="INTEGER" property="delFlag"/>
        <result column="create_by_" jdbcType="INTEGER" property="createBy"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_by_" jdbcType="INTEGER" property="updateBy"/>
        <result column="update_time_" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_
        , name_, num_, apply_start_date_, apply_end_date_, train_start_date_, train_end_date_, state_,pic_url_, im_group_ids_, create_by_, create_time_, update_by_, update_time_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.ym.mec.biz.dal.entity.TempLittleArtistTrainingCamp">
        insert into temp_little_artist_training_camp(name_, num_, apply_start_date_, apply_end_date_, train_start_date_,
        train_end_date_, state_, im_group_ids_,pic_url_, del_flag_,create_by_, create_time_, update_by_, update_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.name}, #{entity.num}, #{entity.applyStartDate}, #{entity.applyEndDate}, #{entity.trainStartDate},
            #{entity.trainEndDate}, #{entity.state}, #{entity.imGroupIds}, #{entity.delFlag},#{entity.picUrl},#{entity.createBy}, #{entity.createTime},
            #{entity.updateBy}, #{entity.updateTime})
        </foreach>
    </insert>

    <select id="queryUserTrainingTime" resultType="com.ym.mec.biz.dal.vo.TempUserTrainingTimeDetailVo" parameterType="java.util.Map">
        SELECT
        a.user_id_        AS userId,
        a.create_time_ AS trainingDate,
        sum(a.play_time_) AS playTime
        FROM
        ( SELECT
              user_id_,
              ifnull(play_time_, 0) AS play_time_,
              date_format(create_time_, '%Y-%m-%d') AS create_time_
        FROM sys_music_compare_record
        <where>
            <if test="param.startTime != null">
                AND create_time_ &gt;= #{param.startTime}
            </if>
            <if test="param.endTime != null">
                AND create_time_ &lt;= #{param.endTime}
            </if>
            <if test="param.userId != null">
                AND user_id_ = #{param.userId}
            </if>
            <if test="param.userIdList !=null and param.userIdList.size()>0">
                AND user_id_ IN
                <foreach collection="param.userIdList" item="userId" open="(" close=")" separator=",">
                    #{userId}
                </foreach>
            </if>
        </where>
        ) AS a
        GROUP BY a.user_id_, a.create_time_
    </select>

    <select id="queryAllUserId" resultType="java.lang.Integer">
        SELECT
        distinct user_id_
        FROM
            temp_little_artist_training_camp_user_relation
       </select>

    <select id="queryCampUser" resultType="com.ym.mec.biz.dal.vo.TempCampUserVo">
        select
        b.id_ as id,
        b.username_ as username,
        b.phone_ as phone,
        a.state_ as state,
        c.name_ as campName
        from temp_little_artist_training_camp_user_relation as a
        left join sys_user as b on a.user_id_ = b.id_
        left join temp_little_artist_training_camp as c on a.activity_id_ = c.id_
        <where>
            <if test="param.state != null ">
                AND a.state_ = #{param.state}
            </if>
            <if test="param.search != null ">
                AND (
                b.`id_` LIKE CONCAT('%', #{param.search},'%')
                OR b.`username_` LIKE CONCAT('%', #{param.search},'%')
                OR b.`phone_` LIKE CONCAT('%', #{param.search},'%')
                )
            </if>
        </where>
    </select>

    <select id="queryUserTrainingDetail" parameterType="java.util.Map" resultType="com.ym.mec.biz.dal.vo.TempCampUserTrainingDetailVo">
     SELECT a.user_id_ as id,
        d.username_ as username,
        d.phone_ as phone,
        e.name_ as imGroupName,
        ifnull(b.playDay, 0) as playDay,
        ifnull(c.playTime, 0) as playTime
        FROM `temp_little_artist_training_camp_user_relation` `a`
        LEFT JOIN (
        SELECT `a`.`user_id_`, COUNT(`playDay`) AS `playDay`
        FROM (
        SELECT `cr`.`user_id_`, DATE(`cr`.`create_time_`) AS `playDay`
        FROM `sys_music_compare_record` `cr`
        WHERE `cr`.`create_time_` &gt;= DATE_ADD(DATE(#{param.startTime}), INTERVAL IF(#{param.startTime} = DATE(#{param.startTime}), 0, 1) DAY)
        AND `cr`.`create_time_` &lt; DATE_ADD(DATE(#{param.endTime}), INTERVAL 1 DAY)
        GROUP BY `cr`.`user_id_`, DATE(`cr`.`create_time_`)
        HAVING SUM(`cr`.`play_time_`) > 1200
        ) `a`
        GROUP BY `a`.`user_id_`
        ) `b`
        ON `a`.`user_id_` = `b`.`user_id_`
        LEFT JOIN (
        SELECT `cr`.`user_id_`, SUM(`cr`.`play_time_`) AS `playTime`
        FROM `sys_music_compare_record` `cr`
        WHERE `cr`.`create_time_` &gt;= DATE_ADD(DATE(#{param.startTime}), INTERVAL IF(#{param.startTime} = DATE(#{param.startTime}), 0, 1) DAY)
        AND `cr`.`create_time_` &lt; DATE_ADD(DATE(#{param.endTime}), INTERVAL 1 DAY)
        GROUP BY `cr`.`user_id_`
        ) `c`
        ON `a`.`user_id_` = `c`.`user_id_`
        LEFT JOIN `sys_user` `d` ON `d`.`id_` = `a`.`user_id_`
        LEFT JOIN `im_group` `e` ON `a`.`im_group_id_` = `e`.`id_`
        <where>
            a.activity_id_ = #{param.campId} and a.state_ = #{param.state}
            <if test="param.imGroupId != null">
                AND e.id_ = #{param.imGroupId}
            </if>
            <if test="param.search != null ">
                AND (
                d.`id_` LIKE CONCAT('%', #{param.search},'%')
                OR d.`username_` LIKE CONCAT('%', #{param.search},'%')
                OR d.`phone_` LIKE CONCAT('%', #{param.search},'%')
                )
            </if>
        </where>
    </select>

    <select id="queryCampImGroup" resultType="com.ym.mec.biz.dal.entity.ImGroup">
        select id_ as id,
               name_ as name
        from im_group
        where find_in_set(id_, #{imGroupIds})
    </select>

    <select id="queryUserTrainingPlayTime"  resultType="int" parameterType="java.util.Map">
        SELECT ifnull(sum(`play_time_`), 0) as playTime
        FROM `sys_music_compare_record`
        WHERE user_id_ = #{param.userId}
          and create_time_ &gt;= #{param.startTime}
          AND create_time_ &lt;= #{param.endTime}
    </select>

    <select id="queryUserGroup" resultType="int" parameterType="int">
        <!--有乐团的用户-->
        <!--select a.user_id_ as userId, a.sortDate, b.subject_id_list_-->
        select a.user_id_ as userId
        from (select a.user_id_, c.bill_start_date_ as sortDate
              from (select a.activity_id_, a.user_id_, b.music_group_id_, a.state_
                    from temp_little_artist_training_camp_user_relation as a
                             left join student_registration as b on a.user_id_ = b.user_id_
                    where a.state_ = 'APPLY'
                      and a.activity_id_ = #{campId}
                      and a.im_group_id_ is null
                    group by b.user_id_
                    having min(b.id_)) as a
                       left join music_group as c on a.music_group_id_ = c.id_
              union all
              <!--没有乐团的用户-取第一次上课时间-->
              select a.user_id_, min(d.class_date_) as sortDate
              from temp_little_artist_training_camp_user_relation as a
                  left join student_registration as b on a.user_id_ = b.user_id_
                  left join course_schedule_student_payment as c on a.user_id_ = c.user_id_
                  left join course_schedule as d on c.course_schedule_id_ = d.id_
              where b.id_ is null
                and a.state_ = 'APPLY'
                and a.activity_id_ = #{campId}
                and a.im_group_id_ is null
              group by a.user_id_
              having min(d.class_date_)
              union all
            <!-- 没有乐团-没有上课的用户 -->
              select a.user_id_, null as sortDate
              from temp_little_artist_training_camp_user_relation as a
                  left join student_registration as b on a.user_id_ = b.user_id_
                  left join course_schedule_student_payment as c on a.user_id_ = c.user_id_
                  left join course_schedule as d on c.course_schedule_id_ = d.id_
              where b.id_ is null
                and c.id_ is null
                and a.state_ = 'APPLY'
                and a.activity_id_ = #{campId}
                and a.im_group_id_ is null
              group by a.user_id_) as a
                 left join student as b on a.user_id_ = b.user_id_
        order by a.sortDate desc, b.subject_id_list_
    </select>

    <select id="queryStartTrainInfo" parameterType="string" resultType="com.ym.mec.biz.dal.vo.BaseVo">
        select
            b.user_id_ as id,
            c.name_ as `name`
        from temp_little_artist_training_camp as a
                 left join temp_little_artist_training_camp_user_relation as b on a.id_ = b.activity_id_
                 left join im_group as c on b.im_group_id_ = c.id_
        where a.train_start_date_ = #{date}
          and a.state_ = 'ING'
          and b.im_group_id_ is not null
    </select>

    <update id="updateUserNum" parameterType="object">
        update temp_little_artist_training_camp
        set num_ = num_ + 1
        where id_ = #{campId}
        and num_ &lt; #{maxNum}
    </update>

</mapper>
