package com.gyee.runeconomy.controller.agc;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.runeconomy.config.GyeeConfig;
import com.gyee.runeconomy.model.PowerLossesDTO;
import com.gyee.runeconomy.model.StatusTime;
import com.gyee.runeconomy.service.PowerLossesService;
import com.gyee.runeconomy.service.agc.AgcDeviateService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

import java.util.stream.Collectors;

/**
 * 获取各个场站AGC偏差分析数据
 */
@RestController
@RequestMapping("/agc")
public class AgcDeviateController {

    @Resource
    private AgcDeviateService agcDeviateService;
    @Resource
    private GyeeConfig gyeeConfig;

    @Resource
    private PowerLossesService powerLossService;

    /**
     * 获取偏差信息
     *
     * @param startTs  开始时间
     * @param endTs    结束时间
     * @param id       场站id
     * @param interval 时间间隔
     * @return
     */
    @GetMapping("/deviate")
    public Map<String, AgcDeviateTag> getData(@RequestParam(value = "startTs") long startTs,
                                              @RequestParam(value = "endTs") long endTs,
                                              @RequestParam(value = "id") String id,
                                              @RequestParam(value = "interval", defaultValue = "60", required = false) int interval) {
        List<AgcDeviateTag> ls = new ArrayList<>();
        if (gyeeConfig.isOffLine()) {
            ls = agcDeviateService.getAgcDeviateTags(id, startTs, endTs, interval);
        } else {
            ls = agcDeviateService.getAgcDeviateTags(id, startTs, endTs, interval);
        }
        return ls.stream().collect(Collectors.toMap(AgcDeviateTag::getName, f -> f));
    }

    /**
     * 限电时间列表
     *
     * @return
     */
    @GetMapping("/xdloss")
    public Page<PowerLossesDTO> getxdData(@RequestParam(value = "startTs") long startTs,
                                          @RequestParam(value = "endTs") long endTs,
                                          @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

        Page<PowerLossesDTO> agcxd = agcDeviateService.getAgcxd(startTs, endTs, pageNum, pageSize);
        return agcxd;
    }

    /**
     * 获取配置
     *
     * @return
     */
    @GetMapping("/config")
    public Object getAgcConifg() {
        return agcDeviateService.getConfig();
    }


    @GetMapping("/windturbine/curve")
    public Map<String, List<SpeedPowerAnalysis>> getWindturbineData(@RequestParam(value = "startTs") long startTs,
                                                                    @RequestParam(value = "endTs") long endTs,
                                                                    @RequestParam(value = "windturbineId") String windturbineId,
                                                                    @RequestParam(value = "interval", defaultValue = "60", required = false) int interval) throws Exception {
        return agcDeviateService.getWindturbineData(startTs,endTs,windturbineId,interval);
    }

    @GetMapping("/windtur")
    public Map<String, List<StatusTime>> getTestData(@RequestParam(value = "startTs") long startTs,
                                                     @RequestParam(value = "endTs") long endTs,
                                                     @RequestParam(value = "uniformcode") String uniformcode) throws Exception {
        return agcDeviateService.getStatusTime(startTs,endTs,uniformcode);
    }

}
