package com.nokia.hb.service;

import com.nokia.common.R;
import com.nokia.hb.dao.entity.PerCfgIndicator;
import com.nokia.hb.dao.mapper.PerCfgIndicatorMapper;
import com.nokia.hb.pojo.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class IndicatorService {
    private final PerCfgIndicatorMapper perCfgIndicatorMapper;

    public IndicatorService(PerCfgIndicatorMapper perCfgIndicatorMapper) {
        this.perCfgIndicatorMapper = perCfgIndicatorMapper;
    }

    public R<List<TreeNode>> initTreeIndicator() {
        List<PerCfgIndicator> indicators = perCfgIndicatorMapper.all();
        Map<String, List<TreeNode>> m = new HashMap<>();
        TreeNode allTree = new TreeNode("指标选择", "指标选择", new ArrayList<>());
        allTree.setSpread(true);
        List<TreeNode> res = new ArrayList<>();
        res.add(allTree);
        indicators.forEach(i -> {
            String indicatorType = i.getIndicatorType();
            String indicatorCn = i.getIndicatorCn();
            String indicatorId = String.valueOf(i.getIndicatorId());
            TreeNode t = new TreeNode(indicatorCn, indicatorId, null);
            m.putIfAbsent(indicatorType, new ArrayList<>());
            m.get(indicatorType).add(t);
        });
        m.forEach((k, v) -> allTree.getChildren().add(new TreeNode(k, k, v)));
        return R.ok(res);
    }
}
