package com.cpp.web.controller.stationDataQuery;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cpp.common.core.domain.R;
import com.cpp.web.domain.station.ElectricField;
import com.cpp.web.domain.station.NwpStation;
import com.cpp.web.service.station.ElectricFieldService;
import com.cpp.web.service.station.NwpStationService;
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.*;

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


/**
 * idp_nwp
 *
 * @author whc
 * @date 2022-03-18 15:49:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/nwp")
@Api(value = "nwp", tags = "idp_nwp管理")
public class NwpStationController {
    @Autowired
    NwpStationService nwpStationService;
    @Autowired
    ElectricFieldService electricFieldService;

    @ApiOperation(value = "根据条件查询", notes = "分页查询")
    @GetMapping("/queryTableData")
    public R queryTableData(String stationCode, Long genDate) {
        QueryWrapper<NwpStation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gen_date", new Date(genDate));
        queryWrapper.eq("station_code", stationCode);
        List<NwpStation> talbeList = nwpStationService.list(queryWrapper);
        talbeList.sort(Comparator.comparing(NwpStation::getTime));
        Map<String, NwpStation> dataGroupMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for (NwpStation nwpStation : talbeList) {
            dataGroupMap.put(sdf.format(nwpStation.getTime()), nwpStation);
        }
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("tableList", talbeList);
        ElectricField electricField = electricFieldService.findByStationCode(stationCode);
        returnMap.put("stationType", electricField.getElectricFieldTypeEnum());
        if (CollUtil.isNotEmpty(talbeList)) {
            List<String> timeList = new ArrayList<>();
            if ("E1".equals(electricField.getElectricFieldTypeEnum())) {
                // 光伏
                List<BigDecimal> globalRList = new ArrayList<>();
                List<BigDecimal> directRList = new ArrayList<>();
                List<BigDecimal> diffuseRList = new ArrayList<>();
                Long startTime = talbeList.get(0).getTime().getTime();
                Long endTime = talbeList.get(talbeList.size() - 1).getTime().getTime();
                Long momentTime = 15 * 60 * 1000L;
                for (Long tempTime = startTime; tempTime <= endTime; tempTime = tempTime + momentTime) {
                    // 设置时间
                    String tempTimeStr = sdf.format(tempTime);
                    timeList.add(tempTimeStr);
                    if (dataGroupMap.get(tempTimeStr) != null) {
                        NwpStation nwpStation = dataGroupMap.get(tempTimeStr);
                        // 总辐射
                        BigDecimal globalR = nwpStation.getGlobalRadiation();
                        if (globalR.compareTo(new BigDecimal("-99")) == 0) {
                            globalRList.add(null);
                        } else {
                            globalRList.add(globalR);
                        }

                        // 直接辐射
                        BigDecimal directR = nwpStation.getDirectRadiation();
                        if (directR.compareTo(new BigDecimal("-99")) == 0) {
                            directRList.add(null);
                        } else {
                            directRList.add(directR);
                        }

                        // 散辐射
                        BigDecimal diffuseR = nwpStation.getDiffuseRadiation();
                        if (diffuseR.compareTo(new BigDecimal("-99")) == 0) {
                            diffuseRList.add(null);
                        } else {
                            diffuseRList.add(diffuseR);
                        }

                    } else {
                        globalRList.add(null);
                        directRList.add(null);
                        diffuseRList.add(null);
                    }
                }
                Map<String, List<BigDecimal>> radiationDataMap = new HashMap<>();
                radiationDataMap.put("globalRs", globalRList);
                radiationDataMap.put("directRs", directRList);
                radiationDataMap.put("diffuseRs", diffuseRList);
                returnMap.put("radiationData", radiationDataMap);
            } else {
                // 风电
                List<BigDecimal> ws10List = new ArrayList<>();
                List<BigDecimal> ws30List = new ArrayList<>();
                List<BigDecimal> ws50List = new ArrayList<>();
                List<BigDecimal> ws70List = new ArrayList<>();
                List<BigDecimal> ws80List = new ArrayList<>();
                List<BigDecimal> ws90List = new ArrayList<>();
                List<BigDecimal> ws100List = new ArrayList<>();

                Long startTime = talbeList.get(0).getTime().getTime();
                Long endTime = talbeList.get(talbeList.size() - 1).getTime().getTime();
                Long momentTime = 15 * 60 * 1000L;
                for (Long tempTime = startTime; tempTime <= endTime; tempTime = tempTime + momentTime) {
                    // 设置时间
                    // 设置时间
                    String tempTimeStr = sdf.format(tempTime);
                    timeList.add(tempTimeStr);
                    if (dataGroupMap.get(tempTimeStr) != null) {
                        NwpStation nwpStation = dataGroupMap.get(tempTimeStr);
                        // ws10
                        BigDecimal ws10 = nwpStation.getWs10();
                        if (ws10.compareTo(new BigDecimal("-99")) == 0) {
                            ws10List.add(null);
                        } else {
                            ws10List.add(ws10);
                        }

                        // ws30
                        BigDecimal ws30 = nwpStation.getWs30();
                        if (ws30.compareTo(new BigDecimal("-99")) == 0) {
                            ws30List.add(null);
                        } else {
                            ws30List.add(ws30);
                        }

                        // ws50
                        BigDecimal ws50 = nwpStation.getWs50();
                        if (ws50.compareTo(new BigDecimal("-99")) == 0) {
                            ws50List.add(null);
                        } else {
                            ws50List.add(ws50);
                        }

                        // ws70
                        BigDecimal ws70 = nwpStation.getWs70();
                        if (ws70.compareTo(new BigDecimal("-99")) == 0) {
                            ws70List.add(null);
                        } else {
                            ws70List.add(ws70);
                        }

                        // ws80
                        BigDecimal ws80 = nwpStation.getWs80();
                        if (ws80.compareTo(new BigDecimal("-99")) == 0) {
                            ws80List.add(null);
                        } else {
                            ws80List.add(ws80);
                        }

                        // ws90
                        BigDecimal ws90 = nwpStation.getWs90();
                        if (ws90.compareTo(new BigDecimal("-99")) == 0) {
                            ws90List.add(null);
                        } else {
                            ws90List.add(ws90);
                        }
                        // ws100
                        BigDecimal ws100 = nwpStation.getWs100();
                        if (ws100.compareTo(new BigDecimal("-99")) == 0) {
                            ws100List.add(null);
                        } else {
                            ws100List.add(ws100);
                        }
                    } else {
                        // ws10
                        ws10List.add(null);
                        // ws30
                        ws30List.add(null);
                        // ws50
                        ws50List.add(null);
                        // ws70
                        ws70List.add(null);
                        // ws80
                        ws80List.add(null);
                        // ws90
                        ws90List.add(null);
                        // ws100
                        ws100List.add(null);
                    }
                }
                LinkedHashMap<String, List> wsMap = new LinkedHashMap<>();

                wsMap.put("10米风速", ws10List);
                wsMap.put("30米风速", ws30List);
                wsMap.put("50米风速", ws50List);
                wsMap.put("70米风速", ws70List);
                wsMap.put("80米风速", ws80List);
                wsMap.put("90米风速", ws90List);
                wsMap.put("100米风速", ws100List);
                returnMap.put("radiationData", wsMap);
            }
            returnMap.put("timeList", timeList);
        }
        return R.ok(returnMap);
    }

}
