package com.trafficaudit.rulemanage.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.trafficaudit.common.Result; import com.trafficaudit.rulemanage.entity.AuditRule; import com.trafficaudit.rulemanage.mapper.AuditRuleMapper; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.time.LocalDateTime; import java.util.List; /** 规则管理:按报表类型查看/启停/调整阈值 */ @RestController @RequestMapping("/api/rules") public class RuleManageController { @Resource private AuditRuleMapper ruleMapper; /** 规则列表,可按报表类型过滤 */ @GetMapping("/list") public Result> list(@RequestParam(value = "reportType", required = false) String reportType) { LambdaQueryWrapper qw = new LambdaQueryWrapper<>(); if (reportType != null && !reportType.trim().isEmpty()) { qw.eq(AuditRule::getReportType, reportType.trim()); } qw.orderByAsc(AuditRule::getSortOrder); return Result.ok(ruleMapper.selectList(qw)); } /** 启停规则 */ @PostMapping("/toggle") public Result toggle(@RequestParam("id") Long id, @RequestParam("enabled") Integer enabled) { AuditRule rule = ruleMapper.selectById(id); if (rule == null) return Result.error("规则不存在"); rule.setIsEnabled(enabled == null || enabled == 0 ? 0 : 1); rule.setUpdatedAt(LocalDateTime.now()); ruleMapper.updateById(rule); return Result.ok("已更新"); } /** 更新阈值/等级/描述 */ @PostMapping("/update") public Result update(@RequestBody AuditRule body) { if (body.getId() == null) return Result.error("缺少规则ID"); AuditRule rule = ruleMapper.selectById(body.getId()); if (rule == null) return Result.error("规则不存在"); if (body.getThreshold() != null) rule.setThreshold(body.getThreshold()); if (body.getAlertLevel() != null) rule.setAlertLevel(body.getAlertLevel()); if (body.getDescription() != null) rule.setDescription(body.getDescription()); rule.setUpdatedAt(LocalDateTime.now()); ruleMapper.updateById(rule); return Result.ok("已保存"); } }