package com.srs.comprehensive.service.impl;

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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.srs.common.annotation.DataSource;
import com.srs.common.enums.DataSourceType;
import com.srs.common.utils.DateUtils;
import com.srs.comprehensive.domain.*;
import com.srs.comprehensive.domain.Vo.SrsClassInfo;
import com.srs.comprehensive.domain.Vo.CphSearch;
import com.srs.comprehensive.mapper.*;
import com.srs.comprehensive.service.CphSqlServerDataService;
import com.srs.comprehensive.service.ISrsClassService;
import com.srs.comprehensive.util.ListSliceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 班级信息
Service业务层处理
 * 
 * @author srs
 * @date 2023-06-27
 */
@Service
public class SrsClassServiceImpl extends ServiceImpl<SrsClassMapper,SrsClass> implements ISrsClassService
{
    @Autowired
    private SrsClassMapper srsClassMapper;

    @Autowired
    private SchoolClassesMapper schoolClassesMapper;
    @Autowired
    private SrsMajorsMapper srsMajorsMapper;

    @Autowired
    private CphTeacherMapper cphTeacherMapper;

    @Autowired
    private SrsGradeMapper srsGradeMapper;

    @Autowired
    CphSqlServerDataService sqlServerDataService;

    @Override
    public List<SrsClass> listInSchoolClass(CphSearch param){
        return srsClassMapper.listInSchoolClass(param);
    }


    @Override
    public List<SrsClassInfo> listClassView(CphSearch param){
        return srsClassMapper.listClassView(param);
    }


    /**
     * 查询班级信息

     * 
     * @param classId 班级信息
主键
     * @return 班级信息

     */
    @Override
    public SrsClass selectSrsClassByClassId(Long classId)
    {
        return srsClassMapper.selectSrsClassByClassId(classId);
    }

    /**
     * 查询班级信息
列表
     * 
     * @param srsClass 班级信息

     * @return 班级信息

     */
    @Override
    public List<SrsClass> selectSrsClassList(SrsClass srsClass)
    {
        return srsClassMapper.selectSrsClassList(srsClass);
    }

    /**
     * 通过班级代码查询班级信息
     * @param classCode 班级代码
     * @return
     */
    @Override
    public SrsClass selectSrsClassByClassCode(String classCode) {
        return srsClassMapper.selectSrsClassByClassCode(classCode);
    }
    /**
     * 查询班级对应的name
     * @param srsClass
     * @return
     */
    public ArrayList<Object> selectSrsClassName(SrsClass srsClass)
    {
        List<SrsClass> classList = srsClassMapper.selectSrsClassList(srsClass);
        ArrayList<Object> list = new ArrayList<>();
        for (SrsClass aClass : classList) {
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("id",aClass.getClassId());
            hashMap.put("name",aClass.getClassName());
            list.add(hashMap);
        }
        return list;
    }
    /**
     * 新增班级信息
     * 
     * @param srsClass 班级信息

     * @return 结果
     */
    @Override
    public int insertSrsClass(SrsClass srsClass)
    {
        srsClass.setCreateTime(DateUtils.getNowDate());
        return srsClassMapper.insertSrsClass(srsClass);
    }

    /**
     * 修改班级信息
     * 
     * @param srsClass 班级信息
     * @return 结果
     */
    @Override
    public int updateSrsClass(SrsClass srsClass)
    {
        srsClass.setUpdateTime(DateUtils.getNowDate());
        return srsClassMapper.updateSrsClass(srsClass);
    }

    /**
     * 批量删除班级信息
     * 
     * @param classIds 需要删除的班级信息主键
     * @return 结果
     */
    @Override
    public int deleteSrsClassByClassIds(Long[] classIds)
    {
        return srsClassMapper.deleteSrsClassByClassIds(classIds);
    }

    /**
     * 删除班级信息信息
     * 
     * @param classId 班级信息主键
     * @return 结果
     */
    @Override
    public int deleteSrsClassByClassId(Long classId)
    {
        return srsClassMapper.deleteSrsClassByClassId(classId);
    }

    /**
     * 从中间表同步班级信息
     * @return 成功行数
     */
    @Override
    public Map<String,Object> datasync(){
        HashMap<String, Object> map = new HashMap<>();
        int sNum = 0; //成功数量
        int eNum = 0;//失败数量
        List<SchoolClasses> schoolClasses1 = schoolClassesMapper.queryAll();
        for (SchoolClasses classes : schoolClasses1) {
            try {
                SrsClass srsClass = new SrsClass();
                SrsMajors srsMajors = srsMajorsMapper.selectSrsMajorsByMajorCode(classes.getMajorCode());
                CphTeacher cphTeacher = cphTeacherMapper.searchTeacherByTno(classes.gettNo());
                SrsGrade srsGrade = srsGradeMapper.selectSrsGradeByGradeCode(classes.getGradeCode());
                srsClass.setClassName(classes.getClassName());
                srsClass.setMajorId(srsMajors.getMajorId());
                srsClass.setTeacherId(cphTeacher.getTeacherId());
                srsClass.setClassCode(classes.getClassCode());
                srsClass.setGradeId(srsGrade.getGradeId());
                int i = srsClassMapper.insertSrsClass(srsClass);
                if(i>0){
                    classes.setChmodStatus("2");
                    classes.setReadTime(DateUtils.getNowDate());
                    schoolClassesMapper.update(classes);
                    sNum++;
                }
            }catch (Exception e){
                System.out.println(e);
                eNum++;

            }
        }
        map.put("success",sNum);
        map.put("error",eNum);
        return  map;

    }


    //同步班级信息
    @Override
    public void sqlserverSynchronousMYSQL(List<Map> list) {
        //班级列表
        List<SrsClass> srsClasses = srsClassMapper.selectSrsClassInfoList();
        //查找重复值
        List<Map> updateMap = list.stream()
                .filter(obj1 -> srsClasses
                        .stream().anyMatch(obj2 -> {
                            Object xhObj = obj1.get("bjdm");
                            if (xhObj != null) {
                                String zgh = xhObj.toString();
                                return obj2.getClassCode().equals(zgh);
                            }
                            return false;
                        }))
                .collect(Collectors.toList());
        //添加需要
        List<String> kshList2 = srsClasses.stream()
                .map(SrsClass::getClassCode)
                .collect(Collectors.toList());

        List<Map> insertMap = list.stream()
                .filter(obj -> {
                    Object xhObj = obj.get("bjdm");
                    return xhObj != null && !kshList2.contains(xhObj.toString());
                })
                .collect(Collectors.toList());
        //添加
        List<SrsClass> insertStudentList = new ArrayList<>();
        for (Map map:insertMap){
            SrsClass srsClass = new SrsClass();
            Object bjdmobj=map.get("bjdm");
            if (bjdmobj!=null) {
                srsClass.setClassCode(bjdmobj.toString());
            }
            Object njid=map.get("njid");
            if (njid!=null) {
                //通过sqlserver数据库的年级id查询年级代码，使用年级代码查询mysql的年级id
                String njdm = gets(Long.valueOf(njid.toString()));//年级代码
                Long gradeId = srsClassMapper.getGradeId(njdm);//使用年级代码查询本地年级id
                if (gradeId!=null) {
                    srsClass.setGradeId(gradeId);//年级编号
                }
            }
            srsClass.setStatus("0");
            Object zyidobj=map.get("zyid");
            if (zyidobj!=null) {
                String zydm = getzydm(Long.valueOf(zyidobj.toString()));//专业代码
                //使用专业代码查询本地专业id
                if (zydm!=null) {
                    Long zyid = srsMajorsMapper.selectMajorId(zydm);
                    if (zyid!=null) {
                        srsClass.setMajorId(zyid);
                    }
                }
            }
            //使用辅导员工号查询辅导员id
            Object bjdmsobj=map.get("bjdm");
            if (bjdmsobj!=null) {
                String gh = getfdy(bjdmsobj.toString());//辅导员职工号
                if (gh!=null) {
                    CphTeacher fdy = cphTeacherMapper.selectCphTeacher(gh);//辅导员
                    if (fdy!=null) {
                        srsClass.setTeacherId(fdy.getTeacherId());//辅导员id
                    }
                }
            }
            Object bjmcobj=map.get("bjmc");
            if(bjmcobj!=null) {
                srsClass.setClassName(bjmcobj.toString());
            }
            srsClass.setCreateTime(DateUtils.getNowDate());
            insertStudentList.add(srsClass);
        }
        //修改
        List<SrsClass> updateSrsStudent = new ArrayList<>();
        for (Map map:updateMap){
            SrsClass srsClass = new SrsClass();
            Object bjdmobj=map.get("bjdm");
            if (bjdmobj!=null) {
                srsClass.setClassCode(bjdmobj.toString());
            }
            Object njid=map.get("njid");
            if (njid!=null) {
                //通过sqlserver数据库的年级id查询年级代码，使用年级代码查询mysql的年级id
                String njdm = gets(Long.valueOf(njid.toString()));//年级代码
                Long gradeId = srsClassMapper.getGradeId(njdm);//使用年级代码查询本地年级id
                if (gradeId!=null) {
                    srsClass.setGradeId(gradeId);//年级编号
                }
            }
            srsClass.setStatus("0");
            Object zyidobj=map.get("zyid");
            if (zyidobj!=null) {
                String zydm = getzydm(Long.valueOf(zyidobj.toString()));//专业代码
                //使用专业代码查询本地专业id
                if (zydm!=null) {
                    Long zyid = srsMajorsMapper.selectMajorId(zydm);
                    if (zyid!=null) {
                        srsClass.setMajorId(zyid);
                    }
                }
            }
            //使用辅导员工号查询辅导员id
            Object bjdmsobj=map.get("bjdm");
            if (bjdmsobj!=null) {
                String gh = getfdy(bjdmsobj.toString());//辅导员职工号
                if (gh!=null) {
                    CphTeacher fdy = cphTeacherMapper.selectCphTeacher(gh);//辅导员
                    if (fdy!=null) {
                        srsClass.setTeacherId(fdy.getTeacherId());//辅导员id
                    }
                }
            }
            Object bjmcobj=map.get("bjmc");
            if(bjmcobj!=null) {
                srsClass.setClassName(bjmcobj.toString());
            }
            srsClass.setUpdateTime(DateUtils.getNowDate());
            updateSrsStudent.add(srsClass);
        }
        if (insertStudentList.size()!= 0) {
            srsClassMapper.insertBatchSomeColumn(insertStudentList);
        }
        List<List<SrsClass>> lists = ListSliceUtil.updateSlice(updateSrsStudent);
        if (lists != null) {
            for (List<SrsClass> studentList:lists){
                srsClassMapper.updateSrsClassList(studentList);
            }
        }
    }

    @DataSource(DataSourceType.DATABASE)
    public String gets(Long njdm){
        return sqlServerDataService.gets(njdm);
    }

    @DataSource(DataSourceType.DATABASE)
    public String getfdy(String bjdm){
        return sqlServerDataService.getfdy(bjdm);
    }

    @DataSource(DataSourceType.DATABASE)
    public String getzydm(Long zyid){
        return sqlServerDataService.getzydm(zyid);
    }
}

