package com.jiayue.ipfcst.controller;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.entity.ElectricField;
import com.jiayue.ipfcst.common.data.entity.ForecastPowerShortTerm;
import com.jiayue.ipfcst.common.data.entity.Nwp;
import com.jiayue.ipfcst.service.ElectricFieldService;
import com.jiayue.ipfcst.service.ForecastPowerShortTermService;
import com.jiayue.ipfcst.service.NwpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短期预测信息restful接口
 *
 * @author yh
 * @version 1.0
 * @since 2019/8/7 10:12
 */
@RestController
@Slf4j
public class ForecastPowerShortTermController {
  private final ForecastPowerShortTermService forecastPowerShortTermService;
  private final ElectricFieldService electricFieldService;
  private final NwpService nwpService;
  @Autowired
  public ForecastPowerShortTermController(ForecastPowerShortTermService forecastPowerShortTermService, ElectricFieldService electricFieldService, NwpService nwpService){
    this.forecastPowerShortTermService = forecastPowerShortTermService;
    this.electricFieldService = electricFieldService;
    this.nwpService = nwpService;
  }

  /**
   * 按时间查询实时预测短期 yh
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @return 结果集
   */
  @GetMapping(value = "/forecastPowerShortTerm/{startTime}/{endTime}")
  public ResponseVO findByForecastTimeBetween(@PathVariable("startTime") Long startTime,
                                          @PathVariable("endTime") Long endTime){
    Map<String,Object> map = new HashMap<>();
    try{
      map = forecastPowerShortTermService.findByForecastTimeBetween(startTime,endTime);
      return ResponseVO.success(map);
    }catch(Exception e){
      e.printStackTrace();
      return ResponseVO.fail(e.getMessage());
    }
  }

  /**
   * 分页查询  实时短期预测
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @param page 页码
   * @param size 条数
   * @param sortOrder 排序
   * @return
   */
  @GetMapping(value = "/forecastPowerShortTerm/{startTime}/{endTime}/{page}/{size}")
  public ResponseVO findByTimeBetweenForPaging(@PathVariable("startTime") Long startTime,
                                               @PathVariable("endTime") Long endTime,
                                               @PathVariable("page") Integer page,
                                               @PathVariable("size") Integer size,
                                               String sortOrder){
    Map<String,Object> map = new HashMap<>();
    try{
      map =  forecastPowerShortTermService.findByTimeBetweenAndNoForPaging(startTime,endTime,page,size,sortOrder);
      return ResponseVO.success(map);
    }catch(Exception e){
      e.printStackTrace();
      return ResponseVO.fail(e.toString());
    }
  }
  /**
   *根据条件查询
   * @param startTimeStr 开始时间
   * @return map
   */
  @GetMapping(value = "/forecastPowerShortTerm/getElectricEnergyTotal/{startTimeStr}")
  public ResponseVO getElectricEnergyTotal(@PathVariable("startTimeStr") Long startTimeStr) throws BusinessException {

    Map<String,Map<String,String>> map = new HashMap<>();
    Map<String,String> mapDay = new HashMap<>();
    Map<String,String> nwpDay = new HashMap<>();
    Map<String,String> mapDate = new HashMap<>();
    Map<String,String> type = new HashMap<>();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    BigDecimal radio = new BigDecimal(0.025);
    Date date;
    String dateStr;
    String[] datas;
    int a;
    BigDecimal sumValue;
    BigDecimal sumNwp;
    ElectricField electricField = electricFieldService.get();
    List<ForecastPowerShortTerm> sendDataForTimeBetween = forecastPowerShortTermService.findSendDataForTimeBetween(startTimeStr, startTimeStr + 1000 * 60 * 60 * 24 * 7 - 1000 * 60 * 15);
    //累计发电量公式  发电量= 预测值*0.25*0.1  累加96个点的发电量得出1天的累计发电量
    for(int i = 1;i<=7;i++){
      String key = "day"+i;
      String nwpKey = "nwpKey"+i;
      sumValue = new BigDecimal(0);
      sumNwp = new BigDecimal(0);
      date = new Date(startTimeStr+1000*60*60*24*(i-1));
      dateStr = sdf.format(date);
      mapDate.put("date"+i,dateStr);
      datas = forecastPowerShortTermService.findSendData(startTimeStr+1000*60*60*24*(i-1),startTimeStr+1000*60*60*24*i-1000*60*15,sendDataForTimeBetween);
      List<Nwp> nwpList = nwpService.findByTimeBetween(startTimeStr+1000*60*60*24*(i-1),startTimeStr+1000*60*60*24*i-1000*60*15);
      //预测发电量
      if(datas.length>0){
        BigDecimal value;
        for(String s:datas){
          if(s!=null){
            value = new BigDecimal(s).multiply(radio).setScale(3, BigDecimal.ROUND_HALF_UP);
            sumValue = sumValue.add(value);
          }
        }
        mapDay.put(key,sumValue.toString());
      }else{
        mapDay.put(key,"0");
      }
      //预测平均风速/预测辐射值
      if(nwpList.size()>0){
        BigDecimal nwp = new BigDecimal(0);
        if(electricField.getElectricFieldTypeEnum().getCode() == 1){
          //光
          //辐射累计值=sum累加（辐射瞬时值*1*60/10的六次方），计算的时间分辨率是1分钟。
          for(Nwp n : nwpList){
            nwp = n.getSwr().multiply(new BigDecimal(15)).multiply(new BigDecimal(60)).divide(new BigDecimal(1000000), 2, BigDecimal.ROUND_HALF_UP);
           sumNwp = sumNwp.add(nwp);
          }
        nwpDay.put(nwpKey,sumNwp.toString());
        }else {
          //风
          //风速平均值
        for (Nwp n : nwpList){
          nwp = nwp.add(n.getWs100());
        }
        sumNwp = nwp.divide(BigDecimal.valueOf(nwpList.size()),2, BigDecimal.ROUND_HALF_UP);
        nwpDay.put(nwpKey,sumNwp.toString());
        }
      }else{
        nwpDay.put(nwpKey,"0");
      }

    }
    type.put("elType",electricField.getElectricFieldTypeEnum().getCode().toString());
    map.put("nwpDay",nwpDay);
    map.put("nwpDate",mapDate);
    map.put("mapDay",mapDay);
    map.put("mapDate",mapDate);
    map.put("type",type);
    return ResponseVO.success(map);

  }

}
