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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.audio.domain.NoiseInfo;
import com.zksy.base.audio.service.NoiseInfoService;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 音频数据分析与监测接口
 */
@RestController
@RequestMapping("/api/audio/noise")
@Api(tags = "音频数据分析与监测接口")
@Anonymous
public class NoiseInfoController {

    @Autowired
    private NoiseInfoService noiseInfoService;

    /**
     * 分页查询音频监测数据
     */
    @GetMapping("/page")
    @ApiOperation("分页查询音频监测数据")
    public AjaxResult queryPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String encode,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {

        try {
            Page<NoiseInfo> page = new Page<>(pageNum, pageSize);
            IPage<NoiseInfo> resultPage = noiseInfoService.queryPage(page, encode, startTime, endTime);
            return AjaxResult.success("查询成功", resultPage);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 查询异常数据（功率超过阈值）
     */
    @GetMapping("/visualization/abnormal")
    @ApiOperation("查询异常数据（功率超过阈值）")
    public AjaxResult queryAbnormalData(
            @RequestParam Double threshold,
            @RequestParam String startTime,
            @RequestParam String endTime,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        try {
            Page<NoiseInfo> page = new Page<>(pageNum, pageSize);
            IPage<NoiseInfo> resultPage = noiseInfoService.queryAbnormalData(page, threshold, startTime, endTime);
            return AjaxResult.success("查询成功", resultPage);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/visualization/stat/device")
    @ApiOperation("按设备统计功率数据")
    public AjaxResult statPowerByDevice(
            @RequestParam String startTime,
            @RequestParam String endTime) {
        try {
            List<Map<String, Object>> data = noiseInfoService.statPowerByDevice(startTime, endTime);
            return AjaxResult.success("统计成功", data);
        } catch (Exception e) {
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    @GetMapping("/visualization/stat/trend")
    @ApiOperation("按小时统计设备瞬时功率数据")
    public AjaxResult statPowerTrendByHour(
            @RequestParam String encode,
            @RequestParam String startTime,
            @RequestParam String endTime) {
        try {
            List<Map<String, Object>> data = noiseInfoService.statPowerTrendByHour(encode, startTime, endTime);
            return AjaxResult.success("统计成功", data);
        } catch (Exception e) {
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    @GetMapping("/visualization/latest")
    @ApiOperation("查询指定设备最新的数据")
    public AjaxResult getLatestDataByDevice(@RequestParam String encode) {
        try {
            NoiseInfo data = noiseInfoService.getLatestDataByDevice(encode);
            if (data == null) {
                return AjaxResult.warn("未查询到该设备的最新数据", null);
            }
            return AjaxResult.success("查询成功", data);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/visualization/devices")
    @ApiOperation("查询所有设备编码")
    public AjaxResult getAllDeviceCodes() {
        try {
            List<String> data = noiseInfoService.getAllDeviceCodes();
            return AjaxResult.success("查询成功", data);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }
}