package com.cpp.web.controller.regulation;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpp.common.core.domain.R;
import com.cpp.common.utils.DateUtils;
import com.cpp.web.domain.station.ElectricField;
import com.cpp.web.domain.station.ForecastPowerShortTermRegulation;
import com.cpp.web.domain.station.PowerStationStatusData;
import com.cpp.web.domain.station.WeatherStationStatusData;
import com.cpp.web.dto.DqHistoryContrastDto;
import com.cpp.web.service.station.ElectricFieldService;
import com.cpp.web.service.station.ForecastPowerShortTermRegulationService;
import com.cpp.web.service.station.PowerStationStatusDataService;
import com.cpp.web.service.station.WeatherStationStatusDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


/**
 * 短期历史对比
 *
 * @author whc
 * @date 2022-03-18 15:49:50
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/dqHistoryContrastController")
public class DqHistoryContrastController {
    @Autowired
    ElectricFieldService electricFieldService;
    @Autowired
    PowerStationStatusDataService powerStationStatusDataService;
    @Autowired
    ForecastPowerShortTermRegulationService forecastPowerShortTermRegulationService;

    @ApiOperation(value = "根据条件查询", notes = "分页查询")
    @GetMapping("/queryTableData")
    public R queryTableData(String stationCode, Long startTime, Long endTime) {
        Map map = new HashMap();
        ElectricField electricField = electricFieldService.findByStationCode(stationCode);
        String stationName = electricField.getName();
        // 查询实际功率
        QueryWrapper<PowerStationStatusData> powerStationStatusDataWrapper = new QueryWrapper<>();
        powerStationStatusDataWrapper.between("time", new Date(startTime), new Date(endTime));
        powerStationStatusDataWrapper.eq("station_code", stationCode);
        List<PowerStationStatusData> powerStationStatusDataList = powerStationStatusDataService.list(powerStationStatusDataWrapper);
        Map<Long, PowerStationStatusData> sjMap = new HashMap<>();
        for (PowerStationStatusData powerStationStatusData : powerStationStatusDataList) {
            sjMap.put(powerStationStatusData.getTime().getTime(), powerStationStatusData);
        }
        // 查询上报短期
        List<ForecastPowerShortTermRegulation> forecastPowerShortTermRegulationList = forecastPowerShortTermRegulationService.findByForecastTimeBetweenAndForecastHowLongAgoAndStationCode(startTime, endTime, 1, stationCode);
        Map<Long, ForecastPowerShortTermRegulation> dqsbMap = new HashMap<>();
        for (ForecastPowerShortTermRegulation forecastPowerShortTermRegulation : forecastPowerShortTermRegulationList) {
            dqsbMap.put(forecastPowerShortTermRegulation.getTime().getTime(), forecastPowerShortTermRegulation);
        }
        // 整合页面列表字段
        Long momentTime = 15 * 60 * 1000L;
        List<DqHistoryContrastDto> tableList = new ArrayList();
        // 定义x轴底部时间
        List<String> xAxisBottomList = new ArrayList<>();
        // 定义短期偏差曲线数组
        List<Object> dqpcLineList = new ArrayList<>();
        // 定义短期偏差比曲线数组
        List<Object> ratioLineList = new ArrayList<>();


        for (Long tempTime = startTime; tempTime <= endTime; tempTime = tempTime + momentTime) {
            xAxisBottomList.add(DateUtils.parseDateToStr("MM-dd HH:mm",new Date(tempTime)));
            DqHistoryContrastDto dqHistoryContrastDto = new DqHistoryContrastDto();
            dqHistoryContrastDto.setStationName(stationName);
            // 设置时间
            String time = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm",new Date(tempTime));
            dqHistoryContrastDto.setTime(time);
            if (sjMap.get(tempTime)!=null){
                PowerStationStatusData powerStationStatusData = sjMap.get(tempTime);
                dqHistoryContrastDto.setOpenCapacity(powerStationStatusData.getOpenCapacity().toString());
                dqHistoryContrastDto.setRealValue(powerStationStatusData.getRealValue().toString());
            }
            if (dqsbMap.get(tempTime)!=null){
                ForecastPowerShortTermRegulation forecastPowerShortTermRegulation = dqsbMap.get(tempTime);
                dqHistoryContrastDto.setDqValue(forecastPowerShortTermRegulation.getFpValue().toString());
            }
            // 计算短期与实际功率偏差及偏差比
            if (StrUtil.isNotBlank(dqHistoryContrastDto.getDqValue()) && StrUtil.isNotBlank(dqHistoryContrastDto.getRealValue())){
                // 偏差（短期-实际功率）
                BigDecimal dqValue = new BigDecimal(dqHistoryContrastDto.getDqValue());
                BigDecimal realValue = new BigDecimal(dqHistoryContrastDto.getRealValue());
                dqHistoryContrastDto.setDqDeviation(dqValue.subtract(realValue).toString());
                BigDecimal ratio = dqValue.subtract(realValue).divide(realValue,3,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).stripTrailingZeros();
                String ratioStr = "";
                if (ratio.compareTo(new BigDecimal("0"))>=0){
                    ratioStr = "+" + ratio.toPlainString() + "%";
                }
                else{
                    ratioStr = ratio.toPlainString() + "%";
                }
                dqHistoryContrastDto.setDqDeviationRatio(ratioStr);

                dqpcLineList.add(dqHistoryContrastDto.getDqDeviation());
                ratioLineList.add(ratio);
            }
            else{
                dqpcLineList.add("");
                ratioLineList.add("");
                dqHistoryContrastDto.setDqDeviation("");
                dqHistoryContrastDto.setDqDeviationRatio("");
            }
            tableList.add(dqHistoryContrastDto);
        }
        map.put("tableList",tableList);
        map.put("dqpcLineList",dqpcLineList);
        map.put("ratioLineList",ratioLineList);
        map.put("xAxisBottomList",xAxisBottomList);

        return R.ok(map);

    }
}
