package com.srs.comprehensive.service.impl;

import com.srs.comprehensive.domain.GraduateStudentNews;
import com.srs.comprehensive.domain.SaveStudent;
import com.srs.comprehensive.mapper.GraduateTextMapper;
import com.srs.comprehensive.service.GraduateTextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
/**
 * 毕业生信息管理serviceImpI
 * 邵政文
 * @date 2025-07-01
 **/
public class GraduateTextImpI implements GraduateTextService {

    @Autowired
    private GraduateTextMapper graduateTextMapper;

    /**
     * 根据学号查询毕业生信息
     * @param studentCode
     * @return
     * @author GraduateStudentNews
     * 邵政文
     * @date 2025-07-02
     **/
    @Override
    public GraduateStudentNews getStudentByName(String studentCode) {
        return graduateTextMapper.selectByStudentCode(studentCode);
    }

    /**
     * 查询毕业生列表信息
     * @param student 学生信息实体
     * 邵政文
     * @date 2025-07-02
     */
    @Override
    public List<GraduateStudentNews> getGraduateList(GraduateStudentNews student) {
        return graduateTextMapper.selectGraduateList(student);
    }

    /**
     * 查询班级信息
     * @return
     * @author classId
     * 邵政文
     * @date 2025-07-02
     **/
    @Override
    public String selectGraduateClassData(Integer classId) {
        return graduateTextMapper.selectGraduateClassData(classId);
    }

    /**
     * 根据id查询毕业生信息
     * @return
     * @author id
     * 邵政文
     * @date 2025-07-02
     **/
    @Override
    public SaveStudent selectGraduateExclById(Long id) {
        return graduateTextMapper.selectByStudentById(id);
    }

    /**
     * 添加毕业生信息
     * @param graduateStudents 毕业生信息列表
     * 邵政文
     * 2025-07-02
     */
    @Override
    public int addGraduateList(List<GraduateStudentNews> graduateStudents) {
        int count = 0;

        for (GraduateStudentNews student : graduateStudents) {
            String studentCode = student.getStudentCode();

            // 根据学号查询是否已存在该学生信息
            GraduateStudentNews existingStudent = graduateTextMapper.selectByStudentCode(studentCode);

            if (existingStudent != null) {
                // 存在，使用现有 id 作为 graduate_id
                Long graduateId = existingStudent.getId();
                student.setGraduateId(graduateId.intValue()); // 设置 graduate_id

                // 查询该学生已有的学年信息
                List<Integer> existingYears = graduateTextMapper.selectStuYearByStudentCode(studentCode);

                // 如果学年信息存在于集合中，则更新子表
                if (existingYears != null && existingYears.contains(student.getStuYear())) {
                    int updateResult = graduateTextMapper.updateGraduateStudentByGraduateId(student);
                    count += updateResult;
                } else {
                    // 否则插入子表
                    int insertResult = graduateTextMapper.insertGraduateStudent(student);
                    count += insertResult;
                }
            } else {
                // 不存在，先插入主表 graduate_studentinfo

                // 注意：由于 useGeneratedKeys=true，插入后会自动回填 id 到 student.id
                graduateTextMapper.inserts(student);

                // 获取新生成的 id 并设置为 graduate_id
                Integer newGraduateId = student.getId().intValue();
                student.setGraduateId(newGraduateId);

                // 插入子表 graduate_student
                int insertResult = graduateTextMapper.insertGraduateStudent(student);
                count += insertResult;
            }
        }

        return count;
    }

    /**
     * 根据ids批量查询毕业生信息
     * @return 毕业生信息列表
     * @author ids
     * 邵政文
     * @date 2025-07-02
     **/
    @Override
    public List<SaveStudent> graduateByIdsList(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return new ArrayList<>();
        }
        return graduateTextMapper.graduateByIdsList(ids);
    }
}