package cn.com.ctop.alarm.modules.service.impl;

import cn.com.ctop.alarm.modules.entity.AlarmMetricValue;
import cn.com.ctop.alarm.modules.mapper.AlarmMetricValueMapper;
import cn.com.ctop.alarm.modules.service.IAlarmMetricValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 指标字段记录表
 * depletion
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-10-29
 */
@Slf4j
@Service
public class AlarmMetricValueServiceImpl extends ServiceImpl<AlarmMetricValueMapper, AlarmMetricValue> implements IAlarmMetricValueService {

    @Autowired
    private AlarmMetricValueMapper alarmMetricValueMapper;

    @Override
    public Result<Object> queryParentChildList(AlarmMetricValue alarmMetricValue) {
        Result<Object> result = new Result<>();
        List<AlarmMetricValue> list = alarmMetricValueMapper.queryListByLevel(alarmMetricValue);
        List<AlarmMetricValue> resultList = new ArrayList<>();
        Map<Integer, List<AlarmMetricValue>> map = new HashMap<>();
        List<Integer> parents = new ArrayList<>();
        for (AlarmMetricValue metricValue : list) {
            if (0 == metricValue.getMetricParentId()) {
                parents.add(metricValue.getMetricValueId());
                resultList.add(metricValue);
            }
        }
        for (Integer id : parents) {
            List<AlarmMetricValue> childs = new ArrayList<>();
            for (AlarmMetricValue metricValue : list) {
                if (0 != metricValue.getMetricParentId()) {
                    if (id == metricValue.getMetricParentId()) {
                        childs.add(metricValue);
                    }
                }
                map.put(id, childs);
            }
        }
        for (AlarmMetricValue metricValue : resultList) {
            metricValue.setChilds(map.get(metricValue.getMetricValueId()));
        }
        return Result.ok(resultList);
    }
}
