package com.ruoyi.sy.service.impl;

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

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.syzy.domain.TreeSelectVocational;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sy.mapper.SyVocationalMapper;
import com.ruoyi.sy.domain.SyVocational;
import com.ruoyi.sy.service.ISyVocationalService;

/**
 * 职业Service业务层处理
 *
 * @author mingxue
 * @date 2022-03-30
 */
@Service
public class SyVocationalServiceImpl implements ISyVocationalService
{
    @Autowired
    private SyVocationalMapper syVocationalMapper;

    /**
     * 查询职业
     *
     * @param id 职业ID
     * @return 职业
     */
    @Override
    public SyVocational selectSyVocationalById(Long id)
    {
        return syVocationalMapper.selectSyVocationalById(id);
    }

    /**
     * 查询职业列表
     *
     * @param syVocational 职业
     * @return 职业
     */
    @Override
    public List<SyVocational> selectSyVocationalList(SyVocational syVocational)
    {
        return syVocationalMapper.selectSyVocationalList(syVocational);
    }

    /**
     * 构建前端所需要下拉树结构
     */
    @Override
    public List<TreeSelectVocational> buildVocationalTreeSelect(List<SyVocational> vocationalList)
    {
        List<SyVocational> vocationalTrees = buildVocationalTree(vocationalList);
        List<TreeSelectVocational> treeSelects =vocationalTrees.stream().map(TreeSelectVocational::new).sorted(Comparator.comparing(TreeSelectVocational::getCode))
                .collect(Collectors.toList());
        return treeSelects;
    }

    /**
     * 构建前端所需要树结构
     *
     * @param vocationals 机构列表
     * @return 树结构列表
     */
    @Override
    public List<SyVocational> buildVocationalTree(List<SyVocational> vocationals)
    {
        List<SyVocational> returnList = new ArrayList<SyVocational>();
        List<String> tempList = vocationals.stream().map(t->t.getParentCode()).collect(Collectors.toList());

        List<String> addedCode = new ArrayList<>();
        for (Iterator<SyVocational> iterator = vocationals.iterator(); iterator.hasNext();)
        {
            SyVocational vocational = (SyVocational) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
//            if (!tempList.contains(vocational.getParentCode()))
            if ("0".equalsIgnoreCase(vocational.getParentCode())&&vocational.getLevel()==1)
            {
                addedCode.add(vocational.getCode());
                recursionFn(vocationals, vocational);
                returnList.add(vocational);
            }else {
                if(!addedCode.contains(vocational.getParentCode())&&tempList.contains(vocational.getCode())){
                    recursionFn(vocationals, vocational);
                    returnList.add(vocational);
                    addedCode.add(vocational.getCode());
                }
            }
        }
        if (returnList.isEmpty())
        {
            returnList = vocationals;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SyVocational> list, SyVocational t)
    {
        // 得到子节点列表
        List<SyVocational> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SyVocational tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SyVocational> getChildList(List<SyVocational> list, SyVocational t)
    {
        List<SyVocational> tlist = new ArrayList<SyVocational>();
        Iterator<SyVocational> it = list.iterator();
        while (it.hasNext())
        {
            SyVocational n = (SyVocational) it.next();
            if (StringUtils.isNotEmpty(n.getParentCode()) && n.getParentCode().equalsIgnoreCase(t.getCode()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SyVocational> list, SyVocational t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    /**
     * 新增职业
     *
     * @param syVocational 职业
     * @return 结果
     */
    @Override
    public int insertSyVocational(SyVocational syVocational)
    {
        syVocational.setCreateTime(DateUtils.getNowDate());
        return syVocationalMapper.insertSyVocational(syVocational);
    }

    /**
     * 修改职业
     *
     * @param syVocational 职业
     * @return 结果
     */
    @Override
    public int updateSyVocational(SyVocational syVocational)
    {
        return syVocationalMapper.updateSyVocational(syVocational);
    }

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

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