tuqin
6 天以前 33fe3368c6692d67e1fae5d2922ff614e5f56404
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package cn.iocoder.yudao.module.mes.controller.admin.safety;
 
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.hazard.MesSafetyHazardCheckReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.hazard.MesSafetyHazardPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.hazard.MesSafetyHazardRectifyReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.hazard.MesSafetyHazardRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.hazard.MesSafetyHazardSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.workstation.MesMdWorkshopDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.MesSafetyHazardDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.MesSafetyRiskDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.MesSafetyRiskUnitDO;
import cn.iocoder.yudao.module.mes.dal.mysql.md.workstation.MesMdWorkshopMapper;
import cn.iocoder.yudao.module.mes.service.safety.MesSafetyHazardService;
import cn.iocoder.yudao.module.mes.service.safety.MesSafetyRiskService;
import cn.iocoder.yudao.module.mes.service.safety.MesSafetyRiskUnitService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
 
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 
/**
 * 安全生产 - 隐患 Controller
 *
 * @author 超级管理员
 */
@Tag(name = "管理后台 - 安全生产-隐患排查治理")
@RestController
@RequestMapping("/mes/safety/hazard")
@Validated
public class MesSafetyHazardController {
 
    @Resource
    private MesSafetyHazardService hazardService;
 
    @Resource
    private MesSafetyRiskUnitService riskUnitService;
 
    @Resource
    private MesSafetyRiskService riskService;
 
    @Resource
    private MesMdWorkshopMapper workshopMapper;
 
    @Resource
    private AdminUserApi adminUserApi;
 
    @PostMapping("/create")
    @Operation(summary = "登记隐患")
    @PreAuthorize("@ss.hasPermission('mes:safety-hazard:create')")
    public CommonResult<Long> createHazard(@Valid @RequestBody MesSafetyHazardSaveReqVO createReqVO) {
        return success(hazardService.createHazard(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新隐患")
    @PreAuthorize("@ss.hasPermission('mes:safety-hazard:update')")
    public CommonResult<Boolean> updateHazard(@Valid @RequestBody MesSafetyHazardSaveReqVO updateReqVO) {
        hazardService.updateHazard(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除隐患")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:safety-hazard:delete')")
    public CommonResult<Boolean> deleteHazard(@RequestParam("id") Long id) {
        hazardService.deleteHazard(id);
        return success(true);
    }
 
    @PutMapping("/rectify")
    @Operation(summary = "提交整改")
    @PreAuthorize("@ss.hasPermission('mes:safety-hazard:rectify')")
    public CommonResult<Boolean> rectifyHazard(@Valid @RequestBody MesSafetyHazardRectifyReqVO reqVO) {
        hazardService.rectifyHazard(reqVO);
        return success(true);
    }
 
    @PutMapping("/check")
    @Operation(summary = "整改验收")
    @PreAuthorize("@ss.hasPermission('mes:safety-hazard:check')")
    public CommonResult<Boolean> checkHazard(@Valid @RequestBody MesSafetyHazardCheckReqVO reqVO) {
        hazardService.checkHazard(reqVO);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得隐患")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:safety-hazard:query')")
    public CommonResult<MesSafetyHazardRespVO> getHazard(@RequestParam("id") Long id) {
        MesSafetyHazardDO hazard = hazardService.getHazard(id);
        if (hazard == null) {
            return success(null);
        }
        List<MesSafetyHazardRespVO> list = buildRespList(Collections.singletonList(hazard));
        return success(list.isEmpty() ? null : list.get(0));
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得隐患分页")
    @PreAuthorize("@ss.hasPermission('mes:safety-hazard:query')")
    public CommonResult<PageResult<MesSafetyHazardRespVO>> getHazardPage(@Valid MesSafetyHazardPageReqVO pageReqVO) {
        PageResult<MesSafetyHazardDO> pageResult = hazardService.getHazardPage(pageReqVO);
        List<MesSafetyHazardRespVO> list = buildRespList(pageResult.getList());
        return success(new PageResult<>(list, pageResult.getTotal()));
    }
 
    /**
     * 转换为 VO 并填充车间、人员、关联风险信息、超期标记
     */
    private List<MesSafetyHazardRespVO> buildRespList(List<MesSafetyHazardDO> list) {
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        List<MesSafetyHazardRespVO> respList = BeanUtils.toBean(list, MesSafetyHazardRespVO.class);
 
        // 车间名称
        Set<Long> workshopIds = list.stream().map(MesSafetyHazardDO::getWorkshopId)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        Map<Long, String> workshopNameMap = new HashMap<>();
        if (!workshopIds.isEmpty()) {
            List<MesMdWorkshopDO> workshops = workshopMapper.selectByIds(workshopIds);
            workshops.forEach(w -> workshopNameMap.put(w.getId(), w.getName()));
        }
 
        // 人员姓名(发现人 / 整改责任人 / 验收人)
        Set<Long> userIds = new HashSet<>();
        list.forEach(h -> {
            if (h.getFounderUserId() != null) userIds.add(h.getFounderUserId());
            if (h.getChargeUserId() != null) userIds.add(h.getChargeUserId());
            if (h.getCheckUserId() != null) userIds.add(h.getCheckUserId());
        });
        Map<Long, AdminUserRespDTO> userMap = userIds.isEmpty()
                ? Collections.emptyMap() : adminUserApi.getUserMap(userIds);
 
        // 风险单元名称
        Set<Long> unitIds = list.stream().map(MesSafetyHazardDO::getUnitId)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        Map<Long, MesSafetyRiskUnitDO> unitMap = riskUnitService.getRiskUnitMap(unitIds);
 
        LocalDate today = LocalDate.now();
        for (MesSafetyHazardRespVO vo : respList) {
            vo.setWorkshopName(workshopNameMap.get(vo.getWorkshopId()));
            AdminUserRespDTO founder = userMap.get(vo.getFounderUserId());
            if (founder != null) vo.setFounderUserName(founder.getNickname());
            AdminUserRespDTO charge = userMap.get(vo.getChargeUserId());
            if (charge != null) vo.setChargeUserName(charge.getNickname());
            AdminUserRespDTO checker = userMap.get(vo.getCheckUserId());
            if (checker != null) vo.setCheckUserName(checker.getNickname());
 
            MesSafetyRiskUnitDO unit = unitMap.get(vo.getUnitId());
            if (unit != null) vo.setUnitName(unit.getName());
 
            if (vo.getRiskId() != null) {
                MesSafetyRiskDO risk = riskService.getRisk(vo.getRiskId());
                if (risk != null) vo.setRiskName(risk.getName());
            }
 
            // 超期判断:有期限、未闭环、且期限早于今天
            vo.setOverdue(vo.getDeadline() != null
                    && !MesSafetyHazardDO.STATUS_CLOSED.equals(vo.getStatus())
                    && vo.getDeadline().isBefore(today));
        }
        return respList;
    }
 
}