package com.srs.comprehensive.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.DateUtils;
import com.srs.comprehensive.domain.CphTeacher;
import com.srs.comprehensive.domain.SrsClass;
import com.srs.comprehensive.util.ListSliceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.srs.comprehensive.mapper.SrsGradeMapper;
import com.srs.comprehensive.domain.SrsGrade;
import com.srs.comprehensive.service.ISrsGradeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 年级
 * Service业务层处理
 *
 * @author srs
 * @date 2023-07-05
 */
@Service
public class SrsGradeServiceImpl extends ServiceImpl<SrsGradeMapper, SrsGrade> implements ISrsGradeService {
    @Autowired
    private SrsGradeMapper srsGradeMapper;


    @Override
    public AjaxResult listNowGrade(){
        QueryWrapper<SrsGrade> searchQuery = new QueryWrapper<>();
        searchQuery.eq("grade_status",1);
        List<SrsGrade> list = srsGradeMapper.selectList(searchQuery);
        return AjaxResult.success(list);
    }


    /**
     * 查询年级
     *
     * @param gradeId 年级
     *                主键
     * @return 年级
     */
    @Override
    public SrsGrade selectSrsGradeByGradeId(Long gradeId) {
        return srsGradeMapper.selectSrsGradeByGradeId(gradeId);
    }

    /**
     * 查询年级
     * 列表
     *
     * @param srsGrade 年级
     * @return 年级
     */
    @Override
    public List<SrsGrade> selectSrsGradeList(SrsGrade srsGrade) {
        return srsGradeMapper.selectSrsGradeList(srsGrade);
    }

    /**
     * 新增年级
     *
     * @param srsGrade 年级
     * @return 结果
     */
    @Override
    public int insertSrsGrade(SrsGrade srsGrade) {
        srsGrade.setCreateTime(DateUtils.getNowDate());
        return srsGradeMapper.insertSrsGrade(srsGrade);
    }

    /**
     * 修改年级
     *
     * @param srsGrade 年级
     * @return 结果
     */
    @Override
    public int updateSrsGrade(SrsGrade srsGrade) {
        srsGrade.setUpdateTime(DateUtils.getNowDate());
        return srsGradeMapper.updateSrsGrade(srsGrade);
    }

    /**
     * 批量删除年级
     *
     * @param gradeIds 需要删除的年级
     *                 主键
     * @return 结果
     */
    @Override
    public int deleteSrsGradeByGradeIds(Long[] gradeIds) {
        return srsGradeMapper.deleteSrsGradeByGradeIds(gradeIds);
    }

    /**
     * 删除年级
     * 信息
     *
     * @param gradeId 年级
     *                主键
     * @return 结果
     */
    @Override
    public int deleteSrsGradeByGradeId(Long gradeId) {
        return srsGradeMapper.deleteSrsGradeByGradeId(gradeId);
    }

    //同步年级
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sqlserverSynchronousMYSQL(List<Map> list) {
        //年级列表
        List<SrsGrade> srsGrades = srsGradeMapper.selectSrsGradeList(null);
        //查找重复值
        List<Map> updateMap = list.stream()
                .filter(obj1 -> srsGrades
                        .stream().anyMatch(obj2 -> obj2.getGradeCode().equals(obj1.get("njdm").toString())))
                .collect(Collectors.toList());

        //添加需要
        List<String> kshList2 = srsGrades.stream()
                .map(SrsGrade::getGradeCode)
                .collect(Collectors.toList());

        List<Map> insertMap = list.stream()
                .filter(obj -> !kshList2.contains(obj.get("njdm").toString()))
                .collect(Collectors.toList());
        //添加
        List<SrsGrade> insertStudentList = new ArrayList<>();
        for (Map map:insertMap){
            SrsGrade srsGrade = new SrsGrade();
            srsGrade.setGradeCode(map.get("njdm").toString());
            srsGrade.setGradeName(map.get("njmc").toString());
            srsGrade.setStatus("0");
            srsGrade.setDelFlag("0");
            srsGrade.setCreateTime(DateUtils.getNowDate());
            insertStudentList.add(srsGrade);
        }
        if (insertStudentList.size()!=0) {
            srsGradeMapper.insertBatchSomeColumn(insertStudentList);
        }
        //修改
        List<SrsGrade> updateSrsStudent = new ArrayList<>();
        for (Map map:updateMap){
            SrsGrade srsGrade = new SrsGrade();
            srsGrade.setGradeCode(map.get("njdm").toString());
            srsGrade.setGradeName(map.get("njmc").toString());
            srsGrade.setUpdateTime(DateUtils.getNowDate());
            updateSrsStudent.add(srsGrade);
        }
        List<List<SrsGrade>> lists = ListSliceUtil.updateSlice(updateSrsStudent);
        if (lists != null) {
            for (List<SrsGrade> studentList:lists){
                srsGradeMapper.updateSrsClassList(studentList);
            }
        }
    }
}
