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

    <!-- 作业信息列表 -->
    <select id="selectPageList" resultType="homeworkInfoDto">
        select * from homework_info
        <where>
            <if test="homeworkInfo.name != null and homeworkInfo.name != ''">
                <bind name="name" value="'%' + homeworkInfo.name + '%'"/>
                and name like #{name}
            </if>
            <if test="homeworkInfo.schoolType != null">
                and school_type = #{homeworkInfo.schoolType}
            </if>
            <if test="homeworkInfo.gradeInfoId != null">
                and grade_info_id = #{homeworkInfo.gradeInfoId}
            </if>
            <if test="homeworkInfo.subjectId != null">
                and subject_id = #{homeworkInfo.subjectId}
            </if>
        </where>
        order by id desc
    </select>

    <!-- 根据 ID 获取目标信息列表 -->
    <select id="getGoals" parameterType="int" resultType="com.education.model.dto.GoalInfoDto">
        -- 查询指定作业ID关联的所有导向目标信息
        SELECT
            g.*,                -- 导向目标表的所有字段
            hg.mark AS goal_max_mark  -- 该目标在作业中的满分值
        FROM
            homework_info h
                JOIN
            homework_goal_info hg ON h.id = hg.homework_info_id
                JOIN
            goal_info g ON hg.goal_info_id = g.id
        WHERE
            h.id = #{id}
        ORDER BY
            hg.sort ASC;        -- 按关联表中的排序字段升序排列
    </select>

    <!-- 根据 ID 查询作业信息 -->
    <select id="selectHomeworkInfoById" parameterType="int" resultType="com.education.model.entity.HomeworkInfo">
        SELECT * FROM homework_info WHERE id = #{id}
    </select>

    <!-- 根据 ID 更新作业信息 -->
    <update id="updateHomeworkInfoById" parameterType="com.education.model.entity.HomeworkInfo">
        UPDATE homework_info
        SET name = #{name},
            mark = #{mark},
            school_type = #{schoolType},
            grade_info_id = #{gradeInfoId},
            subject_id = #{subjectId},
            sort = #{sort},
            remark = #{remark},
            deadline = #{deadline},
            stu_number = #{stuNumber},
            correct_number = #{correctNumber},
            publish_flag = #{publishFlag},
            publish_time = #{publishTime},
            question_number = #{questionNumber},
            create_user = #{createUser},
            create_date = #{createDate},
            update_date = #{updateDate}
        WHERE id = #{id}
    </update>

    <!-- 根据 ID 删除作业信息 -->
    <delete id="deleteHomeworkInfoById" parameterType="int">
        DELETE FROM homework_info WHERE id = #{id}
    </delete>

</mapper>