package com.ruoyi.mall.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.map.MapUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallMajorClassMapper;
import com.ruoyi.mall.domain.MallMajorClass;
import com.ruoyi.mall.service.IMallMajorClassService;

import javax.annotation.Resource;

/**
 * 专业分类Service业务层处理
 *
 * @author LCL
 * @date 2023-10-18
 */
@Service
public class MallMajorClassServiceImpl implements IMallMajorClassService
{
    @Resource
    private MallMajorClassMapper mallMajorClassMapper;

    /**
     * 查询专业分类
     *
     * @param majorId 专业分类主键
     * @return 专业分类
     */
    @Override
    public MallMajorClass selectMallMajorClassByMajorId(Long majorId)
    {
        return mallMajorClassMapper.selectMallMajorClassByMajorId(majorId);
    }

    /**
     * 查询专业分类列表
     *
     * @param mallMajorClass 专业分类
     * @return 专业分类
     */
    @Override
    public List<MallMajorClass> selectMallMajorClassList(MallMajorClass mallMajorClass)
    {
        return mallMajorClassMapper.selectMallMajorClassList(mallMajorClass);
    }

    /**
     * 新增专业分类
     *
     * @param mallMajorClass 专业分类
     * @return 结果
     */
    @Override
    public int insertMallMajorClass(MallMajorClass mallMajorClass)
    {
        mallMajorClass.setCreateAt(DateUtils.getTime());
        return mallMajorClassMapper.insertMallMajorClass(mallMajorClass);
    }

    /**
     * 修改专业分类
     *
     * @param mallMajorClass 专业分类
     * @return 结果
     */
    @Override
    public int updateMallMajorClass(MallMajorClass mallMajorClass)
    {
        mallMajorClass.setUpdateAt(DateUtils.getTime());
        return mallMajorClassMapper.updateMallMajorClass(mallMajorClass);
    }

    /**
     * 批量删除专业分类
     *
     * @param majorIds 需要删除的专业分类主键
     * @return 结果
     */
    @Override
    public int deleteMallMajorClassByMajorIds(Long[] majorIds)
    {
        return mallMajorClassMapper.deleteMallMajorClassByMajorIds(majorIds);
    }

    /**
     * 删除专业分类信息
     *
     * @param majorId 专业分类主键
     * @return 结果
     */
    @Override
    public int deleteMallMajorClassByMajorId(Long majorId)
    {
        return mallMajorClassMapper.deleteMallMajorClassByMajorId(majorId);
    }

    /**
     * 查询一级、二级专业分类列表
     *
     * @return 专业分类集合
     */
    @Override
    public List<Map<String, Object>> selectOneOrTwoMallMajorClassList(Integer type) {
        //return mallMajorClassMapper.selectOneOrTwoMallMajorClassList(majorId);
        List<Map<String, Object>> mapList = mallMajorClassMapper.selectMajorListWithSchoolNum(type);
        if (!mapList.isEmpty()){
            List<Map<String, Object>> collect = mapList.stream().filter(item -> MapUtil.getLong(item, "parentId").equals(0L)).collect(Collectors.toList());
            if (!collect.isEmpty()){
                for (Map<String, Object> map : collect) {
                    List<Map<String, Object>> collect1 = mapList.stream().filter(item -> MapUtil.getLong(item, "parentId").equals(MapUtil.getLong(map, "majorId"))).collect(Collectors.toList());
                    if (!collect1.isEmpty()){
                        map.put("children", collect1);
                    }
                }
            }
            return collect;
        }
        return mapList;
    }

    /**
     * 查询专业前景-岗位（职业）列表
     *
     * @param majorId 专业分类ID
     * @return 专业分类集合
     */
    @Override
    public List<Map<String, Object>> selectMajorProspectList(Long majorId) {
        List<Map<String, Object>> mapList = mallMajorClassMapper.selectRelatedCareerListByMajorId(majorId);
        if (mapList.size() > 0) {
            for (Map<String, Object> map : mapList) {
                List<Map<String, Object>> majorMapList = mallMajorClassMapper.selectRelatedMajorListByCareerId(Long.valueOf(map.get("careerId").toString()));
                map.put("majorMapList", majorMapList);
            }
        }
        return mapList;
    }

    /**
     * 查询二级专业分类列表
     *
     * @return 专业分类集合
     */
    @Override
    public Map<Object, List<Map<String, Object>>> selectAllTwoMallMajorClassList() {
        List<Map<String, Object>> mapList = mallMajorClassMapper.selectAllTwoMallMajorClassList();
        return mapList.stream()
                    .filter(item -> item.get("topMajorName") != null)
                    .collect(
                            Collectors.groupingBy(item -> item.get("topMajorName").toString())
                    );
    }

    /**
     * 根据父级id查询下级列表
     * @param pid  父级id
     * @return 结果
     */
    @Override
    public List<MallMajorClass> selectMajorListByPid(Long pid) {
        return mallMajorClassMapper.selectMajorListByPid(pid);
    }

    /**
     * 二级专业列表
     * @return 结果
     */
    @Override
    public List<MallMajorClass> selectMajorListByLevel2() {
        return mallMajorClassMapper.selectMajorListByLevel2();
    }

    /**
     * 硕士点院校列表
     * @param majorId 专业id
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectOneOrTwoMallMajorClassListByMaster(Long majorId) {
        return mallMajorClassMapper.selectOneOrTwoMallMajorClassListByMaster(majorId);
    }

    /**
     * 专业分类级联列表
     * @return
     */
    @Override
    public List<MallMajorClass> getMajorList() {
        List<MallMajorClass> list = mallMajorClassMapper.selectAllMajorList();
        if (!list.isEmpty()){
            List<MallMajorClass> collect = list.stream().filter(item -> item.getParentId().equals(0L)).collect(Collectors.toList());
            if (!collect.isEmpty()){
                for (MallMajorClass majorClass : collect) {
                    List<MallMajorClass> collect1 = list.stream().filter(obj -> obj.getParentId().equals(majorClass.getMajorId())).collect(Collectors.toList());
                    if (!collect1.isEmpty()){
                        majorClass.setChildren(collect1);
                    }
                }
            }
            return collect;
        }
        return list;
    }
}
