package com.ruoyi.mall.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.Bo.CheckingOutParamBo;
import com.ruoyi.mall.domain.Bo.SchoolMajorLinkBO;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.domain.Vo.MajorItemVo;
import com.ruoyi.mall.mapper.*;
import com.ruoyi.mall.service.IMallLinkMajorSchoolService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 专业与学校关联Service业务层处理
 *
 * @author Lsm
 * @date 2023-10-23
 */
@Service
public class MallLinkMajorSchoolServiceImpl implements IMallLinkMajorSchoolService
{
    @Resource
    private MallLinkMajorSchoolMapper mallLinkMajorSchoolMapper;

    @Resource
    private MallSchoolMapper mallSchoolMapper;

    @Resource
    private MallMajorClassMapper mallMajorClassMapper;

    @Resource
    private MallSchoolAcceptMapper schoolAcceptMapper;

    @Resource
    private MallAcceptPlanMapper mallAcceptPlanMapper;

    @Resource
    private MallSchoolBatchLinkMapper schoolBatchLinkMapper;

    @Resource
    private MallSchoolStatusMapper schoolStatusMapper;

    /**
     * 查询专业与学校关联
     *
     * @param linkId 专业与学校关联主键
     * @return 专业与学校关联
     */
    @Override
    public MallLinkMajorSchool selectMallLinkMajorSchoolByLinkId(Long linkId)
    {
        return mallLinkMajorSchoolMapper.selectMallLinkMajorSchoolByLinkId(linkId);
    }

    /**
     * 查询专业与学校关联列表
     *
     * @param mallLinkMajorSchool 专业与学校关联
     * @return 专业与学校关联
     */
    @Override
    public List<MallLinkMajorSchool> selectMallLinkMajorSchoolList(MallLinkMajorSchool mallLinkMajorSchool)
    {
        return mallLinkMajorSchoolMapper.selectMallLinkMajorSchoolList(mallLinkMajorSchool);
    }

    /**
     * 新增专业与学校关联
     *
     * @param mallLinkMajorSchool 专业与学校关联
     * @return 结果
     */
    @Override
    public AjaxResult insertMallLinkMajorSchool(MallLinkMajorSchool mallLinkMajorSchool)
    {

        if(ObjectUtils.isEmpty(mallLinkMajorSchool.getMajorId())){
            return AjaxResult.error("未选择专业");
        }
        if(ObjectUtils.isEmpty(mallLinkMajorSchool.getSchoolId()) && StringUtils.isNotBlank(mallLinkMajorSchool.getSchoolName())){
            // 学校名查询学校id
            MallSchool mallSchool = mallSchoolMapper.selectMallSchoolBySchoolName(mallLinkMajorSchool.getSchoolName());
            mallLinkMajorSchool.setSchoolId(mallSchool.getSchoolId());
        }

        if (ObjectUtils.isNotEmpty(mallLinkMajorSchool.getSchoolId()) && ObjectUtils.isNotEmpty(mallLinkMajorSchool.getMajorName())){
            MallLinkMajorSchool majorSchool = mallLinkMajorSchoolMapper.selectMajorSchoolBySchoolAndMajorId(mallLinkMajorSchool.getSchoolId(), mallLinkMajorSchool.getMajorName());
            if (ObjectUtils.isNotEmpty(majorSchool)){
                return AjaxResult.error("该专业已经录入");
            }
        }
        // 关联专业分类
        // 专业分类
        List<MallMajorClass> classList = mallMajorClassMapper.selectAllMajorList();
        String majorId = mallLinkMajorSchool.getMajorId();
        String[] split = majorId.split(",");
        StringBuilder twoClassIds = new StringBuilder();
        for (String s : split) {
            List<MallMajorClass> collect2 = classList.stream().filter(item -> item.getMajorId().equals(Long.parseLong(s)) ).collect(Collectors.toList());

            twoClassIds.append(collect2.get(0).getMajorId()).append(",");
        }
        mallLinkMajorSchool.setTwoClassName(twoClassIds.toString());

        mallLinkMajorSchool.setCreateAt(DateUtils.getTime());
        mallLinkMajorSchool.setDelFlag("0");
        return AjaxResult.toAjax(mallLinkMajorSchoolMapper.insertMallLinkMajorSchool(mallLinkMajorSchool));
    }

    /**
     * 修改专业与学校关联
     *
     * @param mallLinkMajorSchool 专业与学校关联
     * @return 结果
     */
    @Override
    public AjaxResult updateMallLinkMajorSchool(MallLinkMajorSchool mallLinkMajorSchool)
    {

        return AjaxResult.toAjax(mallLinkMajorSchoolMapper.updateMallLinkMajorSchool(mallLinkMajorSchool));
    }

    /**
     * 批量删除专业与学校关联
     *
     * @param linkIds 需要删除的专业与学校关联主键
     * @return 结果
     */
    @Override
    public int deleteMallLinkMajorSchoolByLinkIds(Long[] linkIds)
    {
        return mallLinkMajorSchoolMapper.deleteMallLinkMajorSchoolByLinkIds(linkIds);
    }

    /**
     * 删除专业与学校关联信息
     *
     * @param linkId 专业与学校关联主键
     * @return 结果
     */
    @Override
    public int deleteMallLinkMajorSchoolByLinkId(Long linkId)
    {
        return mallLinkMajorSchoolMapper.deleteMallLinkMajorSchoolByLinkId(linkId);
    }

    /** 志愿添加 在筛选学校基础上 筛选专业*/
    @Override
    public List<MajorItemVo> astrictMajorItemList(CheckingOutParamBo bo) {
        //if(null != bo && null != bo.getMajorIds()){
        //    List<MallMajorClass> list = mallMajorClassMapper.selectAllMajorList();
        //    List<String> names = new ArrayList<>();
        //    // 拼接专业名称
        //    for (Long majorId : bo.getMajorIds()) {
        //        List<MallMajorClass> collect = list.stream().filter(item -> item.getMajorId().equals(majorId)).collect(Collectors.toList());
        //        if (!collect.isEmpty()){
        //            names.add(collect.get(0).getMajorName());
        //        }
        //    }
        //    String[] objects =  names.toArray(new String[bo.getMajorIds().length]);
        //    bo.setMajorIds(null);
        //    bo.setMajorNames(objects);
        //}

        assert bo != null;
        if (bo.getRateFlag() != null && bo.getRateFlag() == 1){
            // 更新完录取率数据后联表查询
            return mallLinkMajorSchoolMapper.a4MajorItemList2(bo);
        }else{

            return mallLinkMajorSchoolMapper.a4MajorItemList(bo);
        }

    }

    @Override
    public List<MallLinkMajorSchool> selectMallLinkMajorSchoolRepetitionList() {
        return mallLinkMajorSchoolMapper.selectMallLinkMajorSchoolRepetitionList();
    }

    /**
     * 数据关联学校专业
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int linkAccept() {
        // 学校专业
        List<MallLinkMajorSchool> linkList = mallLinkMajorSchoolMapper.selectAllList();
        // 专业二录取数据
        List<MallSchoolAccept> acceptList = mallLinkMajorSchoolMapper.selectAllAccept();
        List<MallSchoolAccept> list = new ArrayList<>();
        for (MallLinkMajorSchool linkMajorSchool : linkList) {
            for (MallSchoolAccept mallSchoolAccept : acceptList) {
                if (mallSchoolAccept.getMajorId().equals(Long.parseLong(linkMajorSchool.getMajorId())) && mallSchoolAccept.getSchoolId().equals(linkMajorSchool.getSchoolId())){
                    mallSchoolAccept.setLinkId(linkMajorSchool.getLinkId());
                    mallSchoolAccept.setUpdateAt(DateUtils.getTime());
                    list.add(mallSchoolAccept);
                }
            }
        }

        // 批量修改
        return schoolAcceptMapper.batchUpdate(list);

    }

    /**
     * 招生计划
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int linkAcceptPlan() {
        // 学校专业
        List<MallLinkMajorSchool> linkList = mallLinkMajorSchoolMapper.selectAllList();
        // 招生计划
        List<MallAcceptPlan> planList = mallAcceptPlanMapper.selectAllPlan();
        List<MallAcceptPlan> list = new ArrayList<>();
        for (MallLinkMajorSchool linkMajorSchool : linkList) {
            for (MallAcceptPlan acceptPlan : planList) {
                if (acceptPlan.getMajorId().equals(Long.parseLong(linkMajorSchool.getMajorId())) && acceptPlan.getSchoolId().equals(linkMajorSchool.getSchoolId())){
                    acceptPlan.setLinkId(linkMajorSchool.getLinkId());
                    acceptPlan.setUpdateAt(DateUtils.getTime());
                    list.add(acceptPlan);
                }
            }
        }
        // 批量插入
        return  mallAcceptPlanMapper.batchUpdate(list);
    }

    /**
     * 导出学校数据
     * @return
     */
    @Override
    public List<SchoolMajorLinkBO> getAllSchoolAndMajorList() {
        return mallLinkMajorSchoolMapper.getAllSchoolAndMajorList();
    }

    /**
     * 硕士点数
     */
    @Override
    public void getSchool() {
        List<MallSchool> mallSchools = mallSchoolMapper.selectPointSchoolList();
        for (MallSchool mallSchool : mallSchools) {
            if (null != mallSchool.getMastersDegree()){
                if (1 <= mallSchool.getMastersDegree() && 10 > mallSchool.getMastersDegree()){
                    mallSchool.setMastersDegreeType("1-10");
                }else if (11 <= mallSchool.getMastersDegree() && 20 > mallSchool.getMastersDegree()){
                    mallSchool.setMastersDegreeType("11-20");
                }else if (21 <= mallSchool.getMastersDegree() && 30 > mallSchool.getMastersDegree()){
                    mallSchool.setMastersDegreeType("21-30");
                }else if (31 <= mallSchool.getMastersDegree() && 41 > mallSchool.getMastersDegree()){
                    mallSchool.setMastersDegreeType("31-40");
                }else if (41 <= mallSchool.getMastersDegree() && 51 > mallSchool.getMastersDegree()){
                    mallSchool.setMastersDegreeType("41-50");
                }else{
                    mallSchool.setMastersDegreeType("50以上");
                }
            }
            // 博士点数
            if (null != mallSchool.getDoctorate()){
                if (1 <= mallSchool.getDoctorate() && 10 > mallSchool.getDoctorate()){
                    mallSchool.setDoctorateType("1-10");
                }else if (11 <= mallSchool.getDoctorate() && 20 > mallSchool.getDoctorate()){
                    mallSchool.setDoctorateType("11-20");
                }else if (21 <= mallSchool.getDoctorate() && 30 > mallSchool.getDoctorate()){
                    mallSchool.setDoctorateType("21-30");
                }else if (31 <= mallSchool.getDoctorate() && 41 > mallSchool.getDoctorate()){
                    mallSchool.setDoctorateType("31-40");
                }else if (41 <= mallSchool.getDoctorate() && 51 > mallSchool.getDoctorate()){
                    mallSchool.setDoctorateType("41-50");
                }else{
                    mallSchool.setDoctorateType("50以上");
                }
            }

            mallSchoolMapper.updateMallSchool(mallSchool);
        }
    }

    /**
     * 新招处理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void newSchool() {
        String newYear = mallAcceptPlanMapper.selectMaxYear();

        // 查询所有学校
        List<MallSchool> mallSchools = mallSchoolMapper.selectPointSchoolList();
        for (MallSchool mallSchool : mallSchools) {
            // 本科批
            // 查询学校批次
            List<MallSchoolBatchLink> batchLink = schoolBatchLinkMapper.selectSchoolBatch(mallSchool.getSchoolId());
            if (!batchLink.isEmpty()){
                for (MallSchoolBatchLink schoolBatchLink : batchLink) {
                    Long schoolBatchId = schoolBatchLink.getBatchId();
                    // 文科

                    MallSchoolStatus status1 = new MallSchoolStatus();
                    status1.setSchoolId(mallSchool.getSchoolId());
                    status1.setBatchId(schoolBatchId);
                    status1.setSubjectType("0");
                    status1.setCreateTime(DateUtils.getNowDate());

                    MallAcceptPlan newPlan = new MallAcceptPlan();
                    newPlan.setSchoolId(mallSchool.getSchoolId());
                    newPlan.setPlanYear(newYear);
                    newPlan.setBatchId(schoolBatchId);
                    newPlan.setSubjectType(0);
                    // 查询今年是否有招生计划
                    List<MallAcceptPlan> planList = mallAcceptPlanMapper.selectAcceptPlanBySchoolId(newPlan);
                    if (planList.isEmpty()){
                        // 如果没有计划,标记为未公布
                        status1.setNoPlan(1);
                    }else{
                        // 查询去年是否有招生计划
                        MallAcceptPlan afterPlan = new MallAcceptPlan();
                        afterPlan.setSchoolId(mallSchool.getSchoolId());
                        afterPlan.setPlanYear(String.valueOf(Integer.parseInt(newYear) - 1));
                        afterPlan.setBatchId(schoolBatchId);
                        afterPlan.setSubjectType(0);

                        List<MallAcceptPlan> afterPlanList = mallAcceptPlanMapper.selectAcceptPlanBySchoolId(afterPlan);
                        if (afterPlanList.isEmpty()){
                            // 去年未招生, 标记为新招学校
                            status1.setIsNewAccept(1);
                        }else {
                            // 非新招学校, 判断今年是否新招专业
                            for (MallAcceptPlan mallAcceptPlan : planList) {
                                Long schoolId = mallAcceptPlan.getSchoolId();
                                Long batchId = mallAcceptPlan.getBatchId();
                                Long linkId = mallAcceptPlan.getLinkId();
                                Long acceptRuleId = mallAcceptPlan.getAcceptRuleId();
                                Integer subjectType = mallAcceptPlan.getSubjectType();

                                MallAcceptPlan plan = new MallAcceptPlan();
                                plan.setSchoolId(schoolId);
                                plan.setBatchId(batchId);
                                plan.setLinkId(linkId);
                                plan.setSubjectType(subjectType);
                                // 查询该专业对应的去年数据
                                List<MallAcceptPlan> afterSameLinkPlans = mallAcceptPlanMapper.selectAcceptPlanBySchoolId(plan);
                                if (afterSameLinkPlans.isEmpty()){
                                    // 去年同学校,同批次,同学科,专业未招生,标记为新招专业
                                    MallLinkMajorSchool linkMajorSchool = mallLinkMajorSchoolMapper.selectMallLinkMajorSchoolByLinkId(linkId);
                                    linkMajorSchool.setIsNewAccept(1);
                                    mallLinkMajorSchoolMapper.updateMallLinkMajorSchool(linkMajorSchool);

                                    // 学校标记为有新招
                                    status1.setHasNewLink(1);
                                }else{
                                    // 非新招专业,判断招生公式是否变化
                                    Long afterRule = afterSameLinkPlans.get(0).getAcceptRuleId();
                                    if (!afterRule.equals(acceptRuleId)){
                                        status1.setIsChange(1);
                                    }
                                }

                            }
                        }
                    }

                    schoolStatusMapper.insertMallSchoolStatus(status1);
                    // 理科
                    MallSchoolStatus status2 = new MallSchoolStatus();
                    status2.setSchoolId(mallSchool.getSchoolId());
                    status2.setBatchId(schoolBatchId);
                    status2.setSubjectType("1");
                    status2.setCreateTime(DateUtils.getNowDate());

                    MallAcceptPlan newPlan2 = new MallAcceptPlan();
                    newPlan2.setSchoolId(mallSchool.getSchoolId());
                    newPlan2.setPlanYear(newYear);
                    newPlan2.setBatchId(schoolBatchId);
                    newPlan2.setSubjectType(1);
                    // 查询今年是否有招生计划
                    List<MallAcceptPlan> planList2 = mallAcceptPlanMapper.selectAcceptPlanBySchoolId(newPlan);
                    if (planList2.isEmpty()){
                        // 如果没有计划,标记为未公布
                        status2.setNoPlan(1);
                    }else{
                        // 查询去年是否有招生计划
                        MallAcceptPlan afterPlan2 = new MallAcceptPlan();
                        afterPlan2.setSchoolId(mallSchool.getSchoolId());
                        afterPlan2.setPlanYear(String.valueOf(Integer.parseInt(newYear) - 1));
                        afterPlan2.setBatchId(schoolBatchId);
                        afterPlan2.setSubjectType(1);

                        List<MallAcceptPlan> afterPlanList2 = mallAcceptPlanMapper.selectAcceptPlanBySchoolId(afterPlan2);
                        if (afterPlanList2.isEmpty()){
                            // 去年未招生, 标记为新招学校
                            status2.setIsNewAccept(1);
                        }else {
                            // 非新招学校, 判断今年是否新招专业
                            for (MallAcceptPlan mallAcceptPlan : planList2) {
                                Long schoolId = mallAcceptPlan.getSchoolId();
                                Long batchId = mallAcceptPlan.getBatchId();
                                Long linkId = mallAcceptPlan.getLinkId();
                                Long acceptRuleId = mallAcceptPlan.getAcceptRuleId();
                                Integer subjectType = mallAcceptPlan.getSubjectType();

                                MallAcceptPlan plan = new MallAcceptPlan();
                                plan.setSchoolId(schoolId);
                                plan.setBatchId(batchId);
                                plan.setLinkId(linkId);
                                plan.setSubjectType(subjectType);
                                // 查询该专业对应的去年数据
                                List<MallAcceptPlan> afterSameLinkPlans = mallAcceptPlanMapper.selectAcceptPlanBySchoolId(plan);
                                if (afterSameLinkPlans.isEmpty()){
                                    // 去年同学校,同批次,同学科,专业未招生,标记为新招专业
                                    MallLinkMajorSchool linkMajorSchool = mallLinkMajorSchoolMapper.selectMallLinkMajorSchoolByLinkId(linkId);
                                    linkMajorSchool.setIsNewAccept(1);
                                    mallLinkMajorSchoolMapper.updateMallLinkMajorSchool(linkMajorSchool);

                                    // 学校标记为有新招
                                    status2.setHasNewLink(1);
                                }else{
                                    // 非新招专业,判断招生公式是否变化
                                    Long afterRule = afterSameLinkPlans.get(0).getAcceptRuleId();
                                    if (!afterRule.equals(acceptRuleId)){
                                        status2.setIsChange(1);
                                    }
                                }
                            }
                        }
                    }
                    schoolStatusMapper.insertMallSchoolStatus(status2);
                }
            }
        }
    }
}
