package com.srs.comprehensive.service.impl;

import java.util.List;

import com.srs.comprehensive.domain.GraduateStudentNews;
import com.srs.comprehensive.domain.SaveStudent;
import com.srs.comprehensive.domain.SrsIdentifytable;
import com.srs.comprehensive.mapper.GraduateTextMapper;
import com.srs.comprehensive.mapper.SrsIdentifytableMapper;
import com.srs.comprehensive.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.comprehensive.mapper.SrsGraduateStudentMapper;
import com.srs.comprehensive.domain.SrsGraduateStudent;
import com.srs.comprehensive.service.ISrsGraduateStudentService;

/**
 * 学生评语信息Service业务层处理
 *
 * @author srs
 * @date 2025-07-10
 */
@Service
public class SrsGraduateStudentServiceImpl extends ServiceImpl<SrsGraduateStudentMapper,SrsGraduateStudent> implements ISrsGraduateStudentService {
    @Autowired
    private SrsGraduateStudentMapper srsGraduateStudentMapper;

    @Autowired
    private GraduateTextMapper graduateTextMapper;

    @Autowired
    private SrsIdentifytableMapper srsIdentifytableMapper;

    /**
     * 查询学生评语信息
     *
     * @param id 学生评语信息主键
     * @return 学生评语信息
     */
    @Override
    public SrsGraduateStudent selectSrsGraduateStudentById(Long id) {
        SaveStudent saveStudent = graduateTextMapper.selectByStudentById(id);
        return FileUploadUtil.mapToSrsGraduateStudent(saveStudent);
    }

    /**
     * 查询学生评语信息列表
     *
     * @param srsGraduateStudent 学生评语信息导出
     * @return 学生评语信息
     */
    @Override
    public List<SrsGraduateStudent> selectSrsGraduateStudentList(SrsGraduateStudent srsGraduateStudent) {
        return srsGraduateStudentMapper.selectSrsGraduateStudentList(srsGraduateStudent);
    }

    /**
     * 查询学生评语信息列表
     *
     * @param srsGraduateStudent 学生评语信息
     * @return 学生评语信息集合
     */
    @Override
    public List<SrsIdentifytable> selectSrsGraduateStudentLists(SrsGraduateStudent srsGraduateStudent) {
        return srsGraduateStudentMapper.selectSrsGraduateStudentLists(srsGraduateStudent);
    }

    /**
     * 新增学生评语信息
     *
     * @param srsGraduateStudent 学生评语信息
     * @return 结果
     */
    @Override
    public int insertSrsGraduateStudent(SrsGraduateStudent srsGraduateStudent) {
        GraduateStudentNews graduateStudentNews = graduateTextMapper.selectByStudentCode(srsGraduateStudent.getStudentCode());
        int n = 0;
        //Long s = 0L;
        if(graduateStudentNews == null) {
            srsGraduateStudentMapper.inserts(srsGraduateStudent);
            //s = srsGraduateStudent.getId();
            if(srsGraduateStudent.getYearOne() != null) {
                srsGraduateStudent.setGraduateId(srsGraduateStudent.getId());
                n = srsGraduateStudentMapper.insertSrsGraduateStudentYearOne(srsGraduateStudent);
            }
            if(srsGraduateStudent.getYearTwo() != null) {
                srsGraduateStudent.setGraduateId(srsGraduateStudent.getId());
                n = srsGraduateStudentMapper.insertSrsGraduateStudentYearTwo(srsGraduateStudent);
            }
            if(srsGraduateStudent.getYearThree() != null) {
                srsGraduateStudent.setGraduateId(srsGraduateStudent.getId());
                n = srsGraduateStudentMapper.insertSrsGraduateStudentYearThree(srsGraduateStudent);
            }
        }
        return n;
    }

    /**
     * 修改学生评语信息
     *
     * @param srsGraduateStudent 学生评语信息
     * @return 结果
     */
    @Override
    public int updateSrsGraduateStudent(SrsGraduateStudent srsGraduateStudent) {
        GraduateStudentNews graduateStudentNews = graduateTextMapper.selectByStudentCode(srsGraduateStudent.getStudentCode());
        int n = 0;
        if (graduateStudentNews != null) {
            if(srsGraduateStudent.getYearOne() != null) {
                srsGraduateStudent.setGraduateId(graduateStudentNews.getId());
                srsGraduateStudent.getYearOne().setBiaoJit(3);
                n = srsGraduateStudentMapper.updateSrsGraduateStudentYearOne(srsGraduateStudent);
            } else {
                srsGraduateStudent.setGraduateId(graduateStudentNews.getId());
               n = srsGraduateStudentMapper.insertSrsGraduateStudentYearOne(srsGraduateStudent);
            }
            if(srsGraduateStudent.getYearTwo() != null) {
                srsGraduateStudent.setGraduateId(graduateStudentNews.getId());
                srsGraduateStudent.getYearTwo().setBiaoJit(2);
                n += srsGraduateStudentMapper.updateSrsGraduateStudentYearTwo(srsGraduateStudent);
            } else {
                srsGraduateStudent.setGraduateId(graduateStudentNews.getId());
                n += srsGraduateStudentMapper.insertSrsGraduateStudentYearTwo(srsGraduateStudent);
            }
            if(srsGraduateStudent.getYearThree() != null) {
                srsGraduateStudent.setGraduateId(graduateStudentNews.getId());
                srsGraduateStudent.getYearThree().setBiaoJit(1);
                n += srsGraduateStudentMapper.updateSrsGraduateStudentYearThree(srsGraduateStudent);
            } else {
                srsGraduateStudent.setGraduateId(graduateStudentNews.getId());
                n += srsGraduateStudentMapper.insertSrsGraduateStudentYearThree(srsGraduateStudent);
            }
        }
        return n;
    }

    /**
     * 批量删除学生评语信息
     *
     * @param ids 需要删除的学生评语信息主键
     * @return 结果
     */
    @Override
    public int deleteSrsGraduateStudentByIds(Long[] ids) {
        int i = srsIdentifytableMapper.deleteSrsGraduataByIds(ids);
        int n = srsGraduateStudentMapper.deleteSrsGraduateStudentByIds(ids);
        if (i > 0 && n > 0) {
            return i;
        }
        return 0;
    }

    /**
     * 删除学生评语信息信息
     *
     * @param id 学生评语信息主键
     * @return 结果
     */
    @Override
    public int deleteSrsGraduateStudentById(Long id) {
        return srsGraduateStudentMapper.deleteSrsGraduateStudentById(id);
    }
}
