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

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

import com.hw.common.core.utils.DateUtils;
import com.hw.common.core.utils.StringUtils;
import com.hw.ems.base.domain.BaseDnbThreshold;
import com.hw.ems.base.domain.BaseMonitorInfo;
import com.hw.ems.base.domain.vo.TreeSelect;
import com.hw.ems.base.mapper.BaseDnbThresholdMapper;
import com.hw.ems.base.mapper.BaseMonitorInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.ems.base.service.IBaseDnbThresholdService;

/**
 * 电阈值信息Service业务层处理
 *
 * @author sf
 * @date 2023-04-20
 */
@Service
public class BaseDnbThresholdServiceImpl implements IBaseDnbThresholdService {
    @Autowired
    private BaseDnbThresholdMapper baseDnbThresholdMapper;
    @Autowired
    private BaseMonitorInfoMapper baseMonitorInfoMapper;

    /**
     * 查询电阈值信息
     *
     * @param objid 电阈值信息主键
     * @return 电阈值信息
     */
    @Override
    //@DS("#header.poolName")
    public BaseDnbThreshold selectBaseDnbThresholdByObjid(Long objid) {
        return baseDnbThresholdMapper.selectBaseDnbThresholdByObjid(objid);
    }

    /**
     * 查询电阈值信息列表
     *
     * @param baseDnbThreshold 电阈值信息
     * @return 电阈值信息
     */
    @Override
    //@DS("#header.poolName")
    public List<BaseDnbThreshold> selectBaseDnbThresholdList(BaseDnbThreshold baseDnbThreshold) {
        return baseDnbThresholdMapper.selectBaseDnbThresholdList(baseDnbThreshold);
    }

    /**
     * 新增电阈值信息
     *
     * @param baseDnbThreshold 电阈值信息
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int insertBaseDnbThreshold(BaseDnbThreshold baseDnbThreshold) {
        baseDnbThreshold.setCreateTime(DateUtils.getNowDate());
        return baseDnbThresholdMapper.insertBaseDnbThreshold(baseDnbThreshold);
    }

    /**
     * 修改电阈值信息
     *
     * @param baseDnbThreshold 电阈值信息
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int updateBaseDnbThreshold(BaseDnbThreshold baseDnbThreshold) {
        baseDnbThreshold.setUpdateTime(DateUtils.getNowDate());
        return baseDnbThresholdMapper.updateBaseDnbThreshold(baseDnbThreshold);
    }

    /**
     * 批量删除电阈值信息
     *
     * @param objids 需要删除的电阈值信息主键
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int deleteBaseDnbThresholdByObjids(Long[] objids) {
        return baseDnbThresholdMapper.deleteBaseDnbThresholdByObjids(objids);
    }

    /**
     * 删除电阈值信息信息
     *
     * @param objid 电阈值信息主键
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int deleteBaseDnbThresholdByObjid(Long objid) {
        return baseDnbThresholdMapper.deleteBaseDnbThresholdByObjid(objid);
    }

    @Override
    //@DS("#header.poolName")
    public List<TreeSelect> selectMonitorTreeList(BaseMonitorInfo baseMonitorInfo) {
        List<BaseMonitorInfo> monitors = selectMonitorList(baseMonitorInfo);
        return buildMonitorTreeSelect(monitors);
    }

    //@DataScope(deptAlias = "d")
    //@DS("#header.poolName")
    public List<BaseMonitorInfo> selectMonitorList(BaseMonitorInfo baseMonitorInfo) {
        return baseMonitorInfoMapper.selectMonitorList(baseMonitorInfo);
    }

    //@Override
    public List<TreeSelect> buildMonitorTreeSelect(List<BaseMonitorInfo> monitors) {
        List<BaseMonitorInfo> monitorTrees = buildMonitorTree(monitors);
        return monitorTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    //@Override
    public List<BaseMonitorInfo> buildMonitorTree(List<BaseMonitorInfo> monitors) {
        List<BaseMonitorInfo> returnList = new ArrayList<BaseMonitorInfo>();
        List<Long> tempList = monitors.stream().map(BaseMonitorInfo::getObjid).collect(Collectors.toList());
        for (BaseMonitorInfo monitor : monitors) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(monitor.getParentId())) {
                recursionFn(monitors, monitor);
                returnList.add(monitor);
            }
        }
        if (returnList.isEmpty()) {
            returnList = monitors;
        }
        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 ? true : false;
    }
}
