yuan
10 天以前 07496adb3a0c61e48c660e0cb4ee95d44782f05d
src/main/java/com/ruoyi/safe/controller/SafeFacilityLedgerController.java
@@ -4,7 +4,9 @@
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;
@@ -28,6 +30,7 @@
public class SafeFacilityLedgerController {
    private SafeFacilityLedgerService safeFacilityLedgerService;
    private SafeFacilityInspectionService safeFacilityInspectionService;
    @GetMapping("/page")
    @Operation(summary = "分页查询")
@@ -45,6 +48,9 @@
    @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));
    }
@@ -52,13 +58,35 @@
    @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(@RequestBody List<Integer> 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));
    }
}