<?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.education.business.mapper.education.StuGoalInfoMapper">

    <resultMap id="BaseResultMap" type="com.education.model.dto.StuGoalInfoRespDto">
        <id property="id" column="id"/>
        <result property="subjectId" column="subject_id"/>
        <result property="schoolType" column="school_type"/>
        <result property="courseSectionId" column="course_section_id"/>
        <result property="courseId" column="course_id"/>
        <result property="gradeId" column="grade_id"/>
        <result property="courseName" column="course_name"/>
        <result property="courseSectionName" column="course_section_name"/>
        <result property="content" column="content"/>
        <result property="gOriented" column="g_oriented"/>
        <result property="pScoreStd" column="p_score_std"/>
        <result property="sScoreStd" column="s_score_std"/>
        <result property="rScoreStd" column="r_score_std"/>
        <result property="eScoreStd" column="e_score_std"/>
        <result property="nScoreStd" column="n_score_std"/>
        <result property="createUser" column="create_user"/>
        <result property="knowPoints" column="know_points"/>
        <result property="publishFlag" column="publish_flag"/>
        <result property="createDate" column="create_date"/>
        <result property="updateDate" column="update_date"/>

        <result property="stuGoalInfoId" column="stu_goal_info_id"/>
        <result property="studentId" column="student_id"/>
        <result property="correctStatus" column="correct_status"/>
        <result property="pAnswer" column="p_answer"/>
        <result property="pScore" column="p_score"/>
        <result property="sAnswer" column="s_answer"/>
        <result property="sScore" column="s_score"/>
        <result property="rAnswer" column="r_answer"/>
        <result property="rScore" column="r_score"/>
        <result property="eAnswer" column="e_answer"/>
        <result property="eScore" column="e_score"/>
        <result property="nAnswer" column="n_answer"/>
        <result property="nScore" column="n_score"/>
        <result property="comment" column="comment"/>
    </resultMap>

    <!-- 学生导向目标答题记录列表 -->
    <select id="selectPageList" resultType="stuGoalInfoDto">
        select * from stu_goal_info
        <where>
            <if test="stuGoalInfo.studentId != null">
                and student_id = #{stuGoalInfo.studentId}
            </if>
            <if test="stuGoalInfo.homeworkInfoId != null">
                and homework_info_id = #{stuGoalInfo.homeworkInfoId}
            </if>
            <if test="stuGoalInfo.goalInfoId != null">
                and goal_info_id = #{stuGoalInfo.goalInfoId}
            </if>
            <if test="stuGoalInfo.correctStatus != null">
                and correct_status = #{stuGoalInfo.correctStatus}
            </if>
        </where>
        order by id desc
    </select>

    <!-- 根据 ID 查询学生导向目标答题记录 -->
    <select id="selectStuGoalInfoById" parameterType="int" resultType="com.education.model.entity.StuGoalInfo">
        SELECT * FROM stu_goal_info WHERE id = #{id}
    </select>

    <select id="selectPageByCourseId" resultType="com.education.model.dto.StuGoalInfoRespDto">
        select distinct goal_info.*,course_info.name as course_name,course_section.title AS  course_section_name,
        l.id as stuGoalInfoId, l.student_id, l.correct_status,
        l.p_answer, l.p_score, l.s_answer, l.s_score, l.r_answer, l.r_score,
        l.e_answer, l.e_score, l.n_answer, l.n_score, l.comment
        from goal_info
        left join course_info
        on  goal_info.course_id = course_info.id
        left join course_section
        ON  goal_info.course_id = course_section.course_id AND goal_info.course_section_id = course_section.id
        left join (select a.* from stu_goal_info a,
        (select goal_info_id,max(id) as id from stu_goal_info where stu_goal_info.student_id = #{studentId}) AS b
        where a.id = b.id) as l
        on goal_info.id = l.goal_info_id
        <where>
            goal_info.course_id = #{courseId} and goal_info.publish_flag = 1
        </where>
    </select>

    <!-- 根据 ID 更新学生导向目标答题记录 -->
    <update id="updateStuGoalInfoById" parameterType="com.education.model.entity.StuGoalInfo">
        UPDATE stu_goal_info
        SET student_id = #{studentId},
            homework_info_id = #{homeworkInfoId},
            goal_info_id = #{goalInfoId},
            goal_mark = #{goalMark},
            correct_status = #{correctStatus},
            p_answer = #{pAnswer},
            p_score = #{pScore},
            s_answer = #{sAnswer},
            s_score = #{sScore},
            r_answer = #{rAnswer},
            r_score = #{rScore},
            e_answer = #{eAnswer},
            e_score = #{eScore},
            n_answer = #{nAnswer},
            n_score = #{nScore},
            comment = #{comment},
            create_date = #{createDate},
            update_date = #{updateDate}
        WHERE id = #{id}
    </update>

    <!-- 根据 ID 删除学生导向目标答题记录 -->
    <delete id="deleteStuGoalInfoById" parameterType="int">
        DELETE FROM stu_goal_info WHERE id = #{id}
    </delete>

</mapper>