<?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.ExtracurricularExercisesReplyDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.ExtracurricularExercisesReply" id="ExtracurricularExercisesReply">
		<result column="id_" property="id" />
		<result column="extracurricular_exercises_id_" property="extracurricularExercisesId" />
		<result column="title_" property="extracurricularExercises.title" />
		<result column="user_id_" property="userId" />
		<result column="username_" property="user.username" />
		<result column="attachments_" property="attachments" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="remark_" property="remark" />
		<result column="status_" property="status" />
		<result column="is_replied_" property="isReplied" />
		<result column="is_replied_timely_" property="isRepliedTimely" />
		<result column="is_view_" property="isView" />
		<result column="organ_name_" property="organName" />
		<result column="submit_time_" property="submitTime" />
	</resultMap>

	<resultMap id="ExtraExerciseStudentsDto" type="com.ym.mec.biz.dal.dto.ExtraExerciseStudentsDto" extends="ExtracurricularExercisesReply">
		<result column="student_name_" property="studentName"/>
		<result column="teacher_id_" property="teacherId"/>
		<result column="teacher_name_" property="teacherName"/>
		<result column="title_" property="title"/>
		<result column="content_" property="content"/>
		<result column="expire_date_" property="expireDate"/>
	</resultMap>

	<sql id="queryPageCondition">
		<where>
			<if test="extracurricularExercisesId != null">
				AND extracurricular_exercises_id_ = #{extracurricularExercisesId}
			</if>
			<if test="teacherId != null">
				AND teacher_id_ = #{teacherId}
			</if>
			<if test="organIdList != null">
				AND FIND_IN_SET(o.id_, #{organIdList})
			</if>
			<if test="studentId != null">
				AND user_id_ = #{studentId}
			</if>
			<if test="title != null">
				AND title_ like '%' #{title} '%'
			</if>
			<if test="status != null">
				AND eer.status_ = #{status}
			</if>
			<if test="isReplied != null">
				AND is_replied_ = #{isReplied}
			</if>
			<if test="isView != null">
				AND is_view_ = #{isView}
			</if>
			<if test="search != null">
				AND (title_ like concat('%',#{search},'%') or u.username_ like concat('%',#{search},'%'))
			</if>
			<if test="submitStartTime != null">
				AND date(eer.create_time_) &gt;= #{submitStartTime}
			</if>
			<if test="submitEndTime != null">
				AND date(eer.create_time_) &lt;= #{submitEndTime}
			</if>
		</where>
	</sql>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExtracurricularExercisesReply" >
		SELECT * FROM extracurricular_exercises_reply WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExtracurricularExercisesReply">
		SELECT * FROM extracurricular_exercises_reply ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ExtracurricularExercisesReply" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" > 
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL 
		</selectKey>
		-->
		INSERT INTO extracurricular_exercises_reply (extracurricular_exercises_id_,user_id_,attachments_,create_time_,update_time_,remark_,status_,is_replied_,is_view_,is_replied_timely_)
		VALUES(#{extracurricularExercisesId},#{userId},#{attachments},NOW(),NOW(),#{remark},#{status},#{isReplied},#{isView},#{isRepliedTimely})
	</insert>

	<insert id="batchInsert" parameterType="com.ym.mec.biz.dal.entity.ExtracurricularExercisesReply" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO extracurricular_exercises_reply (extracurricular_exercises_id_,user_id_,attachments_,create_time_,update_time_,remark_,status_,is_replied_,is_view_,is_replied_timely_)
		VALUES
		<foreach collection="replies" item="reply" separator=",">
			(#{reply.extracurricularExercisesId},#{reply.userId},#{reply.attachments},NOW(),NOW(),#{reply.remark},#{reply.status},#{reply.isReplied},#{reply.isView},#{reply.isRepliedTimely})
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.ExtracurricularExercisesReply">
		UPDATE extracurricular_exercises_reply
		<set>
			<if test="status != null">
			status_ = #{status},
			</if>
			<if test="isView != null">
			is_view_ = #{isView},
			</if>
			<if test="userId != null">
			user_id_ = #{userId},
			</if>
			<if test="remark != null">
			remark_ = #{remark},
			</if>
			<if test="id != null">
			id_ = #{id},
			</if>
			<if test="isReplied != null">
			is_replied_ = #{isReplied},
			</if>
			<if test="isRepliedTimely != null">
				is_replied_timely_ = #{isRepliedTimely},
			</if>
			<if test="extracurricularExercisesId != null">
			extracurricular_exercises_id_ = #{extracurricularExercisesId},
			</if>
			<if test="attachments != null">
			attachments_ = #{attachments},
			</if>
			<if test="submitTime != null">
				submit_time_ = #{submitTime},
			</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM extracurricular_exercises_reply WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ExtraExerciseStudentsDto" parameterType="map">
		SELECT eer.*,u.username_,ee.title_,o.name_ organ_name_,ee.teacher_id_,ee.expire_date_
		FROM extracurricular_exercises_reply eer
		left join extracurricular_exercises ee on eer.extracurricular_exercises_id_ = ee.id_
		left join sys_user u on eer.user_id_ = u.id_
		left join teacher t on t.id_ = ee.teacher_id_
		left join organization o on o.id_ = t.organ_id_
		<include refid="queryPageCondition"/>
		 ORDER BY eer.id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(eer.id_) FROM extracurricular_exercises_reply eer
		left join extracurricular_exercises ee on eer.extracurricular_exercises_id_ = ee.id_
		left join sys_user u on eer.user_id_ = u.id_
		left join teacher t on t.id_ = ee.teacher_id_
		left join organization o on o.id_ = t.organ_id_
		<include refid="queryPageCondition"/>
	</select>
	
	<select id="findExtraExerciseStudents" resultMap="ExtraExerciseStudentsDto">
		SELECT
			eer.*,
			su.username_ student_name_
		FROM
			extracurricular_exercises_reply eer
			LEFT JOIN sys_user su ON su.id_ = eer.user_id_
		WHERE eer.extracurricular_exercises_id_= #{extraExerciseId}
		<if test="studentName != null">
			AND su.username_ LIKE CONCAT('%',#{studentName},'%')
		</if>
	</select>

	<select id="findExtraExerciseStudentsByExtraExercises" resultMap="ExtracurricularExercisesReply">
		SELECT * FROM extracurricular_exercises_reply WHERE extracurricular_exercises_id_ IN
		<foreach collection="extraExerciseIds" item="extraExerciseId" open="(" close=")" separator=",">
			#{extraExerciseId}
		</foreach>
	</select>

	<select id="findExtraExerciseReply" resultMap="ExtraExerciseStudentsDto">
		SELECT
			eer.*,
			ee.title_,
			ee.content_,
			ee.expire_date_,
			ee.teacher_id_,
			su.username_ student_name_
		FROM
			extracurricular_exercises_reply eer
			LEFT JOIN extracurricular_exercises ee ON ee.id_=eer.extracurricular_exercises_id_
			LEFT JOIN sys_user su ON su.id_ = eer.user_id_
		WHERE
			eer.id_ = #{extraExerciseReplyId}
	</select>

	<sql id="queryStudentExtraExercisesCondition">
		<where>
			<if test="studentId!=null">
				AND eer.user_id_=#{studentId}
			</if>
			<if test="createTime!=null">
				AND DATE_FORMAT(eer.create_time_, '%Y-%m') = DATE_FORMAT(#{createTime}, '%Y-%m')
			</if>
		</where>
	</sql>
	<select id="countStudentExtraExercises" resultType="int">
		SELECT COUNT(id_) FROM extracurricular_exercises_reply eer
		<include refid="queryStudentExtraExercisesCondition"/>
	</select>
	<select id="findStudentExtraExercises" resultMap="ExtraExerciseStudentsDto">
		SELECT
			eer.*,
			ee.title_,
			ee.expire_date_,
			ee.teacher_id_,
			tea.real_name_ teacher_name_,
			su.username_ student_name_
		FROM
			extracurricular_exercises_reply eer
			LEFT JOIN extracurricular_exercises ee ON ee.id_=eer.extracurricular_exercises_id_
			LEFT JOIN sys_user su ON su.id_ = eer.user_id_
			LEFT JOIN sys_user tea ON tea.id_=ee.teacher_id_
			<include refid="queryStudentExtraExercisesCondition"/>
			ORDER BY eer.create_time_ DESC
		<include refid="global.limit"/>
	</select>
	<select id="countIsSubmitStudents" resultType="int">
		SELECT COUNT(id_) FROM extracurricular_exercises_reply WHERE extracurricular_exercises_id_=#{extraExerciseId} AND attachments_ IS NOT NULL
	</select>



	<sql id="queryExtraExercisesCondition">
		<where>
			<if test="extracurricularExercisesId != null">
				AND extracurricular_exercises_id_ = #{extracurricularExercisesId}
			</if>
			<if test="teacherId != null">
				AND teacher_id_ = #{teacherId}
			</if>
			<if test="organIdList != null">
				AND FIND_IN_SET(o.id_, #{organIdList})
			</if>
			<if test="studentId != null">
				AND user_id_ = #{studentId}
			</if>
			<if test="title != null">
				AND title_ like '%' #{title} '%'
			</if>
			<if test="status != null">
				AND eer.status_ = #{status}
			</if>
			<if test="isReplied != null">
				AND is_replied_ = #{isReplied}
			</if>
			<if test="isView != null">
				AND is_view_ = #{isView}
			</if>
			<if test="search != null">
				AND (title_ like concat('%',#{search},'%') or u.id_=#{search} or u.username_ like concat('%',#{search},'%'))
			</if>
			<if test="submitStartTime != null">
				AND date(eer.create_time_) &gt;= #{submitStartTime}
			</if>
			<if test="submitEndTime != null">
				AND date(eer.create_time_) &lt;= #{submitEndTime}
			</if>
			<if test="expireStartDate != null and expireEndDate != null">
				AND DATE_FORMAT(ee.expire_date_,"%Y%m%d") BETWEEN DATE_FORMAT(#{expireStartDate},"%Y%m%d") AND DATE_FORMAT(#{expireEndDate},"%Y%m%d")
			</if>
			<if test="isRepliedTimely!=null">
				AND eer.is_replied_timely_=#{isRepliedTimely}
			</if>
		</where>
	</sql>

	<select id="findExtraExercises" resultMap="ExtraExerciseStudentsDto">
		SELECT eer.*,u.username_,ee.title_,o.name_ organ_name_,ee.teacher_id_,ee.expire_date_
		FROM extracurricular_exercises_reply eer
		left join extracurricular_exercises ee on eer.extracurricular_exercises_id_ = ee.id_
		left join sys_user u on eer.user_id_ = u.id_
		left join teacher t on t.id_ = ee.teacher_id_
		left join organization o on o.id_ = t.organ_id_
		<include refid="queryExtraExercisesCondition"/>
		ORDER BY eer.id_ <include refid="global.limit"/>
	</select>
	<select id="countExtraExercises" resultType="int">
		SELECT COUNT(eer.id_) FROM extracurricular_exercises_reply eer
		left join extracurricular_exercises ee on eer.extracurricular_exercises_id_ = ee.id_
		left join sys_user u on eer.user_id_ = u.id_
		left join teacher t on t.id_ = ee.teacher_id_
		left join organization o on o.id_ = t.organ_id_
		<include refid="queryExtraExercisesCondition"/>
	</select>
    <select id="findNoPracticeStudentExercisesInWeek" resultMap="ExtracurricularExercisesReply">
		SELECT
			eer.id_,
			eer.user_id_,
			eer.status_,
			eer.submit_time_,
			eem.create_time_
		FROM
			extracurricular_exercises_reply eer
		LEFT JOIN extracurricular_exercises_message eem ON eer.id_=eem.extracurricular_exercises_reply_id_ AND eem.role_='TEACHER'
		WHERE
			DATE_FORMAT( eer.create_time_, '%Y-%m-%d' ) BETWEEN #{monday} AND #{sunday}
			AND EXISTS (
				SELECT
					cssp.user_id_
				FROM
					course_schedule_student_payment cssp
					LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
					LEFT JOIN student s ON cssp.user_id_ = s.user_id_
				WHERE
					cssp.group_type_ = 'PRACTICE'
					AND cssp.user_id_=eer.user_id_
					AND s.service_tag_ = 1
					AND cssp.user_id_ NOT IN (
					SELECT
						cssp1.user_id_
					FROM
						course_schedule_student_payment cssp1
						LEFT JOIN course_schedule cs1 ON cssp1.course_schedule_id_ = cs1.id_
					WHERE
						cssp1.group_type_ = 'PRACTICE'
						AND cs1.class_date_ BETWEEN #{monday} AND #{sunday}
					)
			)
	</select>
</mapper>
