package com.gyee.frame.service.export;

import com.gyee.frame.common.conf.ExportConfig;
import com.gyee.frame.common.conf.ThreadPoolTaskConfig;
import com.gyee.frame.common.exception.QiNiuException;
import com.gyee.frame.common.exception.enums.QiNiuErrorEnum;
import com.gyee.frame.common.feign.RemoteServiceBuilder;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.auto.WindpowerstationExample;
import com.gyee.frame.model.custom.export.TsPointData;
import com.gyee.frame.service.AnalysissubtablebottomService;
import com.gyee.frame.service.WindTurbineTestingPointAiService;
import com.gyee.frame.service.WindpowerstationService;
import com.gyee.frame.service.WindturbineService;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.excel.ExcelExportUtils;
import com.gyee.frame.util.excel.FileUtils;
import com.gyee.frame.util.task.TaskTemplateCallable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Future;
import java.util.stream.Collectors;


@Slf4j
@Service
public class GoldenService {


    @Autowired
    private ExportConfig exportConfig;
    @Autowired
    private ThreadPoolTaskConfig taskConfig;
    @Autowired
    private RemoteServiceBuilder remoteService;
    @Autowired
    private WindturbineService windturbineService;
    @Autowired
    private WindTurbineTestingPointAiService windService;
    @Autowired
    private WindpowerstationService windpowerstationService;

    @Autowired
    private AnalysissubtablebottomService analysissubtablebottomService;

    @Autowired
    private FileUtils fileService;


    /**
     * 一天的时间戳
     */
    public static final Integer TIMES_TAMP_DAY = 86400;


    /**
     * 根据场站和风机ID查询
     *
     * @param station
     * @param wtId
     * @param templateId
     * @param startTs
     * @param endTs
     * @param interval
     * @return
     */
//    public List<Object> getHistoryDataSingle(String station, String wtId, int templateId, long startTs, long endTs, int interval) {
//        switch (templateId) {
//            case 1:
//                return getTemplateHistory1(station, wtId, startTs, endTs, interval);
//            default:
//                throw new QiNiuException(QiNiuErrorEnum.TEMPLATE_NO_SUPPORT);
//        }
//    }


    /**
     * 查询所有风机
     *
     * @param station
     * @param startTs
     * @param endTs
     * @return
     */
    public boolean getHistoryDataAll(String station,long startTs, long endTs) {

        List<Integer>  val  =  exportConfig.getExportval();
        WindpowerstationExample example = new WindpowerstationExample();
        example.createCriteria().andIdLike("%"+station+"%");
        List<Windpowerstation> lists = windpowerstationService.selectByExample(example);
        for(int i :val){
            for (Windpowerstation st: lists) {
                getTemplateHistory1(st.getCode(),st.getAddress(),startTs,endTs,i*60);
                getTemplateHistory2(st.getCode(),st.getAddress(),startTs,endTs,i*60);
            }
        }
        return true;



    }



    @Scheduled(cron = "0 0 1 * * ?")
//    @Scheduled(cron = "0/1 * * * * *")
    public void getHistoryData() {

        Calendar c = Calendar.getInstance();
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)-1,0, 0, 0);
        long beginOfDate = c.getTime().getTime();
        c = Calendar.getInstance();
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH),0, 0, 0);
        long endOfDate = c.getTime().getTime();

        List<Integer>  val  =  exportConfig.getExportval();
        WindpowerstationExample example = new WindpowerstationExample();
        example.createCriteria().andIdLike("%_FDC%");
        List<Windpowerstation> lists = windpowerstationService.selectByExample(example);
//        beginOfDate = 1635696000000L;
//        endOfDate = 1637596800000L;
        for(int i :val){
            for (Windpowerstation station: lists) {
                getTemplateHistory1(station.getCode(),station.getAddress(),beginOfDate,endOfDate,i*60);
                getTemplateHistory2(station.getCode(),station.getAddress(),beginOfDate,endOfDate,i*60);
            }
        }
    }




    /**
     * 模板1  场站所有风机数据
     *
     * @param station
     * @param startTs
     * @param endTs
     * @param interval
     * @return
     */
    private Map<String, Map<String, List<Object>>> getTemplateHistory1(String station, String address,long startTs, long endTs, int interval) {

        String val = Integer.valueOf(interval / 60) + "";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startTss = dateFormat.format(startTs);
        // 风机ID、数据
        Map<String, Map<String, List<Object>>> map = new LinkedHashMap<>();
        try {
            List<String> winds = windturbineService.findWindTurbineAll(station);
            if (winds == null)
                return null;
            // 异步返回结果
            List<Future<Map<String, Map<String, List<Object>>>>> list = new LinkedList<>();
            for (String wind : winds) {
                TaskTemplateCallable task = new TaskTemplateCallable(windService, remoteService.ShardingService(),
                        exportConfig.getTemplate1(), station, wind, startTs, endTs, interval);
                Future submit = taskConfig.getExecutor().submit(task);
                list.add(submit);
            }
            // 返回结果处理
            for (Future<Map<String, Map<String, List<Object>>>> future : list) {
                Map<String, Map<String, List<Object>>> wtMap = future.get();
                for (Map.Entry<String, Map<String, List<Object>>> entry : wtMap.entrySet()) {
                    map.put(entry.getKey(), entry.getValue());
                    List<String> title = exportConfig.getTemplate1title();
                    ExcelExportUtils.exelcExportTem(entry.getValue(), entry.getKey(), title, exportConfig.getExportpath() + File.separator + startTss.substring(0, 7) + File.separator + val + "分钟" + File.separator + address + "单机" + File.separator, entry.getKey().replace("01_", ""));
                }
            }
        } catch (Exception e) {
            log.error("error", e);
            throw new QiNiuException(QiNiuErrorEnum.ERROR_DATA);
        }
        return map;
    }


    /**
     * 模板1  单机数据
     *
     * @param station
     * @param wtId
     * @param startTs
     * @param endTs
     * @param interval
     * @return
     */
//    private List<Object> getTemplateHistory1(String station, String wtId, long startTs, long endTs, int interval) {
//        List<Object> list = new ArrayList<>();
//        List<String> codes = exportConfig.getTemplate1();
//
//        // 第一列添加时间
//        List<String> intervals = dateInterval(startTs, endTs, interval);
//        list.add(intervals);
//
//        try {
//            for (String code : codes) {
//                List<WindTurbineTestingPointAi2> windPoints = windService.findPointsByUniformCodeAndStation(station, wtId, code);
//                if (windPoints == null || windPoints.size() == 0)
//                    continue;
//
//                WindTurbineTestingPointAi2 windPoint = windPoints.get(0);
//                List<TsPointData> data = remoteService.ShardingService().getHistorySnap(windPoint.getId(), startTs, endTs, interval);
//
//                List<Object> collect = new ArrayList<>();
//                // 故障状态或限电状态都是整型0或1
//                if (code.equals("GZZT") || code.equals("XDZT")) {
//                    if (data.size() == 0) {
//                        for (int i = 0; i < intervals.size(); i++)
//                            collect.add(0);
//                    } else {
//                        collect = data.stream().map(point -> (int) point.getDoubleValue()).collect(Collectors.toList());
//                    }
//                    list.add(collect);
//                } else {
//                    if (data.size() == 0) {
//                        for (int i = 0; i < intervals.size(); i++)
//                            collect.add(0.0);
//                    } else {
//                        collect = data.stream().map(TsPointData::getDoubleValue).collect(Collectors.toList());
//                    }
//                    list.add(collect);
//                }
//            }
//        } catch (QiNiuException e) {
//            log.error(e.getMessage());
//            throw new QiNiuException(QiNiuErrorEnum.ERROR_DATA);
//        }
//
//        return list;
//    }


    /**
     * 模板2  场站所有风机数据  日上网电量、上网功率、测风塔数据
     *
     * @param station
     * @param startTs
     * @param endTs
     * @return
     */
    private Map<String, List<Object>> getTemplateHistory2(String station,String address, long startTs, long endTs, int interval) {

        String val = Integer.valueOf(interval / 60) + "";
        // 风机ID、数据
        Map<String, List<Object>> map = new LinkedHashMap<>();
        try {

            // 上网电量
            String point = exportConfig.getTemplate2swdl().get(station);
            String[] points = point.split(",");
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String startTss = dateFormat.format(startTs);
            String endTss = dateFormat.format(endTs);
            List<Map> data = analysissubtablebottomService.selectByRecorddateGrop(startTss, endTss, station, points);
            List<Object> list = new ArrayList<>();
            list.add(data.stream().map(p -> dateFormat.format(p.get("RECORDDATE"))).collect(Collectors.toList()));
            list.add(data.stream().map(p -> Double.valueOf(p.get("SWDL").toString())).collect(Collectors.toList()));
            List<String> swdltitle = exportConfig.getTemplate2swdltitle();
            Map<String, List<Object>> swdlVos = new HashMap<>();
            swdlVos.put("F1", data.stream().map(p -> dateFormat.format(p.get("RECORDDATE"))).collect(Collectors.toList()));
            swdlVos.put("F2", data.stream().map(p -> Double.valueOf(p.get("SWDL").toString())).collect(Collectors.toList()));
            ExcelExportUtils.exelcExportTem(swdlVos, "日上网电量", swdltitle, exportConfig.getExportpath() + File.separator + startTss.substring(0, 7) + File.separator + val + "分钟"+File.separator, address+"测风塔");
            map.put("swdl", list);

            // 上网功率
            String point2 = exportConfig.getTemplate2swgl().get(station);
            List<TsPointData> data2 = remoteService.ShardingService().getHistorySnap(point2, startTs, endTs, interval);
            List<Object> list2 = new ArrayList<>();
            list2.add(data2.stream().map(p -> DateUtils.format(p.getTs(), DateUtils.DATE_TIME_PATTERN)).collect(Collectors.toList()));
            list2.add(data2.stream().map(TsPointData::getDoubleValue).collect(Collectors.toList()));
            Map<String, List<Object>> swglVos = new HashMap<>();
            swglVos.put("F1", data2.stream().map(p -> DateUtils.format(p.getTs(), DateUtils.DATE_TIME_PATTERN)).collect(Collectors.toList()));
            swglVos.put("F2", data2.stream().map(TsPointData::getDoubleValue).collect(Collectors.toList()));
            List<String> swgltitle = exportConfig.getTemplate2swgltitle();
            ExcelExportUtils.exelcExportTem(swglVos, "上网功率", swgltitle, exportConfig.getExportpath() + File.separator + startTss.substring(0, 7) + File.separator + val + "分钟"+File.separator, address+"测风塔");
            map.put("swgl", list2);

            // 测风塔数据
            String point3 = exportConfig.getTemplate2cft().get(station);
            Map<String, List<Object>> cftlVos = getTemplateHistory2(startTs, endTs, interval, point3);
            List<String> cftltitle = exportConfig.getTemplate2cfttitle();
            ExcelExportUtils.exelcExportTem(cftlVos, "测风塔数据", cftltitle, exportConfig.getExportpath() + File.separator + startTss.substring(0, 7) + File.separator + val + "分钟"+File.separator, address+"测风塔");
        } catch (Exception e) {
            log.error("error", e);
            throw new QiNiuException(QiNiuErrorEnum.ERROR_DATA);
        }
        return map;
    }


    /**
     * 模板2  场站数据
     *
     * @param startTs
     * @param endTs
     * @param interval
     * @param points
     * @return
     */
    private Map<String, List<Object>> getTemplateHistory2(long startTs, long endTs, int interval, String points) {
        List<Object> list = new ArrayList<>();

        Map<String, List<Object>> map = new HashMap<>();


        // 第一列添加时间
        List<Object> intervals = dateInterval(startTs, endTs, interval);
        list.add(intervals);
        map.put("F1", intervals);

        try {
            String[] splits = points.split(",");
            int num = 2;
            for (String point : splits) {
                List<TsPointData> data = remoteService.ShardingService().getHistorySnap(point, startTs, endTs, interval);

                List<Object> collect = new ArrayList<>();
                if (data.size() == 0) {
                    for (int i = 0; i < intervals.size(); i++)
                        collect.add(0);
                } else {
                    collect = data.stream().map(TsPointData::getDoubleValue).collect(Collectors.toList());
                }
                list.add(collect);
                map.put("F" + num, collect);
                num++;
            }

        } catch (QiNiuException e) {
            log.error(e.getMessage());
            throw new QiNiuException(QiNiuErrorEnum.ERROR_DATA);
        }

        return map;
    }


    /**
     * 分割时间
     *
     * @param st
     * @param et
     * @param interval
     * @return
     */
    private List<Object> dateInterval(long st, long et, int interval) {
        List<Object> list = new ArrayList<>();
        int val = interval * 1000;
        while (st < et) {
            list.add(DateUtils.format(st, DateUtils.DATE_TIME_PATTERN));
            st += val;
        }

        return list;
    }

}
