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

import com.zksy.base.gas.domain.GasMonitorData;
import com.zksy.base.gas.service.GasMonitorDataService;
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.HashMap;
import java.util.Map;

/**
 * 可燃气体监测数据接口
 */
@Api(tags = "可燃气体监测数据", description = "可燃气体监测数据接口")
@RestController
@RequestMapping("/api/gas")
@Anonymous
public class GasMonitorDataController {

    @Autowired
    private GasMonitorDataService gasMonitorDataService;

    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/{deviceId}")
    @ApiOperation("查询最新一条监测数据")
    public AjaxResult getLatestData(@PathVariable String deviceId) {
        return AjaxResult.success(gasMonitorDataService.getLatestByDeviceId(deviceId));
    }

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

    /**
     * 设备在线率统计
     */
    @GetMapping("/visualization/online-rate")
    @ApiOperation("设备在线率统计")
    public AjaxResult getOnlineRate(@RequestParam String deviceId,@RequestParam String start,@RequestParam String end) {
        LocalDate startDate = LocalDate.parse(start, DATE_FORMAT);
        LocalDate endDate = LocalDate.parse(end, DATE_FORMAT);
        return AjaxResult.success(gasMonitorDataService.getOnlineRate(deviceId, startDate, endDate));
    }

    /**
     * 查询报警数据
     */
    @GetMapping("/visualization/alarms")
    @ApiOperation("查询报警数据")
    public AjaxResult getAlarms(@RequestParam String deviceId,@RequestParam String start,@RequestParam String end, @RequestParam(defaultValue = "1000") double threshold) {
        LocalDateTime startTime = LocalDateTime.parse(start, DATETIME_FORMAT);
        LocalDateTime endTime = LocalDateTime.parse(end, DATETIME_FORMAT);
        return AjaxResult.success(gasMonitorDataService.getAlarms(deviceId, startTime, endTime, threshold));
    }

    /**
     * 设备趋势分析（折线图用）
     */
    @GetMapping("/visualization/trend")
    @ApiOperation("设备趋势分析")
    public AjaxResult getTrend(@RequestParam String deviceId, @RequestParam String start, @RequestParam String end) {
        LocalDateTime startTime = LocalDateTime.parse(start, DATETIME_FORMAT);
        LocalDateTime endTime = LocalDateTime.parse(end, DATETIME_FORMAT);
        return AjaxResult.success(gasMonitorDataService.getTrend(deviceId, startTime, endTime));
    }
    /**
     * 查询最新数据（含报警位解析）
     */
    @GetMapping("/visualization/latest-with-alarm/{deviceId}")
    @ApiOperation("查询最新数据（含报警位解析）")
    public AjaxResult getLatestWithAlarm(@PathVariable String deviceId) {
        GasMonitorData data = gasMonitorDataService.getLatestWithAlarmBits(deviceId);
        if (data == null) {
            return AjaxResult.success(null);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("alarmBits", data.parseAlarmBits());
        result.put("otherData", data);
        return AjaxResult.success(result);
    }
    /**
     * 查询设备分组
     */
    @GetMapping("/visualization/device-group")
    @ApiOperation("查询设备分组")
    public AjaxResult getDeviceGroup() {
        return AjaxResult.success(gasMonitorDataService.getDeviceGroup());
    }
}
