package com.jiayue.ipfcst.controller;

import cn.hutool.core.date.DateUtil;
import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.entity.ElectricField;
import com.jiayue.ipfcst.common.data.entity.ForecastPowerThroughoutTheYear;
import com.jiayue.ipfcst.service.ElectricFieldService;
import com.jiayue.ipfcst.service.ForecastPowerThroughoutTheYearService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 全年功率预测restful接口
 *
 * @author shd
 * @since 2022-03-31
 */
@RestController
@Slf4j
@RequiredArgsConstructor
public class ForecastPowerThroughoutTheYearController {
  private final ForecastPowerThroughoutTheYearService forecastPowerThroughoutTheYearService;
  private final ElectricFieldService electricFieldService;

  /**
   * @param startTime
   * @return
   */
  @GetMapping(value = "/getThroughoutTheYearList/{startTime}")
  public ResponseVO getAll(@PathVariable("startTime") Long startTime) throws BusinessException {

    ElectricField electricField = electricFieldService.get();

    int month = DateMomentUtil.getMonth(startTime);
    int year = DateUtil.year(DateUtil.date(startTime));
    String querryYear = String.valueOf(year);
    String querryMonth = String.valueOf(month);
    //展示数据集合（实体为全年预测功率）
    List<ForecastPowerThroughoutTheYear> forecastPowerThroughoutTheYearListBetween = forecastPowerThroughoutTheYearService.findByYearMonthLike("%" + querryYear + "%");
    List<ForecastPowerThroughoutTheYear> forecastPowerThroughoutTheYearList = new ArrayList<>();
    for (ForecastPowerThroughoutTheYear forecastPowerThroughoutTheYear : forecastPowerThroughoutTheYearListBetween) {
      if ((forecastPowerThroughoutTheYear.getYearMonth().split(","))[1].equals(querryMonth)) {
        forecastPowerThroughoutTheYearList.add(forecastPowerThroughoutTheYear);
      }
    }
    if (forecastPowerThroughoutTheYearList.isEmpty()) {
      forecastPowerThroughoutTheYearList = forecastPowerThroughoutTheYearService.ForecastPowerThroughoutTheYear(startTime,electricField);
    }
    //返回map
    Map<String, Object> map = new HashMap<>();
    //预测功率map
    Map<String, String> mapDay = new HashMap<>();
    //日期
    Map<String, String> mapData = new HashMap<>();
    //风速map
    Map<String, String> nwpDay = new HashMap<>();
    //总功率
    BigDecimal totalNwp = new BigDecimal(0);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
    String dataTime;
    for (int i = 0; i <= 12; i++) {
      dataTime = dateFormat.format(DateUtils.addMonths(new Date(startTime), i).getTime());
      mapDay.put("day" + i, dataTime);
    }
    for (int j = 0; j < forecastPowerThroughoutTheYearList.size(); j++) {
      if (forecastPowerThroughoutTheYearList.get(j).getValue() == null) {
        forecastPowerThroughoutTheYearList.get(j).setValue(new BigDecimal(0));
      }
      if (forecastPowerThroughoutTheYearList.get(j).getFpValue() == null) {
        forecastPowerThroughoutTheYearList.get(j).setFpValue(new BigDecimal(0));
      }
      mapData.put("data" + j, forecastPowerThroughoutTheYearList.get(j).getFpValue().toString());
      nwpDay.put("nwpKey" + j, forecastPowerThroughoutTheYearList.get(j).getValue().toString());
      totalNwp = forecastPowerThroughoutTheYearList.get(j).getFpValue().add(totalNwp);
    }
    mapData.put("totalNwp", totalNwp.toString());
    try {
      map.put("mapDay", mapDay);
      map.put("mapData", mapData);
      map.put("nwpDay", nwpDay);
      map.put("ele", electricField.getElectricFieldTypeEnum().name());
      return ResponseVO.success(map);
    } catch (Exception e) {
      e.printStackTrace();
      return ResponseVO.fail(e.getMessage());
    }
  }


  /**
   * 调用预测方法（重新预测）
   *
   * @return
   */
  @GetMapping(value = "/reForecast")
  public ResponseVO reForecast() throws BusinessException {

    forecastPowerThroughoutTheYearService.ForecastPowerThroughoutTheYearCron();
    return ResponseVO.success();
  }

}
