package com.its.op.global;

import com.its.op.entity.its.node.TbNode;
import com.its.op.dao.repository.its.node.TbNodeRepository;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@RequiredArgsConstructor
@Getter
@Component
public class TbNodeManager {

    private final TbNodeRepository repo;

    private static final ConcurrentHashMap<Long, TbNode> nodeMap = new ConcurrentHashMap<>();

    @PostConstruct
    void init() {
        load();
    }

    public void load() {

        List<TbNode> lists = repo.findAll();
        for (TbNode obj : lists) {
            nodeMap.put(obj.getNodeId(), obj);
        }
        //log.info("=== {}", nodeMap);
    }

    public static TbNode get(Long id) {
        if (id == null) {
            return null;
        }
        return nodeMap.get(id);
    }
    public static String getNodeName(Long NODE_ID) {
        if (NODE_ID == null) {
            return "x";
        }
        TbNode obj = nodeMap.get(NODE_ID);
        if (obj != null) {
            return "[" + NODE_ID + "] " + obj.getNodeName();
        }
        return "[" + NODE_ID + "] ?";
    }
    public static String getNodeNameInteger(Long NODE_ID) {
        if (NODE_ID == null) {
            return "x";
        }
        TbNode obj = nodeMap.get(NODE_ID);
        if (obj != null) {
            return obj.getNodeName();
        }
        return "?";
    }
}
