<?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.CourseScheduleStatisticsDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.CourseScheduleStatistics" id="CourseScheduleStatistics">
		<result column="course_schedule_id_" property="courseScheduleId" />
		<result column="music_group_id_" property="musicGroupId" />
		<result column="course_student_total_num_" property="courseStudentTotalNum" />
		<result column="attendance_num_" property="attendanceNum" />
		<result column="late_num_" property="lateNum" />
		<result column="standard_num_" property="standardNum" />
		<result column="homework_flag_" property="homeworkFlag" />
		<result column="homework_commit_num_" property="homeworkCommitNum" />
		<result column="service_flag_" property="serviceFlag" />
		<result column="attendance_rate_" property="attendanceRate" />
		<result column="standard_rate_" property="standardRate" />
		<result column="homework_commit_rate_" property="homeworkCommitRate" />
		<result column="class_date_" property="classDate" />
		<result column="course_status_" property="courseStatus" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="CourseScheduleStatistics" >
		SELECT * FROM course_schedule_statistics WHERE course_schedule_id_ = #{courseScheduleId}
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="CourseScheduleStatistics">
		SELECT * FROM course_schedule_statistics
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleStatistics" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO course_schedule_statistics (course_schedule_id_,music_group_id_,course_student_total_num_,
		                                        attendance_num_,late_num_,standard_num_,homework_flag_,homework_commit_num_,service_flag_,class_date_,course_status_)
		                                        VALUES(#{courseScheduleId},#{musicGroupId},#{courseStudentTotalNum},#{attendanceNum},
		                                               #{lateNum},#{standardNum},#{homeworkFlag},#{homeworkCommitNum},#{serviceFlag},#{classDate},#{courseStatus})
	</insert>
	<insert id="batchInsert">
		INSERT INTO course_schedule_statistics (course_schedule_id_,music_group_id_,course_student_total_num_,
												attendance_num_,late_num_,standard_num_,homework_flag_,homework_commit_num_,service_flag_,class_date_,course_status_) VALUES
		<foreach collection="scheduleStatisticsList" separator="," item="item">
			(#{item.courseScheduleId},#{item.musicGroupId},#{item.courseStudentTotalNum},#{item.attendanceNum},
			#{item.lateNum},#{item.standardNum},#{item.homeworkFlag},#{item.homeworkCommitNum},#{item.serviceFlag},#{classDate},#{courseStatus})
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleStatistics">
		UPDATE course_schedule_statistics <set>
		<if test="classDate != null and classDate != ''">
			class_date_ = #{classDate},
		</if>
		<if test="courseStatus != null and courseStatus != ''">
			course_status_ = #{courseStatus},
		</if>
		<if test="homeworkFlag != null">
		homework_flag_ = #{homeworkFlag},
		</if>
		<if test="attendanceNum != null">
		attendance_num_ = #{attendanceNum},
		</if>
		<if test="serviceFlag != null">
		service_flag_ = #{serviceFlag},
		</if>
		<if test="standardNum != null">
		standard_num_ = #{standardNum},
		</if>
		<if test="homeworkCommitNum != null">
		homework_commit_num_ = #{homeworkCommitNum},
		</if>
		<if test="courseStudentTotalNum != null">
		course_student_total_num_ = #{courseStudentTotalNum},
		</if>
		<if test="lateNum != null">
		late_num_ = #{lateNum},
		</if>
		<if test="musicGroupId != null">
		music_group_id_ = #{musicGroupId},
		</if>
		</set> WHERE course_schedule_id_ = #{courseScheduleId}
	</update>
	<update id="batchUpdate">
		<foreach collection="scheduleStatisticsList" item="item" index="index" open="" close="" separator=";">
			UPDATE course_schedule_statistics
			<set>
			<if test="item.courseStatus != null and item.courseStatus != ''">
				course_status_ = #{item.courseStatus},
			</if>
			<if test="item.classDate != null and item.classDate != ''">
				class_date_ = #{item.classDate},
			</if>
			<if test="item.homeworkFlag != null">
				homework_flag_ = #{item.homeworkFlag},
			</if>
			<if test="item.attendanceNum != null">
				attendance_num_ = #{item.attendanceNum},
			</if>
			<if test="item.serviceFlag != null">
				service_flag_ = #{item.serviceFlag},
			</if>
			<if test="item.standardNum != null">
				standard_num_ = #{item.standardNum},
			</if>
			<if test="item.homeworkCommitNum != null">
				homework_commit_num_ = #{item.homeworkCommitNum},
			</if>
			<if test="item.courseStudentTotalNum != null">
				course_student_total_num_ = #{item.courseStudentTotalNum},
			</if>
			<if test="item.lateNum != null">
				late_num_ = #{item.lateNum},
			</if>
		</set> WHERE course_schedule_id_ = #{item.courseScheduleId}
		</foreach>
	</update>
	<update id="updateCourseService">
		UPDATE course_schedule_statistics SET service_flag_ = #{serviceFlag} WHERE FIND_IN_SET(course_schedule_id_,#{courseIds})
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM course_schedule_statistics WHERE course_schedule_id_ = #{courseScheduleId}
	</delete>
	<delete id="delWaitByCourseId">
		DELETE FROM course_schedule_statistics_wait WHERE course_schedule_id_ IN
		<foreach collection="courseIdList" item="courseId" separator="," open="(" close=")">
			#{courseId}
		</foreach>
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="CourseScheduleStatistics" parameterType="map">
		SELECT * FROM course_schedule_statistics <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM course_schedule_statistics
	</select>
    <select id="queryUpdateCourseScheduleStatistics" resultMap="CourseScheduleStatistics">
		SELECT cssw.course_schedule_id_,cs.music_group_id_,
			   COUNT(DISTINCT cssp.id_) course_student_total_num_,
			   COUNT(DISTINCT CASE WHEN sa.status_ = 'NORMAL' THEN sa.id_ ELSE NULL END) attendance_num_,
			   COUNT(DISTINCT CASE WHEN sa.status_ = 'LATE' THEN sa.id_ ELSE NULL END) late_num_,
			   COUNT(DISTINCT CASE WHEN sa.qualified_flag_ = 1 THEN sa.id_ ELSE NULL END) standard_num_,
			   CASE WHEN sch.id_ IS NOT NULL THEN 1 ELSE 0 END homework_flag_,
			   COUNT(DISTINCT CASE WHEN sch.status_ = 1 THEN sch.id_ ELSE NULL END) homework_commit_num_,cs.class_date_,cs.status_ course_status_
		FROM course_schedule_statistics_wait cssw
				 LEFT JOIN course_schedule_statistics css ON css.course_schedule_id_ = cssw.course_schedule_id_
				 LEFT JOIN course_schedule cs ON cs.id_ = cssw.course_schedule_id_
				 LEFT JOIN course_schedule_student_payment cssp ON cssp.course_schedule_id_ = cssw.course_schedule_id_
				 LEFT JOIN student_attendance sa ON sa.course_schedule_id_ = cssw.course_schedule_id_
				 LEFT JOIN student_course_homework sch ON sch.course_schedule_id_ = cssw.course_schedule_id_
		WHERE cs.group_type_ = 'MUSIC' AND css.course_schedule_id_ IS NOT NULL
		GROUP BY cssw.course_schedule_id_;
	</select>
    <select id="queryInsertCourseScheduleStatistics" resultMap="CourseScheduleStatistics">
		SELECT cssw.course_schedule_id_,cs.music_group_id_,COUNT(cssp.id_) course_student_total_num_,
			   COUNT(CASE WHEN sa.status_ = 'NORMAL' THEN 1 ELSE NULL END) attendance_num_,
			   COUNT(CASE WHEN sa.status_ = 'LATE' THEN 1 ELSE NULL END) late_num_,COUNT(CASE WHEN sa.qualified_flag_ = 1 THEN 1 ELSE NULL END) standard_num_,
			   CASE WHEN sch.id_ IS NOT NULL THEN 1 ELSE 0 END homework_flag_,
			   COUNT(DISTINCT CASE WHEN sch.status_ = 1 THEN sch.id_ ELSE NULL END) homework_commit_num_,
			   CASE WHEN sees.course_ids_ IS NOT NULL THEN 1 ELSE 0 END service_flag_,cs.class_date_,cs.status_ course_status_
		FROM course_schedule_statistics_wait cssw
				 LEFT JOIN course_schedule_statistics css ON css.course_schedule_id_ = cssw.course_schedule_id_
				 LEFT JOIN course_schedule cs ON cs.id_ = cssw.course_schedule_id_
				 LEFT JOIN course_schedule_student_payment cssp ON cssp.course_schedule_id_ = cssw.course_schedule_id_
				 LEFT JOIN student_attendance sa ON sa.course_schedule_id_ = cssw.course_schedule_id_
				 LEFT JOIN student_course_homework sch ON sch.course_schedule_id_ = cssw.course_schedule_id_
				 LEFT JOIN (SELECT DISTINCT course_ids_ FROM student_extracurricular_exercises_situation_ WHERE course_ids_ IS NOT NULL) sees ON FIND_IN_SET(cssw.course_schedule_id_,sees.course_ids_)
		WHERE cs.group_type_ = 'MUSIC' AND css.course_schedule_id_ IS NULL
		GROUP BY cssw.course_schedule_id_;
	</select>
</mapper>
