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

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

    @Autowired
    private MonitoringService monitoringService;

    @GetMapping("/page")
    @ApiOperation("流量监测分页查询")
    @PreAuthorize("@ss.hasPermi('waterSupply:monitoring:flow:list')")
    public AjaxResult getFlowMonitorPage(
            @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 flowMin,
            @ApiParam("流量最大值") @RequestParam(required = false) Double flowMax) {
        Page<FlowMonitoringVO> page = monitoringService.getFlowMonitorPage(
                pageNum, pageSize, equipmentName, equipmentCode, equipmentLocation,
                startTime, endTime, flowMin, flowMax);
        return AjaxResult.success(page);
    }

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

    @GetMapping("/export")
    @ApiOperation("流量监测数据导出")
    @PreAuthorize("@ss.hasPermi('waterSupply:monitoring:flow:export')")
    public AjaxResult exportFlowMonitorData(
            @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 flowMin,
            @ApiParam("流量最大值") @RequestParam(required = false) Double flowMax) {
        Page<FlowMonitoringVO> page = monitoringService.getFlowMonitorPage(
                1, 10000, equipmentName, equipmentCode, equipmentLocation,
                startTime, endTime, flowMin, flowMax);
        return AjaxResult.success(page.getRecords());
    }
}
