package com.gyee.frame.util.task;


import com.gyee.frame.common.exception.QiNiuException;
import com.gyee.frame.common.exception.enums.QiNiuErrorEnum;
import com.gyee.frame.common.feign.IAdapterService;
import com.gyee.frame.model.auto.WindTurbineTestingPointAi;
import com.gyee.frame.model.custom.export.TsPointData;
import com.gyee.frame.service.WindTurbineTestingPointAiService;
import com.gyee.frame.util.DateUtils;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class TaskTemplateCallable implements Callable {

    private WindTurbineTestingPointAiService windService;
    private IAdapterService service;
    private List<String> codes;
    private String station;
    private String wtId;
    private long startTs;
    private long endTs;
    private int interval;


    public TaskTemplateCallable(WindTurbineTestingPointAiService windService, IAdapterService service, List<String> codes,
                                String station, String wtId, long startTs, long endTs, int interval) {
        this.windService = windService;
        this.service = service;
        this.codes = codes;
        this.station = station;
        this.wtId = wtId;
        this.startTs = startTs;
        this.endTs = endTs;
        this.interval = interval;
    }


    @Override
    public Map<String, List<Object>> call()  {

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

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

        try {
            for (String code : this.codes) {
                List<WindTurbineTestingPointAi> windPoints = this.windService.findPointsByUniformCodeAndStation(station, wtId, code);
                if (windPoints == null || windPoints.size() == 0)
                    continue;

                WindTurbineTestingPointAi windPoint = windPoints.get(0);
                List<TsPointData> data = this.service.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);
                }
            }

            map.put(this.wtId, list);

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

        return map;
    }


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

        return list;
    }

}
