<?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.StudentExtracurricularExercisesSituationDao">

	<resultMap type="com.ym.mec.biz.dal.entity.StudentExtracurricularExercisesSituation" id="StudentExtracurricularExercisesSituation">
		<result column="id_" property="id" />
		<result column="student_id_" property="studentId" />
		<result column="week_of_year_" property="weekOfYear" />
		<result column="monday_" property="monday" />
		<result column="sunday_" property="sunday" />
		<result column="teacher_id_" property="teacherId" />
		<result column="expect_exercises_num_" property="expectExercisesNum" />
		<result column="actual_exercises_num_" property="actualExercisesNum" />
		<result column="exercises_reply_num_" property="exercisesReplyNum" />
		<result column="exercises_message_num_" property="exercisesMessageNum" />
		<result column="exercises_message_timely_num_" property="exercisesMessageTimelyNum" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="last_submit_time_" property="lastSubmitTime"/>
		<result column="serve_type_" property="serveType"/>
		<result column="course_ids_" property="courseIds"/>
		<result column="not_over_course_ids_" property="notOverCourseIds"/>
		<result column="not_over_course_num_" property="notOverCourseNum"/>
	</resultMap>

	<resultMap id="StudentExercisesSituationDto" type="com.ym.mec.biz.dal.dto.StudentExercisesSituationDto" extends="StudentExtracurricularExercisesSituation">
		<result column="student_name_" property="studentName"/>
		<result column="teacher_name_" property="teacherName"/>
		<result column="organ_name_" property="organName"/>
		<result column="exist_vip_course_" property="existVipCourse"/>
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="StudentExtracurricularExercisesSituation" >
		SELECT * FROM student_extracurricular_exercises_situation_ WHERE id_ = #{id} 
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="StudentExtracurricularExercisesSituation">
		SELECT * FROM student_extracurricular_exercises_situation_ ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentExtracurricularExercisesSituation" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" >
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
		</selectKey>
		-->
		INSERT INTO student_extracurricular_exercises_situation_ (id_,student_id_,week_of_year_,monday_,sunday_,teacher_id_,expect_exercises_num_,
		                                                          actual_exercises_num_,exercises_reply_num_,exercises_message_num_,
		                                                          exercises_message_timely_num_,create_time_,update_time_,last_submit_time_,
		                                                          serve_type_,course_ids_,not_over_course_ids_,not_over_course_num_)
		VALUES(#{id},#{studentId},#{weekOfYear},#{monday},#{sunday},#{teacherId},#{expectExercisesNum},#{actualExercisesNum},#{exercisesReplyNum},
		       #{exercisesMessageNum},#{exercisesMessageTimelyNum},NOW(),NOW(),#{lastSubmitTime},#{serveType},#{courseIds},#{notOverCourseIds},#{notOverCourseNum})
	</insert>

	<insert id="batchInsert" parameterType="com.ym.mec.biz.dal.entity.StudentExtracurricularExercisesSituation" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO student_extracurricular_exercises_situation_ (student_id_,week_of_year_,monday_,sunday_,teacher_id_,expect_exercises_num_,
		                                                          actual_exercises_num_,exercises_reply_num_,exercises_message_num_,
		                                                          exercises_message_timely_num_,create_time_,update_time_,last_submit_time_,
		                                                          serve_type_,course_ids_,not_over_course_ids_,not_over_course_num_)
		VALUES
		<foreach collection="situations" item="situation" separator=",">
			(#{situation.studentId},#{situation.weekOfYear},#{situation.monday},#{situation.sunday},#{situation.teacherId},#{situation.expectExercisesNum},
			#{situation.actualExercisesNum},#{situation.exercisesReplyNum},#{situation.exercisesMessageNum},#{situation.exercisesMessageTimelyNum},
			NOW(),NOW(),#{situation.lastSubmitTime},#{situation.serveType},#{situation.courseIds},#{situation.notOverCourseIds},#{situation.notOverCourseNum})
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentExtracurricularExercisesSituation">
		UPDATE student_extracurricular_exercises_situation_
		<set>
			<if test="exercisesReplyNum != null">
				exercises_reply_num_ = #{exercisesReplyNum},
			</if>
			<if test="monday != null">
				monday_ = #{monday},
			</if>
			<if test="id != null">
				id_ = #{id},
			</if>
			<if test="weekOfYear != null">
				week_of_year_ = #{weekOfYear},
			</if>
			<if test="exercisesMessageNum != null">
				exercises_message_num_ = #{exercisesMessageNum},
			</if>
			<if test="createTime != null">
				create_time_ = #{createTime},
			</if>
			<if test="teacherId != null">
				teacher_id_ = #{teacherId},
			</if>
			<if test="actualExercisesNum != null">
				actual_exercises_num_ = #{actualExercisesNum},
			</if>
			<if test="exercisesMessageTimelyNum != null">
				exercises_message_timely_num_ = #{exercisesMessageTimelyNum},
			</if>
			<if test="studentId != null">
				student_id_ = #{studentId},
			</if>
			<if test="expectExercisesNum != null">
				expect_exercises_num_ = #{expectExercisesNum},
			</if>
			<if test="sunday != null">
				sunday_ = #{sunday},
			</if>
			<if test="lastSubmitTime!=null">
				last_submit_time_=#{lastSubmitTime},
			</if>
			<if test="serveType!=null">
				serve_type_=#{serveType},
			</if>
			<if test="courseIds!=null and courseIds!=''">
				course_ids_=#{courseIds},
			</if>
			<if test="notOverCourseIds!=null and notOverCourseIds!=''">
				not_over_course_ids_=#{notOverCourseIds},
			</if>
			<if test="notOverCourseNum!=null">
				not_over_course_num_=#{notOverCourseNum},
			</if>
			update_time_ = #{updateTime}
		</set> WHERE id_ = #{id}
	</update>

	<update id="batchUpdate" parameterType="com.ym.mec.biz.dal.entity.StudentExtracurricularExercisesSituation">
		<foreach collection="situations" item="situation" separator=";">
			UPDATE student_extracurricular_exercises_situation_
			<set>
				<if test="situation.exercisesReplyNum != null">
					exercises_reply_num_ = #{situation.exercisesReplyNum},
				</if>
				<if test="situation.monday != null">
					monday_ = #{situation.monday},
				</if>
				<if test="situation.weekOfYear != null">
					week_of_year_ = #{situation.weekOfYear},
				</if>
				<if test="situation.exercisesMessageNum != null">
					exercises_message_num_ = #{situation.exercisesMessageNum},
				</if>
				<if test="situation.teacherId != null">
					teacher_id_ = #{situation.teacherId},
				</if>
				<if test="situation.actualExercisesNum != null">
					actual_exercises_num_ = #{situation.actualExercisesNum},
				</if>
				<if test="situation.exercisesMessageTimelyNum != null">
					exercises_message_timely_num_ = #{situation.exercisesMessageTimelyNum},
				</if>
				<if test="situation.studentId != null">
					student_id_ = #{situation.studentId},
				</if>
				<if test="situation.expectExercisesNum != null">
					expect_exercises_num_ = #{situation.expectExercisesNum},
				</if>
				<if test="situation.sunday != null">
					sunday_ = #{situation.sunday},
				</if>
				<if test="situation.lastSubmitTime!=null">
					last_submit_time_=#{situation.lastSubmitTime},
				</if>
				<if test="situation.serveType!=null">
					serve_type_=#{situation.serveType},
				</if>
				<if test="situation.courseIds!=null">
					course_ids_=#{situation.courseIds},
				</if>
				<if test="situation.notOverCourseIds!=null and situation.notOverCourseIds!=''">
					not_over_course_ids_=#{situation.notOverCourseIds},
				</if>
				<if test="situation.notOverCourseNum!=null">
					not_over_course_num_=#{situation.notOverCourseNum},
				</if>
				update_time_ = NOW()
			</set> WHERE id_ = #{situation.id}
		</foreach>
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM student_extracurricular_exercises_situation_ WHERE id_ = #{id} 
	</delete>
	<delete id="deleteByMonday">
		DELETE FROM student_extracurricular_exercises_situation_ WHERE monday_ = #{monday}
		<if test="studentIds!=null and studentIds.size()>0">
			AND student_id_ IN
			<foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
				#{studentId}
			</foreach>
		</if>
	</delete>
	<delete id="deleteByStudent">
		DELETE FROM student_extracurricular_exercises_situation_ WHERE student_id_ = #{studentId}
		<if test="monday!=null and monday!=''">
			AND monday_=#{monday}
		</if>
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="StudentExtracurricularExercisesSituation" parameterType="map">
		SELECT * FROM student_extracurricular_exercises_situation_ ORDER BY id_ <include refid="global.limit"/>
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM student_extracurricular_exercises_situation_
	</select>

	<select id="findExercisesSituations" resultMap="StudentExercisesSituationDto">
		SELECT
		sees.id_ id_,
		student_id_,
		stu.username_ student_name_,
		tea.real_name_ teacher_name_,
		o.name_ organ_name_,
		sees.monday_,
		sees.sunday_,
		sees.serve_type_,
		sees.course_ids_,
		expect_exercises_num_ expect_exercises_num_,
		actual_exercises_num_ actual_exercises_num_,
		<if test="submitStartDate==null or submitEndDate==null">
			exercises_reply_num_ exercises_reply_num_,
			exercises_message_num_ exercises_message_num_,
			exercises_message_timely_num_ exercises_message_timely_num_,
			last_submit_time_ last_submit_time_
		</if>
		<if test="submitStartDate!=null and submitEndDate!=null">
			IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_reply_num_,0) exercises_reply_num_,
			IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_message_num_,0) exercises_message_num_,
			IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_message_timely_num_,0) exercises_message_timely_num_,
			IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},last_submit_time_, NULL) last_submit_time_
		</if>
		FROM
			student_extracurricular_exercises_situation_ sees
			LEFT JOIN sys_user stu ON stu.id_=sees.student_id_
			LEFT JOIN teacher t ON t.id_=sees.teacher_id_
			LEFT JOIN sys_user tea ON tea.id_=sees.teacher_id_
			LEFT JOIN organization o ON stu.organ_id_=o.id_
		WHERE
		sees.monday_ &gt;= #{monday}
		AND sees.sunday_ &lt;= #{sunday}
		<if test="teacherId!=null">
			AND sees.teacher_id_ = #{teacherId}
		</if>
		<if test="search!=null">
			AND (stu.id_=#{search} OR stu.username_ LIKE CONCAT('%', #{search}, '%'))
		</if>
		<if test="organIdList != null">
			AND FIND_IN_SET(t.organ_id_,#{organIdList})
		</if>
		<if test="existVipCourse!=null and existVipCourse==1">
			AND EXISTS (SELECT cssp.id_ FROM course_schedule_student_payment cssp LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_ WHERE cssp.user_id_=sees.student_id_ AND cssp.group_type_='VIP' AND class_date_ BETWEEN #{monday} AND #{sunday})
		</if>
		<if test="existVipCourse!=null and existVipCourse==0">
			AND NOT EXISTS (SELECT cssp.id_ FROM course_schedule_student_payment cssp LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_ WHERE cssp.user_id_=sees.student_id_ AND cssp.group_type_='VIP' AND class_date_ BETWEEN #{monday} AND #{sunday})
		</if>
		<if test="existPracticeCourse!=null and existPracticeCourse==1">
			AND EXISTS (SELECT cssp.id_ FROM course_schedule_student_payment cssp LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_ LEFT JOIN practice_group pg ON pg.id_=cssp.music_group_id_ WHERE cssp.user_id_=sees.student_id_ AND cssp.group_type_='PRACTICE' AND pg.type_='CHARGE' AND class_date_ BETWEEN #{monday} AND #{sunday})
		</if>
		<if test="existPracticeCourse!=null and existPracticeCourse==0">
			AND NOT EXISTS (SELECT cssp.id_ FROM course_schedule_student_payment cssp LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_ LEFT JOIN practice_group pg ON pg.id_=cssp.music_group_id_ WHERE cssp.user_id_=sees.student_id_ AND cssp.group_type_='PRACTICE' AND pg.type_='CHARGE' AND class_date_ BETWEEN #{monday} AND #{sunday})
		</if>

		<if test="expectExercisesNum!=null">
			AND expect_exercises_num_ = #{expectExercisesNum}
		</if>
		<if test="actualExercisesNumIsAchieve!=null and actualExercisesNumIsAchieve==0">
			AND actual_exercises_num_ &lt; expect_exercises_num_
		</if>
		<if test="actualExercisesNumIsAchieve!=null and actualExercisesNumIsAchieve==1">
			AND actual_exercises_num_ &gt;= expect_exercises_num_
		</if>
		<if test="submitStartDate==null or submitEndDate==null">
			<if test="exercisesReplyNumIsAchieve!=null and exercisesReplyNumIsAchieve==0">
				AND exercises_reply_num_ &lt; actual_exercises_num_
			</if>
			<if test="exercisesReplyNumIsAchieve!=null and exercisesReplyNumIsAchieve==1">
				AND exercises_reply_num_ &gt;= actual_exercises_num_
			</if>
			<if test="exercisesMessageNumIsAchieve!=null and exercisesMessageNumIsAchieve==0">
				AND exercises_message_num_ &lt; exercises_reply_num_
			</if>
			<if test="serviceIsError!=null and serviceIsError">
				AND (actual_exercises_num_ &lt; expect_exercises_num_ OR exercises_message_num_ &lt; exercises_reply_num_)
			</if>
			<if test="exercisesMessageNumIsAchieve!=null and exercisesMessageNumIsAchieve==1">
				AND exercises_message_num_ &gt;= exercises_reply_num_
			</if>
			<if test="exercisesMessageTimelyNumIsAchieve!=null and exercisesMessageTimelyNumIsAchieve==0">
				AND exercises_message_timely_num_ &lt; exercises_message_num_
			</if>
			<if test="exercisesMessageTimelyNumIsAchieve!=null and exercisesMessageTimelyNumIsAchieve==1">
				AND exercises_message_timely_num_ &gt;= exercises_message_num_
			</if>
		</if>
		<if test="submitStartDate!=null and submitEndDate!=null">
			<if test="exercisesReplyNumIsAchieve!=null and exercisesReplyNumIsAchieve==0">
				AND IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_reply_num_,0) &lt; actual_exercises_num_
			</if>
			<if test="exercisesReplyNumIsAchieve!=null and exercisesReplyNumIsAchieve==1">
				AND IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_reply_num_,0) &gt;= actual_exercises_num_
			</if>
			<if test="exercisesMessageNumIsAchieve!=null and exercisesMessageNumIsAchieve==0">
				AND IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_message_num_,0) &lt; exercises_reply_num_
			</if>
			<if test="serviceIsError!=null and serviceIsError">
				AND (actual_exercises_num_ &lt; expect_exercises_num_ OR IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_message_num_,0) &lt; exercises_reply_num_)
			</if>
			<if test="exercisesMessageNumIsAchieve!=null and exercisesMessageNumIsAchieve==1">
				AND IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_message_num_,0) &gt;= exercises_reply_num_
			</if>
			<if test="exercisesMessageTimelyNumIsAchieve!=null and exercisesMessageTimelyNumIsAchieve==0">
				AND IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_message_timely_num_,0) &lt; exercises_message_num_
			</if>
			<if test="exercisesMessageTimelyNumIsAchieve!=null and exercisesMessageTimelyNumIsAchieve==1">
				AND IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_message_timely_num_,0) &gt;= exercises_message_num_
			</if>
		</if>
		ORDER BY student_id_
		<include refid="global.limit"/>
	</select>

	<select id="countExercisesSituations" resultType="int">
		SELECT COUNT(1) FROM (
		SELECT
		1
		FROM
		student_extracurricular_exercises_situation_ sees
		LEFT JOIN sys_user stu ON stu.id_=sees.student_id_
		LEFT JOIN teacher t ON t.id_=sees.teacher_id_
		LEFT JOIN sys_user tea ON tea.id_=sees.teacher_id_
		LEFT JOIN organization o ON stu.organ_id_=o.id_
		WHERE
		sees.monday_ &gt;= #{monday}
		AND sees.sunday_ &lt;= #{sunday}
		<if test="teacherId!=null">
			AND sees.teacher_id_ = #{teacherId}
		</if>
		<if test="search!=null">
			AND (stu.id_=#{search} OR stu.username_ LIKE CONCAT('%', #{search}, '%'))
		</if>
		<if test="organIdList != null">
			AND FIND_IN_SET(t.organ_id_, #{organIdList})
		</if>
		<if test="existVipCourse!=null and existVipCourse==1">
			AND EXISTS (SELECT cssp.id_ FROM course_schedule_student_payment cssp LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_ WHERE cssp.user_id_=sees.student_id_ AND cssp.group_type_='VIP' AND class_date_ BETWEEN #{monday} AND #{sunday})
		</if>
		<if test="existVipCourse!=null and existVipCourse==0">
			AND NOT EXISTS (SELECT cssp.id_ FROM course_schedule_student_payment cssp LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_ WHERE cssp.user_id_=sees.student_id_ AND cssp.group_type_='VIP' AND class_date_ BETWEEN #{monday} AND #{sunday})
		</if>
		<if test="existPracticeCourse!=null and existPracticeCourse==1">
			AND EXISTS (SELECT cssp.id_ FROM course_schedule_student_payment cssp LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_ LEFT JOIN practice_group pg ON pg.id_=cssp.music_group_id_ WHERE cssp.user_id_=sees.student_id_ AND cssp.group_type_='PRACTICE' AND pg.type_='CHARGE' AND class_date_ BETWEEN #{monday} AND #{sunday})
		</if>
		<if test="existPracticeCourse!=null and existPracticeCourse==0">
			AND NOT EXISTS (SELECT cssp.id_ FROM course_schedule_student_payment cssp LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_ LEFT JOIN practice_group pg ON pg.id_=cssp.music_group_id_ WHERE cssp.user_id_=sees.student_id_ AND cssp.group_type_='PRACTICE' AND pg.type_='CHARGE' AND class_date_ BETWEEN #{monday} AND #{sunday})
		</if>

		<if test="expectExercisesNum!=null">
			AND expect_exercises_num_ = #{expectExercisesNum}
		</if>
		<if test="actualExercisesNumIsAchieve!=null and actualExercisesNumIsAchieve==0">
			AND actual_exercises_num_ &lt; expect_exercises_num_
		</if>
		<if test="actualExercisesNumIsAchieve!=null and actualExercisesNumIsAchieve==1">
			AND actual_exercises_num_ &gt;= expect_exercises_num_
		</if>
		<if test="submitStartDate==null or submitEndDate==null">
			<if test="exercisesReplyNumIsAchieve!=null and exercisesReplyNumIsAchieve==0">
				AND exercises_reply_num_ &lt; actual_exercises_num_
			</if>
			<if test="exercisesReplyNumIsAchieve!=null and exercisesReplyNumIsAchieve==1">
				AND exercises_reply_num_ &gt;= actual_exercises_num_
			</if>
			<if test="exercisesMessageNumIsAchieve!=null and exercisesMessageNumIsAchieve==0">
				AND exercises_message_num_ &lt; exercises_reply_num_
			</if>
			<if test="serviceIsError!=null and serviceIsError">
				AND (actual_exercises_num_ &lt; expect_exercises_num_ OR exercises_message_num_ &lt; exercises_reply_num_)
			</if>
			<if test="exercisesMessageNumIsAchieve!=null and exercisesMessageNumIsAchieve==1">
				AND exercises_message_num_ &gt;= exercises_reply_num_
			</if>
			<if test="exercisesMessageTimelyNumIsAchieve!=null and exercisesMessageTimelyNumIsAchieve==0">
				AND exercises_message_timely_num_ &lt; exercises_message_num_
			</if>
			<if test="exercisesMessageTimelyNumIsAchieve!=null and exercisesMessageTimelyNumIsAchieve==1">
				AND exercises_message_timely_num_ &gt;= exercises_message_num_
			</if>
		</if>
		<if test="submitStartDate!=null and submitEndDate!=null">
			<if test="exercisesReplyNumIsAchieve!=null and exercisesReplyNumIsAchieve==0">
				AND IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_reply_num_,0) &lt; actual_exercises_num_
			</if>
			<if test="exercisesReplyNumIsAchieve!=null and exercisesReplyNumIsAchieve==1">
				AND IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_reply_num_,0) &gt;= actual_exercises_num_
			</if>
			<if test="exercisesMessageNumIsAchieve!=null and exercisesMessageNumIsAchieve==0">
				AND IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_message_num_,0) &lt; exercises_reply_num_
			</if>
			<if test="serviceIsError!=null and serviceIsError">
				AND (actual_exercises_num_ &lt; expect_exercises_num_ OR IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_message_num_,0) &lt; exercises_reply_num_)
			</if>
			<if test="exercisesMessageNumIsAchieve!=null and exercisesMessageNumIsAchieve==1">
				AND IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_message_num_,0) &gt;= exercises_reply_num_
			</if>
			<if test="exercisesMessageTimelyNumIsAchieve!=null and exercisesMessageTimelyNumIsAchieve==0">
				AND IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_message_timely_num_,0) &lt; exercises_message_num_
			</if>
			<if test="exercisesMessageTimelyNumIsAchieve!=null and exercisesMessageTimelyNumIsAchieve==1">
				AND IF(DATE_FORMAT(last_submit_time_, '%Y-%m-%d') BETWEEN #{submitStartDate} AND #{submitEndDate},exercises_message_timely_num_,0) &gt;= exercises_message_num_
			</if>
		</if>
		) tmp
	</select>

	<select id="findExercisesSituationsById" resultMap="StudentExtracurricularExercisesSituation">
		SELECT * FROM student_extracurricular_exercises_situation_ WHERE id_ IN
		<foreach collection="ids" item="id" separator="," open="(" close=")">
			#{id}
		</foreach>
	</select>

	<select id="findTeacherExercisesServiceSituations" resultType="com.ym.mec.biz.dal.dto.TeacherExercisesServiceDto">
		SELECT
		teacher_id_ teacherId,
		COUNT( exercises_reply_num_ ) expectExercisesNum,
		COUNT( CASE WHEN actual_exercises_num_ = 1 THEN 1 ELSE NULL END ) actualExercisesNum
		FROM
		student_extracurricular_exercises_situation_
		FORCE INDEX(monday_)
		WHERE monday_ = #{monday}
		AND teacher_id_ IN
		<foreach collection="teacherIds" item="teacherId" open="(" close=")" separator=",">
			#{teacherId}
		</foreach>
		GROUP BY
		teacher_id_;
	</select>

	<select id="findLastWeekTodayUpdateNum" resultType="int">
		SELECT COUNT(id_) FROM student_extracurricular_exercises_situation_ WHERE sunday_=#{sunday} AND DATE_FORMAT(update_time_,'%Y-%m-%d')=DATE_FORMAT(NOW(),'%Y-%m-%d')
    </select>

	<select id="findStudentExercisesSituationsWithMonDay" resultMap="StudentExtracurricularExercisesSituation">
		SELECT * FROM student_extracurricular_exercises_situation_ WHERE monday_=#{monday} AND student_id_=#{studentId}
	</select>

	<select id="getNoFinishedServiceTeacher" resultType="java.util.Map">
		SELECT
			teacher_id_ AS 'key',
			COUNT( student_id_ ) AS 'value'
		FROM
			student_extracurricular_exercises_situation_
		WHERE
			actual_exercises_num_ = 0
			AND serve_type_ = 'EXERCISE'
			AND monday_ = #{monday}
			AND teacher_id_ IS NOT NULL
		GROUP BY
			teacher_id_
	</select>

	<select id="countWeekServiceNum" resultType="int">
		SELECT COUNT(id_) FROM student_extracurricular_exercises_situation_ WHERE monday_=#{sunday}
	</select>

	<select id="findWeekServiceWithStudents" resultMap="StudentExtracurricularExercisesSituation">
		SELECT * FROM student_extracurricular_exercises_situation_
		WHERE monday_=#{monday}
		  <if test="teacherId!=null">
			  AND teacher_id_=#{teacherId}
		  </if>
		  <if test="studentIds!=null and studentIds.size()>0">
			  AND student_id_ IN
			  <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
				  #{studentId}
			  </foreach>
		  </if>
	</select>
    <select id="findTeacherNoStartServices" resultMap="StudentExtracurricularExercisesSituation">
		SELECT * FROM student_extracurricular_exercises_situation_ WHERE teacher_id_=#{teacherId} AND actual_exercises_num_&lt;=0 AND serve_type_='HOMEWORK';
	</select>


	<select id="getTeacherHomeworkCourseIdsWithMonday" resultType="string">
		SELECT course_ids_ FROM student_extracurricular_exercises_situation_
		WHERE teacher_id_=#{teacherId}
		  	AND serve_type_='HOMEWORK'
			<if test="monday!=null and monday!=''">
				AND monday_=#{monday}
			</if>
	</select>

	<select id="findServiceWithCourse"
			resultMap="StudentExtracurricularExercisesSituation">
		SELECT * FROM student_extracurricular_exercises_situation_ WHERE monday_=#{monday} AND FIND_IN_SET(#{courseId}, course_ids_)
	</select>

	<resultMap id="TeacherServeDto" type="com.ym.mec.biz.dal.dto.TeacherServeDto">
		<result column="organ_name_" property="organName"/>
		<result column="monday_" property="monday"/>
		<result column="sunday_" property="sunday"/>
		<result column="teacher_id_" property="teacherId"/>
		<result column="teacher_name_" property="teacherName"/>
		<result column="homework_num_" property="homeworkNum"/>
		<result column="exercise_num_" property="exerciseNum"/>
		<result column="un_done_" property="unDone"/>
		<result column="remind_time_" property="remindDate"/>
		<result column="operator_name_" property="operatorName"/>
		<result column="job_nature_" property="jobNature" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="expect_exercises_num_" property="expectExercisesNum" />
		<result column="actual_exercises_num_" property="actualExercisesNum" />
		<result column="exercises_reply_num_" property="exercisesReplyNum" />
		<result column="exercises_message_num_" property="exercisesMessageNum" />
		<result column="exercises_message_timely_num_" property="exercisesMessageTimelyNum" />
	</resultMap>

	<sql id="queryTeacherServeInfoCondition">
		<where>
			<if test="organIds!=null and organIds.size()>0">
				AND tea.organ_id_ IN
				<foreach collection="organIds" item="organId" open="(" close=")" separator=",">
					#{organId}
				</foreach>
			</if>
			<if test="monday!=null and monday!='' and sunday!=null and sunday!=''">
				AND sees.monday_ BETWEEN #{monday} AND #{sunday}
			</if>
<!--			<if test="unDone!=null and unDone==1">-->
<!--				AND tm.teacher_id_ IS NULL-->
<!--			</if>-->
			<if test="teacherId!=null">
				AND sees.teacher_id_ = #{teacherId}
			</if>
			<if test="reminded!=null and reminded==0">
				AND NOT EXISTS (SELECT id_ FROM teacher_remind WHERE teacher_id_=sees.teacher_id_ AND monday_ = sees.monday_ AND type_='SERVICE')
			</if>
			<if test="reminded!=null and reminded==1">
				AND EXISTS (SELECT id_ FROM teacher_remind WHERE teacher_id_=sees.teacher_id_ AND monday_ = sees.monday_ AND type_='SERVICE')
			</if>
			<if test="operatorId!=null">
				AND EXISTS (SELECT id_ FROM teacher_remind WHERE operator_id_=#{operatorId} AND teacher_id_=sees.teacher_id_ AND monday_ = sees.monday_ AND type_='SERVICE')
			</if>
			<if test="jobNature!=null">
				AND tea.job_nature_=#{jobNature,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
		</where>
	</sql>

	<select id="queryTeacherServeInfo" resultMap="TeacherServeDto">
		SELECT
			organ.name_ organ_name_,
			sees.monday_,
			sees.sunday_,
			sees.teacher_id_,
			su.real_name_ teacher_name_,
		    tea.job_nature_,
			SUM(expect_exercises_num_) expect_exercises_num_,
			SUM(actual_exercises_num_) actual_exercises_num_,
			SUM(exercises_reply_num_) exercises_reply_num_,
			SUM(exercises_message_num_) exercises_message_num_,
			SUM(exercises_message_timely_num_) exercises_message_timely_num_,
			SUM(CASE sees.serve_type_ WHEN 'HOMEWORK' THEN sees.expect_exercises_num_ ELSE 0 END) homework_num_,
			SUM(CASE sees.serve_type_ WHEN 'EXERCISE' THEN 1 ELSE 0 END) exercise_num_,
			SUM( sees.expect_exercises_num_-sees.not_over_course_num_ )> SUM( sees.actual_exercises_num_ ) un_done_
		FROM
			student_extracurricular_exercises_situation_ sees
			LEFT JOIN teacher tea ON tea.id_=sees.teacher_id_
			LEFT JOIN organization organ ON organ.id_=tea.organ_id_
			LEFT JOIN sys_user su ON tea.id_=su.id_
		<include refid="queryTeacherServeInfoCondition" />
		GROUP BY sees.monday_,sees.sunday_,sees.teacher_id_
		<trim prefix="HAVING" prefixOverrides="AND">
			<if test="unDone!=null">
				<if test="unDone==1">
					AND SUM( sees.expect_exercises_num_-sees.not_over_course_num_ ) &gt; SUM( sees.actual_exercises_num_ )
				</if>
				<if test="unDone==0">
					AND SUM( sees.expect_exercises_num_-sees.not_over_course_num_ ) &lt;= SUM( sees.actual_exercises_num_ )
				</if>
			</if>
			<if test="actualExercisesNumIsAchieve!=null and actualExercisesNumIsAchieve==0">
				AND SUM(actual_exercises_num_) &lt; SUM(expect_exercises_num_)
			</if>
			<if test="actualExercisesNumIsAchieve!=null and actualExercisesNumIsAchieve==1">
				AND SUM(actual_exercises_num_) &gt;= SUM(expect_exercises_num_)
			</if>
			<if test="exercisesReplyNumIsAchieve!=null and exercisesReplyNumIsAchieve==0">
				AND SUM(exercises_reply_num_) &lt; SUM(actual_exercises_num_)
			</if>
			<if test="exercisesReplyNumIsAchieve!=null and exercisesReplyNumIsAchieve==1">
				AND SUM(exercises_reply_num_) &gt;= SUM(actual_exercises_num_)
			</if>
			<if test="exercisesMessageNumIsAchieve!=null and exercisesMessageNumIsAchieve==0">
				AND SUM(exercises_message_num_) &lt; SUM(exercises_reply_num_)
			</if>
			<if test="serviceIsError!=null and serviceIsError">
				AND (SUM(actual_exercises_num_) &lt; SUM(expect_exercises_num_) OR SUM(exercises_message_num_) &lt; SUM(exercises_reply_num_))
			</if>
			<if test="exercisesMessageNumIsAchieve!=null and exercisesMessageNumIsAchieve==1">
				AND SUM(exercises_message_num_) &gt;= SUM(exercises_reply_num_)
			</if>
			<if test="exercisesMessageTimelyNumIsAchieve!=null and exercisesMessageTimelyNumIsAchieve==0">
				AND SUM(exercises_message_timely_num_) &lt; SUM(exercises_message_num_)
			</if>
			<if test="exercisesMessageTimelyNumIsAchieve!=null and exercisesMessageTimelyNumIsAchieve==1">
				AND SUM(exercises_message_timely_num_) &gt;= SUM(exercises_message_num_)
			</if>
		</trim>
		ORDER BY sees.monday_,sees.sunday_,sees.teacher_id_
		<include refid="global.limit"></include>
	</select>

	<select id="countTeacherServeInfo" resultType="int">
		SELECT COUNT(1) FROM (
			SELECT
				sees.id_
			FROM
				student_extracurricular_exercises_situation_ sees
				LEFT JOIN teacher tea ON tea.id_=sees.teacher_id_
			<include refid="queryTeacherServeInfoCondition" />
			GROUP BY sees.monday_,sees.sunday_,sees.teacher_id_
			<trim prefix="HAVING" prefixOverrides="AND">
				<if test="unDone!=null">
					<if test="unDone==1">
						AND SUM( sees.expect_exercises_num_-sees.not_over_course_num_ ) &gt; SUM( sees.actual_exercises_num_ )
					</if>
					<if test="unDone==0">
						AND SUM( sees.expect_exercises_num_-sees.not_over_course_num_ ) &lt;= SUM( sees.actual_exercises_num_ )
					</if>
				</if>
				<if test="actualExercisesNumIsAchieve!=null and actualExercisesNumIsAchieve==0">
					AND SUM(actual_exercises_num_) &lt; SUM(expect_exercises_num_)
				</if>
				<if test="actualExercisesNumIsAchieve!=null and actualExercisesNumIsAchieve==1">
					AND SUM(actual_exercises_num_) &gt;= SUM(expect_exercises_num_)
				</if>
				<if test="exercisesReplyNumIsAchieve!=null and exercisesReplyNumIsAchieve==0">
					AND SUM(exercises_reply_num_) &lt; SUM(actual_exercises_num_)
				</if>
				<if test="exercisesReplyNumIsAchieve!=null and exercisesReplyNumIsAchieve==1">
					AND SUM(exercises_reply_num_) &gt;= SUM(actual_exercises_num_)
				</if>
				<if test="exercisesMessageNumIsAchieve!=null and exercisesMessageNumIsAchieve==0">
					AND SUM(exercises_message_num_) &lt; SUM(exercises_reply_num_)
				</if>
				<if test="serviceIsError!=null and serviceIsError">
					AND (SUM(actual_exercises_num_) &lt; SUM(expect_exercises_num_) OR SUM(exercises_message_num_) &lt; SUM(exercises_reply_num_))
				</if>
				<if test="exercisesMessageNumIsAchieve!=null and exercisesMessageNumIsAchieve==1">
					AND SUM(exercises_message_num_) &gt;= SUM(exercises_reply_num_)
				</if>
				<if test="exercisesMessageTimelyNumIsAchieve!=null and exercisesMessageTimelyNumIsAchieve==0">
					AND SUM(exercises_message_timely_num_) &lt; SUM(exercises_message_num_)
				</if>
				<if test="exercisesMessageTimelyNumIsAchieve!=null and exercisesMessageTimelyNumIsAchieve==1">
					AND SUM(exercises_message_timely_num_) &gt;= SUM(exercises_message_num_)
				</if>
			</trim>
			ORDER BY sees.monday_,sees.sunday_,sees.teacher_id_
		) e
	</select>

	<select id="findTeacherServeWithDate" resultMap="StudentExtracurricularExercisesSituation">
		SELECT * FROM student_extracurricular_exercises_situation_
		WHERE monday_ BETWEEN #{monday} AND #{sunday}
			<if test="serveType!=null and serveType!=''">
				AND serve_type_=#{serveType}
			</if>
		  <if test="teacherIds!=null and teacherIds.size()>0">
			  AND teacher_id_ IN
			  <foreach collection="teacherIds" item="teacherId" open="(" close=")" separator=",">
				  #{teacherId}
			  </foreach>
		  </if>
	</select>
</mapper>
