package com.gyee.wisdom.alarm.sharding.cache;

import com.gyee.wisdom.alarm.sharding.entity.TestingPointAI;
import com.gyee.wisdom.alarm.sharding.entity.TestingPointDI;
import com.gyee.wisdom.alarm.sharding.service.TestingPointAIService;
import com.gyee.wisdom.alarm.sharding.service.TestingPointDIService;
import com.gyee.wisdom.alarm.sharding.util.SpringContextUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description:
 * @auther: Wanghs
 * @date: 2022-04-12
 */
public class TestingPointCache {

    //ai点缓存  key为风机型号，value 为统一风机型号下所有测点
    private static Map<String, List<TestingPointAI>> pointAIMap = new HashMap<>();
    //di点缓存  key为风机型号，value 为统一风机型号下所有测点
    private static Map<String, List<TestingPointDI>> pointDIMap = new HashMap<>();

    private static void initPointAIMap() {

        TestingPointAIService bean = SpringContextUtil.getBean(TestingPointAIService.class);

        List<TestingPointAI> list = bean.getAllList();
        Map<String, List<TestingPointAI>> collect = list.stream().collect(Collectors.groupingBy(TestingPointAI::getModel));
        pointAIMap = collect;
    }

    private static void initPointDIMap() {

        TestingPointDIService bean = SpringContextUtil.getBean(TestingPointDIService.class);

        List<TestingPointDI> list = bean.getAllList();
        Map<String, List<TestingPointDI>> collect = list.stream().collect(Collectors.groupingBy(TestingPointDI::getModelId));
        pointDIMap = collect;
    }


    public static List<TestingPointAI> getTestingPointAIList(String model) {
        if (pointAIMap.size() <= 0)
            initPointAIMap();

        if (pointAIMap.containsKey(model))
            return pointAIMap.get(model);
        else
            return new ArrayList<>();
    }

    public static TestingPointAI getTestingPointAIList(String model, String uniformCode) {
        if (pointAIMap.size() <= 0)
            initPointAIMap();
        if (StringUtils.isBlank(model) || StringUtils.isBlank(uniformCode))
            return null;
        if (pointAIMap.containsKey(model)) {
            Optional<TestingPointAI> first = pointAIMap.get(model).stream().filter(s -> uniformCode.equals(s.getUniformCode())).findFirst();
            return first.isPresent() ? first.get() : null;

        } else {
            return null;
        }
    }

    public static List<TestingPointDI> getTestingPointDIList(String model) {
        if (pointDIMap.size() <= 0)
            initPointDIMap();

        if (pointDIMap.containsKey(model))
            return pointDIMap.get(model);
        else
            return new ArrayList<>();
    }

    public static TestingPointDI getTestingPointDIList(String model, String uniformCode) {
        if (pointDIMap.size() <= 0)
            initPointDIMap();
        if (StringUtils.isBlank(model) || StringUtils.isBlank(uniformCode))
            return null;
        if (pointDIMap.containsKey(model)) {
            Optional<TestingPointDI> first = pointDIMap.get(model).stream().filter(s -> uniformCode.equals(s.getUniformCode())).findFirst();
            return first.isPresent() ? first.get() : null;

        } else {
            return null;
        }
    }
}
