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.*;
import java.util.stream.Collectors;


/**
 * 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));
        Map<String, WindTowerStatusData> dataGroupMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for (WindTowerStatusData windTowerStatusData : talbeList) {
            dataGroupMap.put(sdf.format(windTowerStatusData.getTime()), windTowerStatusData);
        }
        // 封装各层高风速
        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<>();
        List<BigDecimal> wdHubHeightList = new ArrayList<>();
        List<BigDecimal> wd10List = new ArrayList<>();
        List<BigDecimal> wd30List = new ArrayList<>();
        List<BigDecimal> wd50List = new ArrayList<>();
        List<BigDecimal> wd60List = new ArrayList<>();
        List<BigDecimal> wd70List = new ArrayList<>();
        List<BigDecimal> wd80List = new ArrayList<>();
        List<BigDecimal> wd90List = new ArrayList<>();
        List<BigDecimal> wd100List = new ArrayList<>();
        LinkedHashMap<String, List> wsMap = new LinkedHashMap<>();

        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){
                WindTowerStatusData windTowerStatusData = dataGroupMap.get(tempTimeStr);
                // 风速轮毂
                BigDecimal wsHubHeight = windTowerStatusData.getWsHubHeight();
                wsHubHeightList.add(wsHubHeight);
                // 轮毂风向
                wdHubHeightList.add(windTowerStatusData.getWdHubHeight());
                // ws10
                BigDecimal ws10 = windTowerStatusData.getWs10();
                ws10List.add(ws10);
                // wd10
                wd10List.add(windTowerStatusData.getWd10());
                // ws30
                BigDecimal ws30 = windTowerStatusData.getWs30();
                ws30List.add(ws30);
                // wd30
                wd30List.add(windTowerStatusData.getWd30());
                // ws50
                BigDecimal ws50 = windTowerStatusData.getWs50();
                ws50List.add(ws50);
                // wd50
                wd50List.add(windTowerStatusData.getWd50());
                // ws60
                BigDecimal ws60 = windTowerStatusData.getWs60();
                ws60List.add(ws60);
                // wd60
                wd60List.add(windTowerStatusData.getWd60());
                // ws70
                BigDecimal ws70 = windTowerStatusData.getWs70();
                ws70List.add(ws70);
                // wd70
                wd70List.add(windTowerStatusData.getWd70());
                // ws80
                BigDecimal ws80 = windTowerStatusData.getWs80();
                ws80List.add(ws80);
                // wd80
                wd80List.add(windTowerStatusData.getWd80());
                // ws90
                BigDecimal ws90 = windTowerStatusData.getWs90();
                ws90List.add(ws90);
                // wd90
                wd90List.add(windTowerStatusData.getWd90());
                // ws100
                BigDecimal ws100 = windTowerStatusData.getWs100();
                ws100List.add(ws100);
                // wd100
                wd100List.add(windTowerStatusData.getWd100());
            }
            else{
                // 风速轮毂
                wsHubHeightList.add(null);
                // ws10
                ws10List.add(null);
                // ws30
                ws30List.add(null);
                // ws50
                ws50List.add(null);
                // ws60
                ws60List.add(null);
                // ws70
                ws70List.add(null);
                // ws80
                ws80List.add(null);
                // ws90
                ws90List.add(null);
                // ws100
                ws100List.add(null);
            }
        }
        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);

        LinkedHashMap<String, Float[]> wdMap = new LinkedHashMap<>();
        Float[] wdHubHeightRate = getCalcWd(wdHubHeightList);
        Float[] wd10Rate = getCalcWd(wd10List);
        Float[] wd30Rate = getCalcWd(wd30List);
        Float[] wd50Rate = getCalcWd(wd50List);
        Float[] wd60Rate = getCalcWd(wd60List);
        Float[] wd70Rate = getCalcWd(wd70List);
        Float[] wd80Rate = getCalcWd(wd80List);
        Float[] wd90Rate = getCalcWd(wd90List);
        Float[] wd100Rate = getCalcWd(wd100List);
        wdMap.put("轮毂风向", wdHubHeightRate);
        wdMap.put("10米风向", wd10Rate);
        wdMap.put("30米风向", wd30Rate);
        wdMap.put("50米风向", wd50Rate);
        wdMap.put("60米风向", wd60Rate);
        wdMap.put("70米风向", wd70Rate);
        wdMap.put("80米风向", wd80Rate);
        wdMap.put("90米风向", wd90Rate);
        wdMap.put("100米风向", wd100Rate);
        returnMap.put("wdMap", wdMap);

        return R.ok(returnMap);
    }

    /**
     * 计算风向
     * @return
     */
    private Float[] getCalcWd(List<BigDecimal> wdList){
        int[] frequency16 = new int[16];
        Float[] frequency16Rate = new Float[16];
        for(BigDecimal b: wdList){
            if(b!=null){
                float wdfloat = b.floatValue();
                //N 北 0 348.76-11.25
                if( (wdfloat>=348.76F && wdfloat<=360F) || (wdfloat>=0 && wdfloat<= 11.25)){
                    frequency16[0]++;
                }
                //NNE 北东北 22.5 11.26-33.75
                if(wdfloat>=11.26F && wdfloat<=33.75F){
                    frequency16[15]++;
                }
                //NE 东北 45 33.76-56.25
                if(wdfloat>=33.76F && wdfloat<=56.25F){
                    frequency16[14]++;
                }
                //ENE 东东北 67.5 56.26-78.75
                if(wdfloat>=56.26F && wdfloat<=78.75F){
                    frequency16[13]++;
                }
                //E 东 90 78.76-101.25
                if(wdfloat>=78.76F && wdfloat<=101.25F){
                    frequency16[12]++;
                }
                //ESE 东东南 112.5 101.26-123.75
                if(wdfloat>=101.26F && wdfloat<=123.75F){
                    frequency16[11]++;
                }
                //SE 东南 135 123.76-146.25
                if(wdfloat>=123.76F && wdfloat<=146.25F){
                    frequency16[10]++;
                }
                //SSE 南东南 157.5 146.26-168.75
                if(wdfloat>=146.26F && wdfloat<=168.75F){
                    frequency16[9]++;
                }
                //S 南 180 168.76-191.25
                if(wdfloat>=168.76F && wdfloat<=191.25F){
                    frequency16[8]++;
                }
                //SSW 南西南 202.5 191.26-213.75
                if(wdfloat>=191.26F && wdfloat<=213.75F){
                    frequency16[7]++;
                }
                //SW 西南  225 213.76-236.25
                if(wdfloat>=213.76F && wdfloat<=236.25F){
                    frequency16[6]++;
                }
                //WSW 西西南 247.5 236.26-258.75
                if(wdfloat>=236.26F && wdfloat<=258.75F){
                    frequency16[5]++;
                }
                //W 西 270 258.76-281.25
                if(wdfloat>=258.76F && wdfloat<=281.25F){
                    frequency16[4]++;
                }
                //WNW 西西北 295.5 281.26-303.75
                if(wdfloat>=281.26F && wdfloat<=303.75F){
                    frequency16[3]++;
                }
                //NW 西北 315 303.76-326.25
                if(wdfloat>=303.76F && wdfloat<=326.25F){
                    frequency16[2]++;
                }
                //NNW 北西北 337.5 326.26-348.
                if(wdfloat>=326.26F && wdfloat<=348.75F){
                    frequency16[1]++;
                }
            }
        }
        BigDecimal sumToltal= new BigDecimal(0);
        for(int wdf:frequency16){
            BigDecimal b = new BigDecimal(wdf);
            sumToltal = sumToltal.add(b);
        }

        for(int i =0; i<frequency16.length;i++){
            if(frequency16[i] ==0){
                frequency16Rate[i]= 0f;
            }else{
                BigDecimal dividend = new BigDecimal(frequency16[i]);
                frequency16Rate[i] =dividend.divide(sumToltal,2, BigDecimal.ROUND_HALF_UP).floatValue();
            }
        }
        return frequency16Rate;
    }
}
