package com.nokia.hb.service;

import cn.hutool.core.util.IdUtil;
import com.nokia.common.R;
import com.nokia.hb.dao.entity.PerCfgArea;
import com.nokia.hb.dao.mapper.PerCfgAreaMapper;
import com.nokia.hb.pojo.TreeNode;
import com.nokia.hb.pojo.enums.NetTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class AreaService {
    private final PerCfgAreaMapper perCfgAreaMapper;

    public AreaService(PerCfgAreaMapper perCfgAreaMapper) {
        this.perCfgAreaMapper = perCfgAreaMapper;
    }

    public R<List<TreeNode>> initTreeCitys() {
        List<PerCfgArea> areas = perCfgAreaMapper.all();
        Map<String, List<TreeNode>> m = new HashMap<>();
        TreeNode lteTree = new TreeNode(NetTypeEnum.LTE.name(), NetTypeEnum.LTE.name(), new ArrayList<>());
        TreeNode nblteTree = new TreeNode(NetTypeEnum.NBLTE.name(), NetTypeEnum.NBLTE.name(), new ArrayList<>());
        List<TreeNode> netList = new ArrayList<>();
        netList.add(lteTree);
        netList.add(nblteTree);
        TreeNode allTree = new TreeNode("地市选择", "地市选择", netList);
        allTree.setSpread(true);
        List<TreeNode> res = new ArrayList<>();
        res.add(allTree);
        areas.forEach(a -> {
            String city = a.getCity();
            String quxian = a.getQuxian();
            String id = city + quxian;
            TreeNode t = new TreeNode(quxian, id, null);
            m.putIfAbsent(city, new ArrayList<>());
            m.get(city).add(t);
        });
        m.forEach((key, value) -> {
            lteTree.getChildren().add(new TreeNode(key, IdUtil.fastSimpleUUID(), value));
            nblteTree.getChildren().add(new TreeNode(key, IdUtil.fastSimpleUUID(), value));
        });
        allTree.getChildren().add(new TreeNode("其他", "OTHER", null));
        return R.ok(res);
    }
}
