package com.srs.comprehensive.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.srs.common.core.domain.entity.SysDept;
import com.srs.common.core.domain.model.LoginUser;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.comprehensive.domain.*;
import com.srs.comprehensive.mapper.*;
import com.srs.comprehensive.service.ICascaderDataStudentService;
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.service.ISrsIdentifytableService;

/**
 * 学生鉴定信息Service业务层处理
 *
 * @author srs
 * @date 2025-07-10
 */
@Service
public class SrsIdentifytableServiceImpl extends ServiceImpl<SrsIdentifytableMapper,SrsIdentifytable> implements ISrsIdentifytableService {
    @Autowired
    private SrsIdentifytableMapper srsIdentifytableMapper;

    @Autowired
    private SrsGraduateStudentMapper srsGraduateStudentMapper;

    @Autowired
    private GraduateTextMapper graduateTextMapper;

    /**
     * 查询学生鉴定信息
     *
     * @param id 学生鉴定信息主键
     * @return 学生鉴定信息
     */
    @Override
    public SrsIdentifytable selectSrsIdentifytableById(Long id) {
        SaveStudent saveStudent = graduateTextMapper.selectByStudentById(id);
        return FileUploadUtil.mapToSrsIdentifytable(saveStudent);
    }

    /**
     * 查询学生鉴定信息列表
     *
     * @param srsIdentifytable 学生鉴定信息
     * @return 学生鉴定信息
     */
    @Override
    public List<SrsIdentifytable> selectSrsIdentifytableList(SrsIdentifytable srsIdentifytable) {
        return srsIdentifytableMapper.selectSrsIdentifytableList(srsIdentifytable);
    }

    /**
     * 新增学生鉴定信息
     *
     * @param srsIdentifytable 学生鉴定信息
     * @return 结果
     */
    @Override
    public int insertSrsIdentifytable(SrsIdentifytable srsIdentifytable) {
                srsIdentifytable.setCreateTime(DateUtils.getNowDate());
        int i = srsIdentifytableMapper.inserts(srsIdentifytable);
        int n = 0;
        Integer newGraduateId = srsIdentifytable.getId().intValue();
        srsIdentifytable.setGraduateId(newGraduateId);
        if(srsIdentifytable.getYearOne() != null) {
            n = srsIdentifytableMapper.insertSrsIdentifytableYearOne(srsIdentifytable);
        }
        if(srsIdentifytable.getYearTwo() != null) {
            n = srsIdentifytableMapper.insertSrsIdentifytableYearTwo(srsIdentifytable);
        }
        if(srsIdentifytable.getYearThree() != null) {
            n = srsIdentifytableMapper.insertSrsIdentifytableYearThree(srsIdentifytable);
        }
        if (i > 0 && n > 0) {
            return i;
        }
        return 0;
    }

    /**
     * 修改学生鉴定信息
     *
     * @param srsIdentifytable 学生鉴定信息
     * @return 结果
     */
    @Override
    public int updateSrsIdentifytable(SrsIdentifytable srsIdentifytable) {
                srsIdentifytable.setUpdateTime(DateUtils.getNowDate());
        //int i = srsIdentifytableMapper.updateGraduateStudentInfo(srsIdentifytable);
        GraduateStudentNews graduateStudentNews = graduateTextMapper.selectByStudentCode(srsIdentifytable.getStudentCode());
        int n = 0;
        if(graduateStudentNews != null && graduateStudentNews.getNation() != null) {
            if(srsIdentifytable.getYearOne() != null) {
                srsIdentifytable.getYearOne().setBiaoJit(3);
                n = srsIdentifytableMapper.updateSrsIdentifytableYearOne(srsIdentifytable);
            }
            if(srsIdentifytable.getYearTwo() != null) {
                srsIdentifytable.getYearTwo().setBiaoJit(2);
                n = srsIdentifytableMapper.updateSrsIdentifytableYearTwo(srsIdentifytable);
            }
            if(srsIdentifytable.getYearThree() != null) {
                srsIdentifytable.getYearThree().setBiaoJit(1);
                n = srsIdentifytableMapper.updateSrsIdentifytableYearThree(srsIdentifytable);
            }
        } else {
            if(srsIdentifytable.getYearOne() != null) {
                srsIdentifytableMapper.updateGraduateStudentInfo(srsIdentifytable);
                srsIdentifytable.setGraduateId(graduateStudentNews.getId().intValue());
                n = srsIdentifytableMapper.insertSrsIdentifytableYearOne(srsIdentifytable);
            }
            if(srsIdentifytable.getYearTwo() != null) {
                srsIdentifytableMapper.updateGraduateStudentInfo(srsIdentifytable);
                srsIdentifytable.setGraduateId(graduateStudentNews.getId().intValue());
                n = srsIdentifytableMapper.insertSrsIdentifytableYearTwo(srsIdentifytable);
            }
            if(srsIdentifytable.getYearThree() != null) {
                srsIdentifytableMapper.updateGraduateStudentInfo(srsIdentifytable);
                srsIdentifytable.setGraduateId(graduateStudentNews.getId().intValue());
                n = srsIdentifytableMapper.insertSrsIdentifytableYearThree(srsIdentifytable);
            }
        }
        if (n > 0) {
            return 1;
        }
        return 2;
    }

    /**
     * 批量删除学生鉴定信息
     *
     * @param ids 需要删除的学生鉴定信息主键
     * @return 结果
     */
    @Override
    public int deleteSrsIdentifytableByIds(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 deleteSrsIdentifytableById(Long id) {
        return srsIdentifytableMapper.deleteSrsIdentifytableById(id);
    }


    /**
     * 获取对应人的班级信息
     *
     * @param
     * @return 集合结果
     */
    @Override
    public List<SrsClass> deptDataLsit() {
        //获取登录人的信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SrsClass> depts = srsIdentifytableMapper.selectDeptByDeptCodes(loginUser.getUsername());
        if(depts != null && depts.size() > 0) {
            return depts;
        }
        return new ArrayList<>();
    }

}
