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

    <resultMap type="com.ym.mec.biz.dal.entity.StudentCourseHomeworkReply" id="StudentCourseHomeworkReply">
        <result column="id_" property="id"/>
        <result column="student_course_homework_id_" property="studentCourseHomeworkId"/>
        <result column="user_id_" property="userId"/>
        <result column="content_" property="content"/>
        <result column="create_time_" property="createTime"/>
        <result column="parent_id_" property="parentId"/>
        <result column="msg_type_" property="msgType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>

    <resultMap id="studentCourseHomeworkComment" type="com.ym.mec.biz.dal.dto.StudentCourseHomeworkCommentDto">
        <result property="replyId" column="comment_id_"/>
        <result property="userId" column="comment_user_id_"/>
        <result property="userName" column="comment_user_name_"/>
        <result property="avatar" column="avatar_"/>
        <result property="isTeacher" column="is_teacher_"/>
        <result property="content" column="comment_content_"/>
        <result property="createTime" column="comment_time"/>
        <result property="msgType" column="msg_type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

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

    <!-- 全查询 -->
    <select id="findAll" resultMap="StudentCourseHomeworkReply">
		SELECT * FROM student_course_homework_reply where tenant_id_ = #{tenantId} ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentCourseHomeworkReply" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO student_course_homework_reply
        (id_,student_course_homework_id_,user_id_,content_,create_time_,parent_id_,msg_type_,tenant_id_)
        VALUES(#{id},#{studentCourseHomeworkId},#{userId},#{content},now(),#{parentId},#{msgType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{tenantId})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentCourseHomeworkReply">
        UPDATE student_course_homework_reply
        <set>
            <if test="parentId != null">
                parent_id_ = #{parentId},
            </if>
            <if test="msgType != null">
                msg_type_ = #{msgType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="studentCourseHomeworkId != null">
                student_course_homework_id_ = #{studentCourseHomeworkId},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="content != null">
                content_ = #{content},
            </if>
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		DELETE FROM student_course_homework_reply WHERE id_ = #{id} 
	</delete>

    <delete id="batchDeleteReplys">
      delete from student_course_homework_reply where id_ IN
      <foreach collection="list" item="id" open="(" close=")" separator=",">
          #{id}
      </foreach>
    </delete>
    <delete id="delByCourseScheduleId">
        DELETE FROM student_course_homework_reply
        WHERE student_course_homework_id_ IN
        (SELECT s.id_ FROM student_course_homework s WHERE s.course_schedule_id_ = #{courseScheduleId})
    </delete>
    <delete id="delByCourseScheduleIds">
        DELETE FROM student_course_homework_reply
        WHERE student_course_homework_id_ IN
        (SELECT s.id_ FROM student_course_homework s
        WHERE s.course_schedule_id_ IN
        <foreach collection="courseScheduleIdList" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>)
    </delete>

    <sql id="queryCondition">
        <where>
        	schr.tenant_id_ = #{tenantId}
            <if test="parentID!=null">
                AND schr.parent_id_ = #{parentID}
            </if>
            <if test="parentID==null">
               AND schr.parent_id_ IS NULL
            </if>
            <if test="studentCourseHomeworkId != null">
               AND schr.student_course_homework_id_=#{studentCourseHomeworkId}
            </if>
            <if test="userId!=null">
                AND sch.user_id_=#{userId}
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="studentCourseHomeworkComment" parameterType="map">
        SELECT
            schr.id_ comment_id_,
            schr.user_id_ comment_user_id_,
            suc.username_ comment_user_name_,
            suc.avatar_,
            IF(suc.user_type_ LIKE '%STUDENT%',0,1) is_teacher_,
            schr.content_ comment_content_,
            schr.create_time_ comment_time,
            schr.msg_type_
        FROM
            student_course_homework_reply schr
            LEFT JOIN sys_user suc ON schr.user_id_=suc.id_
            LEFT JOIN student_course_homework sch ON schr.student_course_homework_id_=sch.id_
        <include refid="queryCondition"/>
        ORDER BY schr.id_ DESC
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM student_course_homework_reply schr
        LEFT JOIN student_course_homework sch ON schr.student_course_homework_id_=sch.id_
		<include refid="queryCondition"/>
	</select>
    <select id="findAllReplyByStudentCourseHomeworkID" resultMap="StudentCourseHomeworkReply">
        select id_,IF(parent_id_ IS NULL,0,parent_id_) parent_id_ from student_course_homework_reply where student_course_homework_id_=#{studentCourseHomeworkID}
    </select>
</mapper>
