<?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="student_id_" jdbcType="INTEGER" property="studentId"/>
        <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="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_,
                                              teaching_material_, teacher_id_, item_, comment_, create_time_,
                                              student_id_list_, version_, status_)
        values (#{musicGroupId,jdbcType=VARCHAR}, #{classGroupId,jdbcType=INTEGER}, #{courseScheduleId,jdbcType=BIGINT},
                #{musicTheory,jdbcType=VARCHAR}, #{song,jdbcType=VARCHAR}, #{teachingMaterial,jdbcType=VARCHAR},
                #{teacherId,jdbcType=INTEGER}, #{item,jdbcType=VARCHAR}, #{comment,jdbcType=LONGVARCHAR}, NOW(),
                #{studentIdList}, #{version,jdbcType=INTEGER}, #{status,jdbcType=TINYINT})
    </insert>

    <update id="update" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleEvaluate">
        update course_schedule_evaluate
        set is_pushed_=#{isPushed}
        where id_ = #{id}
    </update>

    <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}
        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>
    </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="report_nums_" jdbcType="INTEGER" property="reportNums"/>
        <result column="total_money_" jdbcType="DECIMAL" property="totalMoney"/>
    </resultMap>

    <!-- 体验人数 -->
    <select id="getPracticeGroupsFree" resultMap="PracticeGroupsOrgan">
        SELECT pg.organ_id_, count(*) total_nums_
        FROM class_group cg
                 LEFT JOIN practice_group pg on cg.music_group_id_ = pg.id_
        WHERE cg.group_type_ = 'PRACTICE'
          AND pg.buy_months_ IS NULL
          AND pg.group_status_ != 'LOCK'
        GROUP BY pg.organ_id_
    </select>

    <!-- 已购买人数 -->
    <select id="getPracticeGroupsBuy" resultMap="PracticeGroupsOrgan">
        SELECT pg.organ_id_, count(*) buy_nums_
        FROM class_group cg
                 LEFT JOIN practice_group pg on cg.music_group_id_ = pg.id_
        WHERE cg.group_type_ = 'PRACTICE'
          AND pg.buy_months_ >= 1
          AND pg.group_status_ != 'LOCK'
        GROUP BY pg.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.buy_months_ IS NULL
            AND pg.group_status_ != 'LOCK'
        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.buy_months_ IS NULL
          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 create_time_ &lt;= #{expiredDate}
          AND (is_pushed_ = 0 OR is_pushed_ IS NULL)
    </select>

    <select id="findByGroupId" resultMap="CourseScheduleEvaluate">
        SELECT id_, DATE_FORMAT(create_time_, '%Y年%m月') month_
        FROM course_schedule_evaluate
        WHERE music_group_id_ = #{groupId}
        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_,DATE_FORMAT(cse.create_time_,'%Y年%m月') month_,pg.name_ group_name_ FROM course_schedule_evaluate cse
        LEFT JOIN practice_group pg on cse.music_group_id_ = pg.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_,student_id_list_, version_, status_)
        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.studentIdList}, #{evaluate.version,jdbcType=INTEGER}, #{evaluate.status,jdbcType=TINYINT})
        </foreach>
    </insert>
</mapper>
