package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.industry;
import com.ruoyi.system.domain.vo.industryTree;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISysIndustryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 用户 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysIndustryServiceImpl implements ISysIndustryService
{
    private static final Logger log = LoggerFactory.getLogger(SysIndustryServiceImpl.class);

    @Autowired
    private SysIndustryMapper industryMapper;



    @Override
    public List<industry> selectindustryList() {
        return industryMapper.selectIndustryList();
    }

    @Override
    public List<industryTree> getIndustryTree() {
        List<industry> industries = selectindustryList();
        List<industryTree> industryTrees = new ArrayList<>();
        if (industries.size()>0){
            List<industryTree> trees = industries.stream().map(i->{
                industryTree industryTree = new industryTree();
                BeanUtils.copyProperties(i,industryTree);
                return industryTree;
            }).collect(Collectors.toList());
            trees.forEach(e->{
                List<industryTree> list = getChildrenList(e.getCode(),trees);
                e.setIndustryTrees(list);
                    }
                    );
            List<industryTree> parentNodes = trees.stream().filter(e->e.getParent_code()==null||e.getParent_code().equals("")).collect(Collectors.toList());
            industryTrees.addAll(parentNodes);
        }
        return industryTrees;
    }
    public static List<industryTree> getChildrenList(String code,List<industryTree> industryTrees){
       return industryTrees.stream().filter(i->code.equals(i.getParent_code())).collect(Collectors.toList());
    }
}
