郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
package com.gkhy.safePlatform.specialWork.service.impl;
 
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountUserService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.UserInfoRPCRespDTO;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.exception.BusinessException;
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.emergency.rpc.api.EmergencyRpcAPi;
import com.gkhy.safePlatform.emergency.rpc.api.model.dto.resp.*;
import com.gkhy.safePlatform.equipment.rpc.api.EquipmentRpcAPi;
import com.gkhy.safePlatform.equipment.rpc.api.model.dto.req.MaterialReceiveRecordsRPCReq;
import com.gkhy.safePlatform.equipment.rpc.api.model.dto.resp.MaterialReceiveRecordsRPCRespDto;
import com.gkhy.safePlatform.equipment.rpc.api.model.dto.resp.SafeMaterialBsClassifyRPCRespDto;
import com.gkhy.safePlatform.equipment.rpc.api.model.dto.resp.SafeMaterialClassifyRPCRespDto;
import com.gkhy.safePlatform.equipment.rpc.api.model.dto.resp.SafeMaterialClassifyStockRPCRespDto;
import com.gkhy.safePlatform.specialWork.entity.MaterialAllocationBaseInfo;
import com.gkhy.safePlatform.specialWork.entity.MaterialDetailInfo;
import com.gkhy.safePlatform.specialWork.enums.*;
import com.gkhy.safePlatform.specialWork.model.dto.req.*;
import com.gkhy.safePlatform.specialWork.model.dto.resp.*;
import com.gkhy.safePlatform.specialWork.model.query.MaterialAllocationBaseQuery;
import com.gkhy.safePlatform.specialWork.service.MaterialAllocationBaseService;
import com.gkhy.safePlatform.specialWork.service.baseService.MaterialAllocationBaseInfoService;
import com.gkhy.safePlatform.specialWork.service.baseService.MaterialDetailInfoService;
import net.sf.jsqlparser.expression.StringValue;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.list.PredicatedList;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.client.stat.ConsumerStatsManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
@Service("MaterialAllocationBaseService")
public class MaterialAllocationBaseServiceImpl implements MaterialAllocationBaseService {
    @DubboReference(check = false)
    private EmergencyRpcAPi emergencyRpcAPi;
    @DubboReference(check = false)
    private EquipmentRpcAPi equipmentRpcAPi;
    @DubboReference(check = false)
    private AccountUserService accountUserService;
    @Autowired
    private MaterialAllocationBaseInfoService materialAllocationBaseInfoService;
 
    @Autowired
    private MaterialDetailInfoService materialDetailInfoService;
 
    /**
     * 新增
     * @param currentUser
     * @param addReqDTO
     * @return
     */
    @Transactional
    @Override
    public ResultVO save(ContextCacheUser currentUser, MaterialAllocationBaseAddReqDTO addReqDTO) {
 
        if(null == WorkTypeEnum.parse(addReqDTO.getWorkType())){
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"作业类型不合法!");
        }
        if(null != addReqDTO.getWorkLevel()){
            if(null == WorkLevelEnum.parse(addReqDTO.getWorkLevel())){
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"作业级别不合法!");
            }
        }
        List<SafeMaterialBsClassifyRPCRespDto> classifyRPCRespDtoList = new ArrayList<>();
 
        //验证数据
        List<MaterialDetailAddReqDTO> materialDetailList = addReqDTO.getMaterialDetailList();
        if(!CollectionUtils.isEmpty(materialDetailList)){
            List<Long> smallClassifyIdList = materialDetailList
                    .stream().map(MaterialDetailAddReqDTO::getSmallClassifyId)
                    .collect(Collectors.toList());
            classifyRPCRespDtoList = this.getTraceabilityClassifyList(smallClassifyIdList);
 
            for (MaterialDetailAddReqDTO materialDetail : materialDetailList) {
 
                if(null == materialDetail.getStandVal()){
                    throw new AusinessException(E.DATA_PARAM_NULL,"标准值不能为空!");
                }
                if(null == materialDetail.getConfigurationLevel()){
                    throw new AusinessException(E.DATA_PARAM_NULL,"配置级别不能为空!");
                }
                if(null == ConfigurationLevelEnum.parse(materialDetail.getConfigurationLevel())){
                    throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"配置级别不合法!");
                }
                if(null == materialDetail.getSmallClassifyId()){
                    throw new AusinessException(E.DATA_PARAM_NULL,"物资种类id不能为空!");
                }
                if(null != materialDetail.getConsumable()){
                    if(null == ConsumableEnum.getByCode(materialDetail.getConsumable())){
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"是否是耗材数据校验不合法!");
                    }else{
                        if(ConsumableEnum.YES.getCode().equals(materialDetail.getConsumable())){
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"耗材物资暂不支持物资配置!");
                        }
                    }
                }
                List<SafeMaterialBsClassifyRPCRespDto> selectList = classifyRPCRespDtoList
                        .stream()
                        .filter(classify -> classify.getSmallClassifyId().equals(materialDetail.getSmallClassifyId()))
                        .collect(Collectors.toList());
                if(selectList.size() == 0){
                    throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"物资种类不存在");
                }
 
            }
        }
 
        //配置物资表数据
        MaterialAllocationBaseInfo materialAllocationBaseInfo = new MaterialAllocationBaseInfo();
        BeanUtils.copyProperties(addReqDTO,materialAllocationBaseInfo);
        materialAllocationBaseInfo.setId(IdUtil.getSnowflake(0,0).nextId());
        materialAllocationBaseInfo.setCreateUid(currentUser.getUid());
        materialAllocationBaseInfo.setCreateUname(currentUser.getUsername());
 
        List<MaterialDetailInfo> materialDetailInfoList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(materialDetailList)){
            for (MaterialDetailAddReqDTO materialDetail : materialDetailList) {
                //物资详表数据
                MaterialDetailInfo materialDetailInfo = new MaterialDetailInfo();
                BeanUtils.copyProperties(materialDetail,materialDetailInfo);
                materialDetailInfo.setId(IdUtil.getSnowflake(0,0).nextId());
                materialDetailInfo.setCreateUid(currentUser.getUid());
                materialDetailInfo.setCreateUname(currentUser.getUsername());
                materialDetailInfo.setGmtCreate(LocalDateTime.now());
                materialDetailInfo.setMaBaseId(materialAllocationBaseInfo.getId());
                materialDetailInfo.setStatus(MaterialDetailStatusEnum.VALID.getCode());
                List<SafeMaterialBsClassifyRPCRespDto> selectList = classifyRPCRespDtoList
                        .stream()
                        .filter(classify -> classify.getSmallClassifyId().equals(materialDetail.getSmallClassifyId()))
                        .collect(Collectors.toList());
                if(selectList.size()>0){
                    materialDetailInfo.setBigClassifyId(selectList.get(0).getBigClassifyId());
                    materialDetailInfo.setBigClassifyName(selectList.get(0).getBigClassifyName());
                    materialDetailInfo.setMaterialName(selectList.get(0).getSmallClassifyName());
                }
                materialDetailInfoList.add(materialDetailInfo);
 
            }
        }
 
        //插入配置表数据
        materialAllocationBaseInfoService.saveOne(materialAllocationBaseInfo);
        //插入物资详表数据
        if(materialDetailInfoList.size()>0){
            materialDetailInfoService.saveBatch(materialDetailInfoList);
        }
        return new ResultVO<>(ResultCodes.OK);
    }
 
 
 
    @Transactional
    @Override
    public ResultVO update(ContextCacheUser currentUser, MaterialAllocationBaseModReqDTO modReqDTO) {
 
        if(null == WorkTypeEnum.parse(modReqDTO.getWorkType())){
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"作业类型不合法!");
        }
        if(null != modReqDTO.getWorkLevel()){
            if(null == WorkLevelEnum.parse(modReqDTO.getWorkLevel())){
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"作业级别不合法!");
            }
        }
 
        //配置物资表数据
        MaterialAllocationBaseInfo materialAllocationBaseInfo = new MaterialAllocationBaseInfo();
        BeanUtils.copyProperties(modReqDTO,materialAllocationBaseInfo);
        materialAllocationBaseInfo.setModifiedUid(currentUser.getUid());
        materialAllocationBaseInfo.setModifiedUname(currentUser.getUsername());
 
        List<MaterialDetailModReqDTO> materialDetailList = modReqDTO.getMaterialDetailList();
        List<SafeMaterialBsClassifyRPCRespDto> classifyRPCRespDtoList = new ArrayList<>();
        //验证
        if(!CollectionUtils.isEmpty(materialDetailList)){
            List<Long> smallClassifyIdList = materialDetailList
                    .stream().map(MaterialDetailModReqDTO::getSmallClassifyId)
                    .collect(Collectors.toList());
            classifyRPCRespDtoList = this.getTraceabilityClassifyList(smallClassifyIdList);
 
            for (MaterialDetailModReqDTO materialDetail : materialDetailList) {
 
                if (null == materialDetail.getStandVal()) {
                    throw new AusinessException(E.DATA_PARAM_NULL, "标准值不能为空!");
                }
                if (null == materialDetail.getConfigurationLevel()) {
                    throw new AusinessException(E.DATA_PARAM_NULL, "配置级别不能为空!");
                }
                if (null == ConfigurationLevelEnum.parse(materialDetail.getConfigurationLevel())) {
                    throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "配置级别不合法!");
                }
                if (null == materialDetail.getSmallClassifyId()) {
                    throw new AusinessException(E.DATA_PARAM_NULL, "物资种类id不能为空!");
                }
                List<SafeMaterialBsClassifyRPCRespDto> selectList = classifyRPCRespDtoList
                        .stream()
                        .filter(classify -> classify.getSmallClassifyId().equals(materialDetail.getSmallClassifyId()))
                        .collect(Collectors.toList());
                if (selectList.size() == 0) {
                    throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "物资种类不存在");
                }
            }
        }
 
        //获取原有物资配置表关联数据
        List<MaterialDetailInfo> listByMaBaseId = materialDetailInfoService.listByMaBaseId(modReqDTO.getId());
 
        //过滤出要删除的数据 做解绑操作
        List<MaterialDetailInfo> deleteOldStepList = listByMaBaseId.stream().parallel().filter(a ->
                        materialDetailList.stream().noneMatch(b ->
                                a.getId().equals(b.getId())))
                .collect(Collectors.toList());
        List<Long> deleteOldStepIdList = deleteOldStepList.stream().map(md -> md.getId()).collect(Collectors.toList());
        if(deleteOldStepIdList != null && deleteOldStepIdList.size()>0){
            //批量删除
            materialDetailInfoService.batchUpdateStatus(deleteOldStepIdList);
        }
 
        List<MaterialDetailInfo> mdSaveList = new ArrayList<>();
        List<MaterialDetailInfo> mdUpdateList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(materialDetailList)){
            for (MaterialDetailModReqDTO materialDetail : materialDetailList) {
                //物资详表数据
                MaterialDetailInfo materialDetailInfo = new MaterialDetailInfo();
                BeanUtils.copyProperties(materialDetail,materialDetailInfo);
                List<SafeMaterialBsClassifyRPCRespDto> selectList = classifyRPCRespDtoList
                        .stream()
                        .filter(classify -> classify.getSmallClassifyId().equals(materialDetail.getSmallClassifyId()))
                        .collect(Collectors.toList());
                if(selectList.size()>0){
                    materialDetailInfo.setBigClassifyId(selectList.get(0).getBigClassifyId());
                    materialDetailInfo.setBigClassifyName(selectList.get(0).getBigClassifyName());
                    materialDetailInfo.setMaterialName(selectList.get(0).getSmallClassifyName());
                }
                if(null == materialDetail.getId()){//主键不存在
                    //生成主键
                    materialDetailInfo.setId(IdUtil.getSnowflake(0,0).nextId());
                    materialDetailInfo.setCreateUid(currentUser.getUid());
                    materialDetailInfo.setCreateUname(currentUser.getUsername());
                    materialDetailInfo.setMaBaseId(materialAllocationBaseInfo.getId());
                    materialDetailInfo.setStatus(MaterialDetailStatusEnum.VALID.getCode());
                    mdSaveList.add(materialDetailInfo);
                }else{//主键存在
                    materialDetailInfo.setModifiedUname(currentUser.getUsername());
                    materialDetailInfo.setModifiedUid(currentUser.getUid());
                    mdUpdateList.add(materialDetailInfo);
                }
 
            }
        }
        //更新配置表数据
        materialAllocationBaseInfoService.updateOne(materialAllocationBaseInfo);
        //插入物资详表数据
        if(mdSaveList.size()>0){
            materialDetailInfoService.saveBatch(mdSaveList);
        }
        //更新物资详细表
        if(mdUpdateList.size()>0){
            materialDetailInfoService.updateBatch(mdUpdateList);
        }
        return new ResultVO<>(ResultCodes.OK);
    }
 
    @Override
    public ResultVO delete(ContextCacheUser currentUser, Long id) {
        if(null == id){
            throw new AusinessException(E.DATA_PARAM_NULL,"主键不能为空!");
        }
        //逻辑删除物资配置表数据
        materialAllocationBaseInfoService.updateStatus(id);
        //根据配置物资id删除
        materialDetailInfoService.updateStatusByMaBaseId(id);
        return new ResultVO(ResultCodes.OK);
    }
 
    @Override
    public ResultVO deleteBatch(ContextCacheUser currentUser, DeleteForm deleteForm) {
        List<Long> ids = deleteForm.getIds();
        //批量删除-配置数据
        materialAllocationBaseInfoService.batchUpdateStatus(ids);
        //批量删除-物资详细数据
        materialDetailInfoService.batchUpdateStatusByMaBaseIds(ids);
        return new ResultVO<>(ResultCodes.OK);
    }
 
    @Override
    public SearchResultVO<List<MaterialAllcationBasePageRespDTO>> listByPage(ContextCacheUser currentUser, PageQuery<MaterialAllocationBaseQuery> pageQuery) {
        Page<MaterialAllocationBaseInfo> page = new Page(pageQuery.getPageIndex(), pageQuery.getPageSize());
        //获取物资配置分页数据
        List<MaterialAllocationBaseInfo> materialAllocationBaseList = materialAllocationBaseInfoService.listByPage(page, pageQuery.getSearchParams());
        List<MaterialDetailInfo> materialDetailList = new ArrayList<>();
        if(materialAllocationBaseList.size()>0){
            List<Long> idList = materialAllocationBaseList.stream().map(mab -> mab.getId()).collect(Collectors.toList());
            //根据物资配置ids获取物资数据
            materialDetailList = materialDetailInfoService.listByMaBaseIds(idList);
        }
        List<MaterialAllcationBasePageRespDTO> mabRespDTOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(materialAllocationBaseList)){
            for (MaterialAllocationBaseInfo materialAllocationBaseInfo:materialAllocationBaseList) {
                List<ConfigurationLevelRespDTO> clList = new ArrayList<>();
                MaterialAllcationBasePageRespDTO mabPageRespDTO = new MaterialAllcationBasePageRespDTO();
                BeanUtils.copyProperties(materialAllocationBaseInfo, mabPageRespDTO);
                List<MaterialDetailInfo> selectList = materialDetailList
                        .stream()
                        .filter(md -> md.getMaBaseId().equals(materialAllocationBaseInfo.getId()))
                        .collect(Collectors.toList());
                for (ConfigurationLevelEnum configurationLevelEnum : ConfigurationLevelEnum.values()) {
                    ConfigurationLevelRespDTO configurationLevelRespDTO = new ConfigurationLevelRespDTO();
                    configurationLevelRespDTO.setConfigurationLevel(configurationLevelEnum.getCode());
                    configurationLevelRespDTO.setConfigurationLevelName(configurationLevelEnum.getValue());
                    List<MaterialDetailInfo> collect = selectList
                            .stream()
                            .filter(sl -> sl.getConfigurationLevel().equals(configurationLevelEnum.getCode()))
                            .collect(Collectors.toList());
                    //物资转换
                    List<MaterialDetailRespDTO> mdRespDTOList = new ArrayList<>();
                    for (MaterialDetailInfo materialDetailInfo:collect) {
                        MaterialDetailRespDTO materialDetailRespDTO = new MaterialDetailRespDTO();
                        BeanUtils.copyProperties(materialDetailInfo,materialDetailRespDTO);
                        if(null != materialDetailInfo.getConsumable()){
                            materialDetailRespDTO.setConsumableName(ConsumableEnum.getByCode(materialDetailInfo.getConsumable()).getValue());
                        }
                        mdRespDTOList.add(materialDetailRespDTO);
                    }
                    configurationLevelRespDTO.setMdList(mdRespDTOList);
                    clList.add(configurationLevelRespDTO);
                }
                mabPageRespDTO.setClList(clList);
                mabRespDTOList.add(mabPageRespDTO);
            }
        }
 
        return new SearchResultVO<>(true,
                page.getCurrent(),
                page.getSize(),
                page.getPages(),
                page.getTotal(),
                mabRespDTOList,
                ResultCodes.OK);
    }
 
    @Override
    public ResultVO<List<MaterialAllcationBasePageRespDTO>> list(ContextCacheUser currentUser, MaterialAllocationBaseQuery query) {
 
        //获取物资配置分页数据
        List<MaterialAllocationBaseInfo> materialAllocationBaseList = materialAllocationBaseInfoService.listAll(query);
        //获取物资数据
        List<MaterialDetailInfo> materialDetailList = materialDetailInfoService.listAll();
 
        List<MaterialAllcationBasePageRespDTO> mabRespDTO = new ArrayList<>();
        for (MaterialAllocationBaseInfo materialAllocationBaseInfo:materialAllocationBaseList) {
            List<ConfigurationLevelRespDTO> clList = new ArrayList<>();
            MaterialAllcationBasePageRespDTO mabPageRespDTO = new MaterialAllcationBasePageRespDTO();
            BeanUtils.copyProperties(materialAllocationBaseInfo, mabPageRespDTO);
            List<MaterialDetailInfo> selectList = materialDetailList
                    .stream()
                    .filter(md -> md.getMaBaseId().equals(materialAllocationBaseInfo.getId()))
                    .collect(Collectors.toList());
            for (ConfigurationLevelEnum clEnum : ConfigurationLevelEnum.values()) {
                ConfigurationLevelRespDTO clRespDTO = new ConfigurationLevelRespDTO();
                clRespDTO.setConfigurationLevel(clEnum.getCode());
                clRespDTO.setConfigurationLevelName(clEnum.getValue());
                List<MaterialDetailInfo> collect = selectList
                        .stream()
                        .filter(md -> md.getConfigurationLevel().equals(clEnum.getCode()))
                        .collect(Collectors.toList());
                //物资转换
                List<MaterialDetailRespDTO> mdRespDTOList = new ArrayList<>();
                for (MaterialDetailInfo materialDetailInfo:collect) {
                    MaterialDetailRespDTO materialDetailRespDTO = new MaterialDetailRespDTO();
                    BeanUtils.copyProperties(materialDetailInfo,materialDetailRespDTO);
                    mdRespDTOList.add(materialDetailRespDTO);
                }
                clRespDTO.setMdList(mdRespDTOList);
                clList.add(clRespDTO);
            }
            mabPageRespDTO.setClList(clList);
            mabRespDTO.add(mabPageRespDTO);
        }
        return new ResultVO<>(ResultCodes.OK,mabRespDTO);
    }
 
    @Override
    public ResultVO<List<MaterialAllocationBaseRespDTO>> queryByWorkTypeOrLevel(ContextCacheUser currentUser, MaterialAllocationBaseQuery query) {
        if(null == query.getWorkType()){
            throw new AusinessException(E.DATA_PARAM_NULL,"作业类型不可为空!");
        }
        //获取基础配置数据
        List<MaterialAllocationBaseInfo> materialAllocationBaseList = materialAllocationBaseInfoService.listAll(query);
       /* List<MaterialDetailInfo> materialDetailList = new ArrayList<>();
        List<SafeMaterialClassifyStockRPCRespDto> materialStockList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(materialAllocationBaseList)){
            //获取配置下物资
            List<Long> ids = materialAllocationBaseList.stream().map(MaterialAllocationBaseInfo::getId).collect(Collectors.toList());
            materialDetailList= materialDetailInfoService.listByMaBaseIds(ids);
        }
        if(!CollectionUtils.isEmpty(materialDetailList)){
            //获取物资库存
            List<Long> smallClassifyIds = materialDetailList.stream().map(MaterialDetailInfo::getSmallClassifyId).collect(Collectors.toList());
            materialStockList = this.getMaterialStock(smallClassifyIds);
        }*/
        List<MaterialAllocationBaseRespDTO> respDTOList = new ArrayList<>();
        //循环配置
        for (MaterialAllocationBaseInfo allocationBaseInfo:materialAllocationBaseList) {
            MaterialAllocationBaseRespDTO allocationBaseRespDTO = new MaterialAllocationBaseRespDTO();
            BeanUtils.copyProperties(allocationBaseInfo,allocationBaseRespDTO);
           /* //过滤该配置下的物资
            List<MaterialDetailInfo> selectMaterialList = materialDetailList
                    .stream()
                    .filter(md -> md.getMaBaseId().equals(allocationBaseInfo.getId()))
                    .collect(Collectors.toList());
            List<MaterialClassifyRespDTO> classifyRespDTOList = new ArrayList<>();
            for(MaterialDetailInfo materialDetailInfo:selectMaterialList){
                MaterialClassifyRespDTO materialClassifyRespDTO = new MaterialClassifyRespDTO();
                materialClassifyRespDTO.setMaterialId(materialDetailInfo.getId());
                materialClassifyRespDTO.setMaterialName(materialDetailInfo.getMaterialName());
                materialClassifyRespDTO.setConfigurationLevelName(ConfigurationLevelEnum.parse(materialDetailInfo.getConfigurationLevel()).getValue());
                materialClassifyRespDTO.setStandVal(materialDetailInfo.getStandVal());
                //过滤出该种物资各个仓库的存量
                List<SafeMaterialClassifyStockRPCRespDto> collect = materialStockList
                        .stream()
                        .filter(ms -> ms.getSmallClassifyId().equals(materialDetailInfo.getSmallClassifyId()))
                        .collect(Collectors.toList());
                List<MaterialStockRespDTO> stockRespDTOList = new ArrayList<>();
                for(SafeMaterialClassifyStockRPCRespDto stockRPCRespDto:collect){
                    MaterialStockRespDTO materialStockRespDTO = new MaterialStockRespDTO();
                    materialStockRespDTO.setDepId(stockRPCRespDto.getDepId());
                    materialStockRespDTO.setStockCount(stockRPCRespDto.getStockCount());
                    materialStockRespDTO.setDepName(stockRPCRespDto.getDepName());
                    stockRespDTOList.add(materialStockRespDTO);
                }
                materialClassifyRespDTO.setMsList(stockRespDTOList);
                classifyRespDTOList.add(materialClassifyRespDTO);
            }
            allocationBaseRespDTO.setMcList(classifyRespDTOList);*/
            respDTOList.add(allocationBaseRespDTO);
        }
        return new ResultVO<>(ResultCodes.OK,respDTOList);
    }
 
 
    /**
     * 应急物资
     * @param currentUser
     * @return
     */
    @Override
    public ResultVO emergencyMaterial(ContextCacheUser currentUser) {
        try{
            List<EmergencySuppliesTypeRPCResp> list = (List<EmergencySuppliesTypeRPCResp>)emergencyRpcAPi.emergencySuppliesList(currentUser).getData();
            return new ResultVO(ResultCodes.OK,list);
        }catch (Exception e){
            throw new BusinessException(ResultCodes.RPC_ACCESS_EXCEPTION);
        }
    }
 
    @Override
    public List<SafeMaterialClassifyRPCRespDto> materialClassify() {
        ResultVO<List<SafeMaterialClassifyRPCRespDto>> result = equipmentRpcAPi.getMaterialClassifyList();
        List<SafeMaterialClassifyRPCRespDto> classifyRPCRespDtoList = new ArrayList<>();
        if(result.getCode().equals(ResultCodes.OK.getCode())){
            if(null != result.getData()){
                classifyRPCRespDtoList = (List<SafeMaterialClassifyRPCRespDto>) result.getData();
            }else{
                throw new BusinessException(ResultCodes.RPC_DATA_NULL);
            }
        }else{
            throw new BusinessException(result.getCode(),result.getMsg());
        }
 
        return classifyRPCRespDtoList;
    }
 
    @Override
    public List<MaterialReceiveRecordsRespDTO> materialReceive(MaterialReceiveReqDTO receiveReq) {
 
        //获取领取人员信息
        List<UserInfoRPCRespDTO> userInfoRPCRespDTOList = listAccountUsers(receiveReq.getReceiveUids());
        //物资配置清单
        List<MaterialDetailInfo> materialDetailInfoList = materialDetailInfoService.listByMaBaseId(receiveReq.getMaBaseId());
        if(CollectionUtils.isEmpty(materialDetailInfoList)){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"未查询到该物资配置清单!");
        }
        List<Long> smallClassifyIdList = materialDetailInfoList.stream().map(MaterialDetailInfo::getSmallClassifyId).collect(Collectors.toList());
        //根据人员获取未归还相关领取物资 人员,小类别,
        MaterialReceiveRecordsRPCReq materialReceiveRecordsRPCReq = new MaterialReceiveRecordsRPCReq();
        materialReceiveRecordsRPCReq.setReceiveUids(receiveReq.getReceiveUids());
        materialReceiveRecordsRPCReq.setSmallClassifyIds(smallClassifyIdList);
        List<MaterialReceiveRecordsRPCRespDto> list = new ArrayList<>();
        ResultVO<List<MaterialReceiveRecordsRPCRespDto>> result = equipmentRpcAPi.getMaterialReceiveRecords(materialReceiveRecordsRPCReq);
        if(result.getCode().equals(ResultCodes.OK.getCode())){
            if(null != result.getData()){
                list = (List<MaterialReceiveRecordsRPCRespDto>) result.getData();
            }
        }else {
            throw new BusinessException(result.getCode(), result.getMsg());
        }
        List<MaterialReceiveRecordsRespDTO> respDTOList = new ArrayList<>();
        for(MaterialDetailInfo materialDetailInfo:materialDetailInfoList){
            MaterialReceiveRecordsRespDTO receiveRecordsRespDTO = new MaterialReceiveRecordsRespDTO();
            receiveRecordsRespDTO.setMaBaseId(materialDetailInfo.getMaBaseId());
            receiveRecordsRespDTO.setMdId(materialDetailInfo.getId());
            receiveRecordsRespDTO.setMaterialName(materialDetailInfo.getMaterialName());
            receiveRecordsRespDTO.setBigClassifyId(materialDetailInfo.getBigClassifyId());
            receiveRecordsRespDTO.setBigClassifyName(materialDetailInfo.getBigClassifyName());
            receiveRecordsRespDTO.setSmallClassifyId(materialDetailInfo.getSmallClassifyId());
            receiveRecordsRespDTO.setConsumable(materialDetailInfo.getConsumable());
            if(null != materialDetailInfo.getConsumable()){
                receiveRecordsRespDTO.setConsumableName(ConsumableEnum.getByCode(materialDetailInfo.getConsumable()).getValue());
            }
            receiveRecordsRespDTO.setConfigurationLevel(materialDetailInfo.getConfigurationLevel());
            receiveRecordsRespDTO.setConfigurationLevelName(ConfigurationLevelEnum.parse(materialDetailInfo.getConfigurationLevel()).getValue());
            receiveRecordsRespDTO.setStandVal(materialDetailInfo.getStandVal());
            StringBuffer userNames = new StringBuffer();
            StringBuffer userUids = new StringBuffer();
            if(!CollectionUtils.isEmpty(list)){
                List<MaterialReceiveRecordsRPCRespDto> selectReceiveList = list.stream().filter(item -> item.getSmallClassifyId().equals(materialDetailInfo.getSmallClassifyId())).collect(Collectors.toList());
                receiveRecordsRespDTO.setReceiveCount(selectReceiveList.size());
                for(UserInfoRPCRespDTO user:userInfoRPCRespDTOList){
                    List<MaterialReceiveRecordsRPCRespDto> collect = selectReceiveList.stream().filter(item -> item.getReceiveUid().equals(user.getUid())).collect(Collectors.toList());
                    userNames.append(user.getUsername()).append("(").append(String.valueOf(collect.size())).append("),");
                    userUids.append(user.getUid().toString()).append(",");
                }
 
            }else{
                for(UserInfoRPCRespDTO user:userInfoRPCRespDTOList){
                    userNames.append(user.getUsername()).append("(0)");
                    userUids.append(user.getUid().toString()).append(",");
                }
                receiveRecordsRespDTO.setReceiveCount(0);
            }
            receiveRecordsRespDTO.setReceiveUnames(userNames.substring(0,userNames.length()));
            receiveRecordsRespDTO.setReceiveUids(userUids.substring(0,userUids.length()));
            respDTOList.add(receiveRecordsRespDTO);
        }
        return respDTOList;
    }
    @Override
    public List<MaterialReceiveRecordsRPCRespDto> materialDetailReceiveRecords(MaterialReceiveReqDTO receiveReq) {
 
        //物资配置清单
        List<MaterialDetailInfo> materialDetailInfoList = materialDetailInfoService.listByMaBaseId(receiveReq.getMaBaseId());
        if(CollectionUtils.isEmpty(materialDetailInfoList)){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"未查询到该物资配置清单!");
        }
        List<Long> smallClassifyIdList = materialDetailInfoList.stream().map(MaterialDetailInfo::getSmallClassifyId).collect(Collectors.toList());
        //根据人员获取未归还相关领取物资 人员,小类别,
        MaterialReceiveRecordsRPCReq materialReceiveRecordsRPCReq = new MaterialReceiveRecordsRPCReq();
        materialReceiveRecordsRPCReq.setReceiveUids(receiveReq.getReceiveUids());
        materialReceiveRecordsRPCReq.setSmallClassifyIds(smallClassifyIdList);
        List<MaterialReceiveRecordsRPCRespDto> list = new ArrayList<>();
        ResultVO<List<MaterialReceiveRecordsRPCRespDto>> result = equipmentRpcAPi.getMaterialReceiveRecords(materialReceiveRecordsRPCReq);
        if(result.getCode().equals(ResultCodes.OK.getCode())){
            if(null != result.getData()){
                list = (List<MaterialReceiveRecordsRPCRespDto>) result.getData();
            }
        }else {
            throw new BusinessException(result.getCode(), result.getMsg());
        }
        return list;
    }
 
    private List<SafeMaterialClassifyRPCRespDto> getClassifyListByIds(List<Long> ids){
        List<SafeMaterialClassifyRPCRespDto> list = new ArrayList<>();
        ResultVO<List<SafeMaterialClassifyRPCRespDto>> result = equipmentRpcAPi.getClassifyListByIds(ids);
        if(result.getCode().equals(ResultCodes.OK.getCode())){
            if(null == result.getData()){
                throw new BusinessException(ResultCodes.RPC_RESULT_NULL.getCode(),"物资种类不存在!");
            }else{
                list = (List<SafeMaterialClassifyRPCRespDto>) result.getData();
            }
        }else {
            throw new BusinessException(result.getCode(), result.getMsg());
        }
        return list;
    }
    private List<SafeMaterialBsClassifyRPCRespDto> getTraceabilityClassifyList(List<Long> ids){
        List<SafeMaterialBsClassifyRPCRespDto> list = new ArrayList<>();
        ResultVO<List<SafeMaterialBsClassifyRPCRespDto>> result = equipmentRpcAPi.getTraceabilityClassifyList(ids);
        if(result.getCode().equals(ResultCodes.OK.getCode())){
            if(null == result.getData()){
                throw new BusinessException(ResultCodes.RPC_RESULT_NULL.getCode(),"物资种类不存在!");
            }else{
                list = (List<SafeMaterialBsClassifyRPCRespDto>) result.getData();
            }
        }else {
            throw new BusinessException(result.getCode(), result.getMsg());
        }
        return list;
    }
    private List<SafeMaterialClassifyStockRPCRespDto> getMaterialStock(List<Long> smallClassifyIds){
        List<SafeMaterialClassifyStockRPCRespDto> list = new ArrayList<>();
        ResultVO<List<SafeMaterialClassifyStockRPCRespDto>> result = equipmentRpcAPi.getSmallClassifyStockByIds(smallClassifyIds);
        if(result.getCode().equals(ResultCodes.OK.getCode())){
            if(null == result.getData()){
                throw new BusinessException(ResultCodes.RPC_RESULT_NULL.getCode(),"物资种类不存在!");
            }else{
                list = (List<SafeMaterialClassifyStockRPCRespDto>) result.getData();
            }
        }else {
            throw new BusinessException(result.getCode(), result.getMsg());
        }
        return list;
    }
    //调用RPC 获取去人员
    private List<UserInfoRPCRespDTO> listAccountUsers(List<Long> userIds){
        List<UserInfoRPCRespDTO> userInfoRPCRespDTOList = new ArrayList<>();
        ResultVO<List<UserInfoRPCRespDTO>> listResultVO = accountUserService.listUserInfoByUids(userIds);
        if (listResultVO != null && listResultVO.getCode().equals(ResultCodes.OK.getCode())) {
            if (listResultVO.getData() != null) {
                userInfoRPCRespDTOList = (List<UserInfoRPCRespDTO>) listResultVO.getData();
            }else{
                throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"领取人员不存在");
            }
        } else {
            throw new BusinessException(ResultCodes.RPC_ACCESS_EXCEPTION);
        }
        return userInfoRPCRespDTOList;
    }
}