教育训练处考试制证系统后端
huangzhen
2023-09-25 1614615fc9319b8626eb028598b894311992c033
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package com.gkhy.exam.noncoalmine.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.noncoalmine.entity.*;
import com.gkhy.exam.noncoalmine.mapper.NcStaffMapper;
import com.gkhy.exam.noncoalmine.model.addForm.NcExamineesAddForm;
import com.gkhy.exam.noncoalmine.model.addForm.NcStaffAddForm;
import com.gkhy.exam.noncoalmine.model.addForm.NcStaffResumeAddForm;
import com.gkhy.exam.noncoalmine.model.addForm.NcStaffTrainAddForm;
import com.gkhy.exam.noncoalmine.model.modForm.NcStaffModForm;
import com.gkhy.exam.noncoalmine.model.query.NcStaffQuery;
import com.gkhy.exam.noncoalmine.model.vo.NcStaffVO;
import com.gkhy.exam.noncoalmine.model.vo.ViolationRegistrationVO;
import com.gkhy.exam.noncoalmine.model.vo.WorkRegistrationVO;
import com.gkhy.exam.noncoalmine.service.*;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * (NcStaff)表服务实现类
 *
 * @author makejava
 * @since 2023-09-18 09:59:58
 */
@Service("ncStaffService")
public class NcStaffServiceImpl extends ServiceImpl<NcStaffMapper, NcStaff> implements NcStaffService {
    @Autowired
    private NcStaffMapper ncStaffMapper;
    @Autowired
    private NcStaffResumeService ncStaffResumeService;
    @Autowired
    private NcStaffTrainService ncStaffTrainService;
    @Autowired
    private NcExamineesService ncExamineesService;
    @Autowired
    private ViolationRegistrationService violationRegistrationService;
    @Autowired
    private WorkRegistrationService workRegistrationService;
    @Autowired
    private NcCertService ncCertService;
    @Override
    public List<NcStaffVO> selectStaffList(NcStaffQuery query) {
        List<NcStaffVO> staffVOList = ncStaffMapper.getList(query);
        for (NcStaffVO ncStaffVO : staffVOList) {
            List<NcStaffResume> resumeList = ncStaffResumeService.getByStaffId(ncStaffVO.getId());
            List<NcStaffTrain> trainList = ncStaffTrainService.getByStaffId(ncStaffVO.getId());
            List<NcExaminees> examineesList = ncExamineesService.getByIdCard(ncStaffVO.getIdCardNum());
            List<ViolationRegistrationVO> violationList = violationRegistrationService.getByIdCard(ncStaffVO.getIdCardNum(), (byte) 0);
            List<WorkRegistrationVO> workList = workRegistrationService.getByIdCard(ncStaffVO.getIdCardNum(), (byte) 0);
            List<NcCert> certList = ncCertService.getByIdCard(ncStaffVO.getIdCardNum());
            ncStaffVO.setResumeList(resumeList);
            ncStaffVO.setTrainList(trainList);
            ncStaffVO.setExamineeList(examineesList);
            ncStaffVO.setViolationList(violationList);
            ncStaffVO.setWorkList(workList);
            ncStaffVO.setCertList(certList);
            ncStaffVO.setCertCount(certList.size());
            ncStaffVO.setViolationCount(violationList.size());
            ncStaffVO.setWorkCount(workList.size());
        }
        return staffVOList;
    }
 
    /**
     * 新增
     * @param addForm
     * @return
     */
    @Transactional
    @Override
    public int add(NcStaffAddForm addForm) {
        if(isExist(addForm.getIdCardNum(),null)){
            throw new ServiceException("人员已存在");
        }
        NcStaff ncStaff = new NcStaff();
        BeanUtils.copyProperties(addForm,ncStaff);
        ncStaff.setDelFlag((byte) 0);
        //插入人员
        int i = ncStaffMapper.insert(ncStaff);
        //插入个人履历
        saveBatchResume(addForm.getResumeList(),ncStaff.getId());
        //插入培训经历
        saveBatchTrain(addForm.getTrainList(),ncStaff);
        //考试经历
        saveBatchExam(addForm.getExamineeList(),ncStaff);
        return i;
    }
 
    /**
     * 修改
     * @param modForm
     */
    @Transactional
    @Override
    public void mod(NcStaffModForm modForm) {
        if(isExist(modForm.getIdCardNum(),modForm.getId())){
            throw new ServiceException("人员已存在");
        }
        NcStaff ncStaff = new NcStaff();
        BeanUtils.copyProperties(modForm,ncStaff);
        //修改人员
        ncStaffMapper.updateById(ncStaff);
        //个人履历
        updateBatchResume(modForm.getResumeList(),ncStaff);
        //插入培训经历
        updateBatchTrain(modForm.getTrainList(),ncStaff);
        //考试经历
        updateBatchExam(modForm.getExamineeList(),ncStaff);
 
    }
    private boolean isExist(String idCard,Long stuffId){
        NcStaff ncStaff = baseMapper.selectOne(new LambdaQueryWrapper<NcStaff>()
                .eq(NcStaff::getDelFlag, (byte) 0)
                .eq(NcStaff::getIdCardNum, idCard)
                .ne(stuffId != null,NcStaff::getId,stuffId));
        if(ncStaff != null){
            return true;
        }
        return false;
 
    }
 
    @Override
    public NcStaff getByIdCard(String idCard) {
        NcStaff ncStaff = baseMapper.selectOne(new LambdaQueryWrapper<NcStaff>()
                .eq(NcStaff::getDelFlag, (byte) 0)
                .eq(NcStaff::getIdCardNum, idCard));
        return ncStaff;
    }
 
    @Override
    public void delBatch(List<Long> staffIds) {
        UpdateWrapper<NcStaff> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",staffIds)
                .set("del_flag",(byte)2);
        this.update(updateWrapper);
    }
 
    //修改履历
    public void updateBatchResume(List<NcStaffResumeAddForm> resumeFormList,NcStaff ncStaff){
        if(!CollectionUtils.isEmpty(resumeFormList)){
            //历史履历
            List<NcStaffResume> oldResumeList = ncStaffResumeService.list(new LambdaQueryWrapper<NcStaffResume>()
                    .eq(NcStaffResume::getDelFlag, (byte) 0)
                    .eq(NcStaffResume::getStaffId, ncStaff.getId()));
            if(CollectionUtils.isEmpty(oldResumeList)){
                //新增
                saveBatchResume(resumeFormList, ncStaff.getId());
            }else {
 
                //新增
                List<NcStaffResumeAddForm> addResumeList = resumeFormList
                        .stream()
                        .filter(resume -> resume.getId() == null)
                        .collect(Collectors.toList());
                saveBatchResume(addResumeList, ncStaff.getId());
                //修改
                List<NcStaffResumeAddForm> modResumeList = resumeFormList
                        .stream()
                        .filter(resume -> resume.getId() != null)
                        .collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(modResumeList)){
                    List<NcStaffResume> collect = modResumeList.stream().map(modResume -> {
                        NcStaffResume ncStaffResume = new NcStaffResume();
                        BeanUtils.copyProperties(modResume, ncStaffResume);
                        return ncStaffResume;
                    }).collect(Collectors.toList());
                    ncStaffResumeService.updateBatchById(collect);
                }
                //删除
                for (NcStaffResume ncStaffResume : oldResumeList) {
                    List<NcStaffResumeAddForm> selectList = resumeFormList
                            .stream()
                            .filter(resumeAddForm -> resumeAddForm.getId() != null && resumeAddForm.getId().equals(ncStaffResume.getId()))
                            .collect(Collectors.toList());
                    if(selectList.size() == 0){
                        ncStaffResume.setDelFlag((byte) 2);
                        ncStaffResumeService.updateById(ncStaffResume);
                    }
                }
            }
        }
    }
    //插入履历
    private void saveBatchResume(List<NcStaffResumeAddForm> resumeAddFormList, Long stuffId) {
        if(!CollectionUtils.isEmpty(resumeAddFormList)){
            List<NcStaffResume> resumeList = resumeAddFormList.stream().map(resume -> {
                NcStaffResume ncStaffResume = new NcStaffResume();
                BeanUtils.copyProperties(resume, ncStaffResume);
                ncStaffResume.setStaffId(stuffId);
                ncStaffResume.setDelFlag((byte) 0);
                return ncStaffResume;
            }).collect(Collectors.toList());
            ncStaffResumeService.saveBatch(resumeList);
        }
    }
    //修改考试经历
    public void updateBatchTrain(List<NcStaffTrainAddForm> trainFormList,NcStaff ncStaff){
        if(!CollectionUtils.isEmpty(trainFormList)){
            //历史履历
            List<NcStaffTrain> oldList = ncStaffTrainService.list(new LambdaQueryWrapper<NcStaffTrain>()
                    .eq(NcStaffTrain::getDelFlag, (byte) 0)
                    .eq(NcStaffTrain::getStaffId, ncStaff.getId()));
            if(CollectionUtils.isEmpty(oldList)){
                //新增
                saveBatchTrain(trainFormList, ncStaff);
            }else {
                //新增
                List<NcStaffTrainAddForm> addList = trainFormList
                        .stream()
                        .filter(resume -> resume.getId() == null)
                        .collect(Collectors.toList());
                saveBatchTrain(addList, ncStaff);
                //修改
                List<NcStaffTrainAddForm> modList = trainFormList
                        .stream()
                        .filter(resume -> resume.getId() != null)
                        .collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(modList)){
                    List<NcStaffTrain> collect = modList.stream().map(modTrain -> {
                        NcStaffTrain ncStaffTrain = new NcStaffTrain();
                        BeanUtils.copyProperties(modTrain, ncStaffTrain);
                        ncStaffTrain.setIdCardNum(ncStaff.getIdCardNum());
                        ncStaffTrain.setName(ncStaff.getName());
                        return ncStaffTrain;
                    }).collect(Collectors.toList());
                    ncStaffTrainService.updateBatchById(collect);
                }
                //删除
                for (NcStaffTrain ncStaffTrain : oldList) {
                    List<NcStaffTrainAddForm> selectList = trainFormList
                            .stream()
                            .filter(trainForm -> trainForm.getId() != null && trainForm.getId().equals(ncStaffTrain.getId()))
                            .collect(Collectors.toList());
                    if(selectList.size() == 0){
                        ncStaffTrain.setDelFlag((byte) 2);
                        ncStaffTrainService.updateById(ncStaffTrain);
                    }
                }
            }
        }
    }
 
    //插入考试经历
    private void saveBatchTrain(List<NcStaffTrainAddForm> trainAddFormList, NcStaff ncStaff) {
        if(!CollectionUtils.isEmpty(trainAddFormList)){
            List<NcStaffTrain> trainList = trainAddFormList.stream().map(train -> {
                NcStaffTrain ncStaffTrain = new NcStaffTrain();
                BeanUtils.copyProperties(train, ncStaffTrain);
                ncStaffTrain.setStaffId(ncStaff.getId());
                ncStaffTrain.setIdCardNum(ncStaff.getIdCardNum());
                ncStaffTrain.setName(ncStaff.getName());
                ncStaffTrain.setDelFlag((byte) 0);
                return ncStaffTrain;
            }).collect(Collectors.toList());
            ncStaffTrainService.saveBatch(trainList);
        }
    }
    //修改培训经历
    public void updateBatchExam(List<NcExamineesAddForm> examFormList,NcStaff ncStaff){
        if(!CollectionUtils.isEmpty(examFormList)){
            //历史履历
            List<NcExaminees> oldList = ncExamineesService.list(new LambdaQueryWrapper<NcExaminees>()
                    .eq(NcExaminees::getDelFlag, (byte) 0)
                    .eq(NcExaminees::getStaffId, ncStaff.getId()));
            if(CollectionUtils.isEmpty(oldList)){
                //新增
                saveBatchExam(examFormList, ncStaff);
            }else {
                //新增
                List<NcExamineesAddForm> addList = examFormList
                        .stream()
                        .filter(resume -> resume.getId() == null)
                        .collect(Collectors.toList());
                saveBatchExam(addList, ncStaff);
                //修改
                List<NcExamineesAddForm> modList = examFormList
                        .stream()
                        .filter(resume -> resume.getId() != null)
                        .collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(modList)){
                    List<NcExaminees> collect = modList.stream().map(modExam -> {
                        NcExaminees ncExaminees = new NcExaminees();
                        BeanUtils.copyProperties(modExam, ncExaminees);
                        ncExaminees.setStaffId(ncStaff.getId());
                        ncExaminees.setDelFlag((byte) 0);
                        ncExaminees.setCardNum(ncStaff.getIdCardNum());
                        ncExaminees.setCardName("身份证");
                        ncExaminees.setCardType("01");
                        return ncExaminees;
                    }).collect(Collectors.toList());
                    ncExamineesService.updateBatchById(collect);
                }
                //删除
                for (NcExaminees ncExaminees : oldList) {
                    List<NcExamineesAddForm> selectList = examFormList
                            .stream()
                            .filter(examForm -> examForm.getId() != null && examForm.getId().equals(ncExaminees.getId()))
                            .collect(Collectors.toList());
                    if(selectList.size() == 0){
                        ncExaminees.setDelFlag((byte) 2);
                        ncExamineesService.updateById(ncExaminees);
                    }
                }
            }
        }
    }
    //插入培训经历
    private void saveBatchExam(List<NcExamineesAddForm> examineesAddFormList, NcStaff ncStaff) {
        if(!CollectionUtils.isEmpty(examineesAddFormList)){
            List<NcExaminees> examineesList = examineesAddFormList.stream().map(examinees -> {
                NcExaminees ncExaminees = new NcExaminees();
                BeanUtils.copyProperties(examinees, ncExaminees);
                ncExaminees.setStaffId(ncStaff.getId());
                ncExaminees.setDelFlag((byte) 0);
                ncExaminees.setCardNum(ncStaff.getIdCardNum());
                ncExaminees.setName(ncStaff.getName());
                ncExaminees.setCardName("身份证");
                ncExaminees.setCardType("01");
                ncExaminees.setSex(ncStaff.getSex());
                return ncExaminees;
            }).collect(Collectors.toList());
            ncExamineesService.saveBatch(examineesList);
        }
    }
 
}