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

import com.gyee.wisdom.common.data.timeseries.*;
import com.gyee.wisdom.common.exception.WisdomException;
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 java.util.*;


@Service
@Slf4j
public class TsDataService {


    @Autowired
    private ILatestDao latestDao;

    @Autowired
    private IHistoryDao historyDao;

    @Autowired
    private TsPointService tsPointService;


    public Map<String, TsData> getLatest(String... tagNames) {
        try {
            List<TsPoint> tsPoints = tsPointService.getTsPoint(tagNames);
            Map<String, TsData> result = latestDao.getTsDataLatest(tsPoints);
            return result;
        } catch (WisdomException e) {
            log.error("异常点：" + tagNames.toString());
            log.error(e.getMessage());
        } catch (Exception e) {
            log.error("异常点：" + tagNames.toString());
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param thingType
     * @param thingId
     * @param uniformCodes
     * @return 返回值Map中key为统一编码，value为TsData
     * @author wanghs
     * @date 2018-10-23
     */
    public Map<String, TsData> getLatestByUniformCodes(String thingType, String thingId, String... uniformCodes) {
        try {
            List<TsPoint> pointList = tsPointService.getTsPoint(thingType, thingId, uniformCodes);
            Map<String, TsData> dataMap = latestDao.getTsDataLatest(pointList);
            Map<String, TsData> resultMap = new HashMap<>();
            for (Map.Entry<String, TsData> entry : dataMap.entrySet()) {
                for (int i = 0; i < pointList.size(); i++) {
                    if (entry.getKey().equals(pointList.get(i).getId()))
                        resultMap.put(pointList.get(i).getUniformCode(), entry.getValue());
                }
            }
            return resultMap;
        } catch (WisdomException e) {
            log.error("异常统一编码：" + uniformCodes.toString());
            log.error(e.getMessage());
        } catch (Exception e) {
            log.error("异常统一编码：" + uniformCodes.toString());
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public List<TsData> getHistoryRaw(String tagName, long startTs, long endTs) {
        try {
            TsPoint tsPoint = tsPointService.getTsPoint(tagName);
            TsQuery tsQuery = new BaseTsQuery(tsPoint, startTs, endTs);
            //long t1 = new Date().getTime();
            List<TsData> obj = historyDao.getTsDataHistory(tsQuery);
            //long t2 = new Date().getTime();
            //System.out.println("tsxxxxxxxxxxxxx=" + (t2-t1));
            return obj;
        } catch (WisdomException e) {
            log.error("异常点：" + tagName);
            log.error(e.getMessage());
        } catch (Exception e) {
            log.error("异常点：" + tagName);
            log.error(e.getMessage());
            e.printStackTrace();
        }

        return null;
    }

    public List<TsData> getHistoryRaw(String thingType, String thingId, String uniformCode, long startTs, long endTs) {
        try {
            TsPoint tsPoint = tsPointService.getTsPoint(thingType, thingId, uniformCode);
            TsQuery tsQuery = new BaseTsQuery(tsPoint, startTs, endTs);
            return historyDao.getTsDataHistory(tsQuery);
        } catch (WisdomException e) {
            log.error("异常统一编码：" + uniformCode);
            log.error(e.getMessage());
        } catch (Exception e) {
            log.error("异常统一编码：" + uniformCode);
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }


    public List<TsData> getHistorySnap(String tagName, long startTs, long endTs, int interval) {
        try {
            TsPoint tsPoint = tsPointService.getTsPoint(tagName);
            TsQuery tsQuery = new BaseTsQuery(tsPoint, startTs, endTs, interval, 0, Interpolation.SNAP);
            return historyDao.getTsDataHistory(tsQuery);
        } catch (WisdomException e) {
            log.error("异常点：" + tagName);
            log.error(e.getMessage());
        } catch (Exception e) {
            log.error("异常点：" + tagName);
            log.error(e.getMessage());
            e.printStackTrace();
        }

        return null;
    }

    public List<TsData> getHistorySnap(String thingType, String thingId, String uniformCode, long startTs, long endTs, int interval) {
        try {
            TsPoint tsPoint = tsPointService.getTsPoint(thingType, thingId, uniformCode);
            TsQuery tsQuery = new BaseTsQuery(tsPoint, startTs, endTs, interval, 0, Interpolation.SNAP);
            return historyDao.getTsDataHistory(tsQuery);
        } catch (WisdomException e) {
            log.error("异常统一编码：" + uniformCode);
            log.error(e.getMessage());
        } catch (Exception e) {
            log.error("异常统一编码：" + uniformCode);
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public List<DoubleStatData> getHistoryStat(String tagName, long startTs, long endTs, int interval) {
        try {
            TsPoint tsPoint = tsPointService.getTsPoint(tagName);
            TsQuery tsQuery = new BaseTsQuery(tsPoint, startTs, endTs, interval, 0, Interpolation.SNAP);
            return historyDao.getDoubleStatDataHistory(tsQuery);
        } catch (WisdomException e) {
            log.error("异常点：" + tagName);
            log.error(e.getMessage());
        } catch (Exception e) {
            log.error("异常点：" + tagName);
            log.error(e.getMessage());
            e.printStackTrace();
        }

        return null;
    }

    public List<DoubleStatData> getHistoryStat(String thingType, String thingId, String uniformCode, long startTs, long endTs, int interval) {
        try {
            TsPoint tsPoint = tsPointService.getTsPoint(thingType, thingId, uniformCode);
            TsQuery tsQuery = new BaseTsQuery(tsPoint, startTs, endTs, interval, 0, Interpolation.SNAP);
            return historyDao.getDoubleStatDataHistory(tsQuery);
        } catch (WisdomException e) {
            log.error("异常统一编码：" + uniformCode);
            log.error(e.getMessage());
        } catch (Exception e) {
            log.error("异常统一编码：" + uniformCode);
            log.error(e.getMessage());
            e.printStackTrace();
        }

        return null;
    }

    public boolean writeHistory(List<TsPointData> dataList) throws Exception {
        return historyDao.writeHistoryValue(dataList);
    }

    public boolean writeLatest(List<TsPointData> dataList) throws Exception {
        return latestDao.writeLatest(dataList);
    }

    public Map<String, TsData> getHistorySection(long ts, String... tagNames) {
        try {
            List<TsPoint> tsPoints = tsPointService.getTsPoint(tagNames);
            Map<String, TsData> result = historyDao.getHistorySection(tsPoints, ts);
            return result;
        } catch (WisdomException e) {
            log.error("异常点：" + tagNames.toString());
            log.error(e.getMessage());
        } catch (Exception e) {
            log.error("异常点：" + tagNames.toString());
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public Map<String, TsData> getHistorySection(long ts, String thingId, String thingType, String... uniformCode) throws Exception {
        try {
            List<TsPoint> pointList = tsPointService.getTsPoint(thingType, thingId, uniformCode);
            Map<String, TsData> dataMap = historyDao.getHistorySection(pointList, ts);
            Map<String, TsData> resultMap = new HashMap<>();
            for (Map.Entry<String, TsData> entry : dataMap.entrySet()) {
                for (int i = 0; i < pointList.size(); i++) {
                    if (entry.getKey() == pointList.get(i).getId())
                        resultMap.put(pointList.get(i).getUniformCode(), entry.getValue());
                }
            }
            return resultMap;
        } catch (WisdomException e) {
            log.error("异常统一编码：" + uniformCode.toString());
            log.error(e.getMessage());
        } catch (Exception e) {
            log.error("异常统一编码：" + uniformCode.toString());
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

}
