<?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.srs.system.mapper.StudentMentalRatingMapper">
<!--  添加  知无涯-->
    <select id="selectByStudentId" resultType="com.srs.system.domain.StudentMentalRating">
        SELECT id, student_id, rating, created_time, updated_time
        FROM student_mental_rating
        WHERE student_id = #{studentId}
    </select>

    <insert id="insert" parameterType="com.srs.system.domain.StudentMentalRating"
            useGeneratedKeys="true" keyProperty="id">
        INSERT INTO student_mental_rating(student_id, rating)
        VALUES (#{studentId}, #{rating})
    </insert>

    <update id="updateRatingByStudentId" parameterType="com.srs.system.domain.StudentMentalRating">
        UPDATE student_mental_rating
        SET rating = #{rating}
        WHERE student_id = #{studentId}
    </update>


    <!-- 查询辅导员负责的每个学生的最新心理评级记录（带条件查询） -->
    <select id="selectLatestByTeacherIdWithConditions" resultType="com.srs.system.domain.vo.StudentMentalRatingVo">
        SELECT
        a.stu_no,
        a.stu_name,
        a.class_name,
        smr.rating,
        smr.created_time
        FROM view_stu_info a
        LEFT JOIN student_mental_rating smr ON a.stu_no = smr.student_id
        INNER JOIN (
        SELECT student_id, MAX(created_time) as max_time
        FROM student_mental_rating
        WHERE rating IS NOT NULL
        GROUP BY student_id
        ) latest ON smr.student_id = latest.student_id AND smr.created_time = latest.max_time
        WHERE a.t_no = #{teacherId}
        AND smr.rating IS NOT NULL
        AND smr.student_id IS NOT NULL
        <if test="stuNo != null and stuNo != ''">
            AND a.stu_no = #{stuNo}
        </if>
        <if test="stuName != null and stuName != ''">
            AND a.stu_name LIKE CONCAT('%', #{stuName}, '%')
        </if>
        ORDER BY smr.created_time DESC
    </select>

    <!-- 查询所有学生的最新心理评级记录（带条件查询） -->
    <select id="selectLatestAllWithConditions" resultType="com.srs.system.domain.vo.StudentMentalRatingVo">
        SELECT
        a.stu_no,
        a.stu_name,
        a.class_name,
        smr.rating,
        smr.created_time
        FROM view_stu_info a
        LEFT JOIN student_mental_rating smr ON a.stu_no = smr.student_id
        INNER JOIN (
        SELECT student_id, MAX(created_time) as max_time
        FROM student_mental_rating
        WHERE rating IS NOT NULL
        GROUP BY student_id
        ) latest ON smr.student_id = latest.student_id AND smr.created_time = latest.max_time
        WHERE smr.rating IS NOT NULL
        AND smr.student_id IS NOT NULL
        <if test="stuNo != null and stuNo != ''">
            AND a.stu_no = #{stuNo}
        </if>
        <if test="stuName != null and stuName != ''">
            AND a.stu_name LIKE CONCAT('%', #{stuName}, '%')
        </if>
        ORDER BY smr.created_time DESC
    </select>





    <!-- 根据学号查询心理：知无涯 -->
    <select id="selectByStuNo" parameterType="string" resultType="com.srs.system.domain.vo.StudentMentalRatingVo">
        SELECT
               a.stu_no,
               a.stu_name,
               a.class_name,
               smr.rating,
               smr.created_time
        FROM view_stu_info a
        LEFT JOIN student_mental_rating smr ON a.stu_no = smr.student_id
        WHERE student_id = #{stuNo}
        ORDER BY created_time DESC
    </select>

    <!-- 查询今天该学生是否已经填写过心理评测 陈冠元 -->
    <select id="countTodayByStudentId" resultType="int">
        SELECT COUNT(*)
        FROM student_mental_rating
        WHERE student_id = #{studentId}
          AND DATE(created_time) = #{today}
    </select>
</mapper>