liding
2026-06-30 d4a3ddfeac6aea951c51bee91d9cf4eb57c4f4c4
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.ruoyi.safe.controller;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.safe.pojo.SafeFacilityInspection;
import com.ruoyi.safe.pojo.SafeFacilityLedger;
import com.ruoyi.safe.service.SafeFacilityInspectionService;
import com.ruoyi.safe.service.SafeFacilityLedgerService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
 
/**
 * <p>
 * 安全生产--安全设施台账 前端控制器
 * </p>
 *
 * @author 芯导软件(江苏)有限公司
 * @since 2026-06-29
 */
@RestController
@RequestMapping("/safeFacilityLedger")
@AllArgsConstructor
@Tag(name = "安全生产--安全设施台账")
public class SafeFacilityLedgerController {
 
    private SafeFacilityLedgerService safeFacilityLedgerService;
    private SafeFacilityInspectionService safeFacilityInspectionService;
 
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public R page(Page page, SafeFacilityLedger safeFacilityLedger) {
        return R.ok(safeFacilityLedgerService.pageSafeFacilityLedger(page, safeFacilityLedger));
    }
 
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询")
    public R getById(@PathVariable Integer id) {
        return R.ok(safeFacilityLedgerService.getById(id));
    }
 
    @Operation(summary = "新增安全设施台账")
    @Log(title = "安全设施台账", businessType = BusinessType.INSERT)
    @PostMapping()
    public R add(@RequestBody SafeFacilityLedger safeFacilityLedger) {
        if (isCodeExists(safeFacilityLedger.getFacilityCode(), null)) {
            return R.fail("设施编号「" + safeFacilityLedger.getFacilityCode() + "」已存在");
        }
        return R.ok(safeFacilityLedgerService.save(safeFacilityLedger));
    }
 
    @Operation(summary = "修改安全设施台账")
    @Log(title = "安全设施台账", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R update(@RequestBody SafeFacilityLedger safeFacilityLedger) {
        if (isCodeExists(safeFacilityLedger.getFacilityCode(), safeFacilityLedger.getId())) {
            return R.fail("设施编号「" + safeFacilityLedger.getFacilityCode() + "」已存在");
        }
        return R.ok(safeFacilityLedgerService.updateById(safeFacilityLedger));
    }
 
    private boolean isCodeExists(String code, Integer excludeId) {
        if (code == null || code.isEmpty()) return false;
        return safeFacilityLedgerService.lambdaQuery()
                .eq(SafeFacilityLedger::getFacilityCode, code)
                .ne(excludeId != null, SafeFacilityLedger::getId, excludeId)
                .count() > 0;
    }
 
    @Operation(summary = "删除安全设施台账")
    @Log(title = "安全设施台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R delSafeFacilityLedger(@PathVariable List<Integer> ids) {
        // 校验设施是否有关联的巡检记录
        for (Integer id : ids) {
            long count = safeFacilityInspectionService.lambdaQuery()
                    .eq(com.ruoyi.safe.pojo.SafeFacilityInspection::getFacilityId, id)
                    .count();
            if (count > 0) {
                SafeFacilityLedger ledger = safeFacilityLedgerService.getById(id);
                String name = ledger != null ? ledger.getFacilityName() : String.valueOf(id);
                return R.fail("设施「" + name + "」存在关联的巡检记录,无法删除");
            }
        }
        return R.ok(safeFacilityLedgerService.removeBatchByIds(ids));
    }
}