<?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.CourseScheduleEvaluateDao">
    <resultMap id="CourseScheduleEvaluate" type="com.ym.mec.biz.dal.entity.CourseScheduleEvaluate">
        <id column="id_" jdbcType="BIGINT" property="id"/>
        <result column="music_group_id_" jdbcType="VARCHAR" property="musicGroupId"/>
        <result column="class_group_id_" jdbcType="INTEGER" property="classGroupId"/>
        <result column="course_schedule_id_" jdbcType="BIGINT" property="courseScheduleId"/>
        <result column="teacher_id_" jdbcType="INTEGER" property="teacherId"/>
        <result column="item_" jdbcType="VARCHAR" property="item"/>
        <result column="comment_" jdbcType="LONGVARCHAR" property="comment"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="student_id_" jdbcType="INTEGER" property="studentId"/>
        <result column="student_avatar_" property="studentAvatar"/>
        <result column="subject_id_" jdbcType="INTEGER" property="subjectId"/>
        <result column="subject_name_" jdbcType="VARCHAR" property="subjectName"/>
        <result column="is_pushed_" jdbcType="INTEGER" property="isPushed"/>
        <result column="month_" jdbcType="VARCHAR" property="month"/>
        <result column="music_theory_" jdbcType="VARCHAR" property="musicTheory"/>
        <result column="song_" jdbcType="VARCHAR" property="song"/>
        <result column="teaching_material_" jdbcType="VARCHAR" property="teachingMaterial"/>
        <result column="times_" jdbcType="INTEGER" property="times"/>
        <result column="total_minutes_" jdbcType="INTEGER" property="totalMinutes"/>
        <result column="version_" jdbcType="INTEGER" property="version"/>
        <result column="student_id_list_" jdbcType="VARCHAR" property="studentIdList"/>
        <result column="group_name_" jdbcType="VARCHAR" property="groupName"/>
        <result column="status_" jdbcType="TINYINT" property="status"/>
    </resultMap>
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleEvaluate" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO course_schedule_evaluate (music_group_id_, class_group_id_, course_schedule_id_, music_theory_,
                                              song_, times_, total_minutes_,
                                              teaching_material_, teacher_id_, item_, comment_, create_time_,
                                              update_time_,
                                              student_id_list_, version_, status_)
        values (#{musicGroupId,jdbcType=VARCHAR}, #{classGroupId,jdbcType=INTEGER}, #{courseScheduleId,jdbcType=BIGINT},
                #{musicTheory,jdbcType=VARCHAR}, #{song,jdbcType=VARCHAR}, #{times,jdbcType=INTEGER}, #{totalMinutes},
                #{teachingMaterial,jdbcType=VARCHAR},
                #{teacherId,jdbcType=INTEGER}, #{item,jdbcType=VARCHAR}, #{comment,jdbcType=LONGVARCHAR},
                #{createTime,jdbcType=TIMESTAMP},
                #{updateTime,jdbcType=TIMESTAMP}, #{studentIdList}, #{version,jdbcType=INTEGER},
                #{status,jdbcType=TINYINT})
    </insert>

    <update id="update" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleEvaluate">
        update course_schedule_evaluate
        <set>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="classGroupId != null">
                class_group_id_ = #{classGroupId},
            </if>
            <if test="courseScheduleId != null">
                course_schedule_id_ = #{courseScheduleId},
            </if>
            <if test="musicTheory != null">
                music_theory_ = #{musicTheory},
            </if>
            <if test="song != null">
                song_ = #{song},
            </if>
            <if test="teachingMaterial != null">
                teaching_material_ = #{teachingMaterial},
            </if>
            <if test="item != null">
                item_ = #{item},
            </if>
            <if test="comment != null">
                comment_ = #{comment},
            </if>
            <if test="classGroupId != null">
                class_group_id_ = #{classGroupId},
            </if>
            <if test="studentIdList != null">
                student_id_list_ = #{studentIdList},
            </if>
            <if test="teacherId != null">
                teacher_id_ = #{teacherId},
            </if>
            <if test="version != null != null">
                version_ = #{version},
            </if>
            <if test="status != null">
                status_ = #{status},
            </if>
            <if test="isPushed != null">
                is_pushed_ = #{isPushed},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime}
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <select id="get" resultMap="CourseScheduleEvaluate">
        SELECT *
        FROM course_schedule_evaluate
        WHERE id_ = #{id}
    </select>

    <select id="findByClassGroupId" resultMap="CourseScheduleEvaluate">
        SELECT cse.*, s.name_ subject_name_, pg.student_id_
        FROM course_schedule_evaluate cse
                 LEFT JOIN practice_group pg ON pg.id_ = cse.music_group_id_
                 LEFT JOIN subject s on s.id_ = pg.subject_id_
        WHERE cse.class_group_id_ = #{classGroupId}
          AND status_ = 1
          AND version_ = 1
        ORDER BY create_time_ DESC
        LIMIT 1
    </select>

    <select id="findByClassGroupIds" resultMap="CourseScheduleEvaluate">
        SELECT * FROM course_schedule_evaluate WHERE class_group_id_ IN
        <foreach collection="classGroupIds" item="classGroupId" separator="," open="(" close=")">
            #{classGroupId}
        </foreach>
        AND status_=1
        ORDER BY create_time_ ASC
    </select>

    <resultMap id="PracticeGroupsOrgan" type="com.ym.mec.biz.dal.dto.PracticeGroupsDto">
        <result column="organ_id_" jdbcType="INTEGER" property="organId"/>
        <result column="total_nums_" jdbcType="INTEGER" property="totalNums"/>
        <result column="buy_nums_" jdbcType="INTEGER" property="buyNums"/>
        <result column="renew_nums_" jdbcType="INTEGER" property="renewNums"/>
        <result column="report_nums_" jdbcType="INTEGER" property="reportNums"/>
        <result column="total_money_" jdbcType="DECIMAL" property="totalMoney"/>
    </resultMap>

    <!-- 体验人数 -->
    <select id="getPracticeGroupsFree" resultMap="PracticeGroupsOrgan">
        SELECT organ_id_, count(distinct student_id_) total_nums_
        FROM practice_group
        WHERE group_status_ != 'LOCK'
          AND group_status_ != 'CANCEL'
          AND type_='FREE'
        GROUP BY organ_id_
    </select>

    <!-- 已购买人数 -->
    <select id="getPracticeGroupsBuy" resultMap="PracticeGroupsOrgan">
        SELECT organ_id_, count(distinct student_id_) buy_nums_
        FROM practice_group
        WHERE group_status_ != 'LOCK'
          AND group_status_ != 'CANCEL'
          AND buy_months_ >= 1
        GROUP BY organ_id_
    </select>

    <!-- 报告完成数 -->
    <select id="getHasReportNums" resultMap="PracticeGroupsOrgan">
        SELECT pg.organ_id_, count(*) report_nums_
        FROM course_schedule_evaluate cse
                 LEFT JOIN practice_group pg on cse.music_group_id_ = pg.id_
            AND pg.type_='FREE'
            AND pg.group_status_ != 'LOCK'
            AND cse.status_ = 1
        GROUP BY pg.organ_id_
    </select>

    <!-- 已完成课程数统计 -->
    <select id="getOrganPracticeGroups" resultMap="PracticeGroupsOrgan">
        SELECT COUNT(class_group_id_) buy_nums_, cg.total_class_times_ total_nums_, pg.organ_id_
        FROM course_schedule cs
                 LEFT JOIN class_group cg on cg.id_ = cs.class_group_id_
                 LEFT JOIN practice_group pg ON cs.music_group_id_ = pg.id_
        WHERE cs.group_type_ = 'PRACTICE'
          AND cs.status_ != 'NOT_START'
          AND cs.del_flag_ = 0
          AND pg.type_='FREE'
          AND pg.group_status_ != 'LOCK'
        GROUP BY cs.class_group_id_, pg.organ_id_
    </select>

    <!-- 成交金额 -->
    <select id="getOrganMoney" resultMap="PracticeGroupsOrgan">
        SELECT SUM(actual_amount_) total_money_, organ_id_
        FROM student_payment_order
        WHERE group_type_ = 'PRACTICE'
          AND status_ = 'SUCCESS'
        GROUP BY organ_id_
    </select>
    <select id="findExpiredDateBeforeReport" resultMap="CourseScheduleEvaluate">
        SELECT *
        FROM course_schedule_evaluate
        WHERE status_ = 1
          AND create_time_ &lt;= #{expiredDate}
          AND (is_pushed_ = 0 OR is_pushed_ IS NULL)
    </select>

    <select id="findByGroupId" resultMap="CourseScheduleEvaluate">
        SELECT id_,status_,class_group_id_,version_,times_,total_minutes_, DATE_FORMAT(create_time_, '%Y年%m月') month_
        FROM course_schedule_evaluate
        WHERE music_group_id_ = #{groupId}
        <if test="status != null">
            AND status_ = #{status}
        </if>
        ORDER BY create_time_ ASC
    </select>

    <select id="findById" resultMap="CourseScheduleEvaluate">
        SELECT cse.*, s.name_ subject_name_, pg.student_id_
        FROM course_schedule_evaluate cse
                 LEFT JOIN practice_group pg ON pg.id_ = cse.music_group_id_
                 LEFT JOIN subject s on s.id_ = pg.subject_id_
        WHERE cse.id_ = #{id}
    </select>

    <select id="findByCourseAndTeacher" resultMap="CourseScheduleEvaluate">
        SELECT *
        FROM course_schedule_evaluate cse
        WHERE cse.teacher_id_ = #{teacherId}
          AND cse.course_schedule_id_ = #{courseScheduleId}
    </select>

    <select id="getNeedPostReports" resultMap="CourseScheduleEvaluate">
        SELECT cse.id_,cse.class_group_id_,DATE_FORMAT(cse.create_time_,'%Y年%m月') month_,pg.name_
        group_name_,cse.teacher_id_,pg.student_id_,su.avatar_ student_avatar_ FROM course_schedule_evaluate cse
        LEFT JOIN practice_group pg on cse.music_group_id_ = pg.id_
        LEFT JOIN sys_user su ON pg.student_id_=su.id_
        WHERE status_= 0
        <if test="teacherId != null">
            AND cse.teacher_id_ = #{teacherId}
        </if>
        ORDER BY cse.create_time_ ASC
    </select>

    <insert id="batchAdd" parameterType="java.util.List" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO course_schedule_evaluate (music_group_id_, class_group_id_, course_schedule_id_, music_theory_,
        song_,teaching_material_, teacher_id_, item_, comment_, create_time_,update_time_,student_id_list_, version_,
        status_,
        times_,total_minutes_)
        VALUE
        <foreach collection="list" item="evaluate" separator=",">
            (#{evaluate.musicGroupId,jdbcType=VARCHAR}, #{evaluate.classGroupId,jdbcType=INTEGER},
            #{evaluate.courseScheduleId,jdbcType=BIGINT},
            #{evaluate.musicTheory,jdbcType=VARCHAR}, #{evaluate.song,jdbcType=VARCHAR},
            #{evaluate.teachingMaterial,jdbcType=VARCHAR},
            #{evaluate.teacherId,jdbcType=INTEGER}, #{evaluate.item,jdbcType=VARCHAR},
            #{evaluate.comment,jdbcType=LONGVARCHAR}, #{evaluate.createTime,jdbcType=LONGVARCHAR},
            #{evaluate.updateTime,jdbcType=LONGVARCHAR}, #{evaluate.studentIdList},
            #{evaluate.version,jdbcType=INTEGER}, #{evaluate.status,jdbcType=TINYINT},
            #{evaluate.times,jdbcType=INTEGER},#{evaluate.totalMinutes,jdbcType=INTEGER})
        </foreach>
    </insert>

    <select id="getClassGroupCourseTimes" resultType="int">
        SELECT COUNT(*)
        FROM course_schedule
        WHERE class_group_id_ = #{classGroupId}
          AND (del_flag_ = 0 OR del_flag_ IS NULL)
    </select>

    <select id="hasReportList" resultMap="CourseScheduleEvaluate">
        SELECT COUNT(*) times_, class_group_id_ FROM course_schedule_evaluate WHERE class_group_id_ IN
        <foreach collection="classGroupIds" item="classGroupId" open="(" separator="," close=")">
            #{classGroupId}
        </foreach>
        AND DATE_FORMAT(create_time_,'%Y%m%d') = DATE_FORMAT(#{nowDate},'%Y%m%d')
        GROUP BY class_group_id_;
    </select>

    <select id="getPracticeGroupsRenew" resultMap="PracticeGroupsOrgan">
        SELECT organ_id_, count(distinct student_id_) renew_nums_
        FROM practice_group
        WHERE group_status_ != 'LOCK'
          AND group_status_ != 'CANCEL'
          AND buy_months_ >= 1
          AND be_renew_group_id_ >= 1
        GROUP BY organ_id_
    </select>

    <!-- 获取当月成交金额，续费人数（新增人数） -->
    <select id="getMonthOrganMoney" resultMap="PracticeGroupsOrgan">
        SELECT SUM(actual_amount_) total_money_, count(distinct user_id_) buy_nums_, organ_id_
        FROM student_payment_order
        WHERE type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
          AND create_time_ >= #{startTime}
          AND status_ = 'SUCCESS'
        GROUP BY organ_id_
    </select>

    <select id="getOrganNewPracticeGroupsOfMonth" resultMap="PracticeGroupsOrgan">
        SELECT SUM(actual_amount_) total_money_, count(distinct user_id_) buy_nums_, organ_id_
        FROM student_payment_order
        WHERE type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        AND status_ = 'SUCCESS'
        AND create_time_ >= #{startTime}
        AND user_id_ NOT IN (
        SELECT distinct student_id_ FROM practice_group
        WHERE group_status_ != 'LOCK'
        AND group_status_ != 'CANCEL'
        AND buy_months_ >= 1
        <if test="type==@com.ym.mec.biz.dal.enums.OrderTypeEnum@PRACTICE_GROUP_BUY">
            <![CDATA[ AND be_renew_group_id_ IS NULL AND create_time_ < #{startTime} ]]>
        </if>
        <if test="type==@com.ym.mec.biz.dal.enums.OrderTypeEnum@PRACTICE_GROUP_RENEW">
            <![CDATA[ AND be_renew_group_id_ >= 1 AND create_time_ < #{startTime} ]]>
        </if>
        )
        GROUP BY organ_id_
    </select>

    <resultMap id="practice4Organ" type="com.ym.mec.biz.dal.dto.Practice4OrganDto">
        <result column="organ_name_" property="organName"/>
        <result column="organ_id_" property="organId"/>
        <result column="try_num_" property="tryNum"/>
        <result column="vip_num_" property="vipNum"/>
        <result column="practice_num_" property="practiceNum"/>
        <result column="vip4_month_num_" property="vip4MonthNum"/>
        <result column="practice4_month_num_" property="practice4MonthNum"/>
        <result column="renew4_month_num_" property="renew4MonthNum"/>
        <result column="change4_month_num_" property="change4MonthNum"/>
        <result column="total_money_" property="totalMoney"/>
        <result column="student_ids_" property="studentIds"/>
    </resultMap>

    <!-- 体验人数（除去禁止的） -->
    <select id="getTryNums" resultMap="practice4Organ">
        SELECT pg.organ_id_, count(distinct pg.student_id_) try_num_
        FROM practice_group pg
                 LEFT JOIN student s ON s.user_id_ = pg.student_id_
        WHERE pg.group_status_ IN ('NORMAL', 'FINISH')
          AND s.operating_tag_ = 1
          AND pg.type_='FREE'
        GROUP BY pg.organ_id_
    </select>

    <!-- 网管课体验人数（禁止的） -->
    <select id="getPracticeTryNums" resultMap="practice4Organ">
        SELECT pg.organ_id_, count(distinct pg.student_id_) try_num_
        FROM practice_group pg
                 LEFT JOIN student s ON s.user_id_ = pg.student_id_
        WHERE pg.group_status_ IN ('NORMAL', 'FINISH')
          AND pg.buy_months_ >= 1
          AND s.operating_tag_ = 0
        GROUP BY pg.organ_id_
    </select>

    <!-- vip体验人数（禁止的） -->
    <select id="getVipTryNums" resultMap="practice4Organ">
        SELECT vg.organ_id_, COUNT(DISTINCT cssp.user_id_) try_num_
        FROM course_schedule_student_payment cssp
                 LEFT JOIN vip_group vg ON cssp.music_group_id_ = vg.id_ AND cssp.group_type_ = 'VIP'
                 LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_ AND cs.teach_mode_ = 'ONLINE'
                 LEFT JOIN student s ON s.user_id_ = cssp.user_id_
        WHERE cs.id_ >= 1
          AND vg.id_ >= 1
          AND (cs.is_lock_ IS NULL OR cs.is_lock_ = 0)
          AND s.operating_tag_ = 0
        GROUP BY vg.organ_id_
    </select>

    <select id="getPracticeAndVipTryNums" resultMap="practice4Organ">
        SELECT pg.organ_id_, COUNT(DISTINCT pg.student_id_) try_num_
        FROM practice_group pg
                 LEFT JOIN course_schedule_student_payment cssp
                           ON cssp.user_id_ = pg.student_id_ AND cssp.group_type_ = 'VIP'
                 LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_ AND cs.teach_mode_ = 'ONLINE'
                 LEFT JOIN student s ON s.user_id_ = cssp.user_id_
        WHERE pg.group_status_ IN ('NORMAL', 'FINISH')
          AND pg.buy_months_ >= 1
          AND cs.id_ >= 1
          AND (cs.is_lock_ IS NULL OR cs.is_lock_ = 0)
          AND s.operating_tag_ = 0
        GROUP BY pg.organ_id_
    </select>

    <select id="getVipBuyNums" resultMap="practice4Organ">
        SELECT su.organ_id_,COUNT(DISTINCT cssp.user_id_) vip_num_
        FROM course_schedule_student_payment cssp
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_ AND cssp.group_type_='VIP'
        left JOIN sys_user su ON su.id_=cssp.user_id_
        WHERE CONCAT(cs.class_date_, ' ', cs.end_class_time_) >= #{classDate}
        <if test="endDate != null">
            <![CDATA[AND CONCAT(cs.class_date_, ' ', cs.end_class_time_) < #{endDate}]]>
        </if>
        AND cs.teach_mode_ = 'ONLINE'
        AND (cs.is_lock_ IS NULL OR cs.is_lock_=0)
        AND (cs.del_flag_ IS NULL OR cs.del_flag_=0)
        GROUP BY su.organ_id_
    </select>

    <select id="getPracticeBuyNums" resultMap="practice4Organ">
        SELECT pg.organ_id_, COUNT(DISTINCT cssp.user_id_) practice_num_
        FROM course_schedule_student_payment cssp
        LEFT JOIN practice_group pg ON cssp.music_group_id_ = pg.id_ AND cssp.group_type_ = 'PRACTICE'
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
        WHERE pg.id_ >= 1
        AND pg.group_status_ IN ('NORMAL', 'FINISH')
        AND cs.id_ >=1
        AND (cs.is_lock_ IS NULL OR cs.is_lock_=0)
        AND (cs.del_flag_ IS NULL OR cs.del_flag_=0)
        AND CONCAT(cs.class_date_, ' ', cs.end_class_time_) >= #{classDate}
        <if test="endDate != null">
            <![CDATA[AND CONCAT(cs.class_date_, ' ', cs.end_class_time_) < #{endDate}]]>
        </if>
        AND pg.buy_months_ >= 1
        GROUP BY pg.organ_id_
    </select>

    <select id="getPracticeAndVipNums" resultMap="practice4Organ">
        SELECT pg.organ_id_,COUNT(DISTINCT pg.student_id_) practice_num_
        FROM practice_group pg
        LEFT JOIN course_schedule_student_payment vcssp ON vcssp.user_id_ = pg.student_id_ AND vcssp.group_type_='VIP'
        LEFT JOIN course_schedule_student_payment pcssp ON pcssp.user_id_ = pg.student_id_ AND
        pcssp.group_type_='PRACTICE'
        LEFT JOIN course_schedule vcs ON vcs.id_=vcssp.course_schedule_id_ AND vcs.teach_mode_='ONLINE'
        LEFT JOIN course_schedule pcs ON pcs.id_=pcssp.course_schedule_id_
        WHERE pg.group_status_ IN ('NORMAL', 'FINISH')
        AND vcs.id_ >=1
        AND pcs.id_ >= 1
        AND (vcs.is_lock_ IS NULL OR vcs.is_lock_=0)
        AND (vcs.del_flag_ IS NULL OR vcs.del_flag_=0)
        AND (pcs.is_lock_ IS NULL OR pcs.is_lock_=0)
        AND (pcs.del_flag_ IS NULL OR pcs.del_flag_=0)
        AND CONCAT(vcs.class_date_, ' ', vcs.end_class_time_) >= #{classDate}
        AND CONCAT(pcs.class_date_, ' ', pcs.end_class_time_) >= #{classDate}
        <if test="endDate != null">
            <![CDATA[AND CONCAT(vcs.class_date_, ' ', vcs.end_class_time_) < #{endDate}
            AND CONCAT(pcs.class_date_, ' ', pcs.end_class_time_) < #{endDate}]]>
        </if>
        AND pg.buy_months_ >= 1
        GROUP BY pg.organ_id_
    </select>

    <!-- 获取所有体验人数 -->
    <select id="getAllTryNums" resultMap="practice4Organ">
        SELECT su.organ_id_, COUNT(s.user_id_) try_num_
        FROM student s
                 LEFT JOIN sys_user su ON su.id_ = s.user_id_
        WHERE s.operating_tag_ = 1
          AND su.organ_id_ > 0
          AND su.user_type_ LIKE '%STUDENT%'
        GROUP BY su.organ_id_
    </select>
</mapper>
