<?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.CourseScheduleReviewDao">
    <resultMap id="CourseScheduleReview" type="com.ym.mec.biz.dal.entity.CourseScheduleReview">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="course_schedule_id_" jdbcType="INTEGER" property="courseScheduleId"/>
        <result column="class_group_id_" jdbcType="INTEGER" property="classGroupId"/>
        <result column="teacher_id_" jdbcType="INTEGER" property="teacherId"/>
        <result column="educational_teacher_id_" jdbcType="INTEGER" property="educationalTeacherId"/>
        <result column="student_id_" jdbcType="INTEGER" property="studentId"/>
        <result column="teaching_material_" jdbcType="VARCHAR" property="teachingMaterial"/>
        <result column="song_" jdbcType="VARCHAR" property="song"/>
        <result column="memo_" jdbcType="VARCHAR" property="memo"/>
        <result column="pronunciation_" jdbcType="INTEGER" property="pronunciation"/>
        <result column="tempo_" jdbcType="INTEGER" property="tempo"/>
        <result column="music_theory_" jdbcType="INTEGER" property="musicTheory"/>
        <result column="has_liaison_" jdbcType="TINYINT" property="hasLiaison"/>
        <result column="hand_homework_" jdbcType="TINYINT" property="handHomework"/>
        <result column="course_review_" jdbcType="VARCHAR" property="courseReview"/>
        <result column="student_review_" jdbcType="TINYINT" property="studentReview"/>
        <result column="create_time_" jdbcType="DATE" property="createTime"/>
        <result column="update_time_" jdbcType="DATE" property="updateTime"/>
        <result column="course_name_" jdbcType="VARCHAR" property="courseName"/>
        <result column="teacher_name_" jdbcType="VARCHAR" property="teacherName"/>
        <result column="teacher_avatar_" jdbcType="VARCHAR" property="teacherAvatar"/>
        <result column="student_name_" jdbcType="VARCHAR" property="studentName"/>
        <result column="student_avatar_" jdbcType="VARCHAR" property="studentAvatar"/>
        <result column="class_date_" jdbcType="DATE" property="classDate"/>
    </resultMap>

    <select id="get" parameterType="java.lang.Integer" resultMap="CourseScheduleReview">
        select *
        from course_schedule_review
        where id_ = #{id,jdbcType=INTEGER}
    </select>

    <delete id="delete" parameterType="java.lang.Integer">
        delete
        from course_schedule_review
        where id_ = #{id,jdbcType=INTEGER}
    </delete>
    <delete id="delByCourseScheduleId">
        DELETE FROM course_schedule_review WHERE course_schedule_id_ = #{courseScheduleId}
    </delete>
    <delete id="delByCourseScheduleIds">
        DELETE FROM course_schedule_review WHERE course_schedule_id_ IN
        <foreach collection="courseScheduleIds" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleReview"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into course_schedule_review (course_schedule_id_, class_group_id_, teacher_id_,
        educational_teacher_id_, student_id_, teaching_material_,
        song_, memo_, pronunciation_,
        tempo_, music_theory_, has_liaison_,
        hand_homework_, course_review_, create_time_,
        update_time_)
        values (#{courseScheduleId,jdbcType=INTEGER}, #{classGroupId,jdbcType=INTEGER}, #{teacherId,jdbcType=INTEGER},
        #{educationalTeacherId,jdbcType=INTEGER}, #{studentId,jdbcType=INTEGER}, #{teachingMaterial,jdbcType=VARCHAR},
        #{song,jdbcType=VARCHAR}, #{memo,jdbcType=VARCHAR}, #{pronunciation,jdbcType=INTEGER},
        #{tempo,jdbcType=INTEGER}, #{musicTheory,jdbcType=INTEGER}, #{hasLiaison,jdbcType=TINYINT},
        #{handHomework,jdbcType=TINYINT}, #{courseReview,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
        #{updateTime,jdbcType=TIMESTAMP})
    </insert>

    <insert id="batchAdd" parameterType="java.util.List" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO course_schedule_review (course_schedule_id_, class_group_id_, teacher_id_, student_id_,
        teaching_material_,memo_, create_time_,update_time_)
        VALUE
        <foreach collection="courseScheduleReviews" item="courseScheduleReview" separator=",">
            (#{courseScheduleReview.courseScheduleId,jdbcType=INTEGER},#{courseScheduleReview.classGroupId,jdbcType=INTEGER},
            #{courseScheduleReview.teacherId,jdbcType=INTEGER},#{courseScheduleReview.studentId,jdbcType=INTEGER},
            #{courseScheduleReview.teachingMaterial,jdbcType=VARCHAR},#{courseScheduleReview.memo,jdbcType=VARCHAR},NOW(),now())
        </foreach>
    </insert>

    <update id="update" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleReview">
        <!--@mbg.generated-->
        update course_schedule_review
        <set>
            <if test="courseScheduleId != null">
                course_schedule_id_ = #{courseScheduleId,jdbcType=INTEGER},
            </if>
            <if test="classGroupId != null">
                class_group_id_ = #{classGroupId,jdbcType=INTEGER},
            </if>
            <if test="teacherId != null">
                teacher_id_ = #{teacherId,jdbcType=INTEGER},
            </if>
            <if test="educationalTeacherId != null">
                educational_teacher_id_ = #{educationalTeacherId,jdbcType=INTEGER},
            </if>
            <if test="studentId != null">
                student_id_ = #{studentId,jdbcType=INTEGER},
            </if>
            <if test="teachingMaterial != null">
                teaching_material_ = #{teachingMaterial,jdbcType=VARCHAR},
            </if>
            <if test="song != null">
                song_ = #{song,jdbcType=VARCHAR},
            </if>
            <if test="memo != null">
                memo_ = #{memo,jdbcType=VARCHAR},
            </if>
            <if test="pronunciation != null">
                pronunciation_ = #{pronunciation,jdbcType=INTEGER},
            </if>
            <if test="tempo != null">
                tempo_ = #{tempo,jdbcType=INTEGER},
            </if>
            <if test="musicTheory != null">
                music_theory_ = #{musicTheory,jdbcType=INTEGER},
            </if>
            <if test="hasLiaison != null">
                has_liaison_ = #{hasLiaison,jdbcType=TINYINT},
            </if>
            <if test="handHomework != null">
                hand_homework_ = #{handHomework,jdbcType=TINYINT},
            </if>
            <if test="courseReview != null">
                course_review_ = #{courseReview,jdbcType=VARCHAR},
            </if>
            <if test="studentReview != null">
                student_review_ = #{studentReview,jdbcType=TINYINT},
            </if>
            update_time_ = NOW()
        </set>
        where id_ = #{id,jdbcType=INTEGER}
    </update>
    <resultMap id="CourseReviewDto" type="com.ym.mec.biz.dal.dto.CourseReviewDto">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="subject_id_" jdbcType="INTEGER" property="subjectId"/>
        <result column="organ_id_" jdbcType="INTEGER" property="organId"/>
        <result column="class_date_" jdbcType="DATE" property="classDate"/>
        <result column="course_name_" jdbcType="VARCHAR" property="courseName"/>
        <result column="teacher_name_" jdbcType="VARCHAR" property="teacherName"/>
        <result column="edu_teacher_name_" jdbcType="VARCHAR" property="eduTeacherName"/>
        <result column="buy_months_" property="BuyMonths"/>
        <result column="review_id_" property="reviewId"/>
        <result column="student_review_" property="studentReview"/>
        <result column="hand_homework_" property="handHomework"/>
        <result column="course_review_" property="courseReview"/>
        <result column="teacher_id" property="teacherId"/>
        <result column="start_class_time_" jdbcType="DATE" property="startClassTime"/>
        <result column="end_class_time_" jdbcType="DATE" property="endClassTime"/>
        <result column="class_date_Str_" jdbcType="VARCHAR" property="classDateStr"/>
        <result column="start_class_time_Str_" jdbcType="VARCHAR" property="startClassTimeStr"/>
        <result column="end_class_time_Str_" jdbcType="VARCHAR" property="endClassTimeStr"/>
        <result column="teaching_material_" property="teachingMaterial"/>
        <result column="pronunciation_" property="pronunciation"/>
        <result column="tempo_" property="tempo"/>
        <result column="music_theory_" property="musicTheory"/>
        <result column="song_" property="song"/>
        <result column="memo_" property="memo"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="has_liaison_" property="hasLiaison"/>
        <result column="student_id_" property="studentId"/>
        <result column="home_work_replied_" property="homeWorkReplied"/>
        <result column="attendance_id_" property="attendanceId"/>
    </resultMap>

    <sql id="practiceGroupReviewsQueryCondition">
        <where>
            AND pg.id_ > 0 AND cs.group_type_='PRACTICE' AND cs.status_='OVER' AND (cs.del_flag_ = 0 OR cs.del_flag_ IS
            NULL)
            <if test="search!=null and search!=''">
                AND (cs.name_ LIKE CONCAT('%',#{search},'%') OR cs.id_= #{search})
            </if>
            <if test="organId != null">
                AND FIND_IN_SET(pg.organ_id_,#{organId})
            </if>
            <if test="subjectId != null">
                AND pg.subject_id_=#{subjectId}
            </if>
            <if test="teacherId != null">
                AND cs.actual_teacher_id_=#{teacherId}
            </if>
            <if test="eduTeacherId != null">
                AND pg.educational_teacher_id_=#{eduTeacherId}
            </if>
            <if test='isFree !=null and isFree=="0"'>
                AND pg.buy_months_ > 0
            </if>
            <if test='isFree !=null and isFree=="1"'>
                AND pg.buy_months_ IS NULL
            </if>
            <if test="studentReview !=null">
                AND csc.score_ =#{studentReview}
            </if>
            <if test='hasReview !=null and hasReview=="1"'>
                AND csr.id_ > 0
            </if>
            <if test='hasReview !=null and hasReview=="0"'>
                AND csr.id_ IS NULL
            </if>
            <if test='hasHandHomework !=null and hasHandHomework=="1"'>
                AND csr.hand_homework_ = 1
            </if>
            <if test='hasHandHomework !=null and hasHandHomework=="0"'>
                AND csr.hand_homework_ IS NULL
            </if>
            <if test='hasLiaison !=null and hasLiaison=="1"'>
                AND csr.has_liaison_ = 1
            </if>
            <if test='hasLiaison !=null and hasLiaison=="0"'>
                AND (csr.has_liaison_ = 0 OR csr.has_liaison_ IS NULL)
            </if>
            <if test="courseReview != null">
                AND csr.course_review_=#{courseReview}
            </if>
            <if test="startTime !=null">
                <![CDATA[AND cs.class_date_ >= #{startTime} ]]>
            </if>
            <if test="endTime !=null">
                <![CDATA[AND cs.class_date_ <= #{endTime} ]]>
            </if>
            <if test='hasArrived !=null and hasArrived=="1"'>
                AND sa.id_ > 0
            </if>
            <if test='hasArrived !=null and hasArrived=="0"'>
                AND sa.id_ IS NULL
            </if>
            <if test='homeWorkReplied !=null and homeWorkReplied =="1"'>
                AND sch.is_replied_ > 0
            </if>
            <if test='homeWorkReplied !=null and homeWorkReplied =="0"'>
                AND (sch.is_replied_ = 0 OR sch.is_replied_ IS NULL)
            </if>
        </where>
    </sql>

    <select id="getPracticeGroupReviewList" resultMap="CourseReviewDto">
        SELECT cs.id_,cs.actual_teacher_id_ teacher_id ,cs.class_date_, pg.subject_id_,pg.organ_id_,pg.name_
        course_name_,su.real_name_
        teacher_name_,edsu.real_name_
        edu_teacher_name_,pg.buy_months_,csr.id_ review_id_, csc.score_ student_review_,
        csr.hand_homework_,csr.course_review_,pg.student_id_,csr.teaching_material_,csr.pronunciation_,csr.tempo_,
        csr.music_theory_,csr.song_,csr.memo_,csr.create_time_,csr.has_liaison_,csr.update_time_,sa.id_
        attendance_id_,sch.is_replied_ home_work_replied_
        FROM course_schedule cs
        LEFT JOIN practice_group pg ON cs.music_group_id_ = pg.id_ AND cs.group_type_='PRACTICE'
        LEFT JOIN sys_user su ON cs.actual_teacher_id_ = su.id_
        LEFT JOIN sys_user edsu ON pg.educational_teacher_id_ = edsu.id_
        LEFT JOIN course_schedule_review csr on cs.id_ = csr.course_schedule_id_
        LEFT JOIN course_schedule_complaints csc on csc.course_schedule_id_ = cs.id_
        LEFT JOIN student_attendance sa on sa.course_schedule_id_ = cs.id_
        LEFT JOIN student_course_homework sch on cs.id_ = sch.course_schedule_id_
        <include refid="practiceGroupReviewsQueryCondition"/>
        ORDER BY cs.class_date_ DESC
        <include refid="global.limit"/>
    </select>

    <select id="countPracticeGroupReviews" resultType="java.lang.Integer">
        SELECT count(*) FROM course_schedule cs
        LEFT JOIN practice_group pg ON cs.music_group_id_ = pg.id_ AND cs.group_type_='PRACTICE'
        LEFT JOIN course_schedule_review csr on cs.id_ = csr.course_schedule_id_
        LEFT JOIN course_schedule_complaints csc on csc.course_schedule_id_ = cs.id_
        LEFT JOIN student_attendance sa on sa.course_schedule_id_ = cs.id_
        LEFT JOIN student_course_homework sch on cs.id_ = sch.course_schedule_id_
        <include refid="practiceGroupReviewsQueryCondition"/>
    </select>

    <sql id="practiceGroupReviews4EduQueryCondition">
        <where>
            <if test="eduTeacherId != null">
                AND pg.educational_teacher_id_=#{eduTeacherId}
            </if>
            <if test='hasReview !=null and hasReview=="1"'>
                AND csr.course_review_ IS NOT NULL
            </if>
            <if test='hasReview !=null and hasReview=="0"'>
                AND csr.course_review_ IS NULL
            </if>
            <if test='hasHandHomework !=null and hasHandHomework=="1"'>
                AND csr.hand_homework_ = 1
            </if>
            <if test='hasHandHomework !=null and hasHandHomework=="0"'>
                AND csr.hand_homework_ IS NULL
            </if>
            <if test='hasLiaison !=null and hasLiaison=="1"'>
                AND csr.has_liaison_ = 1
            </if>
            <if test='hasLiaison !=null and hasLiaison=="0"'>
                AND csr.has_liaison_ = 0
            </if>
            <if test="startTime !=null">
                <![CDATA[AND cs.class_date_ >= #{startTime} ]]>
            </if>
            <if test="endTime !=null">
                <![CDATA[AND cs.class_date_ <= #{endTime} ]]>
            </if>
        </where>
    </sql>

    <select id="getPracticeGroupReview4EduList" resultMap="CourseScheduleReview">
        SELECT csr.id_,cs.class_date_, pg.name_ course_name_,su.real_name_ teacher_name_,csr.hand_homework_
        FROM course_schedule_review csr
        LEFT JOIN course_schedule cs on cs.id_ = csr.course_schedule_id_
        LEFT JOIN practice_group pg ON cs.music_group_id_ = pg.id_ AND cs.group_type_='PRACTICE'
        LEFT JOIN sys_user su ON cs.actual_teacher_id_ = su.id_
        <include refid="practiceGroupReviews4EduQueryCondition"/>
        <include refid="global.limit"/>
    </select>


    <select id="countPracticeGroupReviews4Edu" resultType="java.lang.Integer">
        SELECT count(*) FROM course_schedule_review csr
        LEFT JOIN course_schedule cs on cs.id_ = csr.course_schedule_id_
        LEFT JOIN practice_group pg ON cs.music_group_id_ = pg.id_ AND cs.group_type_='PRACTICE'
        <include refid="practiceGroupReviews4EduQueryCondition"/>
    </select>

    <select id="findByCourseScheduleAndStudent" resultMap="CourseScheduleReview">
        SELECT *
        FROM course_schedule_review
        WHERE course_schedule_id_ = #{courseScheduleId}
          AND student_id_ = #{studentId}
    </select>

    <select id="findByCourseSchedules" resultMap="CourseScheduleReview">
        SELECT * FROM course_schedule_review WHERE course_schedule_id_ IN
        <foreach collection="courseScheduleIds" item="courseScheduleId" separator="," open="(" close=")">
            #{courseScheduleId}
        </foreach>
    </select>

    <select id="findByCourseSchedulesAndStudentId" resultMap="CourseScheduleReview">
        SELECT * FROM course_schedule_review WHERE course_schedule_id_ IN
        <foreach collection="courseScheduleIds" item="courseScheduleId" separator="," open="(" close=")">
            #{courseScheduleId}
        </foreach>
        AND student_id_ = #{studentId}
    </select>

    <select id="findByClassGroupId" resultMap="CourseReviewDto">
        SELECT id_,
               class_date_,
               name_             course_name_,
               class_date_       class_date_Str_,
               start_class_time_ start_class_time_Str_,
               end_class_time_   end_class_time_Str_
        FROM course_schedule
        WHERE class_group_id_ = #{classGroupId}
          AND status_ = 'OVER'
        ORDER BY class_date_ DESC, start_class_time_ DESC
    </select>

    <select id="findByCourseScheduleId" resultMap="CourseScheduleReview">
        SELECT csr.teacher_id_,
               t.real_name_  teacher_name_,
               t.avatar_     teacher_avatar_,
               csr.student_id_,
               stu.username_ student_name_,
               stu.avatar_   student_avatar_,
               csr.teaching_material_,
               csr.memo_
        FROM course_schedule_review csr
                 LEFT JOIN sys_user t on csr.teacher_id_ = t.id_
                 LEFT JOIN sys_user stu on csr.student_id_ = stu.id_
        WHERE csr.course_schedule_id_ = #{courseScheduleId}
    </select>

    <select id="findStudentByClassGroupId" resultMap="com.ym.mec.biz.dal.dao.TeacherDao.SysUser">
        SELECT su.id_, su.username_, su.avatar_
        FROM class_group_student_mapper cgsm
                 LEFT JOIN sys_user su on cgsm.user_id_ = su.id_
        WHERE cgsm.class_group_id_ = #{classGroupId}
    </select>
</mapper>
