package com.srs.comprehensive.service.impl;

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

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.*;
import com.srs.comprehensive.mapper.SchoolYearMapper;
import com.srs.comprehensive.mapper.SrsStuYearMapper;
import com.srs.comprehensive.mapper.SrsStudentMapper;
import com.srs.comprehensive.service.ISrsStuYearService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 【学年表】Service业务层处理
 * 
 * @author srs
 * @date 2023-06-28
 */
@Service
public class SrsStuYearServiceImpl implements ISrsStuYearService
{
    @Autowired
    private SrsStuYearMapper srsStuYearMapper;
    @Autowired
    private SchoolYearMapper schoolYearMapper;//学年中间表
    @Mapper
    private SrsStudentMapper srsStudentMapper;

    @Override
    public List<SrsStuYear> listEnableYear(){
        return srsStuYearMapper.listEnableYear();
    }

    @Override
    public AjaxResult listAllYear() {
        QueryWrapper<SrsStuYear> query = new QueryWrapper<>();
        query.orderByDesc("stu_year_name");
        List<SrsStuYear> list = srsStuYearMapper.selectList(query);
        return AjaxResult.success(list);
    }


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

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param srsStuYear 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SrsStuYear> selectSrsStuYearList(SrsStuYear srsStuYear)
    {
        return srsStuYearMapper.selectSrsStuYearList(srsStuYear);
    }


    @Override
    public List<SrsStuYear> selectSrsStuYearLists() {
        return srsStuYearMapper.selectSrsStuYearLists();
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param srsStuYear 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSrsStuYear(SrsStuYear srsStuYear)
    {
        srsStuYear.setCreateTime(DateUtils.getNowDate());
        return srsStuYearMapper.insertSrsStuYear(srsStuYear);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param srsStuYear 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSrsStuYear(SrsStuYear srsStuYear)
    {
        srsStuYear.setUpdateTime(DateUtils.getNowDate());
        return srsStuYearMapper.updateSrsStuYear(srsStuYear);
    }

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

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

    /**
     * 从中间表添加数据
     * @return
     */
    @Override
    public List<String> addStuYear() {
        List<SchoolYear> schoolYears = schoolYearMapper.selectSchoolYearAll();
        // List<SumSportTest> sumSportTests = sumSportTestMapper.selectSumSportTestAll();
        List<String> strings = new ArrayList<>();
        if (schoolYears.size()==0){
            strings.add("没有要处理的数据");
            return strings;
        }
        SrsStuYear srsStuYear = new SrsStuYear();
        //SrsSportTest srsSportTest = new SrsSportTest();
        try {
            for (SchoolYear s: schoolYears) {
                //SrsStudent srsStudent = srsStudentMapper.selectSrsStudentByStuNo(s.getStuNo());//查询是否有对应的学生
//                if (srsStudent==null){
//                    strings.add(s.getStuNo());
//                    continue;
//                }
                //srsStuYear.setId(s.getStuYearCode());
                srsStuYear.setStuYearName(s.getStuYearName());
                srsStuYear.setStartTime(s.getStartTime());
                srsStuYear.setEndTime(s.getEndTime());
                int i = srsStuYearMapper.insertSrsStuYear(srsStuYear);
                //更新课程学习中间表读取记录
                if (i>0){
                    s.setChmodStatus("2");
                    s.setReadTime(DateUtils.getNowDate());
                    schoolYearMapper.updateSchoolYear(s);
                }
            }
        }catch (Exception e){
            strings.add("导入中断");
            return strings;
        }
        return strings;
    }


}
