package com.ruoyi.ems.base.service.impl;

import java.util.ArrayList;
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.ems.base.domain.TreeSelects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ems.base.mapper.EmsBaseMonitorInfoMapper;
import com.ruoyi.ems.base.domain.EmsBaseMonitorInfo;
import com.ruoyi.ems.base.service.IEmsBaseMonitorInfoService;

/**
 * 计量设备信息Service业务层处理
 * 
 * @author Yinq
 * @date 2024-05-08
 */
@Service
public class EmsBaseMonitorInfoServiceImpl implements IEmsBaseMonitorInfoService 
{
    @Autowired
    private EmsBaseMonitorInfoMapper emsBaseMonitorInfoMapper;

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

    /**
     * 查询计量设备信息列表
     * 
     * @param emsBaseMonitorInfo 计量设备信息
     * @return 计量设备信息
     */
    @Override
    public List<EmsBaseMonitorInfo> selectEmsBaseMonitorInfoList(EmsBaseMonitorInfo emsBaseMonitorInfo)
    {
        return emsBaseMonitorInfoMapper.selectEmsBaseMonitorInfoList(emsBaseMonitorInfo);
    }

    /**
     * 根据设备编号查询本设备能源类型
     * @param monitorCode
     * @return
     */
    @Override
    public String selectEmsBaseMonitorTypeByMonitorCode(String monitorCode) {
        return emsBaseMonitorInfoMapper.selectEmsBaseMonitorTypeByMonitorCode(monitorCode);
    }

    /**
     * 新增计量设备信息
     * 
     * @param emsBaseMonitorInfo 计量设备信息
     * @return 结果
     */
    @Override
    public int insertEmsBaseMonitorInfo(EmsBaseMonitorInfo emsBaseMonitorInfo)
    {
        emsBaseMonitorInfo.setCreateTime(DateUtils.getNowDate());
        return emsBaseMonitorInfoMapper.insertEmsBaseMonitorInfo(emsBaseMonitorInfo);
    }

    /**
     * 修改计量设备信息
     * 
     * @param emsBaseMonitorInfo 计量设备信息
     * @return 结果
     */
    @Override
    public int updateEmsBaseMonitorInfo(EmsBaseMonitorInfo emsBaseMonitorInfo)
    {
        emsBaseMonitorInfo.setUpdateTime(DateUtils.getNowDate());
        return emsBaseMonitorInfoMapper.updateEmsBaseMonitorInfo(emsBaseMonitorInfo);
    }

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

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

    /**
     * 查询计量设备信息下拉树结构
     * */
    @Override
    public List<TreeSelects> selectBaseMonitorInfoTreeList(EmsBaseMonitorInfo baseMonitorInfo) {
        List<EmsBaseMonitorInfo> baseMonitorInfos = selectEmsBaseMonitorInfoList(baseMonitorInfo);
        return buildMonitorInfoTreeSelect(baseMonitorInfos);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelects> buildMonitorInfoTreeSelect(List<EmsBaseMonitorInfo> baseMonitorInfos)
    {
        List<EmsBaseMonitorInfo> deptTrees = buildMonitorInfoTree(baseMonitorInfos);
        return deptTrees.stream().map(TreeSelects::new).collect(Collectors.toList());
    }

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

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

}
