package com.aucma.base.service.impl;

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

import com.aucma.base.domain.BaseMonitorInfo;
import com.aucma.base.domain.TreeSelects;
import com.aucma.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.base.mapper.BaseWorkUnitMapper;
import com.aucma.base.domain.BaseWorkUnit;
import com.aucma.base.service.IBaseWorkUnitService;

/**
 * 统计单元信息Service业务层处理
 *
 * @author Yinq
 * @date 2023-10-12
 */
@Service
public class BaseWorkUnitServiceImpl implements IBaseWorkUnitService
{
    @Autowired
    private BaseWorkUnitMapper baseWorkUnitMapper;

    /**
     * 查询统计单元信息
     *
     * @param objId 统计单元信息主键
     * @return 统计单元信息
     */
    @Override
    public BaseWorkUnit selectBaseWorkUnitByObjId(Long objId)
    {
        return baseWorkUnitMapper.selectBaseWorkUnitByObjId(objId);
    }

    /**
     * 查询统计单元信息列表
     *
     * @param baseWorkUnit 统计单元信息
     * @return 统计单元信息
     */
    @Override
    public List<BaseWorkUnit> selectBaseWorkUnitList(BaseWorkUnit baseWorkUnit)
    {
        return baseWorkUnitMapper.selectBaseWorkUnitList(baseWorkUnit);
    }

    /**
     * 新增统计单元信息
     *
     * @param baseWorkUnit 统计单元信息
     * @return 结果
     */
    @Override
    public int insertBaseWorkUnit(BaseWorkUnit baseWorkUnit)
    {
        return baseWorkUnitMapper.insertBaseWorkUnit(baseWorkUnit);
    }

    /**
     * 修改统计单元信息
     *
     * @param baseWorkUnit 统计单元信息
     * @return 结果
     */
    @Override
    public int updateBaseWorkUnit(BaseWorkUnit baseWorkUnit)
    {
        return baseWorkUnitMapper.updateBaseWorkUnit(baseWorkUnit);
    }

    /**
     * 批量删除统计单元信息
     *
     * @param objIds 需要删除的统计单元信息主键
     * @return 结果
     */
    @Override
    public int deleteBaseWorkUnitByObjIds(Long[] objIds)
    {
        return baseWorkUnitMapper.deleteBaseWorkUnitByObjIds(objIds);
    }

    /**
     * 删除统计单元信息信息
     *
     * @param objId 统计单元信息主键
     * @return 结果
     */
    @Override
    public int deleteBaseWorkUnitByObjId(Long objId)
    {
        return baseWorkUnitMapper.deleteBaseWorkUnitByObjId(objId);
    }

    @Override
    public List<TreeSelects> selectBaseWorkUnitTreeList(BaseWorkUnit baseWorkUnit) {
        List<BaseWorkUnit> baseWorkUnits = selectBaseWorkUnitList(baseWorkUnit);
        return buildWorkUnitTreeSelect(baseWorkUnits);
    }

    @Override
    public List<TreeSelects> buildWorkUnitTreeSelect(List<BaseWorkUnit> baseWorkUnits) {
        List<BaseWorkUnit> baseWorkUnitsTrees = buildWorkUnitTree(baseWorkUnits);
        return baseWorkUnitsTrees.stream().map(TreeSelects::new).collect(Collectors.toList());
    }

    @Override
    public List<BaseWorkUnit> buildWorkUnitTree(List<BaseWorkUnit> baseWorkUnits) {

        List<BaseWorkUnit> returnList = new ArrayList<BaseWorkUnit>();
        List<Long> tempList = baseWorkUnits.stream().map(BaseWorkUnit::getObjId).collect(Collectors.toList());
        for (BaseWorkUnit baseMonitorInfo : baseWorkUnits)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(baseMonitorInfo.getParentId()))
            {
                recursionFn(baseWorkUnits, baseMonitorInfo);
                returnList.add(baseMonitorInfo);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = baseWorkUnits;
        }
        return returnList;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<BaseWorkUnit> list, BaseWorkUnit t)
    {
        // 得到子节点列表
        List<BaseWorkUnit> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BaseWorkUnit tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<BaseWorkUnit> getChildList(List<BaseWorkUnit> list, BaseWorkUnit t)
    {
        List<BaseWorkUnit> tlist = new ArrayList<BaseWorkUnit>();
        Iterator<BaseWorkUnit> it = list.iterator();
        while (it.hasNext())
        {
            BaseWorkUnit n = (BaseWorkUnit) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getObjId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

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

}
