package com.gyee.frame.util.mongodb;


import com.gyee.frame.common.spring.SpringUtils;
import com.gyee.frame.model.auto.WindPowerStationTestingPoint2;
import com.gyee.frame.model.auto.WindTurbineTestingPointAi2;
import com.gyee.frame.model.custom.DNAStatVal;
import com.gyee.frame.model.custom.DNAVal;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class MongodbUtilImpl implements IRealTimeDataBaseUtil {


    private JedisPoolUtil jedisPoolUtil= SpringUtils.getBean(JedisPoolUtil.class);
    private MongoDbUtil mdb = SpringUtils.getBean(MongoDbUtil.class);
    private static Logger logger = LoggerFactory.getLogger(MongoDbUtil.class);

    public PointData getRealData(WindPowerStationTestingPoint2 point) throws Exception {

        PointData po = jedisPoolUtil.getRealData(point.getCode());
        return po;
    }

    public List<PointData> getHistoryDatasSnap(WindPowerStationTestingPoint2 point, Long beginDate, Long endDate, Long count, Long pried) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));

        List<PointData> ls = mdb.getHistoryDatasSnap(point, beginDate, endDate, count, pried);

//        long endStamp = System.currentTimeMillis();
//        long cost = endStamp - startStamp;
//        logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//        logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
        return ls;
    }

    public List<PointData> getHistoryDatasSnap(String pointid, Long beginDate, Long endDate, Long count, Long pried) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));

        List<PointData> ls = mdb.getHistoryDatasSnap(pointid, beginDate, endDate, count, pried);


//        long endStamp = System.currentTimeMillis();
//        long cost = endStamp - startStamp;
//        logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//        logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
        return ls;
    }

    public List<PointData> getHistStat(WindTurbineTestingPointAi2 point, Long beginDate, Long endDate, Long count, Long pried, int type) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));

        List<PointData> ls = mdb.getHistStat(point, beginDate, endDate, count, pried, type);

//        long endStamp = System.currentTimeMillis();
//        long cost = endStamp - startStamp;
//        logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//        logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
        return ls;
    }

    public DNAStatVal[] getHistStat(String point, Long beginDate, Long endDate, Integer pried) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
        DNAStatVal[] statResult = mdb.getHistStat(point, beginDate, endDate, pried);

//        long endStamp = System.currentTimeMillis();
//        long cost = endStamp - startStamp;
//        logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//        logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
        return statResult;

    }

    public List<PointData> getHistStat(WindPowerStationTestingPoint2 point, Long beginDate, Long endDate, Long count, Long pried, int type) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));

        List<PointData> ls = mdb.getHistStat(point, beginDate, endDate, count, pried, type);

//        long endStamp = System.currentTimeMillis();
//        long cost = endStamp - startStamp;
//        logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//        logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
        return ls;
    }

    public List<PointData> getHistStat(String pointid, Long beginDate, Long endDate, Long count, Long pried, int type) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));

        List<PointData> ls = mdb.getHistStat(pointid, beginDate, endDate, count, pried, type);

//        long endStamp = System.currentTimeMillis();
//        long cost = endStamp - startStamp;
//        logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//        logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
        return ls;
    }

    public PointData getRealData1(WindTurbineTestingPointAi2 point) throws Exception {
//		long startStamp = System.currentTimeMillis();
//		logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
        PointData po = jedisPoolUtil.getRealData(point.getId());

//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");

        return po;
    }

    public PointData getRealData(WindTurbineTestingPointAi2 point) throws Exception {
        // long startStamp = System.currentTimeMillis();
        // logger.info("==================开始时刻: " +
        // DateUtils.parseLongToDate(startStamp));

        PointData po = jedisPoolUtil.getRealData(point.getId());

        // long endStamp = System.currentTimeMillis();
        // long cost = endStamp - startStamp;
        // logger.info("==================结束时刻: " +
        // DateUtils.parseLongToDate(endStamp));
        // logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");

        return po;
    }

    public List<PointData> getHistoryDatasSnap(WindTurbineTestingPointAi2 point, Long beginDate, Long endDate, Long count, Long pried) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));

        List<PointData> ls = mdb.getHistoryDatasSnap(point, beginDate, endDate, count, pried);

//        long endStamp = System.currentTimeMillis();
//        long cost = endStamp - startStamp;
//        logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//        logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
        return ls;
    }

    public PointData getRealData1(String pointid) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
        PointData po = jedisPoolUtil.getRealData(pointid);

        // long endStamp = System.currentTimeMillis();
        // long cost = endStamp - startStamp;
        // logger.info("==================结束时刻: " +
        // DateUtils.parseLongToDate(endStamp));
        // logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");

        return po;
    }

    public PointData getRealData(String pointid) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
        PointData po = jedisPoolUtil.getRealData(pointid);

        // long endStamp = System.currentTimeMillis();
        // long cost = endStamp - startStamp;
        // logger.info("==================结束时刻: " +
        // DateUtils.parseLongToDate(endStamp));
        // logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");

        return po;

    }

    /**
     * 一次读取多个点
     *
     * @param pointids
     * @return
     * @throws Exception
     */
    public List<PointData> getRealData(String... pointids) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
        List<PointData> list = jedisPoolUtil.getRealData(pointids);

        if (StringUtils.isEmpty(list)) {
            list = new ArrayList<PointData>();
        }

//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");

        return list;
    }

    public List<PointData> getRealData(List<String> pointids) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
        List<PointData> list = jedisPoolUtil.getRealData(pointids);

        if (StringUtils.isEmpty(list)) {
            list = new ArrayList<PointData>();
        }

//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
        return list;
    }

    /**
     * 一次读取多个点
     *
     * @param pointids
     * @return
     * @throws Exception
     */
    public Map<String, Double> getRealDataMap(String... pointids) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
        Map<String, Double> map = jedisPoolUtil.getRealDataMap(pointids);

        if (StringUtils.isEmpty(map)) {
            map = new HashMap<String, Double>();
        }

//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");

        return map;
    }


    public Map<String, Double> getRealDataMap(List<String> pointids) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
        Map<String, Double> map = jedisPoolUtil.getRealDataMap(pointids);

        if (StringUtils.isEmpty(map)) {
            map = new HashMap<String, Double>();
        }

//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");

        return map;
    }

    public List<PointData> getHistoryDatasRaw(WindTurbineTestingPointAi2 point, Long beginDate, Long endDate) throws Exception {
        //        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
        List<PointData> ls = mdb.getHistoryDatasRaw(point, beginDate, endDate);

//        long endStamp = System.currentTimeMillis();
//        long cost = endStamp - startStamp;
//        logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//        logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
        return ls;
    }

    public List<PointData> getHistoryDatasRaw(WindPowerStationTestingPoint2 point, Long beginDate, Long endDate) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
        List<PointData> ls = mdb.getHistoryDatasRaw(point, beginDate, endDate);

//        long endStamp = System.currentTimeMillis();
//        long cost = endStamp - startStamp;
//        logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//        logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
        return ls;
    }

    public List<PointData> getHistoryDatasRaw(String pointid, Long beginDate, Long endDate) throws Exception {
//        long startStamp = System.currentTimeMillis();
//        logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
        List<PointData> ls = mdb.getHistoryDatasRaw(pointid, beginDate, endDate);

//        long endStamp = System.currentTimeMillis();
//        long cost = endStamp - startStamp;
//        logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//        logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
        return ls;
    }

    /**
     * 更新单点实时数据
     *
     * @return
     * @throws Exception
     */
    public void updatePoint(PointData point) throws Exception {

        mdb.updatePoint(point, jedisPoolUtil.getJedisPool());
    }

    @Override
    public DNAVal[] getRealtimeTagValues(String... tagNames) throws Exception {
        return jedisPoolUtil.getRealtimeTagValues(tagNames);
    }

    @Override
    public void updatePoint(List<PointData> pointls) throws Exception {

        if(!pointls.isEmpty())
        {
            for(PointData point:pointls)
            {
                mdb.updatePoint(point, jedisPoolUtil.getJedisPool());
            }
        }
    }



    @Override
    public DNAVal[] getHistMatrix(String[] nameList, int tTime) throws Exception {

//        List<PointData> ls = mdb.getHistoryDatasSnap(pointid, beginDate, endDate, count, pried);

        return new DNAVal[0];
    }
}
