package com.ruoyi.mingxue.service.impl;

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

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mingxue.mapper.MxjbOperationManualMapper;
import com.ruoyi.mingxue.domain.MxjbOperationManual;
import com.ruoyi.mingxue.service.IMxjbOperationManualService;

/**
 * 操作手册Service业务层处理
 * 
 * @author mingxue
 * @date 2022-08-30
 */
@Service
public class MxjbOperationManualServiceImpl implements IMxjbOperationManualService 
{
    @Autowired
    private MxjbOperationManualMapper mxjbOperationManualMapper;

    /**
     * 查询操作手册
     * 
     * @param id 操作手册ID
     * @return 操作手册
     */
    @Override
    public MxjbOperationManual selectMxjbOperationManualById(Long id)
    {
        return mxjbOperationManualMapper.selectMxjbOperationManualById(id);
    }

    @Override
    public MxjbOperationManual selectMxjbOperationManualByNameType(String nameType) {
        return mxjbOperationManualMapper.selectMxjbOperationManualByNameType(nameType);
    }

    /**
     * 查询操作手册列表
     * 
     * @param mxjbOperationManual 操作手册
     * @return 操作手册
     */
    @Override
    public List<MxjbOperationManual> selectMxjbOperationManualList(MxjbOperationManual mxjbOperationManual)
    {
        return mxjbOperationManualMapper.selectMxjbOperationManualList(mxjbOperationManual);
    }

    @Override
    public List<MxjbOperationManual> selectMxjbOperationManualAndChildren(MxjbOperationManual mxjbOperationManual) {
        return mxjbOperationManualMapper.selectMxjbOperationManualAndChildren(mxjbOperationManual);
    }

    /**
     * 新增操作手册
     * 
     * @param mxjbOperationManual 操作手册
     * @return 结果
     */
    @Override
    public int insertMxjbOperationManual(MxjbOperationManual mxjbOperationManual)
    {
        mxjbOperationManual.setCreateTime(DateUtils.getNowDate());
        return mxjbOperationManualMapper.insertMxjbOperationManual(mxjbOperationManual);
    }

    /**
     * 修改操作手册
     * 
     * @param mxjbOperationManual 操作手册
     * @return 结果
     */
    @Override
    public int updateMxjbOperationManual(MxjbOperationManual mxjbOperationManual)
    {
        mxjbOperationManual.setUpdateTime(DateUtils.getNowDate());
        return mxjbOperationManualMapper.updateMxjbOperationManual(mxjbOperationManual);
    }

    /**
     * 批量删除操作手册
     * 
     * @param ids 需要删除的操作手册ID
     * @return 结果
     */
    @Override
    public int deleteMxjbOperationManualByIds(Long[] ids)
    {
        return mxjbOperationManualMapper.deleteMxjbOperationManualByIds(ids);
    }

    /**
     * 删除操作手册信息
     * 
     * @param id 操作手册ID
     * @return 结果
     */
    @Override
    public int deleteMxjbOperationManualById(Long id)
    {
        return mxjbOperationManualMapper.deleteMxjbOperationManualById(id);
    }

    @Override
    public List<MxjbOperationManual> buildTree(List<MxjbOperationManual> operationManuals)
    {
        List<MxjbOperationManual> returnList = new ArrayList<MxjbOperationManual>();
        List<Long> tempList = new ArrayList<Long>();
        for (MxjbOperationManual mxjbOperationManual : operationManuals)
        {
            tempList.add(mxjbOperationManual.getId());
        }
        for (Iterator<MxjbOperationManual> iterator = operationManuals.iterator(); iterator.hasNext();)
        {
            MxjbOperationManual manual = (MxjbOperationManual) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(manual.getParentId()))
            {
                recursionFn(operationManuals, manual);
                returnList.add(manual);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = operationManuals;
        }
        return returnList;
    }

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

    /**
     * 得到子节点列表
     */
    private List<MxjbOperationManual> getChildList(List<MxjbOperationManual> list, MxjbOperationManual t)
    {
        List<MxjbOperationManual> tlist = new ArrayList<MxjbOperationManual>();
        Iterator<MxjbOperationManual> it = list.iterator();
        while (it.hasNext())
        {
            MxjbOperationManual n = (MxjbOperationManual) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

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

}
