package com.gyee.wisdom.dataadapter.domain.timeseries;

import com.gyee.wisdom.common.data.timeseries.BasicTsPoint;
import com.gyee.wisdom.common.data.timeseries.TsDataType;
import com.gyee.wisdom.common.data.timeseries.TsPoint;
import com.gyee.wisdom.dao.things.IThingsPointDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service
@Slf4j
public class TsPointService {

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

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

    @Autowired
    private IThingsPointDao thingsPointDao;

    public List<TsPoint> getTsPoint(String... tagNames) {
        List<TsPoint> selectList = new ArrayList<>();
        //先从缓存中获取

       /* Map<String, TsPoint> tMap = tagMap.entrySet().stream()
                .filter(s -> tagNameList.contains(s.getKey()))
                .collect(Collectors.toMap(e -> (String) e.getKey(), e -> e.getValue()));*/

       for (int i=0;i<tagNames.length;i++){
           if(tagMap.containsKey(tagNames[i])){

               selectList.add(tagMap.get(tagNames[i]));
           }

       }

        if (selectList.size() > 0) {
            return selectList;
        }

        if (tagNames.length > 1000) {

            int selectCount = tagNames.length / 1000;
            int remainder = tagNames.length % 1000;

            if (remainder == 0) {

                for (int i = 0; i < selectCount; i++) {
                    String[] subTagName = Arrays.copyOfRange(tagNames, 1000 * i, 1000 * (i + 1));
                    List<TsPoint> subList = thingsPointDao.findTsPointByIds(subTagName);
                    selectList.addAll(subList);
                }
            } else {

                for (int i = 0; i < selectCount; i++) {
                    String[] subTagName = Arrays.copyOfRange(tagNames, 1000 * i, 1000 * (i + 1));
                    List<TsPoint> subList = thingsPointDao.findTsPointByIds(subTagName);
                    selectList.addAll(subList);
                }
                //将数组剩余元素进行查询
                String[] lastTagName = Arrays.copyOfRange(tagNames, tagNames.length - remainder, tagNames.length);
                List<TsPoint> lastSubList = thingsPointDao.findTsPointByIds(lastTagName);
                selectList.addAll(lastSubList);
            }
        } else {
            selectList = thingsPointDao.findTsPointByIds(tagNames);
        }
        return selectList;
    }

    public TsPoint getTsPoint(String tagName) {

        TsPoint tsPoint = null;
        if (this.tagMap.containsKey(tagName)) {
            tsPoint = tagMap.get(tagName);
        }
        if (tsPoint != null) {
            return tsPoint;
        }
        tsPoint = thingsPointDao.findTsPointById(tagName);
        return tsPoint;
    }

    public List<TsPoint> getTsPoint(String thingsType, String thingsId, String... uniformCodes) {

        List<TsPoint> selectList = new ArrayList<>();

        //先从缓存中获取相关测点
        Map<String, TsPoint> thingsUniformCodeMap = null;
        if (windturbineTagMap.containsKey(thingsId)) {
            thingsUniformCodeMap = this.windturbineTagMap.get(thingsId);
        }
        if (thingsUniformCodeMap != null) {

       for (int i=0;i<uniformCodes.length;i++){
           if(thingsUniformCodeMap.containsKey(uniformCodes[i])){
               selectList.add(thingsUniformCodeMap.get(uniformCodes[i]));
           }
       }
        }
        //如果缓存中的数据大于0 则直接返回数据不进行数据库查询
        if (selectList.size() > 0) {
            return selectList;
        }
        if (uniformCodes.length > 1000) {
            int selectCount = uniformCodes.length / 1000;
            int remainder = uniformCodes.length % 1000;

            if (remainder == 0) {

                for (int i = 0; i < selectCount; i++) {
                    String[] uniformCodeArr = Arrays.copyOfRange(uniformCodes, 1000 * i, 1000 * (i + 1));
                    List<TsPoint> subList = thingsPointDao.findTsPointByUnformCodes(thingsType, thingsId, uniformCodeArr);
                    selectList.addAll(subList);
                }
            } else {

                for (int i = 0; i < selectCount; i++) {
                    String[] uniformCodeArr = Arrays.copyOfRange(uniformCodes, 1000 * i, 1000 * (i + 1));
                    List<TsPoint> subList = thingsPointDao.findTsPointByUnformCodes(thingsType, thingsId, uniformCodeArr);
                    selectList.addAll(subList);
                }
                //将数组剩余元素进行查询
                String[] lastUniformCode = Arrays.copyOfRange(uniformCodes, uniformCodes.length - remainder, uniformCodes.length);
                List<TsPoint> lastSubList = thingsPointDao.findTsPointByUnformCodes(thingsType, thingsId, lastUniformCode);
                selectList.addAll(lastSubList);
            }
        } else {
            selectList = thingsPointDao.findTsPointByUnformCodes(thingsType, thingsId, uniformCodes);
        }
        return selectList;

    }

    public TsPoint getTsPoint(String thingsType, String thingsId, String uniformCode) {

        TsPoint tsPoint=null;

        if(windturbineTagMap.containsKey(thingsId)){

            Map<String,TsPoint> mp=windturbineTagMap.get(thingsId);

            if(mp.containsKey(uniformCode)){
                tsPoint=mp.get(uniformCode);
            }
        }
        if(tsPoint!=null)
        {
            return tsPoint;
        }

        List<TsPoint> list = thingsPointDao.findTsPointByUnformCodes(thingsType, thingsId, uniformCode);
        return list.size() > 0 ? list.get(0) : null;
    }

    public void getAllTsPointToCache() {

        try {
            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);
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println("缓存所有测点发生异常");
        }
        System.out.println("测点缓存完毕");

    }

}
