<?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.CourseScheduleRepliedDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.CourseScheduleReplied">
            <result column="id_" property="id" />
	        <result column="student_id_" property="studentId" />
	        <result column="course_schedule_id_" property="courseScheduleId" />
	        <result column="course_group_type_" property="courseGroupType" />
	        <result column="course_group_id_" property="courseGroupId" />
	        <result column="score_" property="score" />
	        <result column="student_replied_" property="studentReplied" />
	        <result column="teacher_replied_" property="teacherReplied" />
	        <result column="create_time_" property="createTime" />
	        <result column="update_time_" property="updateTime" />
		</resultMap>  
    
    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_
        , t.student_id_
        , t.course_schedule_id_
        , t.course_group_type_
        , t.course_group_id_
        , t.score_
        , t.student_replied_
        , t.teacher_replied_
        , t.create_time_
        , t.update_time_
        </sql>
    <insert id="insertReplied" parameterType="com.yonge.cooleshow.biz.dal.entity.CourseScheduleReplied">
        INSERT INTO course_schedule_replied
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="param.studentId != null">
                student_id_,
            </if>
            <if test="param.courseScheduleId != null">
                course_schedule_id_,
            </if>
            <if test="param.courseGroupType != null">
                course_group_type_,
            </if>
            <if test="param.courseGroupId != null">
                course_group_id_,
            </if>
            <if test="param.score != null">
                score_,
            </if>
            <if test="param.studentReplied != null">
                student_replied_,
            </if>
            <if test="param.teacherReplied != null">
                teacher_replied_,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="param.studentId != null">
                studentId,
            </if>
            <if test="param.courseScheduleId != null">
                courseScheduleId,
            </if>
            <if test="param.courseGroupType != null">
                courseGroupType,
            </if>
            <if test="param.courseGroupId != null">
                courseGroupId,
            </if>
            <if test="param.score != null">
                score,
            </if>
            <if test="param.studentReplied != null">
                studentReplied,
            </if>
            <if test="param.teacherReplied != null">
                teacherReplied,
            </if>
        </trim>
    </insert>
    <insert id="insertBatch">
        INSERT INTO course_schedule_replied (student_id_,course_schedule_id_,course_group_type_,course_group_id_) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.studentId},#{item.courseId},#{item.type},#{item.courseGroupId})
        </foreach>
    </insert>
    <delete id="deleteBatch" parameterType="java.util.List">
        DELETE FROM course_schedule_replied WHERE course_schedule_id_ IN
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.CourseScheduleRepliedVo">
        SELECT
            <include refid="baseColumns"/>
        FROM course_schedule_replied t
        where t.id_ = #{id}
    </select>
    
    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.CourseScheduleRepliedVo">
		SELECT         
        	<include refid="baseColumns" />
		FROM course_schedule_replied t
	</select>
    <select id="selectAll" resultType="java.lang.Long">
        SELECT student_id_ FROM course_schedule_replied WHERE course_group_type_='PRACTICE'
    </select>
    <select id="selectReplyStudent" resultType="java.lang.Long">
        SELECT student_id_ FROM course_schedule_replied WHERE course_group_type_='PRACTICE' AND teacher_replied_ != ''
    </select>
    <update id="updateReplied" parameterType="com.yonge.cooleshow.biz.dal.entity.CourseScheduleReplied">
        UPDATE course_schedule_replied
        <set>
            <if test="score !=null">score_ = #{score},</if>
            <if test="studentReplied !=null and studentReplied !=''">student_replied_ = #{studentReplied},</if>
            <if test="teacherReplied !=null and teacherReplied !=''">teacher_replied_ = #{teacherReplied},</if>
            <if test="studentRepliedTime !=null">student_replied_time_ = #{studentRepliedTime},</if>
            <if test="teacherRepliedTime !=null">teacher_replied_time_ = #{teacherRepliedTime},</if>
            update_time_ = SYSDATE()
        </set>
        WHERE id_ = #{id}
    </update>
    <select id="myReplied" resultType="com.yonge.cooleshow.biz.dal.vo.MyRepliedVo">
        SELECT
            r.course_schedule_id_ AS courseId,
            r.course_group_id_ AS courseGroupId,
            r.score_ AS score,
            r.student_replied_ AS studentRepliedStr,
            g.subject_id_ AS subjectId,
            sb.name_ AS subjectName,
            s.class_date_ AS classDate,
            s.start_time_ AS startTime,
            s.end_time_ AS endTime,
            u.id_ AS studentId,
            u.real_name_ AS realName,
            u.username_ AS userName,
            u.avatar_ AS avatar,
            (r.student_replied_ IS NOT NULL) AS studentReplied,
            (r.teacher_replied_ IS NOT NULL) AS teacherReplied
        FROM course_schedule_replied r
        LEFT JOIN course_group g ON g.id_ = r.course_group_id_
        LEFT JOIN `subject` sb ON sb.id_ = g.subject_id_
        LEFT JOIN course_schedule s ON s.id_ = r.course_schedule_id_
        LEFT JOIN sys_user u ON u.id_ = r.student_id_
        WHERE r.student_replied_ != ""
        AND s.teacher_id_ = #{param.teacherId}
        <if test="param.studentName !=null and param.studentName !=''">
            AND u.username_ LIKE CONCAT('%', #{param.studentName}, '%')
        </if>
        <if test="param.courseType !=null and param.courseType !=''">
            AND r.course_group_type_ = #{param.courseType}
        </if>
        <if test="param.classDate !=null and param.classDate !=''">
            AND DATE_FORMAT(s.class_date_,'%Y-%m') = #{param.classDate}
        </if>
        ORDER BY s.start_time_
    </select>
    <select id="selectReplied" resultType="com.yonge.cooleshow.biz.dal.vo.CourseRepliedVo"
            parameterType="com.yonge.cooleshow.biz.dal.entity.CourseScheduleReplied">
        SELECT
            <include refid="baseColumns"/>,
            u.username_ AS userName,
            u.real_name_ AS realName,
            u.avatar_ AS avatar,

            s.class_date_ AS classDate,
            s.start_time_ AS startTime,
            s.end_time_ AS endTime,
            s.status_ AS status,

            g.subject_id_ AS subjectId,
            sb.name_ AS subjectName
        FROM course_schedule_replied t
        LEFT JOIN sys_user u ON t.student_id_=u.id_
        LEFT JOIN course_schedule s ON t.course_schedule_id_=s.id_
        LEFT JOIN course_group g ON t.course_group_id_=g.id_
        LEFT JOIN `subject` sb ON g.subject_id_=sb.id_
        WHERE t.course_schedule_id_=#{courseScheduleId}
        AND g.id_ = #{courseGroupId}
        ORDER BY s.start_time_
    </select>
    <select id="selectAllClass" resultType="java.lang.Long">
        SELECT course_schedule_id_ FROM course_schedule_replied WHERE course_group_type_='PRACTICE'
    </select>
</mapper>