package com.cpp.web.controller.stationDataQuery;

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.web.domain.station.WeatherStationStatusData;
import com.cpp.web.domain.station.WindTowerStatusData;
import com.cpp.web.service.station.WeatherStationStatusDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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


/**
 * idp_weather_station_status_data
 *
 * @author whc
 * @date 2022-03-18 15:49:50
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/weatherstationstatusdata")
@Api(value = "weatherstationstatusdata", tags = "idp_weather_station_status_data管理")
public class WeatherStationStatusDataController {

    private final WeatherStationStatusDataService weatherStationStatusDataService;

    /**
     * 分页查询
     *
     * @param page                     分页对象
     * @param weatherStationStatusData idp_weather_station_status_data
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getWeatherStationStatusDataPage(Page page, WeatherStationStatusData weatherStationStatusData) {
        return R.ok(weatherStationStatusDataService.page(page, Wrappers.query(weatherStationStatusData)).addOrder(OrderItem.asc("time")));
    }

    /**
     * 通过id查询idp_weather_station_status_data
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {
        return R.ok(weatherStationStatusDataService.getById(id));
    }

    @ApiOperation(value = "根据条件查询", notes = "查询")
    @GetMapping("/queryCharts")
    public R queryCharts(String stationCode, Long startTime, Long endTime, String equipmentId) {
        return R.ok(weatherStationStatusDataService.findByStationCodeAndeTimeBetweenAndEquipmentId(stationCode, new Date(startTime), new Date(endTime), equipmentId));
    }

    @ApiOperation(value = "根据条件查询", notes = "分页查询")
    @GetMapping("/queryTableData")
    public R queryTableData(String stationCode, Long startTime, Long endTime) {
        QueryWrapper<WeatherStationStatusData> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("time", new Date(startTime), new Date(endTime));
        queryWrapper.eq("station_code", stationCode);
        List<WeatherStationStatusData> talbeList = weatherStationStatusDataService.list(queryWrapper);
        talbeList.sort(Comparator.comparing(WeatherStationStatusData::getTime));
        Map<String, WeatherStationStatusData> dataGroupMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for (WeatherStationStatusData weatherStationStatusData : talbeList) {
            dataGroupMap.put(sdf.format(weatherStationStatusData.getTime()), weatherStationStatusData);
        }
        // 封装各层高风速
        List<String> timeList = new ArrayList<>();
        List<BigDecimal> globalRList = new ArrayList<>();
        List<BigDecimal> directRList = new ArrayList<>();
        List<BigDecimal> diffuseRList = new ArrayList<>();
        List<BigDecimal> psList = new ArrayList<>();
        List<BigDecimal> rhList = new ArrayList<>();
        Long momentTime = 5 * 60 * 1000L;
        for (Long tempTime = startTime; tempTime <= endTime; tempTime = tempTime + momentTime) {
            // 设置时间
            String tempTimeStr  = sdf.format(tempTime);
            timeList.add(tempTimeStr);
            if (dataGroupMap.get(tempTimeStr)!=null){
                WeatherStationStatusData weatherStationStatusData = dataGroupMap.get(tempTimeStr);
                // 总辐射
                BigDecimal globalR = weatherStationStatusData.getGlobalR();
                globalRList.add(globalR);
                // 直接辐射
                BigDecimal directR = weatherStationStatusData.getDirectR();
                directRList.add(directR);
                // 散辐射
                BigDecimal diffuseR = weatherStationStatusData.getDiffuseR();
                diffuseRList.add(diffuseR);

                // 湿度
                BigDecimal rh = weatherStationStatusData.getRh();
                rhList.add(rh);
                // 压强
                BigDecimal p = weatherStationStatusData.getP();
                psList.add(p);
            }
            else{
                globalRList.add(null);
                directRList.add(null);
                diffuseRList.add(null);
                psList.add(null);
                rhList.add(null);
            }
        }
        Map<String, List<BigDecimal>> radiationDataMap = new HashMap<>();
        radiationDataMap.put("globalRs", globalRList);
        radiationDataMap.put("directRs", directRList);
        radiationDataMap.put("diffuseRs", diffuseRList);

        // 湿度、压强
        Map<String, List<BigDecimal>> sdDataMap = new HashMap<>();
        sdDataMap.put("ps", psList);
        sdDataMap.put("rhs", rhList);

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("tableList", talbeList);
        returnMap.put("timeList", timeList);
        returnMap.put("radiationData", radiationDataMap);
        returnMap.put("sdData", sdDataMap);

        return R.ok(returnMap);

    }
}
