package com.cpp.web.controller.dayPowerGeneration;

import com.cpp.common.core.domain.R;
import com.cpp.web.domain.powerGeneration.DayPowerGeneration;
import com.cpp.web.service.powerGeneation.DayPowerGenerationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequiredArgsConstructor
@RequestMapping("/dayPowerGeneration")
@Api(value = "dayPowerGeneration", tags = "cpp_day_power_generation管理")
public class DayPowerGenerationController {

    private final DayPowerGenerationService dayPowerGenerationService;

    @ApiOperation(value = "根据场站编号查询预测发电量", notes = "根据场站编号查询预测发电量")
    @GetMapping(value = "/getList")
    public R getList(String stationCode,Long genDate,String predictionDataSource) {
        String useDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date(genDate));
        List<DayPowerGeneration> dayPowerGenerationList = dayPowerGenerationService.findByStationCodeAndGenDateAndPredictionDataSource(stationCode,useDate,predictionDataSource);
        List<DayPowerGeneration> ydList = dayPowerGenerationList.stream().filter(f -> f.getPredictionDataSource().equals("云端下发")).collect(Collectors.toList());
        List<DayPowerGeneration> zdList = dayPowerGenerationList.stream().filter(f -> f.getPredictionDataSource().equals("站端上传")).collect(Collectors.toList());
        List<PowerGenerationDto> generationDtoList = new ArrayList<>();
        for (DayPowerGeneration dayPowerGeneration : ydList){
            PowerGenerationDto powerGenerationDto = new PowerGenerationDto();
            powerGenerationDto.setTime(dayPowerGeneration.getTime());
            powerGenerationDto.setStationCode(dayPowerGeneration.getStationCode());
            powerGenerationDto.setForecastPowerGenerationYd(dayPowerGeneration.getForecastPowerGeneration());
            for (int i = 0; i < zdList.size(); i++) {
                if (dayPowerGeneration.getTime().equals(zdList.get(i).getTime())){
                    powerGenerationDto.setForecastPowerGenerationZd(zdList.get(i).getForecastPowerGeneration());
                }
            }
            generationDtoList.add(powerGenerationDto);
        }
        return R.ok(generationDtoList);
    }
    @Data
    public class PowerGenerationDto {

        private String time;

        private String stationCode;

        private BigDecimal forecastPowerGenerationZd;

        private BigDecimal forecastPowerGenerationYd;

    }
}
