package com.ruoyi.syzy.service.impl;

import com.google.common.collect.Maps;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.CommonUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sy.domain.SyMajor;
import com.ruoyi.sy.service.ISyMajorService;
import com.ruoyi.syzy.domain.*;
import com.ruoyi.syzy.dto.UniversityDetailDTO;
import com.ruoyi.syzy.mapper.*;
import com.ruoyi.syzy.service.IBBusiWishUniversitiesEnrollBrochureService;
import com.ruoyi.syzy.service.IBBusiWishUniversitiesService;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.util.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 院校库Service业务层处理
 *
 * @author mingxue
 * @date 2021-07-29
 */
@Service
public class BBusiWishUniversitiesServiceImpl implements IBBusiWishUniversitiesService {
    @Autowired
    private BBusiWishUniversitiesMapper bBusiWishUniversitiesMapper;
    @Autowired
    private ISyMajorService syMajorService;
    @Autowired
    private BBusiWishUniversitiesEmploymentreportMapper universitiesEmploymentreportMapper;
    @Autowired
    private BBusiWishUniversitiesDepartmentMapper universitiesDepartmentMapper;
    @Autowired
    private BBusiWishUniversitiesProfessionMapper universitiesProfessionMapper;
    @Autowired
    private BBusiWishUniversitiesSubjectMapper universitiesSubjectMapper;
    @Autowired
    private IBBusiWishUniversitiesEnrollBrochureService universitiesEnrollBrochureService;
    @Autowired
    private ISysDictTypeService dictTypeService;



    @Override
    public List<BBusiWishUniversities> listMyByPage(String customerCode) {
        return bBusiWishUniversitiesMapper.listMyByPage(customerCode);
    }

    @Override
    public BBusiWishUniversities selectBBusiWishUniversitiesById(Long universityId) {
        return bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesById(universityId);
    }

    @Override
    public BBusiWishUniversities selectBBusiWishUniversitiesByCode(String code) {
        BBusiWishUniversities result  = bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesByCode(code);
        if(result != null){
            // 如果tier有值，则从字典dict_type=tier_type中取到值并将label赋值给tierName
            if(result.getTier() != null){
                String tierName = dictTypeService.getDictDataByType("tier_type", String.valueOf(result.getTier()));
                if(tierName != null && !tierName.isEmpty()){
                    result.setTierName(tierName);
                }
            }
        }
        return result;
    }

    /**
     * 查询院校库列表
     *
     * @param bBusiWishUniversities 院校库
     * @return 院校库
     */
    @Override
    public List<BBusiWishUniversities> selectBBusiWishUniversitiesList(BBusiWishUniversities bBusiWishUniversities) {
        return bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesList(bBusiWishUniversities);
    }

    @Override
    public List<BBusiWishUniversitiesProfession> selectBBusiWishUniversitiesProfessionByCode(String collegeCode) {
        return universitiesProfessionMapper.selectBBusiWishUniversitiesProfessionByCode(collegeCode);
    }

    @Override
    public List<BBusiWishUniversities> selectBBusiWishUniversitiesListSimpleByMap(Map cond) {
        return bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesListSimpleByMap(cond);
    }

    @Override
    public List<BBusiWishUniversities> selectMajorWishUniversitiesListSimpleByMap(Map cond) {
        return bBusiWishUniversitiesMapper.selectMajorWishUniversitiesListSimpleByMap(cond);
    }

    @Override
    public List<BBusiWishUniversities> selectMajorWishUniversitiesListSimpleByMap2(Map cond) {
        return bBusiWishUniversitiesMapper.selectMajorWishUniversitiesListSimpleByMap2(cond);
    }



    @Override
    public List<BBusiWishUniversities> selectBBusiWishUniversitiesLocations(String entranceType) {
        return bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesLocations(entranceType);
    }

    @Override
    public List<BBusiWishUniversities> selectBBusiWishUniversitiesLocations2(String entranceType) {
        return bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesLocations2(entranceType);
    }
    @Override
    public List<BBusiWishUniversities> selectBBusiWishUniversitiesLocations3(String location) {
        return bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesLocations3(location);
    }

    @Override
    public List<BBusiWishUniversities> selectBBusiWishUniversitiesTypes(String entranceType) {
        return bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesTypes(entranceType);
    }

    @Override
    public List<BBusiWishUniversities> selectBBusiWishUniversitiesNatureTypes(String entranceType) {
        return bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesNatureTypes(entranceType);
    }

    @Override
    public List<BBusiWishUniversities> selectBBusiWishUniversitiesListSimple(BBusiWishUniversities bBusiWishUniversities) {
        return bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesListSimple(bBusiWishUniversities);
    }

    @Override
    public List<BBusiWishUniversities> selectBBusiWishUniversitiesFeatures(String entranceType) {
        return bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesFeatures(entranceType);
    }

    @Override
    public List<BBusiWishUniversities> selectBBusiWishUniversitiesBxTypes(String entranceType) {
        return bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesBxTypes(entranceType);
    }

    @Override
    public List<BBusiWishUniversities> selectBBusiWishUniversitiesLevels(String entranceType) {
        return bBusiWishUniversitiesMapper.selectBBusiWishUniversitiesLevels(entranceType);
    }

    /**
     * 修改院校库
     *
     * @param bBusiWishUniversities 院校库
     * @return 结果
     */
    @Override
    public int updateBBusiWishUniversities(BBusiWishUniversities bBusiWishUniversities) {
        return bBusiWishUniversitiesMapper.updateBBusiWishUniversities(bBusiWishUniversities);
    }

    @Override
    public int updateCollect(Long id, Integer collect) {
        return bBusiWishUniversitiesMapper.updateCollect(id, collect);
    }

    @Override
    public UniversityDetailDTO detail(String code, Integer year) {
        long startTime = System.currentTimeMillis();
        long currentTime = startTime;
        UniversityDetailDTO data = new UniversityDetailDTO();
        //基础信息
        data.setBaseInfo(selectBBusiWishUniversitiesByCode(code));
//        System.out.println("Step 1  - 获取基础信息完成, 耗时: {} ms" +( System.currentTimeMillis() - currentTime));

        //院校院系 b_busi_wish_universities_department；院校院系专业 b_busi_wish_universities_department_major
//        List<BBusiWishUniversitiesDepartment>  departmentList=universitiesDepartmentMapper.selectBBusiWishUniversitiesDepartmentList(new BBusiWishUniversitiesDepartment().setCollegeCode(code));
//        data.setDepartments(departmentList);

        //院校特色专业 b_busi_wish_universities_profession
        SysUser u = SecurityUtils.getLoginUser().getUser();
        Map cond = Maps.newHashMap();
        String examTypeData = ConstantUtil.getExamTypeData(u.getExamType().title());
        cond.put("year", year);
        cond.put("collegeCode", code);
        cond.put("examineeType", u.getExamType().title());
        cond.put("examType", examTypeData);
        cond.put("examMajor", u.getExamMajor());

        /****/
        List<BBusiWishUniversitiesProfession> professionList = universitiesProfessionMapper.selectEnrollPlanProfessionByCode(cond);
//        System.out.println("Step 2  - 获取专业列表完成, 耗时: {} ms" +( System.currentTimeMillis() - currentTime));

        //设置数量
        Map<String,Integer> codeCountMap = syMajorService.selectSyMajorByLevel(examTypeData, 3).stream().collect(Collectors.toMap(t -> t.getCode(), SyMajor::getChildCount));
        professionList.stream().forEach(ll->{
            if(codeCountMap.containsKey(ll.getCode())){
                ll.setCount(codeCountMap.get(ll.getCode()));
            }
        });
        data.setProfessions(professionList);
//        System.out.println("Step 3  - 获取专业数量完成, 耗时: {} ms" +( System.currentTimeMillis() - currentTime));


        //院校重点&双一流 b_busi_wish_universities_subject
//        Map<String,List<BBusiWishUniversitiesSubject>> subjectMap = universitiesSubjectMapper.selectBBusiWishUniversitiesSubjectList(new BBusiWishUniversitiesSubject().setCollegeCode(code)).stream().collect(Collectors.groupingBy(entity -> entity.getDataType()));
//        data.setSubjects(subjectMap);

        //学科评估
//        List<BBusiWishUniversitiesSubjectevaluate> subjectevaluateList =universitiesSubjectevaluateMapper.selectBBusiWishUniversitiesSubjectevaluateList(new BBusiWishUniversitiesSubjectevaluate().setCollegeCode(code));
//        data.setSubjectevaluates(subjectevaluateList);

        //就业报告
//        List<BBusiWishUniversitiesEmploymentreport> employmentreportList =universitiesEmploymentreportMapper.selectBBusiWishUniversitiesEmploymentreportList(new BBusiWishUniversitiesEmploymentreport().setCollegeCode(code));
//        data.setEmploymentreports(employmentreportList);

        //最新动态、招生简章
        BBusiWishUniversitiesEnrollBrochure query = new BBusiWishUniversitiesEnrollBrochure();
        query.setCollegeCode(code);
        List<BBusiWishUniversitiesEnrollBrochure> list = universitiesEnrollBrochureService.selectBBusiWishUniversitiesEnrollBrochureList(query);
        data.setEnrollBrochures(list);
//        System.out.println("Step 4  - 获取动态、招生简章完成, 耗时: {} ms" +( System.currentTimeMillis() - currentTime));

        return data;
    }

    @Override
    public List<String> getLocations(String entranceType){
        return CommonUtils.sortByChina(selectBBusiWishUniversitiesLocations(entranceType).stream().map(BBusiWishUniversities::getLocation).collect(Collectors.toList()));
    }

    @Override
    public List<String> getTypes(String entranceType){
        Set<String> types = new HashSet<>();
        selectBBusiWishUniversitiesTypes(entranceType).forEach(ll->{
            types.addAll(Arrays.asList(com.ruoyi.common.utils.StringUtils.split(ll.getType(),",")));
        });

        return CommonUtils.sortByChina(new ArrayList<String>(types));
    }

    /**
     * natureType
     * @return
     */
    @Override
    public List<String> getNatureTypeCN(String entranceType){
        return CommonUtils.sortByChina(selectBBusiWishUniversitiesNatureTypes(entranceType).stream().map(BBusiWishUniversities::getNatureTypeCN).collect(Collectors.toList()));
    }

    @Override
    public List<String> getFeatures(String entranceType){
        Set<String> features = new HashSet<>();
        selectBBusiWishUniversitiesFeatures(entranceType).forEach(ll->{
            features.addAll(Arrays.asList(com.ruoyi.common.utils.StringUtils.split(ll.getFeatures(), ",")));
        });

        return CommonUtils.sortByChina(new ArrayList<String>(features));
    }

    @Override
    public List<String> getBxTypes(String entranceType){
        Set<String> features = new HashSet<>();
        selectBBusiWishUniversitiesBxTypes(entranceType).forEach(ll->{
            features.addAll(Arrays.asList(com.ruoyi.common.utils.StringUtils.split(ll.getBxType(), ",")));
        });

        return CommonUtils.sortByChina(new ArrayList<String>(features));
    }

    @Override
    public List<String> getLevels(String entranceType){
        Set<String> levels = new HashSet<>();
        selectBBusiWishUniversitiesLevels(entranceType).forEach(ll->{
            levels.addAll(Arrays.asList(com.ruoyi.common.utils.StringUtils.split(ll.getLevel(), "//")));
        });

        return CommonUtils.sortByChina(new ArrayList<String>(levels));
    }

    /**
     * 新增院校库
     *
     * @param bBusiWishUniversities 院校库
     * @return 结果
     */
    @Override
    public int insertBBusiWishUniversities(BBusiWishUniversities bBusiWishUniversities)
    {
        bBusiWishUniversities.setCreateTime(DateUtils.getNowDate());
        return bBusiWishUniversitiesMapper.insertBBusiWishUniversities(bBusiWishUniversities);
    }

    /**
     * 批量删除院校库
     *
     * @param ids 需要删除的院校库主键
     * @return 结果
     */
    @Override
    public int deleteBBusiWishUniversitiesByIds(Long[] ids)
    {
        return bBusiWishUniversitiesMapper.deleteBBusiWishUniversitiesByIds(ids);
    }

    /**
     * 删除院校库信息
     *
     * @param id 院校库主键
     * @return 结果
     */
    @Override
    public int deleteBBusiWishUniversitiesById(Long id)
    {
        return bBusiWishUniversitiesMapper.deleteBBusiWishUniversitiesById(id);
    }


}
