package com.cpp.web.service.accuracy;

import com.cpp.system.service.ISysConfigService;
import com.cpp.web.domain.accuracy.AccuracyPassRate;
import com.cpp.web.domain.station.ElectricField;
import com.cpp.web.domain.station.PowerStationStatusData;
import com.cpp.web.service.station.*;
import com.cpp.web.utils.DateTimeUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 准确路计算的主类
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CalcAccuracy {

    public final AccuracyPassRateService accuracyPassRateService;
    private final ElectricFieldService electricFieldService;
    private final ISysConfigService sysConfigService;
    private final PowerStationStatusDataService powerStationStatusDataService;
    private final List<CalculateInterface> calculateInterfaces;

    /**
     * 执行准确率计算
     */
    public void calculate() {
        log.info("-----------------开始执行准确率计算任务----------------------");
        //获取前一天的数据
        Long day = 86400000L;
        Long now = System.currentTimeMillis();

        Date startTime = DateTimeUtil.getDayStartTime(now - day);
        Date endTime = DateTimeUtil.getDayLastTime(now - day);
        List<ElectricField> electricFieldList = electricFieldService.list();
        List<AccuracyPassRate> accuracyPassRateList = new ArrayList<>();
        String province = sysConfigService.selectConfigByKey("province");

        //第一个集中预测作用于新疆，因为用了内部准确率计算包，所以需要省份的枚举标识，这里可以改为动态获取
        if (province.equals("")) province = "E65";
        if (!electricFieldList.isEmpty()) {
            List<PowerStationStatusData> powerStationStatusDataList = powerStationStatusDataService.findByTimeBetween(startTime, endTime);
            if (!powerStationStatusDataList.isEmpty()) {
                //定义了准确率计算的三种方式，同样可以改为动态获取
                List<String> formulaTypes = Arrays.asList("DAY_SHORT_ACCURACY", "POINT_SHORT_ACCURACY", "POINT_ULTRA_SHORT_ACCURACY");
                for (CalculateInterface calculateInterface : calculateInterfaces) {
                    if (listHasIntersection(calculateInterface.calculationTypes(), formulaTypes)) {
                        accuracyPassRateList.addAll(calculateInterface.calc(startTime, endTime, powerStationStatusDataList, electricFieldList, formulaTypes, province));
                    }
                }
            }
        }

        //如果存在准确率结果，则保存
        if (accuracyPassRateList.size() > 0) {
            accuracyPassRateService.saveBatch(accuracyPassRateList);
        }
        log.info("-----------------结束执行准确率计算任务----------------------");
    }


    /**
     * 集合交集判断（此方法还可以写得更好）
     *
     * @param strings
     * @param stringList
     * @return
     */
    public Boolean listHasIntersection(
            List<String> strings,
            List<String> stringList
    ) {
        for (String string : strings) {
            for (String s : stringList) {
                if (string.equals(s)) {
                    return true;
                }
            }
        }
        return false;
    }
}
