tuqin
6 天以前 c35bd84886fb0c4e4ee0ffeeb3819a8a8df532b5
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
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.training.MesSafetyTrainingAttendeeRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.training.MesSafetyTrainingAttendeeSaveReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.training.MesSafetyTrainingPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.training.MesSafetyTrainingRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.training.MesSafetyTrainingSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.MesSafetyTrainingAttendeeDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.MesSafetyTrainingDO;
import cn.iocoder.yudao.module.mes.service.safety.MesSafetyTrainingService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
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.util.*;
import java.util.stream.Collectors;
 
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 
/**
 * 安全生产 - 安全教育培训 Controller
 *
 * @author 超级管理员
 */
@Tag(name = "管理后台 - 安全生产-安全教育培训")
@RestController
@RequestMapping("/mes/safety/training")
@Validated
public class MesSafetyTrainingController {
 
    @Resource
    private MesSafetyTrainingService trainingService;
 
    @Resource
    private AdminUserApi adminUserApi;
 
    @Resource
    private DeptApi deptApi;
 
    @PostMapping("/create")
    @Operation(summary = "创建培训批次")
    @PreAuthorize("@ss.hasPermission('mes:safety-training:create')")
    public CommonResult<Long> createTraining(@Valid @RequestBody MesSafetyTrainingSaveReqVO createReqVO) {
        return success(trainingService.createTraining(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新培训批次")
    @PreAuthorize("@ss.hasPermission('mes:safety-training:update')")
    public CommonResult<Boolean> updateTraining(@Valid @RequestBody MesSafetyTrainingSaveReqVO updateReqVO) {
        trainingService.updateTraining(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除培训批次")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:safety-training:delete')")
    public CommonResult<Boolean> deleteTraining(@RequestParam("id") Long id) {
        trainingService.deleteTraining(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得培训批次")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:safety-training:query')")
    public CommonResult<MesSafetyTrainingRespVO> getTraining(@RequestParam("id") Long id) {
        MesSafetyTrainingDO training = trainingService.getTraining(id);
        return success(training == null ? null : BeanUtils.toBean(training, MesSafetyTrainingRespVO.class));
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得培训批次分页")
    @PreAuthorize("@ss.hasPermission('mes:safety-training:query')")
    public CommonResult<PageResult<MesSafetyTrainingRespVO>> getTrainingPage(@Valid MesSafetyTrainingPageReqVO pageReqVO) {
        PageResult<MesSafetyTrainingDO> pageResult = trainingService.getTrainingPage(pageReqVO);
        return success(new PageResult<>(
                BeanUtils.toBean(pageResult.getList(), MesSafetyTrainingRespVO.class),
                pageResult.getTotal()));
    }
 
    // ==================== 参训人员 ====================
 
    @GetMapping("/attendee-list")
    @Operation(summary = "获得参训人员列表")
    @Parameter(name = "trainingId", description = "培训批次编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:safety-training:query')")
    public CommonResult<List<MesSafetyTrainingAttendeeRespVO>> getAttendeeList(@RequestParam("trainingId") Long trainingId) {
        return success(buildAttendeeRespList(trainingService.getAttendeeList(trainingId)));
    }
 
    @PostMapping("/save-attendees")
    @Operation(summary = "全量保存参训人员")
    @Parameter(name = "trainingId", description = "培训批次编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:safety-training:attendee')")
    public CommonResult<Boolean> saveAttendees(@RequestParam("trainingId") Long trainingId,
                                               @RequestBody List<MesSafetyTrainingAttendeeSaveReqVO> attendees) {
        trainingService.saveAttendees(trainingId, attendees);
        return success(true);
    }
 
    @PutMapping("/update-attendee")
    @Operation(summary = "更新参训人员签到/成绩")
    @PreAuthorize("@ss.hasPermission('mes:safety-training:attendee')")
    public CommonResult<Boolean> updateAttendee(@Valid @RequestBody MesSafetyTrainingAttendeeSaveReqVO reqVO) {
        trainingService.updateAttendee(reqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete-attendee")
    @Operation(summary = "删除参训人员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:safety-training:attendee')")
    public CommonResult<Boolean> deleteAttendee(@RequestParam("id") Long id) {
        trainingService.deleteAttendee(id);
        return success(true);
    }
 
    private List<MesSafetyTrainingAttendeeRespVO> buildAttendeeRespList(List<MesSafetyTrainingAttendeeDO> list) {
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        List<MesSafetyTrainingAttendeeRespVO> respList = BeanUtils.toBean(list, MesSafetyTrainingAttendeeRespVO.class);
 
        Set<Long> userIds = list.stream().map(MesSafetyTrainingAttendeeDO::getUserId)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        Map<Long, AdminUserRespDTO> userMap = userIds.isEmpty()
                ? Collections.emptyMap() : adminUserApi.getUserMap(userIds);
 
        Set<Long> deptIds = list.stream().map(MesSafetyTrainingAttendeeDO::getDeptId)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        Map<Long, String> deptNameMap = new HashMap<>();
        if (!deptIds.isEmpty()) {
            List<DeptRespDTO> depts = deptApi.getDeptList(deptIds);
            if (depts != null) {
                depts.forEach(d -> deptNameMap.put(d.getId(), d.getName()));
            }
        }
 
        for (MesSafetyTrainingAttendeeRespVO vo : respList) {
            AdminUserRespDTO user = userMap.get(vo.getUserId());
            if (user != null) {
                vo.setUserName(user.getNickname());
            }
            vo.setDeptName(deptNameMap.get(vo.getDeptId()));
        }
        return respList;
    }
 
}