<?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.CourseHomeworkDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.CourseHomework">
            <result column="id_" property="id" />
	        <result column="course_group_id_" property="courseGroupId" />
	        <result column="course_group_type_" property="courseGroupType" />
	        <result column="course_schedule_id_" property="courseScheduleId" />
	        <result column="title_" property="title" />
	        <result column="content_" property="content" />
	        <result column="attachments_" property="attachments" />
	        <result column="expect_num_" property="expectNum" />
	        <result column="completed_num_" property="completedNum" />
	        <result column="create_time_" property="createTime" />
	        <result column="update_time_" property="updateTime" />
		</resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_
        , t.course_group_id_
        , t.course_group_type_
        , t.course_schedule_id_
        , t.title_
        , t.content_
        , t.attachments_
        , t.expect_num_
        , t.completed_num_
        , t.create_time_
        , t.update_time_
        </sql>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.CourseHomeworkVo">
        SELECT
            <include refid="baseColumns"/>
        FROM course_homework t
        where t.id_ = #{id}
    </select>

    <select id="selectAdminPage" resultType="com.yonge.cooleshow.biz.dal.vo.CourseHomeworkVo">
        select
        cs.id_ AS courseId,
        cs.course_group_id_ AS courseGroupId,
        cs.class_date_ AS classDate,
        cs.type_ as type,
        cs.start_time_ AS startTime,
        cs.end_time_ AS endTime,
        ch.create_time_ as decorateTime,
        su2.id_ as studentId,
        su2.username_ as studentName,
        su2.avatar_ as studentAvatar,
        su.username_ as teacherName,
        su.avatar_ as teacherAvatar,
        sch.submit_time_ as submitTime,
        su.id_ as teacherId,
        ch.create_time_ as decorateTime
        from course_homework ch
        left join course_schedule cs on ch.course_schedule_id_ = cs.id_
        left join course_schedule_student_payment cssp on cssp.course_id_ = ch.course_schedule_id_
        left join student_course_homework sch on sch.course_schedule_id_ = cssp.course_id_ and sch.student_id_ = cssp.user_id_
        left join sys_user su on cs.teacher_id_ = su.id_
        left join sys_user su2 on cssp.user_id_ = su2.id_
        <where>
            <if test="param.courseStatus != null">
                and cs.status_ = #{param.courseStatus}
            </if>
            <if test="param.courseType != null and param.courseType.size() != 0">
                and  cs.type_ in
                <foreach collection="param.courseType" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="param.startTime != null">
                and #{param.startTime} &lt; ch.create_time_
            </if>
            <if test="param.endTime != null">
                and #{param.endTime} &gt; ch.create_time_
            </if>
            <if test="param.studentSearch != null and param.studentSearch != ''">
                and ( su2.id_ like concat('%',#{param.studentSearch},'%')
                    or su2.username_ like concat('%',#{param.studentSearch},'%')
                    or su2.phone_ like concat('%',#{param.studentSearch},'%') )
            </if>
            <if test="param.teacherSearch != null and param.teacherSearch != ''">
                and ( su.id_ like concat('%',#{param.teacherSearch},'%')
                or su.username_ like concat('%',#{param.teacherSearch},'%')
                or su.phone_ like concat('%',#{param.teacherSearch},'%') )
            </if>
        </where>
        order by cs.start_time_ desc
    </select>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.CourseHomeworkVo">
        select distinct cs.id_              AS courseId,
        cs.course_group_id_ AS courseGroupId,
        cs.class_date_      AS classDate,
        cs.class_num_       as classNum,
        cs.start_time_      AS startTime,
        cs.end_time_        AS endTime,
        cs.type_            as type,
        ch.create_time_     as decorateTime,
        (case when a.commitNum &gt; a.repliedNum then 'NOTREVIEW'
        when a.studentNum &gt; a.commitNum then 'NOTCOMMIT'
        else 'REVIEWED' end ) as homeworkStatus
        from course_schedule cs
        left join course_homework ch on ch.course_schedule_id_ = cs.id_
        left join course_schedule_student_payment cssp on cssp.course_id_ = cs.id_
        left join student_course_homework sch  on sch.course_schedule_id_ = cssp.course_id_ and sch.student_id_ = cssp.user_id_
        left join (select cssp.course_id_,
                count(cssp.id_) as studentNum,
                sum(if(sch.attachments_ is null or sch.attachments_ = '',0,1)) as commitNum,
                sum(if(sch.teacher_replied_ is null or sch.teacher_replied_ = '',0,1)) as repliedNum
                from course_schedule_student_payment cssp
                left join student_course_homework sch on sch.student_id_ = cssp.user_id_ and
                sch.course_schedule_id_ = cssp.course_id_
                <where>
                    <if test="param.studentId != null">
                        and cssp.user_id_ = #{param.studentId}
                    </if>
                </where>
                group by  cssp.course_id_) a on a.course_id_ = cs.id_
        <where>
            <if test="param.decorate != null">
                <if test="param.decorate.code == 0">
                    and ch.id_ is null
                </if>
                <if test="param.decorate.code == 1">
                    and ch.id_ is not null
                </if>
            </if>
            <if test="param.submit != null">
                <if test="param.submit.code == 0">
                    and sch.id_ is null
                </if>
                <if test="param.submit.code == 1">
                    and sch.id_ is not null
                </if>
            </if>
            <if test="param.courseStatus != null">
                and cs.status_ = #{param.courseStatus}
            </if>
            <if test="param.courseType != null and param.courseType.size() != 0">
                and cs.type_ in
                <foreach collection="param.courseType" separator="," close=")" open="(" item="item" >
                    #{item}
                </foreach>
            </if>
            <if test="param.teacherId != null">
                and cs.teacher_id_ = #{param.teacherId}
            </if>
            <if test="param.studentId != null">
                and cssp.user_id_ = #{param.studentId}
            </if>
            <if test="param.courseScheduleId != null ">
                and cs.id_ = #{param.courseScheduleId}
            </if>
            <if test="param.date != null and param.date != ''">
                and date_format(cs.class_date_,'%Y-%m') = #{param.date}
            </if>
            <if test="param.startTime != null">
                and #{param.startTime} &lt; ch.create_time_
            </if>
            <if test="param.endTime != null">
                and #{param.endTime} &gt; ch.create_time_
            </if>
            <if test="param.homeworkStatus != null">
                <choose>
                    <when test="param.homeworkStatus.code  == 'NOTREVIEW'">
                        and a.commitNum &gt; a.repliedNum
                    </when>
                    <when test="param.homeworkStatus.code  == 'NOTCOMMIT'">
                        and a.studentNum &gt; a.commitNum
                    </when>
                    <otherwise>
                        and a.commitNum = a.repliedNum and a.studentNum = a.commitNum
                    </otherwise>
                </choose>
            </if>
        </where>
        order by cs.start_time_
        <if test="param.date == null or param.date == ''">
            desc
        </if>
	</select>

	<select id="selectCourseHomeworkDetailByCourseId"
            resultType="com.yonge.cooleshow.biz.dal.vo.CourseHomeworkDetailVo">
        select  ch.id_ as id
        ,cs.id_ as courseScheduleId
        ,ch.title_ as title
        ,ch.content_ as content
        ,ch.attachments_ as attachments
        ,ch.create_time_ as decorateTime
        ,sch.attachments_ as studentAttachments
        ,sch.teacher_replied_ as teacherReplied
        ,cg.id_ as courseGroupId
        ,concat(cg.name_,'-第',cs.class_num_,'课') as courseGroupName
        ,cs.class_num_ as classNum
        ,sch.submit_time_ as submitTime
        ,sch.id_ as studentHomeworkId
        ,sch.student_id_ as studentId
        ,cs.teacher_id_ as teacherId
        ,cs.type_ as courseType
        ,cs.class_date_ as classDate
        ,cs.start_time_ as startTime
        ,cs.end_time_ as endTime
        ,cs.status_ as courseStatus
        ,cs.course_group_id_ as courseGroupId
        ,if(ch.id_ is null,0,1) as decorateHomework
        ,if(sch.attachments_ is null or sch.attachments_ = '',0,1) as submitHomework
        ,if(sch.teacher_replied_ is null or sch.teacher_replied_ = '',0,1) as reviewHomework
        ,if(sch.id_ is not null and date_add(sch.submit_time_,INTERVAL #{homeworkExpireTime} DAY) &lt; now(),1,0) as homeworkExpire
        from course_schedule cs
        left join course_group cg on cs.course_group_id_ = cg.id_
        left join course_homework ch on ch.course_schedule_id_ = cs.id_
        left join course_schedule_student_payment cssp on cs.id_ = cssp.course_id_
        left join student_course_homework sch  on sch.course_schedule_id_ = cssp.course_id_ and sch.student_id_ = cssp.user_id_
        <where>
            <if test="courseId != null">
                and cs.id_ = #{courseId}
            </if>
            <if test="studentId != null">
                and cssp.user_id_ = #{studentId}
            </if>
        </where>
        limit 1
    </select>

	<select id="selectPaymentStudentInfo" resultType="com.yonge.cooleshow.biz.dal.vo.CourseHomeworkDetailVo">
        select su.username_ as studentName
        ,su.avatar_ as studentAvatar
        ,cssp.user_id_ as studentId
        from course_schedule cs
        left join course_schedule_student_payment cssp on cs.id_ = cssp.course_id_
        left join sys_user su on su.id_ = cssp.user_id_
        <where>
            <if test="courseScheduleId != null">
                and cs.id_ = #{courseScheduleId}
            </if>
            <if test="studentId != null">
                and cssp.user_id_ = #{studentId}
            </if>
        </where>
    </select>


    <select id="selectSubjectAndTeacherInfo" resultType="com.yonge.cooleshow.biz.dal.vo.CourseHomeworkDetailVo">
        select s.name_ as subjectName
        ,su.username_ as teacherName
        ,su.avatar_ as teacherAvatar
        ,su.id_ as teacherId
        from course_group cg
        left join subject s on cg.subject_id_ = s.id_
        left join sys_user su on cg.teacher_id_ = su.id_
        <where>
            <if test="courseGroupId != null">
                and cg.id_ = #{courseGroupId}
            </if>
        </where>
    </select>

	<select id="selectStudentInfo" resultType="com.yonge.cooleshow.biz.dal.vo.CourseHomeworkVo">
        select
        cs.id_ as courseId,
        cssp.user_id_ as studentId
        ,su.username_ as studentName
        ,su.avatar_ as studentAvatar
        from course_schedule cs
        left join  course_schedule_student_payment cssp on cssp.course_id_ = cs.id_
        left join sys_user su on cssp.user_id_ = su.id_
        <where>
            <if test="records != null and records.size() != 0">
                and cs.id_ in
                <foreach collection="records" separator="," close=")" open="(" item="item" >
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

	<select id="selectSubjectAndGroupInfoList" resultType="com.yonge.cooleshow.biz.dal.vo.CourseHomeworkVo">
        select
        cs.id_ as courseId,
        s.name_ as subjectName
        ,concat(cg.name_,'-第',cs.class_num_,'课') as courseGroupName
        ,cg.background_pic_ as backgroundPic
        ,cs.class_num_ as classNum
        ,ig.id_ as imGroupId
        from course_schedule cs
        left join course_group cg on cs.course_group_id_ = cg.id_
        left join subject s on s.id_ = cg.subject_id_
        left join im_group ig on cs.course_group_id_ = ig.course_group_id_
        <where>
            <if test="records != null and records.size() != 0">
                and cs.id_ in
                <foreach collection="records" separator="," close=")" open="(" item="item" >
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

	<select id="selectTeacherInfo" resultType="com.yonge.cooleshow.biz.dal.vo.CourseHomeworkVo">
        select
        cs.id_ as courseId,
        cs.teacher_id_ as teacherId
        ,su.username_ as teacherName
        ,su.avatar_ as teacherAvatar
        from course_schedule cs
        left join sys_user su on cs.teacher_id_ = su.id_
        <where>
            <if test="records != null and records.size() != 0">
                and cs.id_ in
                <foreach collection="records" separator="," close=")" open="(" item="item" >
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

	<select id="selectCourseHomeworkByCourseId" resultType="com.yonge.cooleshow.biz.dal.vo.CourseScheduleHomeworkVo">
        select
            cs.id_ as courseId,
            cg.id_ as courseGroupId,
            cg.name_ as courseGroupName,
            cs.type_ as type,
            cs.class_num_ as classNum,
            cs.class_date_ as classDate,
            cs.start_time_ as startTime,
            cs.end_time_ as endTime,
            s.name_ as subjectName,
            ch.title_ as title,
            ch.content_ as content,
            ch.attachments_ as attachments,
        if(ch.id_ is null,0,1) as decorateHomework,
        ch.create_time_ as decorateTime,
        cs.status_ as courseStatus,
        cg.background_pic_ as backgroundPic
        from course_schedule  cs
        left join course_group cg on cs.course_group_id_ = cg.id_
        left join course_homework ch on cs.id_ = ch.course_schedule_id_
        left join subject s on cg.subject_id_ = s.id_
        where cs.id_ = #{courseId}

    </select>

	<select id="selectStudentHomeworkByCourseId" resultType="com.yonge.cooleshow.biz.dal.vo.StudentHomeworkVo">
        select
        s.id_ as studentId,
        s.username_ as studentName,
        s.avatar_ as studentAvatar,
        if(sch.id_ is not null,1,0) as submitHomework,
        (case when sch.id_ is null then 'NOTCOMMIT'
        when sch.teacher_replied_ is null or sch.teacher_replied_ = '' then 'NOTREVIEW'
        else 'REVIEWED' end ) as homeworkStatus,
        (select group_concat(s2.name_) from subject s2
        where find_in_set(s2.id_,st.subject_id_) and s2.del_flag_ = 0 ) as subjectName

        from course_schedule_student_payment cssp
        left join sys_user s on cssp.user_id_ = s.id_
        left  join student st on st.user_id_ = s.id_
        left join student_course_homework sch on cssp.user_id_ = sch.student_id_ and sch.course_schedule_id_ = cssp.course_id_
        <where>
            <if test="courseId != null">
                and cssp.course_id_ = #{courseId}
            </if>
        </where>
    </select>

	<select id="selectAbsenteeism" resultType="com.yonge.cooleshow.biz.dal.vo.CourseHomeworkVo">
        select
        cs.id_ as courseId
        ,if(sa.id_ is null,1,0) as absenteeism
        from course_schedule cs
        left join student_attendance sa on sa.course_schedule_id_ = cs.id_
        <where>
            sa.student_id_ = #{studentId}
            <if test="practiceRecord != null and practiceRecord.size() != 0">
                and cs.id_ in
                <foreach collection="practiceRecord" separator="," close=")" open="(" item="item" >
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
</mapper>