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.TreeSelects;
import com.aucma.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.base.mapper.BaseMonitorInfoMapper;
import com.aucma.base.domain.BaseMonitorInfo;
import com.aucma.base.service.IBaseMonitorInfoService;

/**
 * 计量设备信息Service业务层处理
 *
 * @author Yinq
 * @date 2023-09-21
 */
@Service
public class BaseMonitorInfoServiceImpl implements IBaseMonitorInfoService
{
    @Autowired
    private BaseMonitorInfoMapper baseMonitorInfoMapper;

    /**
     * 查询计量设备信息
     *
     * @param objId 计量设备信息主键
     * @return 计量设备信息
     */
    @Override
    public BaseMonitorInfo selectBaseMonitorInfoByObjId(Long objId)
    {
        return baseMonitorInfoMapper.selectBaseMonitorInfoByObjId(objId);
    }

    /**
     * 查询计量设备信息列表
     *
     * @param baseMonitorInfo 计量设备信息
     * @return 计量设备信息
     */
    @Override
    public List<BaseMonitorInfo> selectBaseMonitorInfoList(BaseMonitorInfo baseMonitorInfo)
    {
        return baseMonitorInfoMapper.selectBaseMonitorInfoList(baseMonitorInfo);
    }

    @Override
    public List<TreeSelects> selectBaseMonitorInfoTreeList(BaseMonitorInfo baseMonitorInfo) {
        List<BaseMonitorInfo> baseMonitorInfos = selectBaseMonitorInfoList(baseMonitorInfo);
        return buildMonitorInfoTreeSelect(baseMonitorInfos);
    }
    /**
     * 构建前端所需要下拉树结构
     *
     * @param
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelects> buildMonitorInfoTreeSelect(List<BaseMonitorInfo> baseMonitorInfos)
    {
        List<BaseMonitorInfo> deptTrees = buildMonitorInfoTree(baseMonitorInfos);
        return deptTrees.stream().map(TreeSelects::new).collect(Collectors.toList());
    }
    /**
     * 构建前端所需要树结构
     *
     * @param
     * @return 树结构列表
     */
    @Override
    public List<BaseMonitorInfo> buildMonitorInfoTree(List<BaseMonitorInfo> baseMonitorInfos)
    {
        List<BaseMonitorInfo> returnList = new ArrayList<BaseMonitorInfo>();
        List<Long> tempList = baseMonitorInfos.stream().map(BaseMonitorInfo::getObjId).collect(Collectors.toList());
        for (BaseMonitorInfo baseMonitorInfo : baseMonitorInfos)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(baseMonitorInfo.getParentId()))
            {
                recursionFn(baseMonitorInfos, baseMonitorInfo);
                returnList.add(baseMonitorInfo);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = baseMonitorInfos;
        }
        return returnList;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<BaseMonitorInfo> list, BaseMonitorInfo t)
    {
        // 得到子节点列表
        List<BaseMonitorInfo> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BaseMonitorInfo tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<BaseMonitorInfo> getChildList(List<BaseMonitorInfo> list, BaseMonitorInfo t)
    {
        List<BaseMonitorInfo> tlist = new ArrayList<BaseMonitorInfo>();
        Iterator<BaseMonitorInfo> it = list.iterator();
        while (it.hasNext())
        {
            BaseMonitorInfo n = (BaseMonitorInfo) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getObjId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

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


    /**
     * 新增计量设备信息
     *
     * @param baseMonitorInfo 计量设备信息
     * @return 结果
     */
    @Override
    public int insertBaseMonitorInfo(BaseMonitorInfo baseMonitorInfo)
    {
        return baseMonitorInfoMapper.insertBaseMonitorInfo(baseMonitorInfo);
    }

    /**
     * 修改计量设备信息
     *
     * @param baseMonitorInfo 计量设备信息
     * @return 结果
     */
    @Override
    public int updateBaseMonitorInfo(BaseMonitorInfo baseMonitorInfo)
    {
        return baseMonitorInfoMapper.updateBaseMonitorInfo(baseMonitorInfo);
    }

    /**
     * 批量删除计量设备信息
     *
     * @param objIds 需要删除的计量设备信息主键
     * @return 结果
     */
    @Override
    public int deleteBaseMonitorInfoByObjIds(Long[] objIds)
    {
        return baseMonitorInfoMapper.deleteBaseMonitorInfoByObjIds(objIds);
    }

    /**
     * 删除计量设备信息信息
     *
     * @param objId 计量设备信息主键
     * @return 结果
     */
    @Override
    public int deleteBaseMonitorInfoByObjId(Long objId)
    {
        return baseMonitorInfoMapper.deleteBaseMonitorInfoByObjId(objId);
    }

    @Override
    public List<BaseMonitorInfo> selectBaseMonitorInfoByNameLike(String monitorName) {
        return baseMonitorInfoMapper.selectBaseMonitorInfoByNameLike(monitorName);
    }
}
