package com.zksy.web.controller.WaterSupply.PressureMonitoring;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.vo.PressureMonitoringVO;
import com.zksy.base.service.MonitoringService;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "供水管网运行监测-压力监测")
@RestController
@RequestMapping("/waterSupply/monitoring/pressure")
public class PressureMonitoringController {

    @Autowired
    private MonitoringService monitoringService;

    @GetMapping("/page")
    @ApiOperation("压力监测分页查询")
    @PreAuthorize("@ss.hasPermi('waterSupply:monitoring:pressure:list')")
    public AjaxResult getPressureMonitorPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam("设备名称") @RequestParam(required = false) String equipmentName,
            @ApiParam("设备编码") @RequestParam(required = false) String equipmentCode,
            @ApiParam("安装位置") @RequestParam(required = false) String equipmentLocation,
            @ApiParam("开始时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam("结束时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @ApiParam("压力最小值(Mpa)") @RequestParam(required = false) Double pressureMin,
            @ApiParam("压力最大值(Mpa)") @RequestParam(required = false) Double pressureMax) {
        Page<PressureMonitoringVO> page = monitoringService.getPressureMonitorPage(
                pageNum, pageSize, equipmentName, equipmentCode, equipmentLocation,
                startTime, endTime, pressureMin, pressureMax);
        return AjaxResult.success(page);
    }

    @GetMapping("/trend/{equipmentId}")
    @ApiOperation("压力近24小时趋势数据")
    @PreAuthorize("@ss.hasPermi('waterSupply:monitoring:pressure:list')")
    public AjaxResult getPressureTrend24h(@PathVariable String equipmentId) {
        List<Map<String, Object>> trend = monitoringService.getPressureTrend24h(equipmentId);
        return AjaxResult.success(trend);
    }

    @GetMapping("/export")
    @ApiOperation("压力监测数据导出")
    @PreAuthorize("@ss.hasPermi('waterSupply:monitoring:pressure:export')")
    public AjaxResult exportPressureMonitorData(
            @ApiParam("设备名称") @RequestParam(required = false) String equipmentName,
            @ApiParam("设备编码") @RequestParam(required = false) String equipmentCode,
            @ApiParam("安装位置") @RequestParam(required = false) String equipmentLocation,
            @ApiParam("开始时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam("结束时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @ApiParam("压力最小值(Mpa)") @RequestParam(required = false) Double pressureMin,
            @ApiParam("压力最大值(Mpa)") @RequestParam(required = false) Double pressureMax) {
        Page<PressureMonitoringVO> page = monitoringService.getPressureMonitorPage(
                1, 10000, equipmentName, equipmentCode, equipmentLocation,
                startTime, endTime, pressureMin, pressureMax);
        return AjaxResult.success(page.getRecords());
    }
}
