gongchunyi
4 天以前 d2f07163fc4074f335986dd51eedbdc7be9e19ea
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.ruoyi.safe.controller;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.SecurityUtils;
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.dto.SafeLineInspectionExecuteDto;
import com.ruoyi.safe.pojo.SafeLineInspection;
import com.ruoyi.safe.pojo.SafeLineInspectionHazard;
import com.ruoyi.safe.pojo.SafeLineInspectionRecord;
import com.ruoyi.safe.service.SafeLineInspectionHazardService;
import com.ruoyi.safe.service.SafeLineInspectionRecordService;
import com.ruoyi.safe.service.SafeLineInspectionService;
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("/safeLineInspection")
@AllArgsConstructor
@Tag(name = "安全生产--线路巡检任务")
public class SafeLineInspectionController {
 
    private SafeLineInspectionService safeLineInspectionService;
    private SafeLineInspectionHazardService safeLineInspectionHazardService;
    private SafeLineInspectionRecordService safeLineInspectionRecordService;
 
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public R page(Page page, SafeLineInspection safeLineInspection) {
        return R.ok(safeLineInspectionService.pageSafeLineInspection(page, safeLineInspection));
    }
 
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询")
    public R getById(@PathVariable Integer id) {
        return R.ok(safeLineInspectionService.getById(id));
    }
 
    @Operation(summary = "新增线路巡检任务")
    @Log(title = "线路巡检", businessType = BusinessType.INSERT)
    @PostMapping()
    public R add(@RequestBody SafeLineInspection safeLineInspection) {
        if (isInspectionCodeExists(safeLineInspection.getInspectionCode(), null)) {
            return R.fail("巡检编号「" + safeLineInspection.getInspectionCode() + "」已存在");
        }
        safeLineInspection.setStatus("待巡检");
        Long currentUserId = SecurityUtils.getUserId();
        safeLineInspection.setAssignUserId(currentUserId.intValue());
        if (safeLineInspection.getInspectorId() != null && safeLineInspection.getInspectorId().isEmpty()) {
            safeLineInspection.setInspectorId(null);
        }
        return R.ok(safeLineInspectionService.save(safeLineInspection));
    }
 
    @Operation(summary = "修改线路巡检任务")
    @Log(title = "线路巡检", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R update(@RequestBody SafeLineInspection safeLineInspection) {
        if (isInspectionCodeExists(safeLineInspection.getInspectionCode(), safeLineInspection.getId())) {
            return R.fail("巡检编号「" + safeLineInspection.getInspectionCode() + "」已存在");
        }
        if (safeLineInspection.getInspectorId() != null && safeLineInspection.getInspectorId().isEmpty()) {
            safeLineInspection.setInspectorId(null);
        }
        return R.ok(safeLineInspectionService.updateById(safeLineInspection));
    }
 
    private boolean isInspectionCodeExists(String code, Integer excludeId) {
        if (code == null || code.isEmpty()) return false;
        return safeLineInspectionService.lambdaQuery()
                .eq(SafeLineInspection::getInspectionCode, code)
                .ne(excludeId != null, SafeLineInspection::getId, excludeId)
                .count() > 0;
    }
 
    @Operation(summary = "废弃旧巡检完成接口")
    @Log(title = "线路巡检", businessType = BusinessType.UPDATE)
    @PutMapping("/complete/{id}")
    public R complete(@PathVariable Integer id) {
        SafeLineInspection inspection = safeLineInspectionService.getById(id);
        if (inspection == null) {
            return R.fail("巡检任务不存在");
        }
        if ("已完成".equals(inspection.getStatus())) {
            return R.fail("巡检任务已完成,请勿重复操作");
        }
        return R.fail("请通过执行巡检提交正常结果;异常记录请在整改跟踪中由整改责任人完成整改");
    }
 
    @Operation(summary = "执行巡检")
    @Log(title = "线路巡检", businessType = BusinessType.UPDATE)
    @PutMapping("/execute")
    public R execute(@RequestBody SafeLineInspectionExecuteDto executeDto) {
        try {
            return R.ok(safeLineInspectionService.executeInspection(executeDto));
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        }
    }
 
    @Operation(summary = "删除线路巡检任务")
    @Log(title = "线路巡检", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R delSafeLineInspection(@PathVariable List<Integer> ids) {
        for (Integer id : ids) {
            long recordCount = safeLineInspectionRecordService.lambdaQuery()
                    .eq(SafeLineInspectionRecord::getInspectionId, id).count();
            long hazardCount = safeLineInspectionHazardService.lambdaQuery()
                    .eq(SafeLineInspectionHazard::getInspectionId, id).count();
            if (recordCount > 0 || hazardCount > 0) {
                return R.fail("该巡检任务存在关联的巡检记录或隐患,无法删除");
            }
        }
        return R.ok(safeLineInspectionService.removeBatchByIds(ids));
    }
}