<?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.ExtracurricularExercisesMessageDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.ExtracurricularExercisesMessage" id="ExtracurricularExercisesMessage">
		<result column="id_" property="id" />
		<result column="extracurricular_exercises_reply_id_" property="extracurricularExercisesReplyId" />
		<result column="user_id_" property="userId" />
		<result column="role_" property="role" />
		<result column="content_" property="content" />
		<result column="create_time_" property="createTime" />
		<result column="parent_id_" property="parentId" />
		<result column="parent_id_" property="parentId" />
		<result column="msg_type_" property="msgType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExtracurricularExercisesMessage" >
		SELECT * FROM extracurricular_exercises_message WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExtracurricularExercisesMessage">
		SELECT * FROM extracurricular_exercises_message ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ExtracurricularExercisesMessage" 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_message (id_,extracurricular_exercises_reply_id_,user_id_,role_,content_,create_time_,parent_id_,msg_type_)
		VALUES(#{id},#{extracurricularExercisesReplyId},#{userId},#{role},#{content},NOW(),#{parentId},#{msgType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.ExtracurricularExercisesMessage">
		UPDATE extracurricular_exercises_message
		<set>
			<if test="msgType != null">
			msg_type_ = #{msgType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="parentId != null">
			parent_id_ = #{parentId},
			</if>
			<if test="userId != null">
			user_id_ = #{userId},
			</if>
			<if test="role != null">
			role_ = #{role},
			</if>
			<if test="id != null">
			id_ = #{id},
			</if>
			<if test="content != null">
			content_ = #{content},
			</if>
			<if test="extracurricularExercisesReplyId != null">
			extracurricular_exercises_reply_id_ = #{extracurricularExercisesReplyId},
			</if>
		</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM extracurricular_exercises_message WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ExtracurricularExercisesMessage" parameterType="map">
		SELECT * FROM extracurricular_exercises_message ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM extracurricular_exercises_message
	</select>

	<sql id="StudentExtraCommentsQueryCondition">
		<where>
			<if test="studentCourseHomeworkId!=null">
				AND extracurricular_exercises_reply_id_=#{studentCourseHomeworkId}
			</if>
			<if test="userId!=null">
				AND eer.user_id_=#{userId}
			</if>
		</where>
	</sql>
	<select id="countStudentExtraComments" resultType="int">
		SELECT COUNT(eem.id_) FROM extracurricular_exercises_message eem
		LEFT JOIN extracurricular_exercises_reply eer ON eer.id_=eem.extracurricular_exercises_reply_id_
		<include refid="StudentExtraCommentsQueryCondition"/>
	</select>
	<select id="findStudentExtraComments" resultMap="com.ym.mec.biz.dal.dao.StudentCourseHomeworkReplyDao.studentCourseHomeworkComment">
		SELECT
			eem.id_ comment_id_,
			eem.user_id_ comment_user_id_,
			suc.username_ comment_user_name_,
			suc.avatar_,
			IF(suc.user_type_ LIKE '%STUDENT%',0,1) is_teacher_,
			eem.content_ comment_content_,
			eem.create_time_ comment_time,
			eem.msg_type_
		FROM
			extracurricular_exercises_message eem
		LEFT JOIN sys_user suc ON eem.user_id_=suc.id_
		LEFT JOIN extracurricular_exercises_reply eer ON eer.id_=eem.extracurricular_exercises_reply_id_
		<include refid="StudentExtraCommentsQueryCondition"/>
		ORDER BY eem.id_ DESC
		<include refid="global.limit"/>
	</select>
    <select id="findReplyTeacherMessages" resultMap="ExtracurricularExercisesMessage">
		SELECT * FROM extracurricular_exercises_message WHERE role_='TEACHER' AND extracurricular_exercises_reply_id_ IN
		<foreach collection="replyIds" item="replyId" open="(" close=")" separator=",">
			#{replyId}
		</foreach>
	</select>
</mapper>
