<?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.ExtracurricularExercisesDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.ExtracurricularExercises" id="ExtracurricularExercises">
		<result column="id_" property="id" />
		<result column="music_group_id_" property="musicGroupId" />
		<result column="teacher_id_" property="teacherId" />
		<result column="username_" property="teacher.username" />
		<result column="student_id_list_" property="studentIdList" />
		<result column="batch_no_" property="batchNo" />
		<result column="title_" property="title" />
		<result column="attachments_" property="attachments" />
		<result column="content_" property="content" />
		<result column="expire_date_" property="expireDate" />
		<result column="completed_num_" property="completedNum" />
		<result column="expect_num_" property="expectNum" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="organ_name_" property="organName" />
		<result column="tenant_id_" property="tenantId" />
		<result column="music_score_id_" property="musicScoreId" />
	</resultMap>

	<sql id="queryPageCondition">
		<where>
			ee.tenant_id_ = #{tenantId}
			<if test="teacherId != null">
				and teacher_id_ = #{teacherId}
			</if>
			<if test="musicGroupId != null">
				and music_group_id_ = #{musicGroupId}
			</if>
			<if test="title != null">
				and title_ like '%' #{title} '%'
			</if>
			<if test="batchNo != null">
				and batch_no_ = #{batchNo}
			</if>
			<if test="organIdList != null">
				AND FIND_IN_SET(o.id_, #{organIdList})
			</if>
			<if test="assignStartTime != null">
				AND date(ee.create_time_) &gt;= #{assignStartTime}
			</if>
			<if test="assignEndTime != null">
				AND date(ee.create_time_) &lt;= #{assignEndTime}
			</if>
			<if test="cloudHomeworkFlag != null and cloudHomeworkFlag == true">
				AND eer.music_score_id_ IS NOT NULL AND eer.music_score_id_ != ''
			</if>
			<if test="cloudHomeworkFlag != null and cloudHomeworkFlag == false">
				AND (eer.music_score_id_ IS NULL OR eer.music_score_id_ = '')
			</if>
		</where>
	</sql>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExtracurricularExercises" >
		SELECT * FROM extracurricular_exercises WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExtracurricularExercises">
		SELECT * FROM extracurricular_exercises where tenant_id_ = #{tenantId} ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ExtracurricularExercises" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO extracurricular_exercises (music_group_id_,teacher_id_,student_id_list_,batch_no_,
		                                       title_,attachments_,content_,expire_date_,completed_num_,expect_num_,create_time_,update_time_,tenant_id_)
		VALUES(#{musicGroupId},#{teacherId},#{studentIdList},#{batchNo},#{title},#{attachments},
		       #{content},#{expireDate},#{completedNum},#{expectNum},NOW(), NOW(),#{tenantId})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.ExtracurricularExercises">
		UPDATE extracurricular_exercises
		<set>
			<if test="studentIdList != null">
			student_id_list_ = #{studentIdList},
			</if>
			<if test="expireDate != null">
			expire_date_ = #{expireDate},
			</if>
			<if test="batchNo != null">
			batch_no_ = #{batchNo},
			</if>
			<if test="musicGroupId != null">
			music_group_id_ = #{musicGroupId},
			</if>
			<if test="teacherId != null">
			teacher_id_ = #{teacherId},
			</if>
			<if test="title != null">
			title_ = #{title},
			</if>
			<if test="expectNum != null">
			expect_num_ = #{expectNum},
			</if>
			<if test="content != null">
			content_ = #{content},
			</if>
			<if test="attachments != null">
			attachments_ = #{attachments},
			</if>
			<if test="completedNum != null">
			completed_num_ = #{completedNum},
			</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM extracurricular_exercises WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ExtracurricularExercises" parameterType="map">
		SELECT ee.id_,ee.teacher_id_,ee.student_id_list_,ee.batch_no_,ee.title_,ee.attachments_,ee.music_group_id_,
		       ee.content_,ee.expire_date_,ee.completed_num_,ee.expect_num_,ee.create_time_,ee.update_time_,ee.tenant_id_
		     ,u.real_name_ username_,o.name_ organ_name_,eer.music_score_id_
		FROM extracurricular_exercises ee LEFT JOIN sys_user u ON ee.teacher_id_ = u.id_
		LEFT JOIN teacher t ON t.id_ = ee.teacher_id_
		LEFT JOIN organization o ON o.id_ = t.organ_id_
		LEFT JOIN extracurricular_exercises_reply eer ON ee.id_ = eer.extracurricular_exercises_id_
		<include refid="queryPageCondition"/>
		GROUP BY ee.id_
		ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(DISTINCT ee.id_) FROM extracurricular_exercises ee left join sys_user u on ee.teacher_id_ = u.id_
		left join teacher t on t.id_ = ee.teacher_id_
		left join organization o on o.id_ = t.organ_id_
		LEFT JOIN extracurricular_exercises_reply eer ON ee.id_ = eer.extracurricular_exercises_id_
		<include refid="queryPageCondition"/>
	</select>

	<sql id="queryExtraExercisesCondition">
		<where>
			tenant_id_ = #{tenantId}
			<if test="teacherId!=null">
				AND teacher_id_ = #{teacherId}
			</if>
			<if test="createTime!=null">
				AND DATE_FORMAT(create_time_, '%Y-%m') = DATE_FORMAT(#{createTime}, '%Y-%m')
			</if>
		</where>
	</sql>

	<select id="countExtraExercises" resultType="int">
		SELECT COUNT(id_) FROM extracurricular_exercises
		<include refid="queryExtraExercisesCondition"/>
	</select>
	<select id="findExtraExercises" resultMap="ExtracurricularExercises">
		SELECT * FROM extracurricular_exercises
		<include refid="queryExtraExercisesCondition"/>
		ORDER BY create_time_ DESC
		<include refid="global.limit"/>
	</select>
    <select id="findRepeatLastExercises" resultMap="ExtracurricularExercises">
		SELECT
			*
		FROM
			extracurricular_exercises
		WHERE
			teacher_id_ = #{teacherId}
			AND student_id_list_ = #{studentIdList}
			AND content_ = #{content}
		ORDER BY create_time_ DESC LIMIT 1
	</select>
    <select id="findNoExercisesStudentsInThisWeekWithTeacher"
            resultType="com.ym.mec.biz.dal.dto.BasicUserDto">
		SELECT
			su.id_ userId,
			su.username_ name,
			su.avatar_ headUrl,
			GROUP_CONCAT(sub.name_) subjectName,
		    stu.member_rank_setting_id_ memberRankSettingId,
			stu.subject_id_list_ subjectIdList,
			sees.music_group_id_ musicGroupId
		FROM
			student_extracurricular_exercises_situation_ sees
			<if test="classGroupId!=null">
				LEFT JOIN class_group_student_mapper cgsm ON sees.student_id_ = cgsm.user_id_
			</if>
			LEFT JOIN student stu ON stu.user_id_ = sees.student_id_
			LEFT JOIN sys_user su ON su.id_ = sees.student_id_
			LEFT JOIN `subject` sub ON FIND_IN_SET( sub.id_, stu.subject_id_list_ )
		WHERE
			sees.monday_ = #{startDate}
			AND sees.teacher_id_ = #{teacherId}
			AND sees.actual_exercises_num_ &lt; sees.expect_exercises_num_
			AND sees.serve_type_ = 'EXERCISE'
			<if test="hasMember != null">
				<if test="hasMember == 1">
					AND stu.member_rank_setting_id_ IS NOT NULL
				</if>
				<if test="hasMember == 0">
					AND stu.member_rank_setting_id_ IS NULL
				</if>
			</if>
			<if test="studentIds!=null and studentIds!=''">
				AND FIND_IN_SET(sees.student_id_,#{studentIds})
			</if>
			<if test="musicGroupId!=null and musicGroupId!=''">
				AND sees.music_group_id_=#{musicGroupId}
			</if>
			<if test="classGroupId!=null">
				AND cgsm.class_group_id_ = #{classGroupId}
			</if>
			<if test="subjectId!=null">
				AND FIND_IN_SET(#{subjectId}, stu.subject_id_list_)
			</if>
			<if test="search!=null">
				AND (su.username_ LIKE CONCAT('%', #{search}, '%') OR su.phone_ LIKE CONCAT(#{search}, '%'))
			</if>
		GROUP BY su.id_
		ORDER BY
			su.id_;
	</select>
	<resultMap id="ExtracurricularExercisesHomeworkListDto" type="com.ym.mec.biz.dal.dto.ExtracurricularExercisesHomeworkListDto">
		<result property="type" column="type_"/>
		<result property="homeworkId" column="id_"/>
	</resultMap>
	<select id="countExtraExercilsesHomeworks" resultType="int">
		SELECT COUNT(id_) FROM (
		SELECT id_, create_time_,'EXERCISES' type_
		FROM extracurricular_exercises
		<where>
			tenant_id_ = #{tenantId}
			<if test="teacherId!=null">
				AND teacher_id_ = #{teacherId}
			</if>
			<if test="createTime!=null">
				AND DATE_FORMAT(create_time_, '%Y-%m') = DATE_FORMAT(#{createTime}, '%Y-%m')
			</if>
			<if test="startTime != null and startTime != ''">
				AND DATE_FORMAT(#{createTime}, '%Y-%m-%d') BETWEEN #{startTime} AND #{endTime}
			</if>
		</where>
		UNION ALL
		SELECT ch.id_,ch.create_time_,'HOMEWORK' type_ FROM course_homework ch
		LEFT JOIN course_schedule_teacher_salary csts ON ch.course_schedule_id_ = csts.course_schedule_id_ AND csts.teacher_role_ = 'BISHOP'
		<where>
			csts.tenant_id_ = #{tenantId}
			<if test="teacherId!=null">
				AND csts.user_id_ = #{teacherId}
			</if>
			<if test="classGroupId!=null">
				AND ch.class_group_id_=#{classGroupId}
			</if>
			<if test="createTime!=null">
				AND DATE_FORMAT(ch.create_time_,'%Y-%m')=DATE_FORMAT(#{createTime}, '%Y-%m')
			</if>
		</where>
		    ) h
	</select>
	<select id="findExtraExercilsesHomeworks" resultMap="ExtracurricularExercisesHomeworkListDto">
		SELECT * FROM (
		SELECT id_, create_time_,'EXERCISES' type_
		FROM extracurricular_exercises
		<where>
			tenant_id_ = #{tenantId}
			<if test="teacherId!=null">
				AND teacher_id_ = #{teacherId}
			</if>
			<if test="createTime!=null">
				AND DATE_FORMAT(create_time_, '%Y-%m') = DATE_FORMAT(#{createTime}, '%Y-%m')
			</if>
			<if test="startTime != null and startTime != ''">
				AND DATE_FORMAT(#{createTime}, '%Y-%m-%d') BETWEEN #{startTime} AND #{endTime}
			</if>
		</where>
		UNION ALL
		SELECT ch.id_,ch.create_time_,'HOMEWORK' type_ FROM course_homework ch
		LEFT JOIN course_schedule_teacher_salary csts ON ch.course_schedule_id_ = csts.course_schedule_id_ AND csts.teacher_role_ = 'BISHOP'
		<where>
			csts.tenant_id_ = #{tenantId}
			<if test="teacherId!=null">
				AND csts.user_id_ = #{teacherId}
			</if>
			<if test="classGroupId!=null">
				AND ch.class_group_id_=#{classGroupId}
			</if>
			<if test="createTime!=null">
				AND DATE_FORMAT(ch.create_time_,'%Y-%m')=DATE_FORMAT(#{createTime}, '%Y-%m')
			</if>
		</where>) h
	</select>
	<select id="findByIdList" resultMap="com.ym.mec.biz.dal.dao.CourseHomeworkDao.teacherHomeworkListDto">
		SELECT id_ homework_id_,title_,content_,completed_num_,expect_num_,music_score_id_,
		       expire_date_ expiry_date_,DATE_FORMAT(create_time_,'%Y-%m-%d') 'day_'
		FROM extracurricular_exercises WHERE id_ IN
		<foreach collection="exercisesIdList" separator="," item="item" open="(" close=")">
			#{item}
		</foreach>
	</select>
</mapper>
