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.WindTowerStatusData;
import com.cpp.web.service.station.WindTowerStatusDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * idp_wind_tower_status_data
 *
 * @author whc
 * @date 2022-03-18 15:50:05
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/windtowerstatusdata")
@Api(value = "windtowerstatusdata", tags = "idp_wind_tower_status_data管理")
public class WindTowerStatusDataController {

    private final WindTowerStatusDataService windTowerStatusDataService;

    /**
     * 分页查询
     *
     * @param page                分页对象
     * @param windTowerStatusData idp_wind_tower_status_data
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getWindTowerStatusDataPage(Page page, WindTowerStatusData windTowerStatusData) {
        return R.ok(windTowerStatusDataService.page(page, Wrappers.query(windTowerStatusData)));
    }

    @ApiOperation(value = "通过条件查询", notes = "通过条件查询")
    @GetMapping("/queryCharts")
    public R queryCharts(String stationCode,
                         Long startTime,
                         Long endTime,
                         String equipmentId) throws Exception {
        return R.ok(windTowerStatusDataService.queryCharts(stationCode, new Date(startTime), new Date(endTime), equipmentId));
    }

    @ApiOperation(value = "根据条件查询", notes = "分页查询")
    @GetMapping("/queryTableData")
    public R queryTableData(String stationCode, Long startTime, Long endTime) {
        QueryWrapper<WindTowerStatusData> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("time", new Date(startTime), new Date(endTime));
        queryWrapper.eq("station_code", stationCode);
        List<WindTowerStatusData> talbeList = windTowerStatusDataService.list(queryWrapper);
        talbeList.sort(Comparator.comparing(WindTowerStatusData::getTime));

        // 封装各层高风速
        List<String> timeList = new ArrayList<>();
        List<BigDecimal> wsHubHeightList = new ArrayList<>();
        List<BigDecimal> ws10List = new ArrayList<>();
        List<BigDecimal> ws30List = new ArrayList<>();
        List<BigDecimal> ws50List = new ArrayList<>();
        List<BigDecimal> ws60List = new ArrayList<>();
        List<BigDecimal> ws70List = new ArrayList<>();
        List<BigDecimal> ws80List = new ArrayList<>();
        List<BigDecimal> ws90List = new ArrayList<>();
        List<BigDecimal> ws100List = new ArrayList<>();
        LinkedHashMap<String, List> wsMap = new LinkedHashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < talbeList.size(); i++) {
            WindTowerStatusData windTowerStatusData = talbeList.get(i);
            timeList.add(sdf.format(windTowerStatusData.getTime()));
            // 风速轮毂
            BigDecimal wsHubHeight = windTowerStatusData.getWsHubHeight();
            wsHubHeightList.add(wsHubHeight);
            // ws10
            BigDecimal ws10 = windTowerStatusData.getWs10();
            ws10List.add(ws10);
            // ws30
            BigDecimal ws30 = windTowerStatusData.getWs30();
            ws30List.add(ws30);
            // ws50
            BigDecimal ws50 = windTowerStatusData.getWs50();
            ws50List.add(ws50);
            // ws60
            BigDecimal ws60 = windTowerStatusData.getWs60();
            ws60List.add(ws60);
            // ws70
            BigDecimal ws70 = windTowerStatusData.getWs70();
            ws70List.add(ws70);
            // ws80
            BigDecimal ws80 = windTowerStatusData.getWs80();
            ws80List.add(ws80);
            // ws90
            BigDecimal ws90 = windTowerStatusData.getWs90();
            ws90List.add(ws90);
            // ws100
            BigDecimal ws100 = windTowerStatusData.getWs100();
            ws100List.add(ws100);
        }

        wsMap.put("轮毂风速", wsHubHeightList);
        wsMap.put("10米风速", ws10List);
        wsMap.put("30米风速", ws30List);
        wsMap.put("50米风速", ws50List);
        wsMap.put("60米风速", ws60List);
        wsMap.put("70米风速", ws70List);
        wsMap.put("80米风速", ws80List);
        wsMap.put("90米风速", ws90List);
        wsMap.put("100米风速", ws100List);

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("tableList", talbeList);
        returnMap.put("wsTime", timeList);
        returnMap.put("wsMap", wsMap);
        return R.ok(returnMap);
    }
}
