zhangfeng
2022-09-21 fee66fde68610850d4c8c52df022b9d53a0cd3f7
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package com.gkhy.safePlatform.equipment.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
import com.gkhy.safePlatform.equipment.entity.*;
import com.gkhy.safePlatform.equipment.enums.EquipmentResultCodes;
import com.gkhy.safePlatform.equipment.enums.KeypointEquipmentPartTypeEnum;
import com.gkhy.safePlatform.equipment.excepiton.EquipmentException;
import com.gkhy.safePlatform.equipment.model.dto.req.*;
import com.gkhy.safePlatform.equipment.model.dto.resp.KeypointEquipmentInfoExcel;
import com.gkhy.safePlatform.equipment.repository.EquipmentTypeMngRepository;
import com.gkhy.safePlatform.equipment.repository.KeypointEquipmentInfoRepository;
import com.gkhy.safePlatform.equipment.service.*;
import com.gkhy.safePlatform.equipment.service.baseService.*;
import com.gkhy.safePlatform.equipment.utils.DateUtils;
import com.gkhy.safePlatform.equipment.utils.poihelper.ExcelLogs;
import com.gkhy.safePlatform.equipment.utils.poihelper.ExcelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.equipment.model.dto.resp.KeypointEquipmentInfoDto;
import com.gkhy.safePlatform.equipment.utils.QueryHelpPlus;
import com.gkhy.safePlatform.commons.utils.BeanCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 重点监管装置/设备 详细信息(KeypointEquipmentInfo)表服务实现类
 *
 * @author xurui
 * @since 2022-07-19 13:36:27
 */
@Service("keypointEquipmentInfoService")
public class KeypointEquipmentInfoServiceImpl implements KeypointEquipmentInfoService {
 
    @Autowired
    private KeypointEquipmentInfoBaseService keypointEquipmentInfoBaseService;
 
 
    @Autowired
    private EquipmentTakecareDetailBaseService equipmentTakecareDetailBaseService;
 
    @Autowired
    private EquipmentTestDetailBaseService equipmentTestDetailBaseService;
 
    @Autowired
    private EquipmentRepairDetailBaseService equipmentRepairDetailBaseService;
 
    @Autowired
    private EquipmentTakecareStardardDetailBaseService equipmentTakecareStardardDetailBaseService;
 
    @Autowired
    private EquipmentCheckStandardDetailBaseService equipmentCheckStandardDetailBaseService;
 
    @DubboReference(check = false)
    private AccountDepartmentService accountDepartmentService;
 
    @Autowired
    private EquipmentInfoService equipmentInfoService;
 
    @Autowired
    private EquipmentTypeMngRepository equipmentTypeMngRepository;
 
    @Autowired
    public HttpServletResponse response;
 
    @Override
    public ResultVO queryAll(PageQuery<KeypointEquipmentInfoQueryCriteria> pageQuery) {
        Long pageIndex = pageQuery.getPageIndex();
        Long pageSize = pageQuery.getPageSize();
        IPage<KeypointEquipmentInfo> page = new Page<>(pageIndex, pageSize);
 
        page = keypointEquipmentInfoBaseService.selectPage(page,
                QueryHelpPlus.getPredicate(KeypointEquipmentInfo.class, pageQuery.getSearchParams()));
        List<KeypointEquipmentInfoDto> respList = BeanCopyUtils.copyBeanList(page.getRecords(), KeypointEquipmentInfoDto.class);
 
        Set<Long> collectTypeIdSet = new HashSet();
        //获取额外信息
        respList.forEach(f->{
            //设备保养
            EquipmentTakecareDetailQueryCriteria criteria = new EquipmentTakecareDetailQueryCriteria();
            criteria.setEquipmentId(f.getId());
            criteria.setInfoTpe(3);
            List<EquipmentTakecareDetail> takecareDetailList = equipmentTakecareDetailBaseService.queryAll(criteria);
            f.setTakecareDetailList(takecareDetailList);
 
            //设备检测
            EquipmentTestDetailQueryCriteria criteria1 = new EquipmentTestDetailQueryCriteria();
            criteria1.setEquipmentId(f.getId());
            criteria1.setInfoTpe(3);
            List<EquipmentTestDetail> checkDetailList = equipmentTestDetailBaseService.queryAll(criteria1);
            f.setCheckDetailList(checkDetailList);
 
            //设备检查 TODO
 
 
            //设备维修
            EquipmentRepairDetailQueryCriteria criteria2 = new EquipmentRepairDetailQueryCriteria();
            criteria2.setEquipmentId(f.getId());
            criteria2.setInfoTpe(3);
            List<EquipmentRepairDetail> repairDetailList = equipmentRepairDetailBaseService.queryAll(criteria2);
            f.setRepaireDetailList(repairDetailList);
 
            // --------------------------- 获取设备设施类型管理ID-----------------------
            collectTypeIdSet.add(f.getEquipmentTypeId());
 
            // --------------------------- 获取部门信息-----------------------
            //收集所用到的部门ID
            Set<Long> collectDepIdSet = new HashSet();
            collectDepIdSet.add(f.getDepartmentId());
            takecareDetailList.forEach(info->{collectDepIdSet.add(info.getLeadingPersonDepartmentId());});
            checkDetailList.forEach(info->{collectDepIdSet.add(info.getTestPersonDepartmentId());});
            repairDetailList.forEach(info->{collectDepIdSet.add(info.getRepairPersonDepartmentId());});
 
            //获取部门名集合
            Map<Long,String> depNameMap = equipmentInfoService.getDepName(collectDepIdSet);
 
            //set
            f.setDepartmentName(depNameMap.get(f.getDepartmentId()));
            takecareDetailList.forEach(info->{info.setLeadingPersonDepartmentName(depNameMap.get(info.getLeadingPersonDepartmentId()));});
            checkDetailList.forEach(info->{info.setTestPersonDepartmentName(depNameMap.get(info.getTestPersonDepartmentId()));});
            repairDetailList.forEach(info->{info.setRepairPersonDepartmentName(depNameMap.get(info.getRepairPersonDepartmentId()));});
        });
 
 
        //获取设备设施类型名称
        if(!collectTypeIdSet.isEmpty()){
            List<EquipmentTypeMng> typeMngList = equipmentTypeMngRepository.selectList(new QueryWrapper<EquipmentTypeMng>().in("id",collectTypeIdSet));
            Map<Long, String> typePool = new HashMap<>();
            typeMngList.forEach(f->{
                typePool.put(f.getId(),f.getTypeName());
            });
            respList.forEach(f->{
                f.setEquipmentTypeName(typePool.get(f.getEquipmentTypeId()));
            });
        }
 
        return new SearchResultVO<>(
                true,
                pageIndex,
                pageSize,page.getPages(),
                page.getTotal(),
                respList,
                ResultCodes.OK
        );
    }
 
 
    @Override
    public List<KeypointEquipmentInfo> queryAll(KeypointEquipmentInfoQueryCriteria criteria) {
        return keypointEquipmentInfoBaseService.queryAll( criteria);
    }
 
    @Override
    public KeypointEquipmentInfoDto selectOne(Serializable id) {
        KeypointEquipmentInfo keypointEquipmentInfo = keypointEquipmentInfoBaseService.getById(id);
        if(keypointEquipmentInfo == null){
            return null;
        }
 
        KeypointEquipmentInfoDto infoDto = BeanCopyUtils.copyBean(keypointEquipmentInfo, KeypointEquipmentInfoDto.class);
 
        //设备保养
        EquipmentTakecareDetailQueryCriteria criteria = new EquipmentTakecareDetailQueryCriteria();
        criteria.setEquipmentId(infoDto.getId());
        criteria.setInfoTpe(3);
        List<EquipmentTakecareDetail> takecareDetailList = equipmentTakecareDetailBaseService.queryAll(criteria);
        infoDto.setTakecareDetailList(takecareDetailList);
 
        //设备检测
        EquipmentTestDetailQueryCriteria criteria1 = new EquipmentTestDetailQueryCriteria();
        criteria1.setEquipmentId(infoDto.getId());
        criteria1.setInfoTpe(3);
        List<EquipmentTestDetail> checkDetailList = equipmentTestDetailBaseService.queryAll(criteria1);
        infoDto.setCheckDetailList(checkDetailList);
 
        //设备检查 TODO
 
 
        //设备维修
        EquipmentRepairDetailQueryCriteria criteria2 = new EquipmentRepairDetailQueryCriteria();
        criteria2.setEquipmentId(infoDto.getId());
        criteria2.setInfoTpe(3);
        List<EquipmentRepairDetail> repairDetailList = equipmentRepairDetailBaseService.queryAll(criteria2);
        infoDto.setRepaireDetailList(repairDetailList);
 
        //检查标准
        EquipmentCheckStandardDetailQueryCriteria criteria3 = new EquipmentCheckStandardDetailQueryCriteria();
        criteria3.setEquipmentId(infoDto.getId());
        criteria3.setInfoTpe(3);
        List<EquipmentCheckStandardDetail> checkStandardDetailList = equipmentCheckStandardDetailBaseService.queryAll(criteria3);
        infoDto.setCheckStandardeDetailList(checkStandardDetailList);
 
        //保养标准
        EquipmentTakecareStardardDetailQueryCriteria criteria4 = new EquipmentTakecareStardardDetailQueryCriteria();
        criteria4.setEquipmentId(infoDto.getId());
        criteria4.setInfoTpe(3);
        List<EquipmentTakecareStardardDetail> takecareStardardDetailList = equipmentTakecareStardardDetailBaseService.queryAll(criteria4);
        infoDto.setTakecareStardardeDetailList(takecareStardardDetailList);
 
        // --------------------------- 获取部门信息-----------------------
        //收集所用到的部门ID
        Set<Long> collectDepIdSet = new HashSet();
        collectDepIdSet.add(infoDto.getDepartmentId());
        takecareDetailList.forEach(info->{collectDepIdSet.add(info.getLeadingPersonDepartmentId());});
        checkDetailList.forEach(info->{collectDepIdSet.add(info.getTestPersonDepartmentId());});
        repairDetailList.forEach(info->{collectDepIdSet.add(info.getRepairPersonDepartmentId());});
 
        //获取部门名集合
        Map<Long,String> depNameMap = equipmentInfoService.getDepName(collectDepIdSet);
 
        //set
        infoDto.setDepartmentName(depNameMap.get(infoDto.getDepartmentId()));
        takecareDetailList.forEach(info->{info.setLeadingPersonDepartmentName(depNameMap.get(info.getLeadingPersonDepartmentId()));});
        checkDetailList.forEach(info->{info.setTestPersonDepartmentName(depNameMap.get(info.getTestPersonDepartmentId()));});
        repairDetailList.forEach(info->{info.setRepairPersonDepartmentName(depNameMap.get(info.getRepairPersonDepartmentId()));});
 
        EquipmentTypeMng typeMng = equipmentTypeMngRepository.selectOne(new QueryWrapper<EquipmentTypeMng>().eq("id",infoDto.getEquipmentTypeId()));
        if(typeMng != null){
            infoDto.setEquipmentTypeName(typeMng.getTypeName());
        }
 
        // TODO 调用应急预案RPC
        infoDto.setEmergencePlanName("");
        return infoDto;
    }
 
    @Override
    public void addOrUpdate(KeypointEquipmentInfoDto infoDto) {
 
        // 验证枚举
        checkEnum(infoDto);
 
        KeypointEquipmentInfo equipmentInfo = BeanCopyUtils.copyBean(infoDto, KeypointEquipmentInfo.class);
        //保存主数据
        if (infoDto.getId() == null) { //新增
            keypointEquipmentInfoBaseService.save(equipmentInfo);
        } else {
            keypointEquipmentInfoBaseService.update(equipmentInfo,new UpdateWrapper<KeypointEquipmentInfo>().eq("id",infoDto.getId()));
        }
 
 
        //保存设备保养
        List<EquipmentTakecareDetail> takecareDetailList = infoDto.getTakecareDetailList();
        takecareDetailList.forEach(f->{
            f.setEquipmentId(equipmentInfo.getId());
            f.setInfoTpe(3);
        });
        equipmentTakecareDetailBaseService.saveOrUpdateBatch(takecareDetailList);
        if(StringUtils.hasText(infoDto.getDelTakecareDetails())){
            List<Long> idList = Arrays.stream(infoDto.getDelTakecareDetails().split(",")).map(s-> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
 
            List<EquipmentTakecareDetail> delList = new ArrayList<>();
            idList.forEach(f->{
                EquipmentTakecareDetail info = new EquipmentTakecareDetail();
                info.setDelFlag(1);
                info.setId(f);
                delList.add(info);
            });
            equipmentTakecareDetailBaseService.updateBatchById(delList);
        }
 
        //保存设备检测
        List<EquipmentTestDetail> testDetailList = infoDto.getTestDetailList();
        testDetailList.forEach(f->{
            f.setEquipmentId(equipmentInfo.getId());
            f.setInfoTpe(3);
        });
        equipmentTestDetailBaseService.saveOrUpdateBatch(testDetailList);
        if(StringUtils.hasText(infoDto.getDelTestDetails())){
            List<Long> idList = Arrays.stream(infoDto.getDelTestDetails().split(",")).map(s-> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
            List<EquipmentTestDetail> delList = new ArrayList<>();
            idList.forEach(f->{
                EquipmentTestDetail info = new EquipmentTestDetail();
                info.setDelFlag(1);
                info.setId(f);
                delList.add(info);
            });
            equipmentTestDetailBaseService.updateBatchById(delList);
 
        }
        //设备检查 TODO
 
 
        //保存设备维修
        List<EquipmentRepairDetail> repaireDetailList = infoDto.getRepaireDetailList();
        repaireDetailList.forEach(f->{
            f.setEquipmentId(equipmentInfo.getId());
            f.setInfoTpe(3);
        });
        equipmentRepairDetailBaseService.saveOrUpdateBatch(repaireDetailList);
        if(StringUtils.hasText(infoDto.getDelRepaireDetails())){
            List<Long> idList = Arrays.stream(infoDto.getDelRepaireDetails().split(",")).map(s-> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
            List<EquipmentRepairDetail> delList = new ArrayList<>();
            idList.forEach(f->{
                EquipmentRepairDetail info = new EquipmentRepairDetail();
                info.setDelFlag(1);
                info.setId(f);
                delList.add(info);
            });
            equipmentRepairDetailBaseService.updateBatchById(delList);
 
        }
        //保存检查标准
        List<EquipmentCheckStandardDetail> checkStandardeDetailList = infoDto.getCheckStandardeDetailList();
        checkStandardeDetailList.forEach(f->{
            f.setEquipmentId(equipmentInfo.getId());
            f.setInfoTpe(3);
        });
        equipmentCheckStandardDetailBaseService.saveOrUpdateBatch(checkStandardeDetailList);
        if(StringUtils.hasText(infoDto.getDelCheckStandardeDetails())){
            List<Long> idList = Arrays.stream(infoDto.getDelCheckStandardeDetails().split(",")).map(s-> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
            List<EquipmentCheckStandardDetail> delList = new ArrayList<>();
            idList.forEach(f->{
                EquipmentCheckStandardDetail info = new EquipmentCheckStandardDetail();
                info.setDelFlag(1);
                info.setId(f);
                delList.add(info);
            });
            equipmentCheckStandardDetailBaseService.updateBatchById(delList);
 
        }
        //保存保养标准
        List<EquipmentTakecareStardardDetail> takecareStardardeDetailList = infoDto.getTakecareStardardeDetailList();
        takecareStardardeDetailList.forEach(f->{
            f.setEquipmentId(equipmentInfo.getId());
            f.setInfoTpe(3);
        });
        equipmentTakecareStardardDetailBaseService.saveOrUpdateBatch(takecareStardardeDetailList);
        if(StringUtils.hasText(infoDto.getDelTakecareStardardeDetails())){
            List<Long> idList = Arrays.stream(infoDto.getDelTakecareStardardeDetails().split(",")).map(s-> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
            List<EquipmentTakecareStardardDetail> delList = new ArrayList<>();
            idList.forEach(f->{
                EquipmentTakecareStardardDetail info = new EquipmentTakecareStardardDetail();
                info.setDelFlag(1);
                info.setId(f);
                delList.add(info);
            });
            equipmentTakecareStardardDetailBaseService.updateBatchById(delList);
 
        }
    }
 
    @Override
    public void importData(MultipartFile file) throws IOException {
        String contentType = file.getContentType();
        if(!"application/vnd.ms-excel".equals(contentType)
                && !"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(contentType)) {
            throw new EquipmentException("上传的excel格式错误");
        }
 
        Collection<KeypointEquipmentInfoImportExcel> importExcel = ExcelUtil.importExcel(KeypointEquipmentInfoImportExcel.class, file.getInputStream(), "yyyy-MM-dd HH:mm:ss", new ExcelLogs() , 0);
 
        if (CollectionUtils.isEmpty(importExcel)) {
            return ;
        }
 
        List<KeypointEquipmentInfo> respList = BeanCopyUtils.copyBeanList((List<KeypointEquipmentInfoImportExcel>)importExcel, KeypointEquipmentInfo.class);
 
        keypointEquipmentInfoBaseService.saveBatch(respList);
    }
 
    @Override
    public void delete(Long[] ids) {
        if(ids == null){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_ILLEGAL);
        }
        List<Long> idList = Arrays.asList(ids);
 
        List<KeypointEquipmentInfo> delList = new ArrayList<>();
        idList.forEach(f->{
            KeypointEquipmentInfo info = new KeypointEquipmentInfo();
            info.setDelFlag(1);
            info.setId(f);
            delList.add(info);
        });
        keypointEquipmentInfoBaseService.updateBatchById(delList);
    }
 
    @Override
    public void exportTemplate() throws IOException {
        Map<String,String> map = new LinkedHashMap<>();
        map.put("1","类型/类别外键 ");
        map.put("2","装置/部位名称");
        map.put("3","所属单位部门/外键");
        map.put("4","具体位置");
        map.put("5","负责人姓名");
        map.put("6","联系人/外键");
        map.put("7","录入人/外键");
        map.put("8","责任人/外键");
        map.put("9","装置部位分类 1:关键装置 2:重点部位");
        map.put("10","检查周期");
        map.put("11","应急预案/外键");
        map.put("12","主要危险有害因素");
        map.put("13","易导致风险");
        map.put("14","应急处置措施");
        map.put("15","现场图片地址路径");
 
 
        String fileName = URLEncoder.encode("重点监管装置设备数据导入模板.xls", "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
 
        ExcelUtil.exportExcel(map,new ArrayList<>() , response.getOutputStream());
        response.getOutputStream().close();
    }
 
    @Override
    public void exportData(KeypointEquipmentInfoQueryCriteria queryCriteria) throws IOException {
        Map<String,String> map = new LinkedHashMap<>();
        map.put("0","ID");
        map.put("1","类型/类别外键");
        map.put("2","装置/部位名称");
        map.put("3","所属单位部门/外键");
        map.put("4","具体位置");
        map.put("5","负责人姓名");
        map.put("6","联系人/外键");
        map.put("7","录入人/外键");
        map.put("8","责任人/外键");
        map.put("9","装置部位分类 1:关键装置 2:重点部位");
        map.put("10","检查周期");
        map.put("11","应急预案/外键");
        map.put("12","主要危险有害因素");
        map.put("13","易导致风险");
        map.put("14","应急处置措施");
        map.put("15","现场图片地址路径");
 
        String key = DateUtils.date2String(new Date(), DateUtils.PATTERN_ALLTIME_NOSIGN) ;
        String fileName = URLEncoder.encode("重点监管装置设备"+key+".xls", "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
 
 
        List<KeypointEquipmentInfoExcel> respList = BeanCopyUtils.copyBeanList(this.queryAll(queryCriteria), KeypointEquipmentInfoExcel.class);
 
        ExcelUtil.exportExcel(map,respList , response.getOutputStream(),DateUtils.PATTERN_STANDARD);
        response.getOutputStream().close();
    }
 
    /**
     * 枚举验证
     * @param infoDto
     */
    public void checkEnum(KeypointEquipmentInfoDto infoDto){
        if (KeypointEquipmentPartTypeEnum.getByCode(infoDto.getPartType()) == null) {
            throw new EquipmentException(EquipmentResultCodes.KEYPOINT_EQUIPMENT_ENUM_PART_TYPE_NOT_EXIST);
        }
    }
}