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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.WaterSupply.MonitorAlarm.dto.in.WaterThresholdSaveInDTO;
import com.zksy.WaterSupply.MonitorAlarm.service.IWaterSupplyThresholdService;
import com.zksy.base.alarm.domain.WarningThreshold;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.enums.BusinessType;
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.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供水管网-监测报警阈值管理接口
 * 支持批量/分类设置传感器阈值、分时阈值、分类查询阈值详情
 */
@Slf4j
@RestController
@RequestMapping("/waterSupply/alarm/threshold")
@Api(tags = "供水管网-监测报警阈值管理")
public class WaterSupplyThresholdController {

    @Autowired
    private IWaterSupplyThresholdService waterSupplyThresholdService;

    @GetMapping("/page")
    @ApiOperation("阈值分页查询（分类查询阈值详情）")
    @PreAuthorize("@ss.hasPermi('waterSupply:alarm:threshold:list')")
    public AjaxResult getThresholdPage(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam(value = "每页数量", defaultValue = "10") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam(value = "设备编码") @RequestParam(required = false) String deviceCode,
            @ApiParam(value = "预警类型") @RequestParam(required = false) String warningType,
            @ApiParam(value = "预警编码") @RequestParam(required = false) String warningCode,
            @ApiParam(value = "关键字") @RequestParam(required = false) String keyword) {
        Page<WarningThreshold> page = waterSupplyThresholdService.findThresholdPage(
                pageNum, pageSize, deviceCode, warningType, warningCode, keyword);
        return AjaxResult.success(page);
    }

    @GetMapping("/list")
    @ApiOperation("阈值列表查询（分类查询阈值详情）")
    @PreAuthorize("@ss.hasPermi('waterSupply:alarm:threshold:list')")
    public AjaxResult getThresholdList(
            @ApiParam(value = "设备编码") @RequestParam(required = false) String deviceCode,
            @ApiParam(value = "预警类型") @RequestParam(required = false) String warningType,
            @ApiParam(value = "预警编码") @RequestParam(required = false) String warningCode) {
        return AjaxResult.success(waterSupplyThresholdService.findThresholdList(deviceCode, warningType, warningCode));
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("阈值详情")
    @PreAuthorize("@ss.hasPermi('waterSupply:alarm:threshold:list')")
    public AjaxResult getThresholdDetail(@PathVariable String id) {
        try {
            return AjaxResult.success(waterSupplyThresholdService.getThresholdDetail(id));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/save")
    @ApiOperation("批量保存阈值（一个规则应用到多个设备，支持分时阈值）")
    @Log(title = "供水报警阈值批量保存", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('waterSupply:alarm:threshold:add')")
    public AjaxResult saveThresholdBatch(@RequestBody WaterThresholdSaveInDTO inDTO) {
        try {
            int count = waterSupplyThresholdService.saveThresholdBatch(inDTO);
            return AjaxResult.success("保存成功，共设置" + count + "条阈值", count);
        } catch (Exception e) {
            log.error("供水报警阈值批量保存失败", e);
            return AjaxResult.error("保存失败：" + e.getMessage());
        }
    }

    @PutMapping("/update")
    @ApiOperation("修改阈值")
    @Log(title = "供水报警阈值修改", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('waterSupply:alarm:threshold:edit')")
    public AjaxResult updateThreshold(@RequestBody WarningThreshold threshold) {
        try {
            boolean result = waterSupplyThresholdService.updateThreshold(threshold);
            return result ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
        } catch (Exception e) {
            log.error("供水报警阈值修改失败", e);
            return AjaxResult.error("修改失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除阈值")
    @Log(title = "供水报警阈值删除", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('waterSupply:alarm:threshold:remove')")
    public AjaxResult deleteThresholdBatch(@ApiParam(value = "阈值ID列表", required = true)
                                           @RequestParam("ids") List<String> ids) {
        try {
            boolean result = waterSupplyThresholdService.deleteThresholdBatch(ids);
            return result ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
        } catch (Exception e) {
            log.error("供水报警阈值删除失败", e);
            return AjaxResult.error("删除失败：" + e.getMessage());
        }
    }
}
