package com.srs.comprehensive.service.impl;

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

import com.srs.common.utils.DateUtils;
import com.srs.comprehensive.domain.*;
import com.srs.comprehensive.domain.Vo.*;
import com.srs.comprehensive.mapper.SrsAddRecordMapper;
import com.srs.comprehensive.mapper.SrsCourseScoreMapper;
import com.srs.comprehensive.mapper.SrsStudentMapper;
import com.srs.comprehensive.service.ISrsCourseScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 【课程学习】Service业务层处理
 *
 * @author srs
 * @date 2023-06-28
 */
@Service
public class SrsCourseScoreServiceImpl implements ISrsCourseScoreService {
    @Autowired
    private SrsCourseScoreMapper srsCourseScoreMapper;

    @Autowired
    private SrsStudentMapper srsStudentMapper;
    //加分记录
    @Autowired
    private SrsAddRecordMapper srsAddRecordMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SrsCourseScore selectSrsCourseScoreById(Long id) {
        return srsCourseScoreMapper.selectSrsCourseScoreById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param srsCourseScore 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SrsCourseScore> selectSrsCourseScoreList(SrsCourseScore srsCourseScore) {
        return srsCourseScoreMapper.selectSrsCourseScoreList(srsCourseScore);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param srsCourseScore 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSrsCourseScore(SrsCourseScore srsCourseScore) {
        srsCourseScore.setCreateTime(DateUtils.getNowDate());
        return srsCourseScoreMapper.insertSrsCourseScore(srsCourseScore);
    }

    /**
     * 学生课程批量新增
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)//开启事务
    @Override
    public int forInsertSrsCourseScore(List<SreCourseScoreStudent> sreCourseScoreStudent) {

        List<String> needImportStuNo = new ArrayList<>();
        for (SreCourseScoreStudent i:sreCourseScoreStudent) {
            needImportStuNo.add(i.getStuNo());
        }

        Long year = sreCourseScoreStudent.get(0).getStuYearId();

        List<CphScoreVo> allList = srsCourseScoreMapper.selectAllCourse(year);

        List<String> allStuNo = new ArrayList<>();
        for(CphScoreVo i : allList){
            allStuNo.add(i.getStuNo());
        }






        int number = 0;
//        for (SreCourseScoreStudent i : sreCourseScoreStudent) {
//            //try {
//                SrsCourseScore srsCourseScore = new SrsCourseScore();//课程学习综合分id
//                SrsStudent srsStudent = srsStudentMapper.selectSrsStudentByStuNo(i.getStuNo());//查询学生信息
//                srsCourseScore.setStuId(srsStudent.getStuId());//学生id
//                srsCourseScore.setStuYearId(i.getStuYearId());//学年
//            List<SrsCourseScore> srsCourseScores = srsCourseScoreMapper.selectSrsCourseScoreList(srsCourseScore);//查询重复
////BeanUtils.copyProperties(srsCourseScore,sreCourseScoreStudent);//将第一个差数copy到第二个参数
//                srsCourseScore.setAvgScore(i.getAvgScore());
////                SrsAddRecord srsAddRecord = new SrsAddRecord();//加分记录表
////                srsAddRecord.setStuNo(i.getStuNo());//学号
////                srsAddRecord.setRuleId(2L);//类型
////                srsAddRecord.setStuYearId(Math.toIntExact(i.getStuYearId()));//学年
////                List<SrsAddRecord> ifSrsAddRecord = srsAddRecordMapper.selectSrsAddRecordList(srsAddRecord);//查询加分记录重复
////                //添加到加分记录表
////                srsAddRecord.setRecordScore(i.getAvgScore());//平均分
////                srsAddRecord.setStatusCode("6");//状态
//                if (srsCourseScores.size() == 0) {
//                    //判断课程学习是否有该学生
//                    srsCourseScore.setCreateBy(getUsername());
//                    srsCourseScore.setCreateTime(DateUtils.getNowDate());
//                    srsCourseScoreMapper.insertSrsCourseScore(srsCourseScore);//没有则添加
//                    //加分记录
//                    //ifSrsAdd(srsAddRecord,ifSrsAddRecord);
//                    number++;
//                    continue;
//                }
//                //修改课堂学习
//                srsCourseScore.setUpdateBy(getUsername());
//                srsCourseScore.setUpdateTime(DateUtils.getNowDate());
//                srsCourseScore.setId(srsCourseScores.get(0).getId());
//                srsCourseScoreMapper.updateSrsCourseScore(srsCourseScore);//有则修改
//                //修改加分记录表
//                //ifSrsAdd(srsAddRecord,ifSrsAddRecord);
////            } catch (Exception e) {
////                return 0;
////            }
//            number++;
//        }

        return number;
    }

    private void ifSrsAdd(SrsAddRecord srsAddRecord, List<SrsAddRecord> ifSrsAddRecord) {
        if (ifSrsAddRecord.size()==0){
            //添加
            srsAddRecord.setCreateBy(getUsername());
            srsAddRecord.setCreateTime(DateUtils.getNowDate());
            srsAddRecordMapper.insertSrsAddRecord(srsAddRecord);
        }else {
            //修改加分记录表
            srsAddRecord.setUpdateBy(getUsername());
            srsAddRecord.setUpdateTime(DateUtils.getNowDate());
            srsAddRecord.setId(ifSrsAddRecord.get(0).getId());
            srsAddRecordMapper.updateSrsAddRecord(srsAddRecord);
        }
    }


    /**
     * 修改【请填写功能名称】
     *
     * @param srsCourseScore 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSrsCourseScore(SrsCourseScore srsCourseScore) {
        srsCourseScore.setUpdateTime(DateUtils.getNowDate());
        return srsCourseScoreMapper.updateSrsCourseScore(srsCourseScore);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSrsCourseScoreByIds(Long[] ids) {
        return srsCourseScoreMapper.deleteSrsCourseScoreByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSrsCourseScoreById(Long id) {
        return srsCourseScoreMapper.deleteSrsCourseScoreById(id);
    }

    /**
     * 科学文化素质连表查询
     *
     * @return 结果
     */
    @Override
    public List<SrsCourseScoreStudentStuYear> selectSrsCourseScoreStudentStuYear(SrsCourseScore srsCourseScore) {
        srsCourseScore.setDelFlag("0");
        return srsCourseScoreMapper.selectSrsCourseScoreStudentStuYear(srsCourseScore);
    }

    /**
     * 体能测试连表查询
     *
     * @return 结果
     */
    @Override
    public List<SrsSportTestStudentStuYear> selectSrsSportTestStudentStuYear(SrsSportTest srsSportTest) {
        srsSportTest.setDelFlag("0");
        return srsCourseScoreMapper.selectSrsSportTestStudentStuYear(srsSportTest);
    }

    /**
     * 课程学习连表查询
     */
    @Override
    public List<SrsStuYearS> selectStuYearStudent() {
        return srsCourseScoreMapper.selectStuYearStudent();
    }

    @Override
    public List<SrsCeScoreClassDeptGradeMajorsStudentStuYear> gradeRanking(SrsCourseScore srsCourseScore) {
        Map<String, Object> params = srsCourseScore.getParams();

        return srsCourseScoreMapper.selectGradeRanking(params);
    }

    //中间表课程成绩添加
    @Override
    public List<String> addCourseScoreData() {
        List<SumCourseScore> sumCourseScores = srsCourseScoreMapper.selectSumCourseScore();
       // List<SumSportTest> sumSportTests = sumSportTestMapper.selectSumSportTestAll();
        List<String> strings = new ArrayList<>();
        if (sumCourseScores.size()==0){
            strings.add("没有要处理的数据");
            return strings;
        }
        SrsCourseScore srsCourseScore = new SrsCourseScore();
        //SrsSportTest srsSportTest = new SrsSportTest();
        try {
            for (SumCourseScore s: sumCourseScores) {
                SrsStudent srsStudent = srsStudentMapper.selectSrsStudentByStuNo(s.getStuNo());//查询是否有对应的学生
                if (srsStudent==null){
                    strings.add(s.getStuNo());
                    continue;
                }
                srsCourseScore.setStuId(srsStudent.getStuId());
                srsCourseScore.setAvgScore(s.getAvgScore());
                srsCourseScore.setStuYearId((long) s.getStuYearId());
                int i = srsCourseScoreMapper.insertSrsCourseScore(srsCourseScore);
                //更新课程学习中间表读取记录
                if (i>0){
                    s.setChmodStatus("2");
                    s.setReadTime(DateUtils.getNowDate());
                    srsCourseScoreMapper.updateSum(s);
                }


            }

        }catch (Exception e){
            strings.add("导入中断");
            return strings;
        }
        return strings;
    }

}
