<?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.comprehensive.mapper.CphStudentDetailExportMapper">

    <!-- 学生查新详情导出结果映射 -->
    <resultMap type="CphStudentDetailExport" id="CphStudentDetailExportResult">
        <!-- 基本信息 -->
        <result property="stuNo" column="stu_no"/>
        <result property="name" column="name"/>
        <result property="gender" column="gender"/>
        <result property="idCard" column="id_card"/>
        <result property="birthday" column="birthday"/>
        <result property="phone" column="phone"/>
        <result property="xhk" column="xhk"/>
        <result property="bankAddr" column="bank_addr"/>
        
        <!-- 班级专业信息 -->
        <result property="className" column="class_name"/>
        <result property="majorName" column="major_name"/>
        <result property="deptName" column="dept_name"/>
        <result property="gradeName" column="grade_name"/>
        <result property="teacherName" column="teacher_name"/>
        
        <!-- 其他信息 -->
        <result property="mz" column="mz"/>
        <result property="zzmm" column="zzmm"/>
        <result property="xx" column="xx"/>
        <result property="ksh" column="ksh"/>
        <result property="jg" column="jg"/>
        <result property="hkszd" column="hkszd"/>
        <result property="hksz1" column="hksz1"/>
        <result property="hksz2" column="hksz2"/>
        <result property="hksz3" column="hksz3"/>
        <result property="hkxz" column="hkxz"/>
        <result property="hkxxdz" column="hkxxdz"/>
        <result property="sspcs" column="sspcs"/>
        <result property="ssjd" column="ssjd"/>
        <result property="chcqj" column="chcqj"/>
        <result property="xxlxdz" column="xxlxdz"/>
        <result property="xjtdz" column="xjtdz"/>
        <result property="jtyzbm" column="jtyzbm"/>
        <result property="jtdh" column="jtdh"/>
        
        <!-- 家庭成员信息（使用MySQL的GROUP_CONCAT函数聚合） -->
        <result property="familyMembers" column="family_members"/>
        
        <!-- 教育经历信息（使用MySQL的GROUP_CONCAT函数聚合） -->
        <result property="educationExperiences" column="education_experiences"/>
    </resultMap>

    <!-- 查询学生查新详情导出数据 -->
    <select id="selectStudentDetailExportList" parameterType="SrsStudent" resultMap="CphStudentDetailExportResult">
        SELECT
            s.stu_no,
            s.name,
            s.gender,
            AES_DECRYPT(UNHEX(s.id_card), 'zhxg') as id_card,
            s.birthday,
            s.phone,
            s.xhk,
            s.bank_addr,
            c.class_name,
            m.major_name,
            d.dept_name,
            f.grade_name,
            e.name as teacher_name,
            g.mz,
            g.zzmm,
            g.xx,
            g.ksh,
            g.jg,
            g.hkszd,
            g.hkxz,
            g.hkxxdz,
            g.hksz1,
            g.hksz2,
            g.hksz3,
            g.sspcs,
            g.ssjd,
            g.chcqj,
            g.xxlxdz,
            g.xjtdz,
            g.jtyzbm,
            g.jtdh,
            
            -- 聚合家庭成员信息（使用DISTINCT避免重复）
            GROUP_CONCAT(
                DISTINCT CONCAT(
                    '姓名: ', fm.family_name,
                    '，关系: ', fm.family_relation,
                    '，年龄: ', fm.age,
                    '，政治面貌: ', fm.zzmm,
                    '，职业: ', fm.job,
                    '，工作单位: ', fm.work_place,
                    '，年收入: ', COALESCE(fm.year_money, ''),
                    '，联系方式: ', fm.phone,
                    '，健康状况: ', fm.health
                ) SEPARATOR '\n'
            ) as family_members,
            
            -- 聚合教育经历信息（使用DISTINCT避免重复）
            GROUP_CONCAT(
                DISTINCT CONCAT(
                    '开始时间: ', DATE_FORMAT(ee.start_time, '%Y-%m-%d'),
                    '，结束时间: ', DATE_FORMAT(ee.end_time, '%Y-%m-%d'),
                    '，学校: ', ee.school_name,
                    '，职务: ', ee.post,
                    '，证明人: ', ee.certifier
                ) SEPARATOR '\n'
            ) as education_experiences
        
        FROM srs_student s
        LEFT JOIN srs_class c ON s.class_id = c.class_id
        LEFT JOIN srs_majors m ON c.major_id = m.major_id
        LEFT JOIN sys_dept d ON m.college_id = d.dept_id
        LEFT JOIN cph_teacher e ON c.teacher_id = e.teacher_id
        LEFT JOIN srs_grade f ON f.grade_id = c.grade_id
        LEFT JOIN cph_stu_extra_info g ON g.stu_no = s.stu_no
        LEFT JOIN cph_family_member fm ON fm.stu_no = s.stu_no
        LEFT JOIN srs_education_experience ee ON ee.stu_no = s.stu_no
        
        <where>
            <if test="stuNo != null  and stuNo != ''">and s.stu_no like concat('%', #{stuNo}, '%')</if>
            <if test="name != null  and name != ''">and s.name like concat('%', #{name}, '%')</if>
            <if test="gender != null  and gender != ''">and s.gender = #{gender}</if>
            <if test="birthday != null ">and s.birthday = #{birthday}</if>
            <if test="idCard != null  and idCard != ''">and s.id_card = #{idCard}</if>
            <if test="deptId != null ">and d.dept_id = #{deptId}</if>
            <if test="majorId != null ">and m.major_id = #{majorId}</if>
            <if test="classId != null ">and s.class_id = #{classId}</if>
            <if test="phone != null  and phone != ''">and s.phone like concat('%', #{phone} , '%')</if>
            <if test="address != null  and address != ''">and s.address like concat('%', #{address} , '%')</if>
            <if test="status != null  and status != ''">and s.status = #{status}</if>
            <if test="tNo != null  and tNo != ''">and e.employee_id = #{tNo}</if>
            <if test="gradeId != null">and c.grade_id = #{gradeId}</if>
            <if test="cphName != null and cphName!=''">and e.name = #{cphName}</if>
        </where>
        
        GROUP BY s.stu_no
        ORDER BY s.stu_no desc
    </select>

    <!-- 根据学号查询学生查新详情导出数据 -->
    <select id="selectStudentDetailExportByStuNo" parameterType="String" resultMap="CphStudentDetailExportResult">
        SELECT
            s.stu_no,
            s.name,
            s.gender,
            AES_DECRYPT(UNHEX(s.id_card), 'zhxg') as id_card,
            s.birthday,
            s.phone,
            s.xhk,
            s.bank_addr,
            c.class_name,
            m.major_name,
            d.dept_name,
            f.grade_name,
            e.name as teacher_name,
            g.mz,
            g.zzmm,
            g.xx,
            g.ksh,
            g.jg,
            g.hkszd,
            g.hkxz,
            g.hkxxdz,
            g.hksz1,
            g.hksz2,
            g.hksz3,
            g.sspcs,
            g.ssjd,
            g.chcqj,
            g.xxlxdz,
            g.xjtdz,
            g.jtyzbm,
            g.jtdh,
            
            -- 聚合家庭成员信息（使用DISTINCT避免重复）
            GROUP_CONCAT(
                DISTINCT CONCAT(
                    '姓名: ', fm.family_name,
                    '，关系: ', fm.family_relation,
                    '，年龄: ', fm.age,
                    '，政治面貌: ', fm.zzmm,
                    '，职业: ', fm.job,
                    '，工作单位: ', fm.work_place,
                    '，年收入: ', COALESCE(fm.year_money, ''),
                    '，联系方式: ', fm.phone,
                    '，健康状况: ', fm.health
                ) SEPARATOR '\n'
            ) as family_members,
            
            -- 聚合教育经历信息（使用DISTINCT避免重复）
            GROUP_CONCAT(
                DISTINCT CONCAT(
                    '开始时间: ', DATE_FORMAT(ee.start_time, '%Y-%m-%d'),
                    '，结束时间: ', DATE_FORMAT(ee.end_time, '%Y-%m-%d'),
                    '，学校: ', ee.school_name,
                    '，职务: ', ee.post,
                    '，证明人: ', ee.certifier
                ) SEPARATOR '\n'
            ) as education_experiences
        
        FROM srs_student s
        LEFT JOIN srs_class c ON s.class_id = c.class_id
        LEFT JOIN srs_majors m ON c.major_id = m.major_id
        LEFT JOIN sys_dept d ON m.college_id = d.dept_id
        LEFT JOIN cph_teacher e ON c.teacher_id = e.teacher_id
        LEFT JOIN srs_grade f ON f.grade_id = c.grade_id
        LEFT JOIN cph_stu_extra_info g ON g.stu_no = s.stu_no
        LEFT JOIN cph_family_member fm ON fm.stu_no = s.stu_no
        LEFT JOIN srs_education_experience ee ON ee.stu_no = s.stu_no
        
        WHERE s.stu_no = #{stuNo}
        
        GROUP BY s.stu_no
    </select>

</mapper>
