package com.gyee.runeconomy.controller.agc;


import com.gyee.runeconomy.config.GyeeConfig;
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.ArrayList;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 获取各个场站AGC偏差分析数据
 */
@RestController
@RequestMapping("/agc")
public class AgcDeviateController {
    @Resource
    private AgcDeviateService agcDeviateService;
    @Resource
    private GyeeConfig gyeeConfig;

    /**
     * 获取偏差信息
     *
     * @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("/config")
    public Object getAgcConifg() {
        return agcDeviateService.getConfig();
    }
}
