package com.srs.comprehensive.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.DateUtils;
import com.srs.comprehensive.domain.SrsStuYear;
import com.srs.comprehensive.domain.ViewStuInfo;
import com.srs.comprehensive.mapper.SrsStuYearMapper;
import com.srs.comprehensive.mapper.ViewStuInfoMapper;
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.CphIdeologyScoreMapper;
import com.srs.comprehensive.domain.CphIdeologyScore;
import com.srs.comprehensive.service.ICphIdeologyScoreService;

import static com.srs.common.utils.SecurityUtils.getUsername;

/**
 * 思想品德初始分数Service业务层处理
 *
 * @author srs
 * @date 2024-07-01
 */
@Service
public class CphIdeologyScoreServiceImpl extends ServiceImpl<CphIdeologyScoreMapper,CphIdeologyScore> implements ICphIdeologyScoreService {
    @Autowired
    private CphIdeologyScoreMapper cphIdeologyScoreMapper;

    @Autowired
    public SrsStuYearMapper  srsStuYearMapper;

    @Autowired
    public ViewStuInfoMapper   viewStuInfoMapper;

    @Override
    public AjaxResult doInit(){
        try{

            QueryWrapper<SrsStuYear> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status","1");
            queryWrapper.last("limit 1");
            SrsStuYear srsStuYear = srsStuYearMapper.selectOne(queryWrapper);
            if(srsStuYear == null){
                throw new Exception("未找到有效学年信息");
            }


            QueryWrapper<ViewStuInfo> viewStuInfoQueryWrapper = new QueryWrapper<>();
            viewStuInfoQueryWrapper.notIn("status","02","03");
            List<ViewStuInfo> viewStuInfoList = viewStuInfoMapper.selectList(viewStuInfoQueryWrapper);

            Set<String> stuNoSet = viewStuInfoList.stream().map(ViewStuInfo::getStuNo).collect(Collectors.toSet());
            QueryWrapper<CphIdeologyScore> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("stu_year_id",srsStuYear.getId());
            queryWrapper1.in("stu_no",stuNoSet);
            List<CphIdeologyScore> cphIdeologyScoreList = cphIdeologyScoreMapper.selectList(queryWrapper1);
            if(!cphIdeologyScoreList.isEmpty()){
                // 提取已评分的学生学号
                Set<String> scoredStuNoSet = cphIdeologyScoreList.stream()
                        .map(CphIdeologyScore::getStuNo)
                        .collect(Collectors.toSet());

                // 移除已评分的学生学号
                stuNoSet.removeAll(scoredStuNoSet);
            }

            List<CphIdeologyScore> needInsertList = new ArrayList<>();
            for(String stuNo : stuNoSet){
                CphIdeologyScore cphIdeologyScore = new CphIdeologyScore();
                cphIdeologyScore.setStuNo(stuNo);
                cphIdeologyScore.setStuYearId(srsStuYear.getId());
                cphIdeologyScore.setScore(new BigDecimal(80));
                cphIdeologyScore.setDescription("初始分数");
                cphIdeologyScore.setStatus("1");
                cphIdeologyScore.setDelFlag("0");
                cphIdeologyScore.setCreateTime(DateUtils.getNowDate());
                cphIdeologyScore.setUpdateTime(DateUtils.getNowDate());
                cphIdeologyScore.setCreateBy(getUsername());
                cphIdeologyScore.setUpdateBy(getUsername());
                needInsertList.add(cphIdeologyScore);
            }

            if(!needInsertList.isEmpty()){
                boolean res = this.saveBatch(needInsertList);
                if(!res){
                    throw new Exception("初始化分数失败");
                }
            }

            return AjaxResult.success();
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }

    /**
     * 查询思想品德初始分数
     *
     * @param id 思想品德初始分数主键
     * @return 思想品德初始分数
     */
    @Override
    public CphIdeologyScore selectCphIdeologyScoreById(Long id) {
        return cphIdeologyScoreMapper.selectCphIdeologyScoreById(id);
    }

    /**
     * 查询思想品德初始分数列表
     *
     * @param cphIdeologyScore 思想品德初始分数
     * @return 思想品德初始分数
     */
    @Override
    public List<CphIdeologyScore> selectCphIdeologyScoreList(CphIdeologyScore cphIdeologyScore) {
        return cphIdeologyScoreMapper.selectCphIdeologyScoreList(cphIdeologyScore);
    }

    /**
     * 新增思想品德初始分数
     *
     * @param cphIdeologyScore 思想品德初始分数
     * @return 结果
     */
    @Override
    public int insertCphIdeologyScore(CphIdeologyScore cphIdeologyScore) {
                cphIdeologyScore.setCreateTime(DateUtils.getNowDate());
            return cphIdeologyScoreMapper.insertCphIdeologyScore(cphIdeologyScore);
    }

    /**
     * 修改思想品德初始分数
     *
     * @param cphIdeologyScore 思想品德初始分数
     * @return 结果
     */
    @Override
    public int updateCphIdeologyScore(CphIdeologyScore cphIdeologyScore) {
                cphIdeologyScore.setUpdateTime(DateUtils.getNowDate());
        return cphIdeologyScoreMapper.updateCphIdeologyScore(cphIdeologyScore);
    }

    /**
     * 批量删除思想品德初始分数
     *
     * @param ids 需要删除的思想品德初始分数主键
     * @return 结果
     */
    @Override
    public int deleteCphIdeologyScoreByIds(Long[] ids) {
        return cphIdeologyScoreMapper.deleteCphIdeologyScoreByIds(ids);
    }

    /**
     * 删除思想品德初始分数信息
     *
     * @param id 思想品德初始分数主键
     * @return 结果
     */
    @Override
    public int deleteCphIdeologyScoreById(Long id) {
        return cphIdeologyScoreMapper.deleteCphIdeologyScoreById(id);
    }
}
