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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.vo.LeakageMonitoringVO;
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/leakage")
public class LeakageMonitoringController {

    @Autowired
    private MonitoringService monitoringService;

    @GetMapping("/page")
    @ApiOperation("漏失监测分页查询")
    @PreAuthorize("@ss.hasPermi('waterSupply:monitoring:leakage:list')")
    public AjaxResult getLeakageMonitorPage(
            @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("功率最小值") @RequestParam(required = false) Double powerMin,
            @ApiParam("功率最大值") @RequestParam(required = false) Double powerMax) {
        Page<LeakageMonitoringVO> page = monitoringService.getLeakageMonitorPage(
                pageNum, pageSize, equipmentName, equipmentCode, equipmentLocation,
                startTime, endTime, powerMin, powerMax);
        return AjaxResult.success(page);
    }

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

    @GetMapping("/export")
    @ApiOperation("漏失监测数据导出")
    @PreAuthorize("@ss.hasPermi('waterSupply:monitoring:leakage:export')")
    public AjaxResult exportLeakageMonitorData(
            @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("功率最小值") @RequestParam(required = false) Double powerMin,
            @ApiParam("功率最大值") @RequestParam(required = false) Double powerMax) {
        Page<LeakageMonitoringVO> page = monitoringService.getLeakageMonitorPage(
                1, 10000, equipmentName, equipmentCode, equipmentLocation,
                startTime, endTime, powerMin, powerMax);
        return AjaxResult.success(page.getRecords());
    }
}
