package com.ruoyi.sy.service.impl;

import com.ruoyi.common.core.content.VistorContextHolder;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.ExamType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.dz.domain.DzControl;
import com.ruoyi.dz.service.IDzControlService;
import com.ruoyi.sy.domain.SyMajor;
import com.ruoyi.sy.mapper.SyMajorMapper;
import com.ruoyi.sy.service.ISyMajorService;
import com.ruoyi.util.ConstantUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专业Service业务层处理
 *
 * @author mingxue
 * @date 2022-04-27
 */
@Service
public class SyMajorServiceImpl implements ISyMajorService
{
    @Autowired
    private SyMajorMapper syMajorMapper;

    Map<String,List<SyMajor>> codeCountMap = new HashMap<>();
    /**
     * 查询专业
     *
     * @param id 专业ID
     * @return 专业
     */
    @Override
    public SyMajor selectSyMajorById(Long id)
    {
        return syMajorMapper.selectSyMajorById(id);
    }

    @Override
    public SyMajor selectSyMajorByCode(String code) {
        String examTypeData = ConstantUtil.getExamTypeData(VistorContextHolder.getExamType().title()); // TODO MF
        return syMajorMapper.selectSyMajorByCode(examTypeData, code);
    }

    @Override
    public List<SyMajor> selectSyMajorByLevel(String examType, Integer level) {
        String key = examType+level;
        if (MapUtils.isNotEmpty(codeCountMap)&&codeCountMap.containsKey(key)) {
            return codeCountMap.get(key);
        }
        List<SyMajor> ll = syMajorMapper.selectSyMajorByLevel(examType, level);
        codeCountMap.put(key,ll);
        return ll;
    }

    /**
     * 查询专业列表
     *
     * @param syMajor 专业
     * @return 专业
     */
    @Override
    public List<SyMajor> selectSyMajorList(SyMajor syMajor)
    {
        return syMajorMapper.selectSyMajorList(syMajor);
    }

    @Override
    public List<SyMajor> selectSyMajorAndChildrenByCode(SyMajor syMajor) {
        return syMajorMapper.selectSyMajorAndChildrenByCode(syMajor);
    }

    /**
     * 新增专业
     *
     * @param syMajor 专业
     * @return 结果
     */
    @Override
    public int insertSyMajor(SyMajor syMajor)
    {
        return syMajorMapper.insertSyMajor(syMajor);
    }

    /**
     * 修改专业
     *
     * @param syMajor 专业
     * @return 结果
     */
    @Override
    public int updateSyMajor(SyMajor syMajor)
    {
        return syMajorMapper.updateSyMajor(syMajor);
    }

    /**
     * 批量删除专业
     *
     * @param ids 需要删除的专业ID
     * @return 结果
     */
    @Override
    public int deleteSyMajorByIds(Long[] ids)
    {
        return syMajorMapper.deleteSyMajorByIds(ids);
    }

    /**
     * 删除专业信息
     *
     * @param id 专业ID
     * @return 结果
     */
    @Override
    public int deleteSyMajorById(Long id)
    {
        return syMajorMapper.deleteSyMajorById(id);
    }

    @Override
    public List<SyMajor> selectPlanMajorList(Integer planYear, SysUser user, Long universityId) {
        return syMajorMapper.selectSyMajorByPlan(planYear, user.getExamType().title(), universityId, user.getLocation());
    }

    @Override
    public List<SyMajor> selectMajorsByPlan(String location) {
        return syMajorMapper.selectMajorsByPlan(location);
    }
}
