package com.dcsoft.system.service.impl;

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

import com.dcsoft.common.core.utils.CollUtil;
import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.system.domain.SysPoint;
import com.dcsoft.system.domain.SysSpace;
import com.dcsoft.system.domain.vo.SysAuthTree;
import com.dcsoft.system.domain.vo.TreeSelect;
import com.dcsoft.system.mapper.SysPointMapper;
import com.dcsoft.system.mapper.SysSpaceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dcsoft.system.mapper.SysRuleMapper;
import com.dcsoft.system.domain.SysRule;
import com.dcsoft.system.service.ISysRuleService;

/**
 * 通行规则Service业务层处理
 *
 * @author nichun
 * @date 2023-03-08
 */
@Service
public class SysRuleServiceImpl implements ISysRuleService
{
    @Autowired
    private SysRuleMapper sysRuleMapper;

    @Autowired
    private SysSpaceMapper spaceManager;

    @Autowired
    private SysPointMapper pointManager;

    /**
     * 查询通行规则
     *
     * @param id 通行规则主键
     * @return 通行规则
     */
    @Override
    public SysRule selectSysRuleById(Long id)
    {
        return sysRuleMapper.selectSysRuleById(id);
    }

    /**
     * 查询通行规则列表
     *
     * @param sysRule 通行规则
     * @return 通行规则
     */
    @Override
    public List<SysRule> selectSysRuleList(SysRule sysRule)
    {
        return sysRuleMapper.selectSysRuleList(sysRule);
    }

    /**
     * 新增通行规则
     *
     * @param sysRule 通行规则
     * @return 结果
     */
    @Override
    public int insertSysRule(SysRule sysRule)
    {
        sysRule.setCreateTime(DateUtils.getNowDate());
        return sysRuleMapper.insertSysRule(sysRule);
    }

    /**
     * 修改通行规则
     *
     * @param sysRule 通行规则
     * @return 结果
     */
    @Override
    public int updateSysRule(SysRule sysRule)
    {
        sysRule.setUpdateTime(DateUtils.getNowDate());
        return sysRuleMapper.updateSysRule(sysRule);
    }

    /**
     * 批量删除通行规则
     *
     * @param ids 需要删除的通行规则主键
     * @return 结果
     */
    @Override
    public int deleteSysRuleByIds(Long[] ids)
    {
        return sysRuleMapper.deleteSysRuleByIds(ids);
    }

    /**
     * 删除通行规则信息
     *
     * @param id 通行规则主键
     * @return 结果
     */
    @Override
    public int deleteSysRuleById(Long id)
    {
        return sysRuleMapper.deleteSysRuleById(id);
    }

    @Override
    public List<SysAuthTree> selectRuleAuthScope() {
        List<SysSpace> modules = spaceManager.selectSysSpaceList(new SysSpace());
        return buildSpaceTreeSelect(modules);
    }

    @Override
    public List<SysAuthTree> queryRuleAuth() {
        return null;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param spaces 空间列表
     * @return 下拉树结构列表
     */
    public List<SysAuthTree> buildSpaceTreeSelect(List<SysSpace> spaces)
    {
        List<SysSpace> spaceTrees = buildSpaceTree(spaces);
        return spaceTrees.stream().map(SysAuthTree::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param spaces 空间列表
     * @return 树结构列表
     */
    public List<SysSpace> buildSpaceTree(List<SysSpace> spaces)
    {
        List<SysSpace> returnList = new ArrayList<SysSpace>();
        List<Long> tempList = spaces.stream().map(SysSpace::getId).collect(Collectors.toList());
        for (SysSpace space : spaces)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(space.getParentId()))
            {
                recursionFn(spaces, space);
                returnList.add(space);
            }

        }
        if (returnList.isEmpty())
        {
            returnList = spaces;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysSpace> list, SysSpace t)
    {
        // 得到子节点列表
        List<SysSpace> childList = getChildList(list, t);
        t.setChildren(childList);
        SysPoint point=new SysPoint();
        point.setSpaceId(t.getId());
        List<SysPoint> pointList = pointManager.selectSysPointList(point);
        t.setChildren1(pointList);
        for (SysSpace tChild : childList)
        {
            recursionFn(list, tChild);
            /* if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }*/
        }
    }

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

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