package com.gyee.wisdom.domain.timeseries;

import com.gyee.wisdom.common.data.timeseries.TsData;
import com.gyee.wisdom.common.data.timeseries.TsPoint;
import com.gyee.wisdom.common.exception.WisdomException;
import com.gyee.wisdom.dao.things.IThingsPointDao;
import com.gyee.wisdom.dao.timeseries.IHistoryDao;
import com.gyee.wisdom.dao.timeseries.ILatestDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
@Slf4j
public class TsPointService {

    //key:测点
    private Map<String, TsPoint> tagMap = new HashMap<>();

    //key1 风机编号,key2:风机统一编码
    private Map<String, Map<String, TsPoint>> windturbineTagMap = new HashMap<>();

    private List<TsPoint> tsPointList = new ArrayList<>();

    @Autowired
    private IThingsPointDao thingsPointDao;


    public List<TsPoint> getAllTsPointToCache() {
        try {
            if (tsPointList.size() <= 0) {
                List<TsPoint> lst = thingsPointDao.getAllTsPoint();
                for (TsPoint point : lst) {
                    if (!tagMap.containsKey(point.getId())) {
                        tagMap.put(point.getId(), point);
                    }
                    if (!windturbineTagMap.containsKey(point.getThingId())) {
                        Map<String, TsPoint> uniformCodeMap = new HashMap<>();
                        uniformCodeMap.put(point.getUniformCode(), point);
                        windturbineTagMap.put(point.getThingId(), uniformCodeMap);
                    } else {
                        Map<String, TsPoint> uniformCodeMap = windturbineTagMap.get(point.getThingId());

                        if (!uniformCodeMap.containsKey(point.getUniformCode())) {
                            uniformCodeMap.put(point.getUniformCode(), point);
                        }
                    }
                }
                tsPointList = lst;
            }
            return tsPointList;
        } catch (Exception ex) {
            System.out.println("缓存所有测点发生异常");
        }
        System.out.println("测点缓存完毕");
        return null;
    }

}
