package com.srs.comprehensive.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.srs.common.core.domain.entity.SysDept;
import com.srs.common.utils.DateUtils;
import com.srs.comprehensive.domain.SchoolMajor;
import com.srs.comprehensive.mapper.*;
import com.srs.comprehensive.service.CphSqlServerDataService;
import com.srs.comprehensive.util.ListSliceUtil;
import com.srs.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.srs.comprehensive.domain.SrsMajors;
import com.srs.comprehensive.service.ISrsMajorsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author srs
 * @date 2023-06-27
 */
@Service
public class SrsMajorsServiceImpl implements ISrsMajorsService
{
    @Autowired
    private SrsMajorsMapper srsMajorsMapper;

    @Autowired
    private SchoolMajorMapper schoolMajorMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    CphSqlServerDataMapper sqlServerDataMapper;

    @Autowired
    CphTeacherMapper cphTeacherMapper;

    @Autowired
    CphSqlServerDataService sqlServerDataService;

    /**
     * 查询【请填写功能名称】
     *
     * @param majorId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SrsMajors selectSrsMajorsByMajorId(Long majorId)
    {
        return srsMajorsMapper.selectSrsMajorsByMajorId(majorId);
    }
    @Override
    public  SrsMajors getOwnMajorName(String  stuNo){
        return  srsMajorsMapper.getOwnMajorName(stuNo);
    }
   @Override
   public Long getMajorIdByName(String name){
       return srsMajorsMapper.getMajorIdByName(name);
   }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param srsMajors 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SrsMajors> selectSrsMajorsList(SrsMajors srsMajors)
    {
        return srsMajorsMapper.selectSrsMajorsList(srsMajors);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param srsMajors 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSrsMajors(SrsMajors srsMajors)
    {
        srsMajors.setCreateTime(DateUtils.getNowDate());
        return srsMajorsMapper.insertSrsMajors(srsMajors);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param srsMajors 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSrsMajors(SrsMajors srsMajors)
    {
        srsMajors.setUpdateTime(DateUtils.getNowDate());
        return srsMajorsMapper.updateSrsMajors(srsMajors);
    }

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

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

    @Override
    public Map<String, Object> dataSync(){
        HashMap<String, Object> map = new HashMap<>();
        int sNum = 0; //成功数量
        int eNum = 0;//失败数量

        List<SchoolMajor> schoolMajors = schoolMajorMapper.queryAll();
        for (SchoolMajor schoolMajor : schoolMajors) {
            try {
                SysDept sysDept = sysDeptMapper.selectDeptByDeptCode(schoolMajor.getDeptCode());
                SrsMajors srsMajors = new SrsMajors();
                srsMajors.setCollegeId(sysDept.getDeptId());
                srsMajors.setMajorName(schoolMajor.getMajorName());
                srsMajors.setMajorCode(schoolMajor.getMajorCode());
                int ref = srsMajorsMapper.insertSrsMajors(srsMajors);
                if (ref>0){
                    schoolMajor.setChmodStatus("2");
                    schoolMajor.setReadTime(DateUtils.getNowDate());
                    schoolMajorMapper.update(schoolMajor);
                    sNum++;
                }

            }catch (Exception e){
                eNum++;
                System.out.println(e);
            }

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

    //同步专业
    @Override
    public void sqlserverSynchronousMYSQL(List<Map> list) {
        List<SrsMajors> srsMajors = srsMajorsMapper.selectSrsmajorsList();//数据库存在的专业列表
        //查找重复值
        List<Map> updateMap = list.stream()
                .filter(obj1 -> srsMajors
                        .stream().anyMatch(obj2 -> {
                            Object xhObj = obj1.get("zydm");
                            if (xhObj != null) {
                                String xh = xhObj.toString();
                                return obj2.getMajorCode().equals(xh);
                            }
                            return false;
                        }))
                .collect(Collectors.toList());

        //添加需要
        List<String> kshList2 = srsMajors.stream()
                .map(SrsMajors::getMajorCode)
                .collect(Collectors.toList());

        List<Map> insertMap = list.stream()
                .filter(obj -> {
                    Object xhObj = obj.get("zydm");
                    return xhObj != null && !kshList2.contains(xhObj.toString());
                })
                .collect(Collectors.toList());
        //添加
        List<SrsMajors> insertStudentList = new ArrayList<>();
        for (Map map:insertMap){
            SrsMajors srsMajors1 = new SrsMajors();
            Object zydmobj=map.get("zydm");
            if (zydmobj!=null) {
                srsMajors1.setMajorCode(zydmobj.toString());
            }
            Object zymcobj=map.get("zymc");
            if (zymcobj!=null) {
                srsMajors1.setMajorName(zymcobj.toString());
            }
            srsMajors1.setStatus("0");

            if (map.get("yxmc")!=null) {
                //使用代码查询学院id
                Long yxdm = srsMajorsMapper.selectOldDeptIdByDeptName(map.get("yxmc").toString());
                if (yxdm!=null) {
                    srsMajors1.setCollegeId(yxdm);//学院id
                }else {
                    srsMajors1.setCollegeId(0L);
                }
            }else {
                srsMajors1.setCollegeId(0L);
            }
            srsMajors1.setCreateTime(DateUtils.getNowDate());
            insertStudentList.add(srsMajors1);
        }
        //修改
        List<SrsMajors> updateSrsStudent = new ArrayList<>();
        for (Map map:updateMap){
            SrsMajors srsMajors1 = new SrsMajors();
            Object zydmobj=map.get("zydm");
            if (zydmobj!=null) {
                srsMajors1.setMajorCode(zydmobj.toString());
            }
            Object zymcobj=map.get("zymc");
            if (zymcobj!=null) {
                srsMajors1.setMajorName(zymcobj.toString());
            }

            if (map.get("yxmc")!=null) {
                //使用代码查询学院id
                Long yxdm = srsMajorsMapper.selectOldDeptIdByDeptName(map.get("yxmc").toString());
                if (yxdm!=null) {
                    srsMajors1.setCollegeId(yxdm);//学院id
                }else {
                    srsMajors1.setCollegeId(0L);
                }
            }else {
                srsMajors1.setCollegeId(0L);
            }
            srsMajors1.setUpdateTime(DateUtils.getNowDate());
            updateSrsStudent.add(srsMajors1);
        }
        if (insertStudentList.size()!=0) {
            srsMajorsMapper.insertBatchSomeColumn(insertStudentList);
        }
        List<List<SrsMajors>> lists = ListSliceUtil.updateSlice(updateSrsStudent);
        if (lists != null) {
            for (List<SrsMajors> studentList:lists){
                 srsMajorsMapper.updateSrsStudentList(studentList);
            }
        }
    }

    //同步专业
    @Override
    public void synchronousMYSQL(List<Map> list) {
        List<SrsMajors> srsMajors = srsMajorsMapper.selectSrsmajorsList();//数据库存在的专业列表
        //查找重复值
        List<Map> updateMap = list.stream()
                .filter(obj1 -> srsMajors
                        .stream().anyMatch(obj2 -> {
                            Object xhObj = obj1.get("zydm");
                            if (xhObj != null) {
                                String xh = xhObj.toString();
                                return obj2.getMajorCode().equals(xh);
                            }
                            return false;
                        }))
                .collect(Collectors.toList());

        //添加需要
        List<String> kshList2 = srsMajors.stream()
                .map(SrsMajors::getMajorCode)
                .collect(Collectors.toList());

        List<Map> insertMap = list.stream()
                .filter(obj -> {
                    Object xhObj = obj.get("zydm");
                    return xhObj != null && !kshList2.contains(xhObj.toString());
                })
                .collect(Collectors.toList());
        //添加
        List<SrsMajors> insertStudentList = new ArrayList<>();
        for (Map map:insertMap){
            SrsMajors srsMajors1 = new SrsMajors();
            Object zydmobj=map.get("zydm");
            if (zydmobj!=null) {
                srsMajors1.setMajorCode(zydmobj.toString());
            }
            Object zymcobj=map.get("zymc");
            if (zymcobj!=null) {
                srsMajors1.setMajorName(zymcobj.toString());
            }
            srsMajors1.setStatus("0");

            if (map.get("yxmc")!=null) {
                //使用代码查询学院id
                Long yxdm = srsMajorsMapper.selectOldDeptIdByDeptName(map.get("yxmc").toString());
                if (yxdm != null) {
                    srsMajors1.setCollegeId(yxdm);//学院id
                } else {
                    srsMajors1.setCollegeId(0L);
                }
            }else {
                srsMajors1.setCollegeId(0L);
            }
            srsMajors1.setCreateTime(DateUtils.getNowDate());
            insertStudentList.add(srsMajors1);
        }

        //修改
        List<SrsMajors> updateSrsStudent = new ArrayList<>();
        for (Map map:updateMap){
            SrsMajors srsMajors1 = new SrsMajors();
            Object zydmobj=map.get("zydm");
            if (zydmobj!=null) {
                srsMajors1.setMajorCode(zydmobj.toString());
            }
            Object zymcobj=map.get("zymc");
            if (zymcobj!=null) {
                srsMajors1.setMajorName(zymcobj.toString());
            }
            if (map.get("yxmc")!=null) {
                //使用代码查询学院id
                Long yxdm = srsMajorsMapper.selectOldDeptIdByDeptName(map.get("yxmc").toString());
                if (yxdm != null) {
                    srsMajors1.setCollegeId(yxdm);//学院id
                } else {
                    srsMajors1.setCollegeId(0L);
                }
            }else {
                srsMajors1.setCollegeId(0L);
            }
            srsMajors1.setUpdateTime(DateUtils.getNowDate());
            updateSrsStudent.add(srsMajors1);
        }
        if (insertStudentList.size()!=0) {
            srsMajorsMapper.insertBatchSomeColumn(insertStudentList);
        }
        List<List<SrsMajors>> lists = ListSliceUtil.updateSlice(updateSrsStudent);
        if (lists != null) {
            for (List<SrsMajors> studentList : lists) {
                srsMajorsMapper.updateSrsStudentList(studentList);
            }
        }
    }

}
