<?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.pangu.student.mapper.StudentMapper">

    <!-- 学生VO结果映射 -->
    <resultMap id="StudentVOResult" type="com.pangu.student.domain.vo.StudentVO">
        <id property="studentId" column="student_id"/>
        <result property="studentName" column="student_name"/>
        <result property="studentNo" column="student_no"/>
        <result property="gender" column="gender"/>
        <result property="genderName" column="gender_name"/>
        <result property="birthday" column="birthday"/>
        <result property="regionId" column="region_id"/>
        <result property="regionPath" column="region_path"/>
        <result property="schoolId" column="school_id"/>
        <result property="schoolName" column="school_name"/>
        <result property="schoolGradeId" column="school_grade_id"/>
        <result property="gradeName" column="grade_name"/>
        <result property="schoolClassId" column="school_class_id"/>
        <result property="className" column="class_name"/>
        <result property="subjectId" column="subject_id"/>
        <result property="subjectName" column="subject_name"/>
        <result property="memberId" column="member_id"/>
        <result property="memberNickname" column="member_nickname"/>
        <result property="memberPhone" column="member_phone"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <!-- 查询学生列表 -->
    <select id="selectStudentVOList" resultMap="StudentVOResult">
        SELECT
            s.student_id,
            s.student_name,
            s.student_no,
            s.gender,
            CASE s.gender
                WHEN '1' THEN '男'
                WHEN '2' THEN '女'
                ELSE '未知'
            END AS gender_name,
            s.birthday,
            s.region_id,
            s.region_path,
            s.school_id,
            sch.school_name,
            s.school_grade_id,
            g.grade_name,
            s.school_class_id,
            c.class_name,
            s.subject_id,
            sub.subject_name,
            s.member_id,
            m.nickname AS member_nickname,
            m.phone AS member_phone,
            s.status,
            s.create_time,
            s.remark
        FROM pg_student s
        LEFT JOIN pg_school sch ON s.school_id = sch.school_id AND sch.del_flag = '0'
        LEFT JOIN pg_school_grade sg ON s.school_grade_id = sg.id AND sg.del_flag = '0'
        LEFT JOIN pg_grade g ON sg.grade_id = g.grade_id AND g.del_flag = '0'
        LEFT JOIN pg_school_class sc ON s.school_class_id = sc.id AND sc.del_flag = '0'
        LEFT JOIN pg_class c ON sc.class_id = c.class_id AND c.del_flag = '0'
        LEFT JOIN pg_subject sub ON s.subject_id = sub.subject_id AND sub.del_flag = '0'
        LEFT JOIN pg_member m ON s.member_id = m.member_id AND m.del_flag = '0'
        WHERE s.del_flag = '0'
        <if test="dto.studentName != null and dto.studentName != ''">
            AND s.student_name LIKE CONCAT('%', #{dto.studentName}, '%')
        </if>
        <if test="dto.studentNo != null and dto.studentNo != ''">
            AND s.student_no = #{dto.studentNo}
        </if>
        <if test="dto.gender != null and dto.gender != ''">
            AND s.gender = #{dto.gender}
        </if>
        <if test="dto.schoolId != null">
            AND s.school_id = #{dto.schoolId}
        </if>
        <if test="dto.schoolGradeId != null">
            AND s.school_grade_id = #{dto.schoolGradeId}
        </if>
        <if test="dto.schoolClassId != null">
            AND s.school_class_id = #{dto.schoolClassId}
        </if>
        <if test="dto.subjectId != null">
            AND s.subject_id = #{dto.subjectId}
        </if>
        <if test="dto.memberPhone != null and dto.memberPhone != ''">
            AND m.phone LIKE CONCAT('%', #{dto.memberPhone}, '%')
        </if>
        <if test="dto.beginTime != null and dto.beginTime != ''">
            AND DATE_FORMAT(s.create_time,'%Y-%m-%d') &gt;= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null and dto.endTime != ''">
            AND DATE_FORMAT(s.create_time,'%Y-%m-%d') &lt;= #{dto.endTime}
        </if>
        ORDER BY s.create_time DESC
    </select>

    <!-- 根据ID查询学生详情 -->
    <select id="selectStudentVOById" resultMap="StudentVOResult">
        SELECT
            s.student_id,
            s.student_name,
            s.student_no,
            s.gender,
            CASE s.gender
                WHEN '1' THEN '男'
                WHEN '2' THEN '女'
                ELSE '未知'
            END AS gender_name,
            s.birthday,
            s.region_id,
            s.region_path,
            s.school_id,
            sch.school_name,
            s.school_grade_id,
            g.grade_name,
            s.school_class_id,
            c.class_name,
            s.subject_id,
            sub.subject_name,
            s.member_id,
            m.nickname AS member_nickname,
            m.phone AS member_phone,
            s.status,
            s.create_time,
            s.remark
        FROM pg_student s
        LEFT JOIN pg_school sch ON s.school_id = sch.school_id AND sch.del_flag = '0'
        LEFT JOIN pg_school_grade sg ON s.school_grade_id = sg.id AND sg.del_flag = '0'
        LEFT JOIN pg_grade g ON sg.grade_id = g.grade_id AND g.del_flag = '0'
        LEFT JOIN pg_school_class sc ON s.school_class_id = sc.id AND sc.del_flag = '0'
        LEFT JOIN pg_class c ON sc.class_id = c.class_id AND c.del_flag = '0'
        LEFT JOIN pg_subject sub ON s.subject_id = sub.subject_id AND sub.del_flag = '0'
        LEFT JOIN pg_member m ON s.member_id = m.member_id AND m.del_flag = '0'
        WHERE s.student_id = #{studentId} AND s.del_flag = '0'
    </select>

    <!-- 检查学号是否唯一 -->
    <select id="countByStudentNo" resultType="int">
        SELECT COUNT(1)
        FROM pg_student
        WHERE student_no = #{studentNo}
        AND del_flag = '0'
        <if test="studentId != null">
            AND student_id != #{studentId}
        </if>
    </select>

    <!-- 根据会员ID统计学生数量 -->
    <select id="countByMemberId" resultType="int">
        SELECT COUNT(1)
        FROM pg_student
        WHERE member_id = #{memberId}
        AND del_flag = '0'
    </select>

    <!-- 根据会员ID查询学生列表 -->
    <select id="selectStudentVOsByMemberId" resultMap="StudentVOResult">
        SELECT
            s.student_id,
            s.student_name,
            s.student_no,
            s.gender,
            CASE s.gender
                WHEN '1' THEN '男'
                WHEN '2' THEN '女'
                ELSE '未知'
            END AS gender_name,
            s.birthday,
            s.region_id,
            s.region_path,
            s.school_id,
            sch.school_name,
            s.school_grade_id,
            g.grade_name,
            s.school_class_id,
            c.class_name,
            s.subject_id,
            sub.subject_name,
            s.create_time
        FROM pg_student s
        LEFT JOIN pg_school sch ON s.school_id = sch.school_id AND sch.del_flag = '0'
        LEFT JOIN pg_school_grade sg ON s.school_grade_id = sg.id AND sg.del_flag = '0'
        LEFT JOIN pg_grade g ON sg.grade_id = g.grade_id AND g.del_flag = '0'
        LEFT JOIN pg_school_class sc ON s.school_class_id = sc.id AND sc.del_flag = '0'
        LEFT JOIN pg_class c ON sc.class_id = c.class_id AND c.del_flag = '0'
        LEFT JOIN pg_subject sub ON s.subject_id = sub.subject_id AND sub.del_flag = '0'
        WHERE s.member_id = #{memberId} AND s.del_flag = '0'
        ORDER BY s.create_time DESC
    </select>

</mapper>
