package com.zksy.web.controller.base.environment;

import com.zksy.base.environment.domain.ERealTimeData;
import com.zksy.base.environment.service.ERealTimeDataService;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.core.domain.AjaxResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
@RequestMapping("/api/env")
@Api(tags = "环境数据接口")
@Anonymous
public class ERealTimeDataController {

    @Autowired
    private ERealTimeDataService dataService;

    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter DATETIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @GetMapping("/visualization/latest")
    @ApiOperation("查询最新数据")
    public AjaxResult getLatest(@RequestParam Integer deviceId,
                                   @RequestParam Integer nodeId) {
        return AjaxResult.success(dataService.getLatestByDeviceNode(deviceId, nodeId));
    }

    @GetMapping("/visualization/daily-stats")
    @ApiOperation("按天统计（均值、最大值、最小值）")
    public AjaxResult getDailyStats(@RequestParam Integer deviceId,
                                             @RequestParam Integer nodeId,
                                             @RequestParam String date) {
        LocalDate localDate = LocalDate.parse(date, DATE_FORMAT);
        return AjaxResult.success(dataService.getDailyStats(deviceId, nodeId, localDate));
    }

    @GetMapping("/visualization/trend")
    @ApiOperation("设备趋势分析")
    public AjaxResult getTrend(@RequestParam Integer deviceId,
                                              @RequestParam Integer nodeId,
                                              @RequestParam String start,
                                              @RequestParam String end) {
        LocalDateTime startTime = LocalDateTime.parse(start, DATETIME_FORMAT);
        LocalDateTime endTime = LocalDateTime.parse(end, DATETIME_FORMAT);
        return AjaxResult.success(dataService.getTrend(deviceId, nodeId, startTime, endTime));
    }

    @GetMapping("/visualization/alarms")
    @ApiOperation("查询报警数据")
    public AjaxResult getAlarms(@RequestParam Integer deviceId,
                                @RequestParam Integer nodeId,
                                @RequestParam String start,
                                @RequestParam String end,
                                @RequestParam(required = false) Double threshold) {
        LocalDateTime startTime = LocalDateTime.parse(start, DATETIME_FORMAT);
        LocalDateTime endTime = LocalDateTime.parse(end, DATETIME_FORMAT);
        return AjaxResult.success(dataService.getAlarms(deviceId, nodeId, startTime, endTime, threshold));
    }

    @GetMapping("/visualization/device-group")
    @ApiOperation("根据设备分组查询")
    public AjaxResult getDeviceGroup() {
        return AjaxResult.success(dataService.getDeviceGroup());
    }
}
