package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.domain.Bo.*;
import com.ruoyi.mall.domain.Vo.*;
import com.ruoyi.mall.mapper.*;
import com.ruoyi.mall.service.IMallAcceptRuleService;
import com.ruoyi.mall.service.IMallCountMethodService;
import com.ruoyi.mall.service.IMallSchoolAcceptService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 学校专业录取数据Service业务层处理
 *
 * @author lsm
 * @date 2023-10-18
 */
@Service
public class MallSchoolAcceptServiceImpl implements IMallSchoolAcceptService {
    @Resource
    private MallSchoolAcceptMapper mallSchoolAcceptMapper;

    @Resource
    private MallAcceptPlanMapper mallAcceptPlanMapper;

    @Resource
    private MallMemberUserMapper mallMemberUserMapper;

    @Resource
    private IMallCountMethodService mallCountMethodService;

    @Resource
    private MallSchoolMapper mallSchoolMapper;

    @Resource
    private MallLinkMajorSchoolMapper mallLinkMajorSchoolMapper;

    @Resource
    private MallMajorRankingMapper mallMajorRankingMapper;

    @Resource
    private MallMemberExpectMapper mallMemberExpectMapper;

    @Resource
    private MallMajorClassMapper mallMajorClassMapper;

    @Resource
    private MallBatchMapper mallBatchMapper;

    @Resource
    private MallAcceptRuleMapper mallAcceptRuleMapper;

    @Resource
    private MallSchoolAcceptRecordMapper mallSchoolAcceptRecordMapper;

    @Resource
    private IMallAcceptRuleService mallAcceptRuleService;

    @Resource
    private MallSchoolBatchLinkMapper mallSchoolBatchLinkMapper;

    private static final Logger log = LoggerFactory.getLogger(MallSchoolAcceptServiceImpl.class);



    /**
     * 查询学校专业录取数据
     *
     * @param schoolAcceptId 学校专业录取数据主键
     * @return 学校专业录取数据
     */
    @Override
    public MallSchoolAccept selectMallSchoolAcceptBySchoolAcceptId(Long schoolAcceptId) {
        return mallSchoolAcceptMapper.selectMallSchoolAcceptBySchoolAcceptId(schoolAcceptId);
    }

    /**
     * 查询学校专业录取数据列表
     *
     * @param mallSchoolAccept 学校专业录取数据
     * @return 学校专业录取数据
     */
    @Override
    public List<MallSchoolAccept> selectMallSchoolAcceptList(MallSchoolAccept mallSchoolAccept) {
        return mallSchoolAcceptMapper.selectMallSchoolAcceptList(mallSchoolAccept);
    }

    /**
     * 新增学校专业录取数据
     *
     * @param mallSchoolAccept 学校专业录取数据
     * @return 结果
     */
    @Override
    public int insertMallSchoolAccept(MallSchoolAccept mallSchoolAccept) {
        return mallSchoolAcceptMapper.insertMallSchoolAccept(mallSchoolAccept);
    }

    /**
     * 修改学校专业录取数据
     *
     * @param mallSchoolAccept 学校专业录取数据
     * @return 结果
     */
    @Override
    public int updateMallSchoolAccept(MallSchoolAccept mallSchoolAccept) {
        return mallSchoolAcceptMapper.updateMallSchoolAccept(mallSchoolAccept);
    }

    /**
     * 批量删除学校专业录取数据
     *
     * @param schoolAcceptIds 需要删除的学校专业录取数据主键
     * @return 结果
     */
    @Override
    public int deleteMallSchoolAcceptBySchoolAcceptIds(Long[] schoolAcceptIds) {
        return mallSchoolAcceptMapper.deleteMallSchoolAcceptBySchoolAcceptIds(schoolAcceptIds);
    }

    /**
     * 删除学校专业录取数据信息
     *
     * @param schoolAcceptId 学校专业录取数据主键
     * @return 结果
     */
    @Override
    public int deleteMallSchoolAcceptBySchoolAcceptId(Long schoolAcceptId) {
        return mallSchoolAcceptMapper.deleteMallSchoolAcceptBySchoolAcceptId(schoolAcceptId);
    }

    /**
     * 根据批次 查询最新一年的 学校专业录取数据
     * 用于 一键生成志愿填报 的基础数据
     */
    @Override
    public List<ReportIntelligenceVo> getListForIntelligence(String subjectType,Long batchId) {

        return mallSchoolAcceptMapper.getListForIntelligence(subjectType,batchId);
    }

    /**
     * 招生数据分析学校列表(更新数据后)
     * @param bo
     * @return
     */
    @Override
    public PageInfo<SchoolVO> selectSteadySchoolList2(CheckingOutParamBo bo) {
        // 我的成绩
        MallMemberUser mallMemberUser = mallMemberUserMapper.selectMallMemberUserById(bo.getMemberId());
        String subjectType = mallMemberUser.getSubjectType();
        Integer cultureScore = mallMemberUser.getCultureScore();
        Integer majorScore = mallMemberUser.getMajorScore();
        bo.setSubjectType(subjectType);
        // 查询学校
        PageHelper.startPage(bo.getPageNum(), bo.getPageSize());
        List<SchoolVO> list = mallSchoolMapper.selectTypeSchoolList(bo);

        if (!list.isEmpty()){
            for (SchoolVO schoolMajorVO : list) {
                // 查询学校专业
                List<MajorItemVo> majorItemVos = mallSchoolMapper.selectMajorList(schoolMajorVO.getSchoolId(), bo.getMajorIds());
                if (!majorItemVos.isEmpty()){
                    for (MajorItemVo majorItemVo : majorItemVos) {

                        // 招生人数查询
                        List<MallAcceptPlan> planList = mallAcceptPlanMapper.selectTwoPlan(majorItemVo.getMajorId(), bo.getBatchId(), bo.getSubjectType());
                        if (!planList.isEmpty()){
                            majorItemVo.setaYear(planList.get(0).getPlanYear());  // 最新一年分数
                            majorItemVo.setaNum(String.valueOf(planList.get(0).getAcceptNum()));
                            majorItemVo.setLittleScoreLimit(planList.get(0).getLittleScoreLimit());  // 小分限制
                            BigDecimal totalScore = mallCountMethodService.getTotalScore(cultureScore, majorScore, planList.get(0).getAcceptRuleId());
                            majorItemVo.setMyScore(totalScore);  // 用户综合分
                            majorItemVo.setSchoolPrice(planList.get(0).getSchoolPrice());

                            if (planList.size() == 2){
                                majorItemVo.setbYear(planList.get(1).getPlanYear());
                                majorItemVo.setbNum(String.valueOf(planList.get(1).getAcceptNum()));
                            }

                            // 三年平均分
                            List<MallSchoolAccept> accepts = mallSchoolAcceptMapper.selectAfterAcceptThree(bo.getBatchId(), schoolMajorVO.getSchoolId(), majorItemVo.getMajorId(), bo.getSubjectType());
                            BigDecimal score = BigDecimal.ZERO;
                            if (!accepts.isEmpty()){
                                majorItemVo.setRuleStr(accepts.get(0).getRuleStr());
                                if (accepts.size() >= 2){
                                    majorItemVo.setAfterYearScore(accepts.get(1).getAcceptLowest());
                                }
                                for (MallSchoolAccept accept : accepts) {
                                    score = score.add(accept.getAcceptLowest());
                                }
                                // 三年平均分
                                majorItemVo.setAverage(score.divide(new BigDecimal(accepts.size()), 2, RoundingMode.HALF_UP));
                            }

                        }

                    }
                }
            }
        }
        return new PageInfo<>(list);
    }

    /**
     * 招生分析
     *
     * @param checkingOutParamBo 参数
     * @return 结果
     */
    @Override
    public PageInfo<SchoolVO> selectSteadySchoolList(CheckingOutParamBo checkingOutParamBo) {

        Integer schoolType = checkingOutParamBo.getSchoolType();
        Integer pageNum = checkingOutParamBo.getPageNum();
        Integer pageSize = checkingOutParamBo.getPageSize();
        Long batchId = checkingOutParamBo.getBatchId();

        // 我的成绩
        MallMemberUser mallMemberUser = mallMemberUserMapper.selectMallMemberUserById(checkingOutParamBo.getMemberId());
        String subjectType = mallMemberUser.getSubjectType();
        Integer cultureScore = mallMemberUser.getCultureScore();
        Integer majorScore = mallMemberUser.getMajorScore();

        List<SchoolVO> schoolList = new ArrayList<>();
        // 今年招生记录
        List<MajorItemVo> newAcceptPlans = mallSchoolAcceptMapper.selectNewAcceptPlanList(checkingOutParamBo, subjectType);

        List<Long> newIds = newAcceptPlans.stream().map(MajorItemVo::getSchoolId).distinct().collect(Collectors.toList());
        // 所有学校id
        List<Long> allSchoolIds = mallSchoolMapper.selectAllSchoolIds(checkingOutParamBo.getBatchId());
        List<Long> noIdList = new ArrayList<>();
        // 已公布学校
        if (!newIds.isEmpty()){
            for (Long allSchoolId : allSchoolIds) {
                if (!newIds.contains(allSchoolId)){
                    noIdList.add(allSchoolId);
                }
            }
        }
        Long[] ids = noIdList.toArray(new Long[noIdList.size()]);
        // 未公布学校
        List<MajorItemVo>  oldAcceptList = mallSchoolAcceptMapper.selectOldAcceptList(checkingOutParamBo, subjectType,ids);

        if (!oldAcceptList.isEmpty()){
            try{
                CountDownLatch countDownLatch1 = new CountDownLatch(oldAcceptList.size());
                for (MajorItemVo majorItemVo : oldAcceptList) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            // 未公布学校数据处理
                            // 计算综合分
                            BigDecimal totalScore = mallCountMethodService.getTotalScore(cultureScore, majorScore, majorItemVo.getAcceptRuleId());
                            majorItemVo.setMyScore(totalScore.setScale(0, RoundingMode.HALF_UP));
                            // 查找3年记录

                            List<MallSchoolAccept> mallSchoolAccept = mallSchoolAcceptMapper.selectAfterAcceptThree(majorItemVo.getBatchId(), majorItemVo.getSchoolId(), majorItemVo.getMajorId(), subjectType);

                            if (!mallSchoolAccept.isEmpty()) {
                                BigDecimal score = BigDecimal.ZERO;
                                for (MallSchoolAccept schoolAccept : mallSchoolAccept) {
                                    score = score.add(schoolAccept.getAcceptLowest());
                                }
                                BigDecimal divide = score.divide(new BigDecimal(mallSchoolAccept.size()), 0, RoundingMode.HALF_UP);
                                majorItemVo.setAverage(divide);
                                // 去年分数
                                majorItemVo.setAfterYearScore(majorItemVo.getLowestScore());
                                // 前年录取人数及年份
                                majorItemVo.setbNum(mallSchoolAccept.get(1).getAcceptNum().toString());
                                majorItemVo.setbYear(mallSchoolAccept.get(1).getAcceptYear());
                                // 学费
                                MallAcceptPlan afterPlan = mallAcceptPlanMapper.selectSchoolPrice(majorItemVo.getSchoolId(), majorItemVo.getMajorId(), majorItemVo.getBatchId(), mallSchoolAccept.get(0).getAcceptYear(), subjectType);
                                majorItemVo.setSchoolPrice(afterPlan.getSchoolPrice());
                                majorItemVo.setLittleScoreLimit(afterPlan.getLittleScoreLimit());
                                majorItemVo.setIsNew(0);
                            }
                            countDownLatch1.countDown();
                        }
                    }).start();
                }
                countDownLatch1.await();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        // 招生稳定
        List<MajorItemVo> list1 = new ArrayList<>();
        // 公式变化
        List<MajorItemVo> list2 = new ArrayList<>();
        // 新招
        List<MajorItemVo> list3 = new ArrayList<>();
        if (!newAcceptPlans.isEmpty()) {

            try{
                CountDownLatch countDownLatch = new CountDownLatch(newAcceptPlans.size());

                for (MajorItemVo majorItemVo : newAcceptPlans) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            // 最新录取年份
                            majorItemVo.setaYear(majorItemVo.getPlanYear());
                            // 计算我综合分数
                            BigDecimal totalScore = mallCountMethodService.getTotalScore(cultureScore, majorScore, majorItemVo.getAcceptRuleId());
                            majorItemVo.setMyScore(totalScore.setScale(0, RoundingMode.HALF_UP));
                            // 查找过去3年记录
                            List<MallSchoolAccept> mallSchoolAccept = mallSchoolAcceptMapper.selectAfterAcceptThree(majorItemVo.getBatchId(), majorItemVo.getSchoolId(), majorItemVo.getMajorId(), subjectType);

                            if (ObjectUtils.isNotEmpty(mallSchoolAccept)) {
                                majorItemVo.setLowestScore(mallSchoolAccept.get(0).getAcceptLowest());
                                // 去年招生人数
                                majorItemVo.setbNum(mallSchoolAccept.get(0).getAcceptNum().toString());
                                majorItemVo.setbYear(mallSchoolAccept.get(0).getAcceptYear());
                                // 去年录取分
                                majorItemVo.setAfterYearScore(mallSchoolAccept.get(0).getAcceptLowest());
                                // 三年平均分
                                if (!mallSchoolAccept.isEmpty()) {
                                    BigDecimal score = BigDecimal.ZERO;
                                    for (MallSchoolAccept schoolAccept : mallSchoolAccept) {
                                        if(null == schoolAccept.getAcceptLowest()){
                                            continue;
                                        }else{
                                            score = score.add(new BigDecimal(schoolAccept.getAcceptLowest().toString()));
                                        }
                                    }
                                    majorItemVo.setAverage(score.divide(BigDecimal.valueOf(mallSchoolAccept.size()), 0, RoundingMode.HALF_UP));
                                }


                                if (majorItemVo.getAcceptRuleId().equals(mallSchoolAccept.get(0).getAcceptRuleId())) {
                                    // 公式未变化
                                    majorItemVo.setIsNew(0);
                                    list1.add(majorItemVo);
                                } else {
                                    // 公式变化
                                    majorItemVo.setIsNew(0);
                                    list2.add(majorItemVo);
                                }
                            } else {
                                majorItemVo.setIsNew(1);
                                list3.add(majorItemVo);
                            }
                            countDownLatch.countDown();
                        }
                    }).start();
                }
                countDownLatch.await();
            }catch (Exception e){
                e.printStackTrace();
            }


            if (schoolType == 0) {
                // 招生稳定
                if (!list1.isEmpty()) {
                    List<Long> collect = list1.stream().map(MajorItemVo::getSchoolId).distinct().sorted().collect(Collectors.toList());

                    try{
                        CountDownLatch countDownLatch = new CountDownLatch(collect.size());
                        for (Long schoolId : collect) {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    // 查找学校信息
                                    SchoolVO schoolVO = mallSchoolMapper.selectSchoolAcceptInfo(schoolId);
                                    List<MajorItemVo> majorList = list1.stream().filter(item -> item.getSchoolId().equals(schoolId)).collect(Collectors.toList());

                                    schoolVO.setMajorList(majorList);

                                    // 学科评估
                                    List<String> majorList1 = mallLinkMajorSchoolMapper.selectMajorListBySchoolId(schoolVO.getSchoolId());
                                    // 专业榜单
                                    List<String> majorList2 = mallMajorRankingMapper.selectMajorBySchoolId(schoolVO.getSchoolId());
                                    majorList1.addAll(majorList2);
                                    if (!majorList1.isEmpty()){
                                        List<String> collect = majorList1.stream().distinct().collect(Collectors.toList());
                                        schoolVO.setAssessList(collect);
                                    }

                                    // 查询是否意向
                                    MallMemberExpect mallMemberExpect = mallMemberExpectMapper.selectMallMemberExpectBySchoolId(checkingOutParamBo.getMemberId(), schoolVO.getSchoolId(), checkingOutParamBo.getBatchId());
                                    if (ObjectUtils.isEmpty(mallMemberExpect)){
                                        schoolVO.setExpectStatus(0);
                                    }else{
                                        schoolVO.setExpectStatus(1);
                                    }
                                    schoolList.add(schoolVO);
                                    countDownLatch.countDown();
                                }
                            }).start();
                        }
                        countDownLatch.await();
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            } else if (schoolType == 2) {
                // 变化

                if (!list2.isEmpty()) {
                    List<Long> collect = list2.stream().map(MajorItemVo::getSchoolId).distinct().sorted().collect(Collectors.toList());
                    try{
                        CountDownLatch countDownLatch = new CountDownLatch(collect.size());
                        for (Long schoolId : collect) {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    // 查找学校信息
                                    SchoolVO schoolVO = mallSchoolMapper.selectSchoolAcceptInfo(schoolId);
                                    List<MajorItemVo> majorList = list2.stream().filter(item -> item.getSchoolId().equals(schoolId)).collect(Collectors.toList());
                                    schoolVO.setMajorList(majorList);

                                    // 学科评估
                                    List<String> majorList1 = mallLinkMajorSchoolMapper.selectMajorListBySchoolId(schoolVO.getSchoolId());
                                    // 专业榜单
                                    List<String> majorList2 = mallMajorRankingMapper.selectMajorBySchoolId(schoolVO.getSchoolId());
                                    majorList1.addAll(majorList2);
                                    if (!majorList1.isEmpty()){
                                        List<String> collect = majorList1.stream().distinct().collect(Collectors.toList());
                                        schoolVO.setAssessList(collect);
                                    }

                                    // 查询是否意向
                                    MallMemberExpect mallMemberExpect = mallMemberExpectMapper.selectMallMemberExpectBySchoolId(checkingOutParamBo.getMemberId(), schoolVO.getSchoolId(), checkingOutParamBo.getBatchId());
                                    if (ObjectUtils.isEmpty(mallMemberExpect)){
                                        schoolVO.setExpectStatus(0);
                                    }else{
                                        schoolVO.setExpectStatus(1);
                                    }
                                    schoolList.add(schoolVO);
                                    countDownLatch.countDown();
                                }
                            }).start();
                        }
                        countDownLatch.await();

                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            } else {
                // 新招和未公布
                if (!list3.isEmpty()) {
                    list3.addAll(oldAcceptList);
                    List<Long> collect = list3.stream().map(MajorItemVo::getSchoolId).distinct().sorted().collect(Collectors.toList());
                   try{
                       CountDownLatch countDownLatch = new CountDownLatch(collect.size());
                       for (Long schoolId : collect) {
                           new Thread(new Runnable() {
                               @Override
                               public void run() {
                                   // 查找学校信息
                                   SchoolVO schoolVO = mallSchoolMapper.selectSchoolAcceptInfo(schoolId);
                                   List<MajorItemVo> majorList = list3.stream().filter(item -> item.getSchoolId().equals(schoolId)).collect(Collectors.toList());
                                   schoolVO.setMajorList(majorList);

                                   // 学科评估
                                   List<String> majorList1 = mallLinkMajorSchoolMapper.selectMajorListBySchoolId(schoolVO.getSchoolId());
                                   // 专业榜单
                                   List<String> majorList2 = mallMajorRankingMapper.selectMajorBySchoolId(schoolVO.getSchoolId());
                                   majorList1.addAll(majorList2);
                                   if (!majorList1.isEmpty()){
                                       List<String> collect = majorList1.stream().distinct().collect(Collectors.toList());
                                       schoolVO.setAssessList(collect);
                                   }

                                   // 查询是否意向
                                   MallMemberExpect mallMemberExpect = mallMemberExpectMapper.selectMallMemberExpectBySchoolId(checkingOutParamBo.getMemberId(), schoolVO.getSchoolId(), checkingOutParamBo.getBatchId());
                                   if (ObjectUtils.isEmpty(mallMemberExpect)){
                                       schoolVO.setExpectStatus(0);
                                   }else{
                                       schoolVO.setExpectStatus(1);
                                   }
                                   schoolList.add(schoolVO);
                                   countDownLatch.countDown();
                               }
                           }).start();
                       }
                       countDownLatch.await();
                   }catch (Exception e){

                   }

                }
            }
        }
        //PageUtils.startPage(checkingOutParamBo.getPageNum(), checkingOutParamBo.getPageSize());
        //if (!schoolList.isEmpty()){
        //    try{
        //        CountDownLatch countDownLatch = new CountDownLatch(schoolList.size());
        //        for (SchoolVO schoolVO : schoolList) {
        //            new Thread(new Runnable() {
        //                @Override
        //                public void run() {
        //                    // 学科评估
        //                    List<String> majorList1 = mallLinkMajorSchoolMapper.selectMajorListBySchoolId(schoolVO.getSchoolId());
        //                    // 专业榜单
        //                    List<String> majorList2 = mallMajorRankingMapper.selectMajorBySchoolId(schoolVO.getSchoolId());
        //                    majorList1.addAll(majorList2);
        //                    if (!majorList1.isEmpty()){
        //                        List<String> collect = majorList1.stream().distinct().collect(Collectors.toList());
        //                        schoolVO.setAssessList(collect);
        //                    }
        //
        //                    // 查询是否意向
        //                    MallMemberExpect mallMemberExpect = mallMemberExpectMapper.selectMallMemberExpectBySchoolId(checkingOutParamBo.getMemberId(), schoolVO.getSchoolId(), checkingOutParamBo.getBatchId());
        //                    if (ObjectUtils.isEmpty(mallMemberExpect)){
        //                        schoolVO.setExpectStatus(0);
        //                    }else{
        //                        schoolVO.setExpectStatus(1);
        //                    }
        //                    countDownLatch.countDown();
        //                }
        //            }).start();
        //            countDownLatch.await();
        //        }
        //    }catch (Exception e){
        //        e.printStackTrace();
        //    }
        //}
        //java8 stream实现list分页
        List<SchoolVO> pageList = schoolList.stream().sorted().skip((long) (pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());
        PageInfo<SchoolVO> pageInfo = new PageInfo<SchoolVO>(pageList);
        //获取PageInfo其他参数
        pageInfo.setTotal(schoolList.size());

        int endRow = pageInfo.getEndRow() == 0 ? 0 : (int)((pageNum - 1) * pageSize + pageInfo.getEndRow() + 1);
        pageInfo.setEndRow(endRow);
        boolean hasNextPage = schoolList.size() <= pageSize * pageNum ? false : true;
        pageInfo.setHasNextPage(hasNextPage);
        boolean hasPreviousPage = pageNum == 1 ? false : true;
        pageInfo.setHasPreviousPage(hasPreviousPage);
        pageInfo.setIsFirstPage(!hasPreviousPage);
        boolean isLastPage = (schoolList.size() > pageSize * (pageNum - 1) && schoolList.size() <= pageSize * pageNum) ? true : false;
        pageInfo.setIsLastPage(isLastPage);
        int pages = schoolList.size() % pageSize == 0 ? schoolList.size() / pageSize : (schoolList.size() / pageSize) + 1;
        pageInfo.setNavigateLastPage(pages);
        int[] navigatePageNums = new int[pages];
        for (int i = 1; i < pages; i++) {
            navigatePageNums[i - 1] = i;
        }
        pageInfo.setNavigatepageNums(navigatePageNums);
        int nextPage = pageNum < pages ? pageNum + 1 : 0;
        pageInfo.setNextPage(nextPage);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPages(pages);
        pageInfo.setPrePage(pageNum - 1);
        pageInfo.setSize(pageInfo.getList().size());
        int starRow = schoolList.size() < pageSize * pageNum ? 1 + pageSize * (pageNum - 1) : 0;
        pageInfo.setStartRow(starRow);
        return pageInfo;
    }

    /**
     * 招生数据分析
     *
     * @param id 用户id
     * @return
     */
    @Override
    public AjaxResult getAcceptData(Long id, Long batchId) {

        // 我的成绩
        MallMemberUser mallMemberUser = mallMemberUserMapper.selectMallMemberUserById(id);
        Integer cultureScore = mallMemberUser.getCultureScore();
        Integer majorScore = mallMemberUser.getMajorScore();
        String subjectType = mallMemberUser.getSubjectType();

        List<SchoolVO> schoolList = new ArrayList<>();
        // 今年招生记录
        CheckingOutParamBo checkingOutParamBo = new CheckingOutParamBo();
        checkingOutParamBo.setBatchId(batchId);
        List<MajorItemVo> newAcceptPlans = mallSchoolAcceptMapper.selectNewAcceptPlanList(checkingOutParamBo, subjectType);

        List<Long> newIds = newAcceptPlans.stream().map(MajorItemVo::getSchoolId).distinct().collect(Collectors.toList());

        // 所有学校id
        List<Long> allSchoolIds = mallSchoolMapper.selectAllSchoolIds(batchId);
        List<Long> noIdList = new ArrayList<>();
        // 已公布学校
        if (!newIds.isEmpty()){
            for (Long allSchoolId : allSchoolIds) {
                if (!newIds.contains(allSchoolId)){
                    noIdList.add(allSchoolId);
                }
            }
        }
        Long[] ids = noIdList.toArray(new Long[noIdList.size()]);

        // 未公布学校
        //List<MajorItemVo> oldAcceptList = mallSchoolAcceptMapper.selectOldAcceptList(checkingOutParamBo, subjectType,ids);

        // 招生稳定
        List<MajorItemVo> list1 = new ArrayList<>();
        // 公式变化
        List<MajorItemVo> list2 = new ArrayList<>();
        // 新招
        List<MajorItemVo> list3 = new ArrayList<>();

        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        if (!newAcceptPlans.isEmpty()) {
            //// 查找去年记录
            int afterYear = mallSchoolAcceptMapper.selectAfterYear();

            try{
                CountDownLatch countDownLatch = new CountDownLatch(newAcceptPlans.size());
                for (MajorItemVo majorItemVo : newAcceptPlans) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            // 计算我综合分数
                            BigDecimal totalScore = mallCountMethodService.getTotalScore(cultureScore, majorScore, majorItemVo.getAcceptRuleId());
                            majorItemVo.setMyScore(totalScore.setScale(0, RoundingMode.HALF_UP));


                            MallSchoolAccept mallSchoolAccept = mallSchoolAcceptMapper.selectAfterAccept(majorItemVo.getBatchId(), majorItemVo.getSchoolId(), majorItemVo.getLinkId(), subjectType, afterYear);

                            if (ObjectUtils.isNotEmpty(mallSchoolAccept)) {
                                majorItemVo.setLowestScore(mallSchoolAccept.getAcceptLowest());
                                if(null == majorItemVo.getLowestScore()){
                                    //System.out.println("找到null啦：majorItem/学校"+majorItemVo.getSchoolId()+"/专业"+majorItemVo.getMajorId()+"/批次"+majorItemVo.getBatchId());
                                    countDownLatch.countDown();
                                }
                                if (majorItemVo.getAcceptRuleId().equals(mallSchoolAccept.getAcceptRuleId())) {
                                    // 公式未变化
                                    list1.add(majorItemVo);
                                } else {
                                    // 公式变化
                                    list2.add(majorItemVo);
                                }
                            } else {
                                // 新招
                                list3.add(majorItemVo);
                            }
                            countDownLatch.countDown();
                        }
                    }).start();
                }
                countDownLatch.await();
            }catch (Exception e){
                e.printStackTrace();
                throw new ServiceException();
            }

            if (!list1.isEmpty()) {
                // 稳定专业学校数量
                List<MajorItemVo> collect = list1.stream().filter(distinctByKey1(MajorItemVo::getSchoolId)).collect(Collectors.toList());
                List<Long> schoolIds = collect.stream().map(MajorItemVo::getSchoolId).collect(Collectors.toList());
                map1.put("schoolNum", schoolIds.size());

                // 我的分数高于的学校
                List<MajorItemVo> collect1 = collect.stream().filter(item -> item.getMyScore().compareTo(item.getLowestScore()) >= 0 ).collect(Collectors.toList());
                List<Long> higherSchoolList = collect1.stream().map(MajorItemVo::getSchoolId).distinct().collect(Collectors.toList());
                map1.put("higherSchoolNum", higherSchoolList.size());

                // 我的分数低于的学校
                List<MajorItemVo> collect2 = collect.stream().filter(item -> item.getMyScore().compareTo(item.getLowestScore()) < 0).collect(Collectors.toList());
                List<Long> lowerSchoolList = collect2.stream().map(MajorItemVo::getSchoolId).distinct().collect(Collectors.toList());
                map1.put("lowerSchoolNum", lowerSchoolList.size());
            }

            // 招生公式变化数量
            if (!list2.isEmpty()) {
                // 新招合并未公布学校专业
                List<Long> collect = list2.stream().map(MajorItemVo::getSchoolId).distinct().collect(Collectors.toList());
                map2.put("schoolNum", collect.size());
            }

            // 新招或未公布数量
            if (!list3.isEmpty()) {
                List<Long> collect = list3.stream().map(MajorItemVo::getSchoolId).distinct().collect(Collectors.toList());
                map3.put("schoolNum", collect.size()  + ids.length);
            }


        }
        Map<String, Object> result = new HashMap<>();
        result.put("steadySchool", map1);
        result.put("newSchool", map3);
        result.put("changeSchool", map2);

        return AjaxResult.success(result);
    }

    /**
     * 招生数据分析统计学校数量
     * @param id
     * @param batchId
     * @return
     */
    @Override
    public AjaxResult getAcceptData2(Long id, Long batchId) {
        MallMemberUser mallMemberUser = mallMemberUserMapper.selectMallMemberUserById(id);
        Integer cultureScore = mallMemberUser.getCultureScore();
        Integer majorScore = mallMemberUser.getMajorScore();
        String subjectType = mallMemberUser.getSubjectType();
        String s = mallAcceptPlanMapper.selectMaxYear(); // 去年年份
        String afterYear = String.valueOf(Integer.valueOf(s) - 1);
        // 招生稳定
        List<MallSchoolAccept> list = mallSchoolMapper.selectSchoolSteadyNum(batchId, subjectType, afterYear);
        // 新招和未公布
        int b = mallSchoolMapper.selectSchoolNewNum(batchId, subjectType);
        // 招生公式变化
        int c = mallSchoolMapper.selectSchoolChangeNum(batchId, subjectType);

        List<MallSchoolAccept> collectList = new ArrayList<>();

        if (!list.isEmpty()){
            // 计算学生各种录取率下的成绩
            List<MallAcceptRule> rules = mallAcceptRuleMapper.selectNewAcceptRule();
            List<UserScoreRuleBO> scoreList = new ArrayList<>();
            // 计算综合分
            CountDownLatch countDownLatch = new CountDownLatch(rules.size());
            try {
                for (MallAcceptRule rule : rules) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            BigDecimal totalScore = mallCountMethodService.getTotalScore(cultureScore, majorScore, rule.getAcceptRuleId());
                            UserScoreRuleBO scoreRuleBO = new UserScoreRuleBO();
                            //scoreRuleBO.setTotalScore(totalScore);
                            //scoreRuleBO.setAcceptRuleId(rule.getAcceptRuleId());
                            //scoreList.add(scoreRuleBO);

                            List<MallSchoolAccept> collect = list.stream().filter(item -> null != item.getAcceptRuleId() && item.getAcceptRuleId().equals(rule.getAcceptRuleId()) && item.getAcceptLowest() != null && item.getAcceptLowest().compareTo(totalScore) >= 0).collect(Collectors.toList());
                            collectList.addAll(collect);
                            countDownLatch.countDown();
                        }
                    }).start();
                }
                countDownLatch.await();
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        int higherNum = collectList.size();

        Map<String, Object> map1 = new HashMap<>();
        map1.put("schoolNum", list.size());
        map1.put("higherSchoolNum", higherNum);
        map1.put("lowerSchoolNum", list.size() - higherNum);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("schoolNum", b);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("schoolNum", c);

        Map<String, Object> result = new HashMap<>();
        result.put("steadySchool", map1);
        result.put("newSchool", map2);
        result.put("changeSchool", map3);

        return AjaxResult.success(result);
    }

    static <T> Predicate<T> distinctByKey1(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    /**
     * 导入专业录取数据
     * @param list
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importAcceptData(List<NewAcceptBO> list) {
//        String afterYear = "2023";
        //  重置去年数据为旧数据
//        mallSchoolAcceptMapper.resetAfterData(afterYear);
//        mallSchoolAcceptMapper.resetAfterRecordData(afterYear);

        // 删除24年录取记录
        mallSchoolAcceptRecordMapper.deleteRecord();
        int a = 0;
        int b = 0;

        int successNum = 0;
        int failureNum = 0;
        List<MallSchool> schoolList = mallSchoolMapper.selectMallSchoolList(null);
        // 专业分类
        List<MallMajorClass> classList = mallMajorClassMapper.selectAllMajorList();
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        StringBuilder sb = new StringBuilder();

        //要插入数据库的 院校录取数据
        List<MallSchoolAcceptRecord> schoolAcceptRecordList = new ArrayList<>();
        List<ErrorDataBO> errorList = new ArrayList<>();
        for (NewAcceptBO newAcceptBO : list) {
            // 错误信息
            ErrorDataBO errorDataBO = new ErrorDataBO();
            int index = list.indexOf(newAcceptBO) + 2;
            try{
                newAcceptBO.setBatchName("本科");
                String batchName = newAcceptBO.getBatchName().replaceAll("\\s*|\r|\n|\t", "");; // 批次

                String schoolName = replaceOther(newAcceptBO.getSchoolName()); // 学校名称
                String majorNameStr = replaceOther(newAcceptBO.getMajorNameStr()); // 专业名

                String ruleStr = replaceOther(newAcceptBO.getRuleStr());  // 录取原则

                BigDecimal reference = newAcceptBO.getReference(); // 参考分
                String acceptYear = newAcceptBO.getAcceptYear(); // 年份

                String subjectType = newAcceptBO.getSubjectType().contains("历") ? "0" : "1"; // 学科
                if (StringUtils.isBlank(batchName)){
                    //return "第" + index + "行缺少批次名称";
                    sb.append("第【" +index + "】行学校【"+schoolName+"】缺少批次名称;").append(System.lineSeparator());
                    errorDataBO.setMsg("第" +index + "行学校【"+schoolName+"】行缺少批次名称");
                    errorList.add(errorDataBO);
                    continue;

                }
                if (StringUtils.isBlank(schoolName)){
                    //return "第" + index + "行缺少学校名称数据";
                    sb.append("第【" +index + "】行学校【"+schoolName+"】缺少学校名称数据;").append(System.lineSeparator());
                    errorDataBO.setMsg("第" +index + "行学校【"+schoolName+"】行缺少学校名称数据");
                    errorList.add(errorDataBO);
                    continue;
                }
                if (StringUtils.isBlank(majorNameStr)){
                    //return "第" + index + "行缺少专业名称";
                    sb.append("第【" +index + "】行学校【"+schoolName+"】缺少专业名称").append(System.lineSeparator());
                    errorDataBO.setMsg("第" +index + "行学校【"+schoolName+"】行缺少专业名称");
                    errorList.add(errorDataBO);
                    continue;
                }

                if (StringUtils.isBlank(ruleStr)){
                    //return  "第" + index + "行缺少录取原则";
                    sb.append("第【" +index + "】行学校【"+schoolName+"】缺少录取原则;").append(System.lineSeparator());
                    errorDataBO.setMsg("第" +index + "行学校【"+schoolName+"】行缺少录取原则");
                    errorList.add(errorDataBO);
                    continue;
                }
//                if (ObjectUtils.isEmpty(reference)){
//                    sb.append("第【" +index + "】行学校【"+schoolName+"】缺少参考分;").append(System.lineSeparator());
//                    errorDataBO.setMsg("第" +index + "行学校【"+schoolName+"】行缺少参考分");
//                    errorList.add(errorDataBO);
//                    continue;
//                }
                if (StringUtils.isBlank(acceptYear)){
                    sb.append("第【" +index + "】行学校【"+schoolName+"】缺少年份;").append(System.lineSeparator());
                     errorDataBO.setMsg("第" +index + "行学校【"+schoolName+"】行缺少年份;");
                     errorList.add(errorDataBO);
                    continue;
                }
                if (StringUtils.isBlank(subjectType)){
                    sb.append("第【" +index + "】行学校【"+schoolName+"】缺少学科分类;").append(System.lineSeparator());
                    errorDataBO.setMsg("第" +index + "行学校【"+schoolName+"】行缺少学科分类;");
                    errorList.add(errorDataBO);
                    continue;
                }

                Integer acceptNum = 0; // 录取数量
                if (ObjectUtils.isEmpty(newAcceptBO.getAcceptNum())){
                    acceptNum = 0;
                }else{
                    acceptNum = newAcceptBO.getAcceptNum();
                }

                // 批次查询
                MallBatch mallBatch = mallBatchMapper.selectMallBatchByBatchName(batchName);
                if (ObjectUtils.isEmpty(mallBatch)){
                    //return   "第" + index + "行批次不存在,请先添加批次或批次名称不正确";
                }
                // 查询学校
                List<MallSchool> collect = schoolList.stream().filter(item -> replaceOther(item.getSchoolName()).equals(schoolName)).collect(Collectors.toList());
                if (ObjectUtils.isEmpty(collect)){
                    sb.append("第【" +index + "】行学校【"+schoolName+"】不存在或学校名称错误").append(System.lineSeparator());
                    errorDataBO.setMsg("第" +index + "行学校【"+schoolName+"】不存在或学校名称错误:");
                    errorList.add(errorDataBO);
                    continue;
                    //return "第" + index + "学校不存在或学校名称错误:"+ schoolName;
                }
                MallSchool school = collect.get(0);

                BigDecimal acceptLowest = BigDecimal.ZERO;
                if (ObjectUtils.isEmpty(newAcceptBO.getAcceptLowest()) ){
                    // acceptLowest = new BigDecimal("500");
                    acceptLowest = null;
                }else{
                    acceptLowest = newAcceptBO.getAcceptLowest().setScale(2,RoundingMode.HALF_UP); // 最低分
                }

                String littleScoreLimit = newAcceptBO.getLittleScoreLimit();// 小分限制

                BigDecimal cultureLowest = BigDecimal.ZERO ;              // 文化分数线
                if (ObjectUtils.isEmpty(newAcceptBO.getCultureLowest())){
                    sb.append("第【" +index + "】行学校【"+schoolName+"】缺少文化分数线;").append(System.lineSeparator());
                    errorDataBO.setMsg("第" +index + "行学校【"+schoolName+"】缺少文化分数线");
                    errorList.add(errorDataBO);
                    continue;
                    //return "缺少文化分数线" + index;
                }else{
                    cultureLowest = newAcceptBO.getCultureLowest();
                }

                BigDecimal majorLowest = new BigDecimal("0") ;        // 专业分数线
                if (ObjectUtils.isEmpty(newAcceptBO.getMajorLowest())){
                    //majorLowest = new BigDecimal("216");
                    sb.append("第【" +index + "】行学校【"+schoolName+"】缺少联考分数线;").append(System.lineSeparator());
                    errorDataBO.setMsg("第" +index + "行学校【"+schoolName+"】缺少联考分数线");
                    errorList.add(errorDataBO);
                    continue;
                    //return "缺少文化分数线" + index;
                }else{

                    majorLowest = newAcceptBO.getMajorLowest();
                }
                BigDecimal schoolPrice = newAcceptBO.getSchoolPrice();    // 学费

                //// 查询学校批次关联
                //MallSchoolBatchLink mallSchoolBatchLink = mallSchoolBatchLinkMapper.selectSchoolBatchLinkBySchoolIdAndBatchId(collect.get(0).getSchoolId(), mallBatch.getBatchId());
                //if (ObjectUtils.isEmpty(mallSchoolBatchLink)){
                //    // 生成新的关联
                //    MallSchoolBatchLink schoolBatchLink = new MallSchoolBatchLink();
                //    schoolBatchLink.setSchoolId(school.getSchoolId());
                //    schoolBatchLink.setBatchId(mallBatch.getBatchId());
                //    schoolBatchLink.setBatchName(mallBatch.getBatchName());
                //    schoolBatchLink.setCreateAt(DateUtils.getTime());
                //    schoolBatchLink.setDelFlag("0");
                //    mallSchoolBatchLinkMapper.insertMallSchoolBatchLink(schoolBatchLink);
                //}

                // 查询专业是否存在
                MallLinkMajorSchool linkMajorSchool = mallLinkMajorSchoolMapper.selectMajorSchoolBySchoolAndMajorId(school.getSchoolId(), majorNameStr);
                Long linkId = 0L;
                //List<MallMajorClass> collect1 = classList.stream().filter(item -> item.getMajorName().equals(parentClassName) && item.getParentId().equals(0L)).collect(Collectors.toList());
                //if (collect1.isEmpty()){
                //    return "一级分类不存在,请先添加.行数: " + index;
                //}
                //String[] split = className.split(",");
                //StringBuilder twoIds = new StringBuilder();
                //for (String s : split) {
                //    List<MallMajorClass> collect2 = classList.stream().filter(item -> item.getMajorName().equals(s) && !item.getParentId().equals(0L)).collect(Collectors.toList());
                //    if (collect2.isEmpty()){
                //        return "二级专业分类不存在,请先添加.行数" + index;
                //    }
                //    twoIds.append(collect2.get(0).getMajorId()).append(",");
                //}
                if (ObjectUtils.isEmpty(linkMajorSchool)){
                    sb.append("第【" +index + "】行学校【"+schoolName+"】【"+majorNameStr+"】专业未录入;").append(System.lineSeparator());
                    errorDataBO.setMsg("第" +index + "行学校【"+schoolName+"】专业未录入或名称错误");
                    errorList.add(errorDataBO);
                    continue;
                    //return "专业未录入" + index + "-" + schoolName + "-" + majorNameStr;
                    //// 专业不存在新增
                    //MallLinkMajorSchool newLink = new MallLinkMajorSchool();
                    //newLink.setSchoolId(school.getSchoolId());
                    //newLink.setSchoolName(schoolName);
                    //newLink.setMajorName(majorNameStr);
                    ////newLink.setParentMajorId(collect1.get(0).getMajorId()); // 一级分类
                    //newLink.setMajorId(twoIds.toString());  //二级分类
                    //newLink.setType("2");
                    //newLink.setCreateAt(DateUtils.getTime());
                    //newLink.setTwoClassName(className);
                    //mallLinkMajorSchoolMapper.insertMallLinkMajorSchool(newLink);
                    //linkId = newLink.getLinkId();
                }else{
                    ////linkMajorSchool.setParentMajorId(collect1.get(0).getMajorId());
                    //linkMajorSchool.setMajorId(twoIds.toString());
                    //linkMajorSchool.setTwoClassName(className);
                    //linkMajorSchool.setUpdateAt(DateUtils.getTime());
                    //mallLinkMajorSchoolMapper.updateMallLinkMajorSchool(linkMajorSchool);

                    linkId = linkMajorSchool.getLinkId();
                }

                // 查询录取原则
                List<MallAcceptRule> acceptRule = mallAcceptRuleMapper.selectMallAcceptRuleByAcceptRuleStr2(ruleStr);
                Long acceptRuleId = 0L;
                if (acceptRule.isEmpty()){
                    sb.append("第【" +index + "】行学校【"+schoolName+"】录取原则【"+ruleStr+"】不存在或录入错误;").append(System.lineSeparator());
                    errorDataBO.setMsg("第" +index + "行学校【"+schoolName+"】录取原则不存在或录入错误");
                    errorList.add(errorDataBO);
                    continue;
                }else{
                    acceptRuleId = acceptRule.get(0).getAcceptRuleId();
                }

                // 查询是否已导入数据
                List<MallSchoolAccept> schoolAcceptList = mallSchoolAcceptMapper.selectBySchoolIdAndLinkId(school.getSchoolId(), mallBatch.getBatchId(), linkId, acceptYear, subjectType);
                if (schoolAcceptList.isEmpty()){
                    // 生成新专业录取数据
                    MallSchoolAccept mallSchoolAccept = new MallSchoolAccept();
                    mallSchoolAccept.setSchoolId(school.getSchoolId());
                    mallSchoolAccept.setBatchId(mallBatch.getBatchId());
                    mallSchoolAccept.setLinkId(linkId);   // 对应专业表id
                    mallSchoolAccept.setAcceptNum(acceptNum);
                    mallSchoolAccept.setAcceptLowest(acceptLowest);
                    mallSchoolAccept.setAcceptYear(acceptYear);
                    mallSchoolAccept.setAcceptRuleId(acceptRuleId);
                    mallSchoolAccept.setSubjectType(subjectType);
                    mallSchoolAccept.setCreateAt(DateUtils.getTime());
                    mallSchoolAccept.setDelFlag("0");
                    mallSchoolAccept.setIsReference(1);
                    mallSchoolAccept.setReference(reference);
                    mallSchoolAcceptMapper.insertMallSchoolAccept(mallSchoolAccept);


                    // add list 新专业录取数
                    //addSchoolAcceptRecord( schoolAcceptRecordList,mallSchoolAccept,cultureLowest,majorLowest);

                    // 生成新招生计划
                    MallAcceptPlan mallAcceptPlan = new MallAcceptPlan();
                    mallAcceptPlan.setSchoolId(school.getSchoolId());
                    mallAcceptPlan.setBatchId(mallBatch.getBatchId());
                    mallAcceptPlan.setLinkId(linkId);
                    mallAcceptPlan.setPlanYear(acceptYear);
                    mallAcceptPlan.setSubjectType(Integer.valueOf(subjectType));
                    mallAcceptPlan.setAcceptNum(acceptNum);
                    mallAcceptPlan.setSchoolPrice(schoolPrice);
                    mallAcceptPlan.setAcceptRuleId(acceptRuleId);
                    mallAcceptPlan.setLittleScoreLimit(littleScoreLimit);
                    mallAcceptPlan.setCreateAt(DateUtils.getTime());
                    mallAcceptPlan.setDelFlag("0");
                    mallAcceptPlanMapper.insertMallAcceptPlan(mallAcceptPlan);

                    // 生成记录
                    MallSchoolAcceptRecord record = new MallSchoolAcceptRecord();
                    record.setSchoolId(school.getSchoolId());
                    record.setBatchId(mallBatch.getBatchId());
                    record.setAcceptNum(acceptNum);
                    record.setAcceptYear(acceptYear);
                    record.setLowestScore(acceptLowest);
                    record.setCultureLowest(cultureLowest);
                    record.setMajorLowest(majorLowest);
                    record.setAcceptRuleId(acceptRuleId);
                    record.setSubjectType(subjectType);
                    record.setCreateAt(DateUtils.getTime());
                    record.setDelFlag("0");
                    mallSchoolAcceptRecordMapper.insertMallSchoolAcceptRecord(record);
                    a++;
                }else{
                    b++;
                    // 修改数据
                    MallSchoolAccept schoolAccept = schoolAcceptList.get(0);
                    // 生成新专业录取数据
                    schoolAccept.setAcceptNum(acceptNum);
                    schoolAccept.setAcceptLowest(acceptLowest);
                    schoolAccept.setAcceptRuleId(acceptRuleId);
                    schoolAccept.setUpdateAt(DateUtils.getTime());
                    schoolAccept.setIsReference(1);
                    schoolAccept.setReference(reference);  // 参考分
                    mallSchoolAcceptMapper.updateMallSchoolAccept(schoolAccept);

                    // 修改招生计划
                    List<MallAcceptPlan> mallAcceptPlanList = mallAcceptPlanMapper.selectPlan(school.getSchoolId(), mallBatch.getBatchId(), acceptYear, Integer.valueOf(subjectType), linkId);
                    if (!mallAcceptPlanList.isEmpty()){
                        MallAcceptPlan plan = mallAcceptPlanList.get(0);
                        plan.setAcceptNum(acceptNum);
                        plan.setSchoolPrice(schoolPrice);
                        plan.setAcceptRuleId(acceptRuleId);
                        plan.setLittleScoreLimit(littleScoreLimit);
                        plan.setUpdateAt(DateUtils.getTime());
                        plan.setDelFlag("0");
                        mallAcceptPlanMapper.updateMallAcceptPlan(plan);
                    }

                    // 生成记录
                    MallSchoolAcceptRecord record = new MallSchoolAcceptRecord();
                    record.setSchoolId(school.getSchoolId());
                    record.setBatchId(mallBatch.getBatchId());
                    record.setAcceptNum(acceptNum);
                    record.setAcceptYear(acceptYear);
                    record.setLowestScore(acceptLowest);
                    record.setCultureLowest(cultureLowest);
                    record.setMajorLowest(majorLowest);
                    record.setAcceptRuleId(acceptRuleId);
                    record.setSubjectType(subjectType);
                    record.setCreateAt(DateUtils.getTime());
                    record.setDelFlag("0");
                    mallSchoolAcceptRecordMapper.insertMallSchoolAcceptRecord(record);
                }
                successNum++;

            }catch (Exception e){
                failureNum++;
                String msg = "<br/>" +"第" + index + "行的录取信息:" + newAcceptBO.getSchoolName() + newAcceptBO.getAcceptYear() + newAcceptBO.getMajorNameStr() + " 导入失败;";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        //// 数据库 批量插入 院校录取数据
        //if (!schoolAcceptRecordList.isEmpty()){
        //    mallSchoolAcceptRecordMapper.insertByList(schoolAcceptRecordList);
        //}

        //System.out.println(errorList.size());
        ExcelUtil<ErrorDataBO> util = new ExcelUtil<ErrorDataBO>(ErrorDataBO.class);
         util.exportLocal(errorList, "错误数据");
        System.out.println("======新增数量" + a + "======修改数量" + b + "----错误数据" + sb.toString());
        if (failureNum > 0)
        {
            failureMsg.insert(0, "导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "数据已全部导入成功！共 " + successNum + " 条");
        }

        return successMsg.toString();
        //return errorList;
    }

    /**
     * 专科导入
     * @param list
     * @return
     */
    @Override
    public String importZhuankeData(List<ZhuankeBO> list) {

        //  重置去年数据为旧数据
        //mallSchoolAcceptMapper.resetAfterData("2023");
        //mallSchoolAcceptMapper.resetAfterRecordData("2023");

        int successNum = 0;
        int failureNum = 0;
        //List<MallSchool> schoolList = mallSchoolMapper.selectMallSchoolList(null);
        // 专业分类
        //List<MallMajorClass> classList = mallMajorClassMapper.selectAllMajorList();
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        StringBuilder sb = new StringBuilder();

        //要插入数据库的 院校录取数据
        //List<MallSchoolAcceptRecord> schoolAcceptRecordList = new ArrayList<>();
        List<MallSchool> schoolList = mallSchoolMapper.selectMallSchoolList(null);
        for (ZhuankeBO zhuankeBO : list) {
            int index = list.indexOf(zhuankeBO) + 2;
            try{
                String batchName = replaceOther(zhuankeBO.getBatchName());

                String schoolNumber = replaceOther(zhuankeBO.getSchoolNumber()); // 代码 去空格回车等
                String schoolName = replaceOther(zhuankeBO.getSchoolName()); // 学校名称
                String majorName = replaceOther(zhuankeBO.getMajorName()); // 专业名
                String ruleStr = replaceOther(zhuankeBO.getRuleStr());  // 录取原则
                String acceptYear = zhuankeBO.getAcceptYear(); // 年份
                String subjectType = replaceOther(zhuankeBO.getSubjectType()).contains("历") ? "0" : "1"; // 学科

                if (StringUtils.isBlank(batchName)){
                    return "第" + index + "行缺少批次名称";
                }
                if (StringUtils.isBlank(schoolName)){
                    sb.append("第" + index + "行缺少学校名称数据;") ;
                    continue;
                }
                if (StringUtils.isBlank(majorName)) {
                    sb.append("第" + index + "行缺少专业名称;") ;
                    continue;
                }
                if (StringUtils.isBlank(ruleStr)){
                    return  "第" + index + "行缺少录取原则";
                }

                //if (StringUtils.isBlank(subjectType)){
                //    return "第" + index + "行缺少学科分类(文科,理科)" + schoolName + "-专业" + majorName + ";" ;
                //
                //}

                Integer acceptNum = 0; // 录取数量
                if (!ObjectUtils.isEmpty(zhuankeBO.getAcceptNum())){
                    acceptNum = zhuankeBO.getAcceptNum();
                }

                // 批次查询
                Long schoolId = 0L;
                MallBatch mallBatch = mallBatchMapper.selectMallBatchByBatchName(batchName);
                if (ObjectUtils.isEmpty(mallBatch)){
                    return   "第" + index + "行批次不存在,请先添加批次或批次名称不正确";
                }
                // 查询学校
                List<MallSchool> collect = schoolList.stream().filter(item -> replaceOther(item.getSchoolName()).equals(schoolName)).collect(Collectors.toList());
//                if (ObjectUtils.isEmpty(collect)){
//                    sb.append("第【").append(index).append("】行学校【").append(schoolName).append("】不存在或学校名称错误").append(System.lineSeparator());
//                    continue;
//                }
                if (ObjectUtils.isNotEmpty(collect)){
                    // 已有学校
                    schoolId = collect.get(0).getSchoolId();
                    // 查询是否有专科批次
                    MallSchoolBatchLink schoolBatchLink = mallSchoolBatchLinkMapper.selectSchoolBatchLinkBySchoolIdAndBatchId(schoolId, 4L);
                    if (ObjectUtils.isEmpty(schoolBatchLink)){
                        // 批次关联
                        MallSchoolBatchLink batchLink = new MallSchoolBatchLink();
                        batchLink.setSchoolId(schoolId);
                        batchLink.setBatchId(4L);
                        batchLink.setBatchName(batchName);
                        batchLink.setCreateAt(DateUtils.getTime());
                        batchLink.setDelFlag("0");
                        mallSchoolBatchLinkMapper.insertMallSchoolBatchLink(batchLink);
                    }
                }else {
                    MallSchool newSchool = new MallSchool();
                    newSchool.setSchoolName(schoolName);
                    newSchool.setSchoolNumber(schoolNumber);
                    newSchool.setDelFlag("0");
                    int i = mallSchoolMapper.insertMallSchool(newSchool);
                    schoolId = newSchool.getSchoolId();
                    schoolList.add(newSchool);

                    // 批次关联
                    MallSchoolBatchLink batchLink = new MallSchoolBatchLink();
                    batchLink.setSchoolId(schoolId);
                    batchLink.setBatchId(4L);
                    batchLink.setBatchName(batchName);
                    batchLink.setCreateAt(DateUtils.getTime());
                    batchLink.setDelFlag("0");
                    mallSchoolBatchLinkMapper.insertMallSchoolBatchLink(batchLink);
                }


                BigDecimal acceptLowest = zhuankeBO.getAcceptLowest(); // 最低分
                BigDecimal schoolPrice = zhuankeBO.getPrice();    // 学费

                // 查询专业是否存在
                MallLinkMajorSchool linkMajorSchool = mallLinkMajorSchoolMapper.selectMajorSchoolBySchoolAndMajorId(schoolId, majorName);
                Long linkId = 0L;

                if (ObjectUtils.isEmpty(linkMajorSchool)){
                    // 专业不存在新增
                    MallLinkMajorSchool newLink = new MallLinkMajorSchool();
                    newLink.setSchoolId(schoolId);
                    newLink.setSchoolName(schoolName);
                    newLink.setMajorName(majorName);
                    newLink.setType("2");
                    newLink.setCreateAt(DateUtils.getTime());
                    mallLinkMajorSchoolMapper.insertMallLinkMajorSchool(newLink);
                    linkId = newLink.getLinkId();
                }else{

                    linkId = linkMajorSchool.getLinkId();
                }

                // 查询录取原则
                MallAcceptRule acceptRule = mallAcceptRuleMapper.selectRuleStr(ruleStr);
                // 查询是否已导入数据
                List<MallSchoolAccept> schoolAcceptList = mallSchoolAcceptMapper.selectBySchoolIdAndLinkId(schoolId, 4L, linkId, acceptYear, subjectType);
                if (schoolAcceptList.isEmpty()){
                    // 生成新专业录取数据
                    MallSchoolAccept mallSchoolAccept = new MallSchoolAccept();
                    mallSchoolAccept.setSchoolId(schoolId);
                    mallSchoolAccept.setBatchId(4L);
                    mallSchoolAccept.setLinkId(linkId);   // 对应专业表id
                    mallSchoolAccept.setAcceptNum(acceptNum);
                    mallSchoolAccept.setAcceptLowest(acceptLowest);  //
                    mallSchoolAccept.setAcceptYear(acceptYear);
                    mallSchoolAccept.setAcceptRuleId(acceptRule.getAcceptRuleId());
                    mallSchoolAccept.setSubjectType(subjectType);
                    mallSchoolAccept.setCreateAt(DateUtils.getTime());
                    mallSchoolAccept.setDelFlag("0");
                    mallSchoolAccept.setIsReference(1);
                    mallSchoolAcceptMapper.insertMallSchoolAccept(mallSchoolAccept);
                    successNum++;

                    // add list 新专业录取数

                    MallSchoolAcceptRecord acceptRecord = new MallSchoolAcceptRecord();
                    acceptRecord.setSchoolId(schoolId);
                    acceptRecord.setBatchId(4L);
                    acceptRecord.setAcceptNum(acceptNum);
                    acceptRecord.setAcceptYear(acceptYear);
                    acceptRecord.setAcceptLowest(acceptLowest);
                    acceptRecord.setLowestScore(acceptLowest);
                    acceptRecord.setAcceptRuleId(acceptRule.getAcceptRuleId());
                    acceptRecord.setSubjectType(subjectType);
                    mallSchoolAcceptRecordMapper.insertMallSchoolAcceptRecord(acceptRecord);


                    // 生成新招生计划
                    MallAcceptPlan mallAcceptPlan = new MallAcceptPlan();
                    mallAcceptPlan.setSchoolId(schoolId);
                    mallAcceptPlan.setBatchId(4L);
                    mallAcceptPlan.setLinkId(linkId);
                    mallAcceptPlan.setPlanYear(acceptYear);
                    mallAcceptPlan.setSubjectType(subjectType.equals("0") ? 0 : 1);
                    mallAcceptPlan.setAcceptNum(acceptNum);
                    mallAcceptPlan.setSchoolPrice(schoolPrice);
                    mallAcceptPlan.setAcceptRuleId(acceptRule.getAcceptRuleId());
                    mallAcceptPlan.setCreateAt(DateUtils.getTime());
                    mallAcceptPlan.setDelFlag("0");
                    mallAcceptPlanMapper.insertMallAcceptPlan(mallAcceptPlan);
                }else{

                    MallSchoolAcceptRecord acceptRecord = new MallSchoolAcceptRecord();
                    acceptRecord.setSchoolId(schoolId);
                    acceptRecord.setBatchId(4L);
                    acceptRecord.setAcceptNum(acceptNum);
                    acceptRecord.setAcceptYear(acceptYear);
                    acceptRecord.setAcceptLowest(acceptLowest);
                    acceptRecord.setLowestScore(acceptLowest);
                    acceptRecord.setAcceptRuleId(acceptRule.getAcceptRuleId());
                    acceptRecord.setSubjectType(subjectType);
                    acceptRecord.setCreateAt(DateUtils.getTime());
                    acceptRecord.setDelFlag("0");
                    acceptRecord.setIsReference(1);
                    mallSchoolAcceptRecordMapper.insertMallSchoolAcceptRecord(acceptRecord);
                }


            }catch (Exception e){
                failureNum++;
                String msg = "<br/>" +"第" + index + "行的录取信息:导入失败;" + zhuankeBO.getSchoolName() + "," + zhuankeBO.getMajorName();
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }


        System.out.println("专业错误数据" + sb.toString());
        if (failureNum > 0)
        {
            failureMsg.insert(0, "导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }

    // 根据 专业院校录取数据 添加 院校录取数据
    public void addSchoolAcceptRecord( List<MallSchoolAcceptRecord> schoolAcceptRecordList,MallSchoolAccept mallSchoolAccept,BigDecimal cultureLowest,BigDecimal majorLowest){
        //学校id 批次id 招生数量 年份 投档最低分 录取最低分  录取原则 学科 创建时间
        //文化最低分 专业最低分
        MallSchoolAcceptRecord newRecord = BeanUtil.toBean(mallSchoolAccept,MallSchoolAcceptRecord.class);

        newRecord.setCreateAt(DateUtils.getTime());
        if(null != newRecord.getAcceptLowest()){
            newRecord.setLowestScore(newRecord.getAcceptLowest());
        }
        if(null != cultureLowest){
            newRecord.setCultureLowest(cultureLowest);
        }
        if(null != majorLowest){
            newRecord.setMajorLowest(majorLowest);
        }
        //根据 学校 批次 年份 文理科  distinct add
        Optional<MallSchoolAcceptRecord> any = schoolAcceptRecordList.stream()
                                    .filter(s ->
                                            s.getSchoolId() == newRecord.getSchoolId()
                                            && s.getBatchId() == newRecord.getBatchId()
                                            && s.getAcceptYear() == newRecord.getAcceptYear()
                                            && s.getSubjectType() == newRecord.getSubjectType())
                                    .findAny();
        //院校录取数据里 已有当前专业院校录取数据的相关数据
        if(any.isPresent()) {
            MallSchoolAcceptRecord oldRecord = any.orElse(null);
            //比较 最低分 存最低分
            BigDecimal new_acceptLowest = newRecord.getAcceptLowest();
            BigDecimal old_acceptLowest = oldRecord.getAcceptLowest();
            if(null != new_acceptLowest && null != old_acceptLowest){
                //存最低分
                if(new_acceptLowest.compareTo(old_acceptLowest) < 0){
                    //更新最低分 录取原则 投档最低分
                    oldRecord.setAcceptLowest(new_acceptLowest);
                    oldRecord.setLowestScore(new_acceptLowest);
                    oldRecord.setAcceptRuleId(newRecord.getAcceptRuleId());
                }

            }
            //比较文化最低分
            BigDecimal new_cultureLowest = newRecord.getCultureLowest();
            BigDecimal old_cultureLowest = oldRecord.getCultureLowest();
            if(null != new_cultureLowest && null != old_cultureLowest){
                //更新最低 文化最低分
                if(new_cultureLowest.compareTo(old_cultureLowest) < 0){
                    oldRecord.setCultureLowest(new_cultureLowest);
                }
            }
            //比较专业最低分
            BigDecimal new_majorLowest = newRecord.getMajorLowest();
            BigDecimal old_majorLowest = oldRecord.getMajorLowest();
            if(null != new_majorLowest && null != old_majorLowest){
                //更新最低 专业最低分
                if(new_majorLowest.compareTo(old_majorLowest) < 0 ){
                    oldRecord.setMajorLowest(new_majorLowest);
                }
            }
            //累计招生数量
            if(null != newRecord.getAcceptNum()) {
                oldRecord.setAcceptNum(oldRecord.getAcceptNum() + newRecord.getAcceptNum());
            }
        }else {
            //院校录取数据里 没有当前专业院校录取数据的相关数据
            //直接add
            //累计招生数量
            newRecord.setIsReference(1);
            schoolAcceptRecordList.add(newRecord);

        }
    }



    // 处理专业字符串
    public String getMajorName(String majorNameStr){
        // 如果带括号取括号前面的数据
            int i = 0 ;
            int j = 0 ;
            int length = majorNameStr.length();

            if (majorNameStr.contains("（") && majorNameStr.contains("、") ){
                for ( i = 0; i < length; i++) {
                    if (majorNameStr.charAt(i) == '（'){
                        break;
                    }
                }
                String substring = majorNameStr.substring(0, i);
                return substring;
            }else{
                return majorNameStr;
            }
    }

    /**
     * 去除空格回车
     * @param str
     * @return
     */
    public String replaceOther(String str){

        return str.replaceAll("\\s*|\r|\n|\t", "");
    }

    /**
     *
     * @param list
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importMajorClassData(List<MajorClassBO> list) {
        String message = "";
        // 查询已有分类
        //List<MallMajorClass> oldList = mallMajorClassMapper.selectAllMajorList();
        for (MajorClassBO majorClassBO : list) {
            if (StringUtils.isBlank(majorClassBO.getParentMajorName()) || StringUtils.isBlank(majorClassBO.getMajorName())){
                  message = "数据不全, 行数:" + (list.indexOf(majorClassBO) + 1);
                  return message;
            }
            // 去除空格
            String parentMajorName = majorClassBO.getParentMajorName().replaceAll("\\s*","");
            String majorName = majorClassBO.getMajorName().replaceAll("\\s*","");

            // 一级分类是否存在
            MallMajorClass mallMajorClass = mallMajorClassMapper.selectMajorClassByName(parentMajorName, 0L);

            Long parentId = -1L;
            if (ObjectUtils.isEmpty(mallMajorClass)){
                // 一级分类不存在则新建
                MallMajorClass newOne = new MallMajorClass();
                newOne.setParentId(0L);
                newOne.setLevel(1);
                newOne.setMajorName(parentMajorName);
                newOne.setIsShow("0");
                newOne.setStatus("0");
                newOne.setCreateAt(DateUtils.getTime());
                int i = mallMajorClassMapper.insertMallMajorClass(newOne);
                parentId = newOne.getMajorId();
            }else{
                parentId = mallMajorClass.getMajorId();
            }

            // 查询二级分类是否存在
            MallMajorClass twoClass = mallMajorClassMapper.selectMajorClassByName(majorName, parentId);

            if (ObjectUtils.isEmpty(twoClass)){
                // 二级不存在新增
                MallMajorClass newTwo = new MallMajorClass();
                newTwo.setMajorName(majorName);
                newTwo.setParentId(parentId);
                newTwo.setLevel(2);
                newTwo.setIsShow("0");
                newTwo.setStatus("0");
                newTwo.setCreateAt(DateUtils.getTime());
                mallMajorClassMapper.insertMallMajorClass(newTwo);
            }
        }
        message = "success";
        return message;
    }

    /**
     * 导入学校专业关联
     * @param list
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importSchoolMajorLinkData(List<SchoolMajorLinkBO> list) {
        // 学校关联专业旧数据
        List<MallLinkMajorSchool> oldList = mallLinkMajorSchoolMapper.selectAllList();
        // 专业分类
        List<MallMajorClass> classList = mallMajorClassMapper.selectAllMajorList();
        // 批次列表
        List<BatchVO> batchList = mallBatchMapper.selectAllBatchList();

        List<MallLinkMajorSchool> updateLinkList = new ArrayList<>();
        List<MallLinkMajorSchool> insertLinkList = new ArrayList<>();

        List<MallSchool> schoolList = mallSchoolMapper.selectMallSchoolList(null);
        // 学校批次关联
        List<MallSchoolBatchLink> insertSchoolBatchList = new ArrayList<>();

        String message = "";
        String ids = "";
        for (SchoolMajorLinkBO bo : list) {
            if (StringUtils.isBlank(bo.getSchoolName())
                    || StringUtils.isBlank(bo.getMajorName())
                    || StringUtils.isBlank(bo.getMajorClass())){
                message = "数据不全,对应行数:" + (list.indexOf(bo) + 1);
                return message;
            }
//            bo.setBatchName("本科");
            String schoolName = bo.getSchoolName().replaceAll("\\s*","");
            String batchName = bo.getBatchName().replaceAll("\\s*","");
            String majorName = bo.getMajorName().replaceAll("\\s*","");
            String parentMajorClass = bo.getParentMajorClass().replaceAll("\\s*","");
            String majorClass = bo.getMajorClass().replaceAll("\\s*","").replaceAll("，", ",").replaceAll("、", ",");

            // 查询学校
            List<MallSchool> mallSchoolList = schoolList.stream().filter(item -> replaceOther(item.getSchoolName()).equals(schoolName)).collect(Collectors.toList());
            if (ObjectUtils.isEmpty(mallSchoolList)){
//                return "学校信息不存在【"+schoolName+"】,行数: " + (list.indexOf(bo) + 1);
                continue;
            }
            // 对应批次
            List<BatchVO> batchCollect = batchList.stream().filter(item -> item.getBatchName().equals(batchName)).collect(Collectors.toList());
            if (batchCollect.isEmpty()){
                return "批次错误, 行数: " + (list.indexOf(bo) + 1);
            }

            // 二级分类

            String[] split = majorClass.split(",");
            StringBuilder twoClassIds = new StringBuilder();

//            for (String s : split) {
//                List<MallMajorClass> collect2 = classList.stream().filter(item -> item.getMajorName().equals(s) && !item.getParentId().equals(0L)).collect(Collectors.toList());
//                if (collect2.isEmpty()){
//                    message = "未查询到二级专业分类:" + s + ",对应行数: " + (list.indexOf(bo) + 1);
//                    return message;
//                }
//                twoClassIds.append(collect2.get(0).getMajorId()).append(",");
//            }
            if (oldList.stream().noneMatch(item -> item.getSchoolName().equals(schoolName) && item.getMajorName().equals(majorName))) {
                // 新增专业
                MallLinkMajorSchool newLink = new MallLinkMajorSchool();
                // 查询学校id
                newLink.setSchoolId(mallSchoolList.get(0).getSchoolId());
                newLink.setSchoolName(schoolName);
                newLink.setMajorName(majorName);
                //newLink.setParentMajorId(collect1.get(0).getMajorId()); // 一级分类
//                newLink.setMajorId(twoClassIds.toString());  //二级分类
                newLink.setType("2");
                newLink.setCreateAt(DateUtils.getTime());
                newLink.setTwoClassName(majorClass);
                mallLinkMajorSchoolMapper.insertMallLinkMajorSchool(newLink);
                oldList.add(newLink);
            }
//            if (ObjectUtils.isNotEmpty(bo.getLinkId())){
//
//                // 已有专业关联
//                List<MallLinkMajorSchool> collect = oldList.stream().filter(item -> item.getLinkId().equals(bo.getLinkId())).collect(Collectors.toList());
//
//                MallLinkMajorSchool oldLink = collect.get(0);
//                oldLink.setMajorName(majorName);  // 专业名称(可能更新)
//                //oldLink.setParentMajorId(collect1.get(0).getMajorId());// 一级分类id
////                oldLink.setMajorId(twoClassIds.toString()); // 二级分类id
//                oldLink.setUpdateAt(DateUtils.getTime());
//                oldLink.setTwoClassName(majorClass);
//                updateLinkList.add(oldLink);
//            }else{
//                // 新增专业
//                MallLinkMajorSchool newLink = new MallLinkMajorSchool();
//                // 查询学校id
//
//                newLink.setSchoolId(mallSchoolList.get(0).getSchoolId());
//                newLink.setSchoolName(schoolName);
//                newLink.setMajorName(majorName);
//                //newLink.setParentMajorId(collect1.get(0).getMajorId()); // 一级分类
////                newLink.setMajorId(twoClassIds.toString());  //二级分类
//                newLink.setType("2");
//                newLink.setCreateAt(DateUtils.getTime());
//                newLink.setTwoClassName(majorClass);
//                insertLinkList.add(newLink);
//            }

            // 学校批次关联
            MallSchoolBatchLink schoolBatchLink = mallSchoolBatchLinkMapper.selectSchoolBatchLinkBySchoolIdAndBatchId(mallSchoolList.get(0).getSchoolId(), batchCollect.get(0).getBatchId());
            if (ObjectUtils.isEmpty(schoolBatchLink)){
                List<MallSchoolBatchLink> collect = insertSchoolBatchList.stream().filter(item -> item.getSchoolId().equals(mallSchoolList.get(0).getSchoolId()) && item.getBatchId().equals(batchCollect.get(0).getBatchId())).collect(Collectors.toList());
                if (collect.isEmpty()){
                    // 未加入集合,新增
                    MallSchoolBatchLink newSchoolBatchLink = new MallSchoolBatchLink();
                    newSchoolBatchLink.setSchoolId(mallSchoolList.get(0).getSchoolId());
                    newSchoolBatchLink.setBatchId(batchCollect.get(0).getBatchId());
                    newSchoolBatchLink.setBatchName(batchCollect.get(0).getBatchName());
                    newSchoolBatchLink.setCreateAt(DateUtils.getTime());
                    newSchoolBatchLink.setDelFlag("0");
                    mallSchoolBatchLinkMapper.insertMallSchoolBatchLink(newSchoolBatchLink);
//                    insertSchoolBatchList.add(newSchoolBatchLink);
                }
            }
        }

        // 批量新增或修改
//        if (!insertSchoolBatchList.isEmpty()){
//            mallSchoolBatchLinkMapper.batchInsert(insertSchoolBatchList);
//        }
//
//        if (!insertLinkList.isEmpty()){
//            mallLinkMajorSchoolMapper.batchInsert(insertLinkList);
//        }
//        if (!updateLinkList.isEmpty()){
//            mallLinkMajorSchoolMapper.batchUpdate(updateLinkList);
//        }

        return "success";

    }
}
