package com.bonus.sgzb.machine.service.impl;

import com.bonus.sgzb.common.core.utils.StringUtils;
import com.bonus.sgzb.machine.damain.MaType;
import com.bonus.sgzb.machine.mapper.MaTypeMapper;
import com.bonus.sgzb.machine.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工机具类型管理实现层
 */
@Service
public class MaTypeServiceImpl implements ITypeService {

    @Autowired
    private MaTypeMapper maTypeMapper;

    @Override
    public List<MaType> getMaTypeList(String typeName) {

        List<MaType> maTypes = maTypeMapper.selectMaTypeList();

        if(StringUtils.isNotNull(typeName)){
            //根据类型名称过滤集合
            List<MaType> types = maTypes
                    .stream()
                    .filter(maType -> maType.getTypeName().contains(typeName))
                    .collect(Collectors.toList());
            //如果没有查询到那么返回空
            if(types.size()==0){
                return null;
            }
            //递归获取树
            return getTypeListByMaType(Collections.singletonList(types.get(0)),maTypes,null);
        }
        return maTypes;
    }

    /**
     * 根据左列表类型id查询右表格
     * @param typeId
     * @return
     */
    @Override
    public List<MaType> getListByMaType(Long typeId, String typeName) {

        List<MaType> maTypes = maTypeMapper.selectMaTypeList();
        MaType maType = new MaType();
        maType.setTypeId(typeId);

        List<MaType> typeListByMaType = getTypeListByMaType(Collections.singletonList(maType), maTypes, new ArrayList<>());

        if(StringUtils.isNotNull(typeName)){
            //根据类型名称过滤集合
            List<MaType> types = typeListByMaType
                    .stream()
                    .filter(ma -> ma.getTypeName().contains(typeName))
                    .collect(Collectors.toList());
            //如果没有查询到那么返回空
            if(types.size()==0){
                return null;
            }
            //递归获取树
            return getTypeListByMaType(Collections.singletonList(types.get(0)),maTypes,null);
        }
        return typeListByMaType;
    }

    /**
     * 根据父级类型递归获取树结构
     * @param
     * @return
     */
    private List<MaType> getTypeListByMaType(List<MaType> parentTypes,List<MaType> oldMaTypes,List<MaType> newMaTypes) {

        //初始化新容器
        if(newMaTypes==null){
            newMaTypes = new ArrayList<>();
            //添加父级对象 左边需要添加因为需要显示  右边不需要添加因为不需要显示
            newMaTypes.addAll(parentTypes);
        }
        //递归父级容器
        List<MaType> parentList = new ArrayList<>();
        //过滤相同数据,减少循环,性能优化
        oldMaTypes = oldMaTypes.stream().filter(oldMaType->!parentTypes.contains(oldMaType)).collect(Collectors.toList());
        for (MaType maType : oldMaTypes) {
            for (MaType parentType : parentTypes) {
                if(maType.getParentId().equals(parentType.getTypeId())){
                    newMaTypes.add(maType);
                    parentList.add(maType);
                }
            }
        }
        //如果还有父级递归查询
        if(parentList.size()>0){
            return getTypeListByMaType(parentList,oldMaTypes,newMaTypes);
        }else{
            //返回
            return newMaTypes;
        }
    }
}
