zhizhijie
6 小时以前 799ec6799ad9e994f7d369f059ca7682962f648f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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<AuditRule>> list(@RequestParam(value = "reportType", required = false) String reportType) {
        LambdaQueryWrapper<AuditRule> 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<String> 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<String> 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("已保存");
    }
}