heheng
2 天以前 caf2acbee7e25afde41c169a0eba471e5b08b523
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
package com.gkhy.system.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.common.enums.DeleteFlagEnum;
import com.gkhy.common.exception.ServiceException;
import com.gkhy.common.utils.DateUtils;
import com.gkhy.common.utils.SecurityUtils;
import com.gkhy.common.utils.StringUtils;
import com.gkhy.common.utils.uuid.RandomStringGenerator;
import com.gkhy.system.domain.*;
import com.gkhy.system.domain.vo.request.*;
import com.gkhy.system.domain.vo.response.*;
import com.gkhy.system.mapper.*;
import com.gkhy.system.service.IProjectFileService;
import com.gkhy.system.service.IProjectManagementService;
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.*;
import java.util.stream.Collectors;
 
/**
 * 项目管理Service业务层处理
 *
 * @author expert
 * @date 2024-11-14
 */
@Service
public class ProjectManagementServiceImpl extends ServiceImpl<ProjectManagementMapper, ProjectManagement> implements IProjectManagementService {
    @Autowired
    private ProjectManagementMapper projectManagementMapper;
 
    @Autowired
    private ProjectExpertMapper projectExpertMapper;
 
    @Autowired
    private ProjectFileMapper projectFileMapper;
 
    @Autowired
    private ProjectExpertDetailMapper projectExpertDetailMapper;
 
    @Autowired
    private IProjectFileService projectFileService;
    @Autowired
    private SysExpertInfoMapper sysExpertInfoMapper;
 
    @Autowired
    private OutProjectExpertMapper outProjectExpertMapper;
 
    @Autowired
    private OutProjectExpertDetailMapper outProjectExpertDetailMapper;
 
    /**
     * 查询项目管理
     *
     * @param id 项目管理主键
     * @return 项目管理
     */
    @Override
    public ProjectManagement selectProjectManagementById(Long id) {
        return projectManagementMapper.selectProjectManagementById(id);
    }
 
    /**
     * 查询项目管理列表
     *
     * @param projectManagement 项目管理
     * @return 项目管理
     */
    @Override
    public List<ProjectManagement> selectProjectManagementList(ProjectManagement projectManagement) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId()) && SecurityUtils.getUserId() != 130L) {
            projectManagement.setDeptId(SecurityUtils.getDeptId());
        }
        return projectManagementMapper.selectProjectManagementList(projectManagement);
    }
 
    /**
     * 新增项目管理
     *
     * @param projectManagement 项目管理
     * @return 结果
     */
    @Override
    public Long insertProjectManagement(ProjectManageSaveReq projectManagement) {
        ProjectManagement projectManagementSave = new ProjectManagement();
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            throw new ServiceException("无权新增");
        }
        projectManagementSave.setDelFlag(DeleteFlagEnum.UN_DELETE.getCode());
        BeanUtil.copyProperties(projectManagement, projectManagementSave);
        if (projectManagement.getId() != null) {
            checkDataPer(projectManagement.getId());
 
            projectManagementSave.setUpdateBy(SecurityUtils.getUsername());
            projectManagementSave.setUpdateTime(DateUtils.getNowDate());
            // projectManagementMapper.updateProjectManagement(projectManagementSave);
            updateById(projectManagementSave);
        } else {
            projectManagementSave.setProjectCode(RandomStringGenerator.generateRandomString(10));
            projectManagementSave.setStep(1L);
            projectManagementSave.setCreateBy(SecurityUtils.getUsername());
            projectManagementSave.setCreateTime(DateUtils.getNowDate());
            save(projectManagementSave);
            //  projectManagementMapper.insertProjectManagement(projectManagementSave);
        }
 
 
        return projectManagementSave.getId();
    }
 
    /**
     * 修改项目管理
     *
     * @param projectManagement 项目管理
     * @return 结果
     */
    @Override
    @Transactional
    public void updateProjectManagement(ProjectExpertSaveBatchReqDto projectManagement) {
        ProjectManagement projectManagement1 = checkDataPer(projectManagement.getId());
        //专家选取
 
        ProjectManagement projectManagementSave = new ProjectManagement();
        projectManagementSave.setUpdateBy(SecurityUtils.getUsername());
        projectManagementSave.setUpdateTime(DateUtils.getNowDate());
        projectManagementSave.setId(projectManagement.getId());
        if (projectManagement1.getState() != 1 && projectManagement1.getState() != 0) {
            throw new ServiceException("该步骤不可操作专家数据!");
        }
 
        //outExpertInfoMapper
        List<ProjectExpertSaveReqDto> saveData = projectManagement.getSaveData();
        List<OutProjectExpert> saveOutData = projectManagement.getSaveOutData();
        if (ObjectUtil.isEmpty(saveData) && ObjectUtil.isEmpty(saveOutData)) {
            throw new ServiceException("专家不可为空!");
        }
        //专家库校验重复
        if (ObjectUtil.isNotEmpty(saveData)) {
            if (hasDuplicateExpertId(saveData)) {
                throw new ServiceException("专家重复!");
            }
        }
        if (ObjectUtil.isNotEmpty(saveOutData)){
            List<OutProjectExpert> uniqueList = saveOutData.stream()
                    .collect(Collectors.collectingAndThen(
                            Collectors.toMap(OutProjectExpert::getIdCard, o -> o, (existing, replacement) -> existing, LinkedHashMap::new),
                            map -> new ArrayList<>(map.values())
                    ));
            if (uniqueList.size() != saveOutData.size()){
                throw new ServiceException("外部专家身份证重复!");
            }
 
        }
 
        boolean flag = false;
 
 
        if ((projectManagement1.getStep() == 1 || projectManagement1.getStep() == 2) && (projectManagement.getStep() == 2)) {
            projectManagementSave.setStep(2L);
            projectManagementSave.setState(1L);
 
            List<ProjectExpertSaveReqDto> collect = new ArrayList<>();
            List<OutProjectExpert> saveOutDataSp = new ArrayList<>();
            List<OutProjectExpert> saveOutDataSp11 = new ArrayList<>();
            if (ObjectUtil.isNotEmpty(saveData)) {
                 collect = saveData.stream()
                        .filter(dto -> "1".equals(dto.getTeamLeader().toString()))
                        .collect(Collectors.toList());
            }
            if (ObjectUtil.isNotEmpty(saveOutData)) {
                 saveOutDataSp = saveOutData.stream()
                        .filter(dto -> "1".equals(dto.getTeamLeader().toString()))
                        .collect(Collectors.toList());
                saveOutDataSp11 = saveOutData.stream()
                        .filter(dto -> "0".equals(dto.getTeamLeader().toString()))
                        .collect(Collectors.toList());
            }
            if (StringUtils.isEmpty(collect) && StringUtils.isEmpty(saveOutDataSp)) {
                throw new ServiceException("缺少组长!");
            }
            if (collect.size()+saveOutDataSp.size() > 1) {
                throw new ServiceException("组长只能选一人!");
            }
            if (ObjectUtil.isNotEmpty( collect)){
                projectManagementSave.setExpertName(collect.get(0).getExpertName());
                projectManagementSave.setExpertId(collect.get(0).getExpertId());
                projectManagementSave.setExpertType(1L);
            }else {
                OutProjectExpert outExpertInfo = saveOutDataSp.get(0);
                flag = true;
                if (outExpertInfo.getId() != null){
                    outExpertInfo.setUpdateTime(LocalDateTime.now());
                    outExpertInfo.setUpdateBy(SecurityUtils.getUsername());
                    int update = outProjectExpertMapper.updateById(outExpertInfo);
                    if (update < 1) {
                        throw new ServiceException("保存专家失败!");
                    }
                }else {
                    outExpertInfo.setProjectId(projectManagement.getId());
                    int insert = outProjectExpertMapper.insert(outExpertInfo);
                    if (insert < 1) {
                        throw new ServiceException("保存专家失败!");
                    }
                }
 
                projectManagementSave.setExpertName(saveOutDataSp.get(0).getName());
                projectManagementSave.setExpertId(saveOutDataSp.get(0).getId());
                projectManagementSave.setExpertType(2L);
                if (ObjectUtil.isNotEmpty(saveOutDataSp11)){
                    flag = true;
                    saveOutDataSp11.forEach(
                            outExpertInfo1 -> {
                                if (outExpertInfo1.getId() != null){
                                    outExpertInfo1.setUpdateTime(LocalDateTime.now());
                                    outExpertInfo1.setUpdateBy(SecurityUtils.getUsername());
                                    int insert2 = outProjectExpertMapper.updateById(outExpertInfo1);
                                    if (insert2 < 1) {
                                        throw new ServiceException("保存专家失败!");
                                    }
                                }else {
                                    outExpertInfo1.setProjectId(projectManagement.getId());
                                    outExpertInfo1.setCreateTime(LocalDateTime.now());
                                    outExpertInfo1.setCreateBy(SecurityUtils.getUsername());
                                    int insert2 = outProjectExpertMapper.insert(outExpertInfo1);
                                    if (insert2 < 1) {
                                        throw new ServiceException("保存专家失败!");
                                    }
                                }
 
                            }
                    );
                }
 
            }
 
 
 
        } else if (projectManagement1.getStep() == 2 && projectManagement.getStep() == 3) {
            projectManagementSave.setStep(2L);
            projectManagementSave.setState(2L);
            projectManagementSave.setProjectCheckTime(new Date());
 
        }
        if (ObjectUtil.isNotEmpty(saveData)){
            List<ProjectExpertSaveReqDto> saveList = saveData.stream()
                    .filter(dto -> dto.getId() == null)
                    .collect(Collectors.toList());
            if (StringUtils.isNotEmpty(saveList)) {
                List<ProjectExpert> projectExperts = convertToProjectExpertList(saveList, projectManagement.getId());
                int i = projectExpertMapper.batchInsertProjectExpert(projectExperts);
                if (i < 1) {
                    throw new ServiceException("保存专家失败!");
                }
            }
            List<ProjectExpertSaveReqDto> editList = saveData.stream()
                    .filter(dto -> dto.getId() != null)
                    .collect(Collectors.toList());
            if (StringUtils.isNotEmpty(editList)) {
                List<ProjectExpert> projectExperts = convertToProjectExpertList(editList, projectManagement.getId());
                int i = projectExpertMapper.batchUpdateProjectExpert(projectExperts);
                if (i < 1) {
                    throw new ServiceException("保存专家失败!");
                }
            }
        }
        if (!flag){
            if (ObjectUtil.isNotEmpty(saveOutData)){
                saveOutData.forEach(
                        outExpertInfo1 -> {
                            if (outExpertInfo1.getId() != null){
                                outExpertInfo1.setUpdateTime(LocalDateTime.now());
                                outExpertInfo1.setUpdateBy(SecurityUtils.getUsername());
                                int insert2 = outProjectExpertMapper.updateById(outExpertInfo1);
                                if (insert2 < 1) {
                                    throw new ServiceException("保存专家失败!");
                                }
                            }else {
                                outExpertInfo1.setProjectId(projectManagement.getId());
                                outExpertInfo1.setCreateTime(LocalDateTime.now());
                                outExpertInfo1.setCreateBy(SecurityUtils.getUsername());
                                int insert2 = outProjectExpertMapper.insert(outExpertInfo1);
                                if (insert2 < 1) {
                                    throw new ServiceException("保存专家失败!");
                                }
                            }
 
                        }
 
                );
            }
        }
//        int outData = 0;
//        if(!ObjectUtil.isEmpty(saveOutData)){
//            outData = saveOutData.size();
//        }
//        int inData = 0;
//        if (!ObjectUtil.isEmpty(saveData)){
//            inData = saveData.size();
//        }
 
        List<Long> delData = projectManagement.getDelData();
        if (delData != null && delData.size() > 0) {
            int i = projectExpertMapper.deleteProjectExpertByIds(delData.toArray(new Long[delData.size()]));
            if (i < 1) {
                throw new ServiceException("保存专家失败!");
            }
        }
        List<Long> delOutData = projectManagement.getDelOutData();
        if (ObjectUtil.isNotEmpty(delOutData)){
            // 删除外部专家数据
            int update = outProjectExpertMapper.update(new OutProjectExpert(),
                    new LambdaUpdateWrapper<OutProjectExpert>().set(OutProjectExpert::getDelFlag, 1)
                            .set(OutProjectExpert::getUpdateTime, new Date()).set(OutProjectExpert::getUpdateBy, SecurityUtils.getUsername())
                            .in(OutProjectExpert::getId, delOutData));
            if (update < 1) {
                throw new ServiceException("保存外部专家失败!");
            }
        }
        Long l = outProjectExpertMapper.selectCount(new LambdaQueryWrapper<OutProjectExpert>().eq(OutProjectExpert::getDelFlag, 0).eq(OutProjectExpert::getProjectId, projectManagement.getId()));
        Long l1 = projectExpertMapper.selectCount(new LambdaQueryWrapper<ProjectExpert>().eq(ProjectExpert::getDelFlag, 0).eq(ProjectExpert::getProjectId, projectManagement.getId()));
        projectManagementSave.setExpertNum(l+l1);
 
        int i = projectManagementMapper.updateProjectManagement(projectManagementSave);
        if (i < 1) {
            throw new ServiceException("保存专家失败!");
        }
    }
 
 
    public List<ProjectExpert> convertToProjectExpertList(List<ProjectExpertSaveReqDto> reqDtos, Long projectId) {
        List<ProjectExpert> projectExpertList = new ArrayList<>();
        reqDtos.forEach(reqDto -> {
            ProjectExpert projectExpert = convertToProjectExpert(reqDto, projectId);
            projectExpertList.add(projectExpert);
        });
        return projectExpertList;
    }
 
    private ProjectExpert convertToProjectExpert(ProjectExpertSaveReqDto reqDto, Long projectId) {
        ProjectExpert projectExpert = new ProjectExpert();
        projectExpert.setProjectId(projectId);
        projectExpert.setExpertId(reqDto.getExpertId());
        projectExpert.setSelectionMode(reqDto.getSelectionMode());
        projectExpert.setTeamLeader(reqDto.getTeamLeader());
        if (reqDto.getId() == null) {
            projectExpert.setCreateBy(SecurityUtils.getUsername());
        } else {
            projectExpert.setUpdateBy(SecurityUtils.getUsername());
            projectExpert.setId(reqDto.getId());
        }
        return projectExpert;
    }
 
 
    public boolean hasDuplicateExpertId(List<ProjectExpertSaveReqDto> saveData) {
        // 使用 Stream API 收集 expertId 的频率
        Map<Long, Long> frequencyMap = saveData.stream()
                .collect(Collectors.groupingBy(ProjectExpertSaveReqDto::getExpertId, Collectors.counting()));
 
        // 检查是否有任何 expertId 的频率大于 1
        return frequencyMap.values().stream().anyMatch(count -> count > 1);
    }
 
    /**
     * 批量删除项目管理
     *
     * @param ids 需要删除的项目管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectManagementByIds(Long[] ids) {
        // 校验
        for (Long id : ids) {
            checkDelData(id);
        }
//        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId()) ){
//            throw new ServiceException("无权操作!");
//        }
        return projectManagementMapper.deleteProjectManagementByIds(ids);
    }
 
    /**
     * 删除项目管理信息
     *
     * @param id 项目管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectManagementById(Long id) {
        return projectManagementMapper.deleteProjectManagementById(id);
    }
 
    @Override
    public ProjectExpertManagementInfoRes projectExpertCheckInfo(Long id) {
        // 查询
        ProjectExpertManagementInfoRes projectExpertManagementInfoRes = projectManagementMapper.projectExpertCheckInfo(id);
 
        List<ProjectExpertCheckResp> projectExpertCheckResps = projectExpertManagementInfoRes.getProjectExpertCheckResp();
        if (ObjectUtil.isNotEmpty(projectExpertCheckResps)){
            projectExpertCheckResps.forEach(projectExpertCheckResp -> {
                projectExpertCheckResp.setSelectionTime(projectExpertManagementInfoRes.getCreateTime());
            });
            projectExpertCheckResps.sort(Comparator
                    .comparing(ProjectExpertCheckResp::getTeamLeader, Comparator.nullsLast(Comparator.reverseOrder()))
                    .thenComparing(ProjectExpertCheckResp::getExpertType, Comparator.nullsLast(Comparator.naturalOrder())));
        }
 
        return projectExpertManagementInfoRes;
    }
 
    @Override
    @Transactional
    public void projectCheck(ProjectCheckReq req) {
        ProjectManagement projectManagement = checkDataPer(req.getId());
 
        if (req.getState() != 3L && req.getState() != 1) {
            throw new ServiceException("参数异常!");
        }
        Long state = projectManagement.getState();
        if (state != 2) {
            throw new ServiceException("未生成审批单不可审批通过操作!");
        }
 
        List<ProjectFile> projectFile = req.getProjectFile();
        ProjectManagement projectManagementUp = new ProjectManagement();
        projectManagementUp.setId(req.getId());
        projectManagementUp.setState(req.getState());
        projectManagementUp.setUpdateBy(SecurityUtils.getUsername());
        if (req.getState() == 3) {
            projectManagementUp.setStep(3L);
        } else if (req.getState() == 1) {
            projectManagementUp.setExpertName("");
            projectManagementUp.setExpertId(0l);
            projectManagementUp.setExpertNum(0L);
            projectManagementUp.setStep(1L);
        }
        int i = projectManagementMapper.updateProjectManagement(projectManagementUp);
        //驳回删
        if (req.getState() == 1) {
            projectFileMapper.deleteProjectFileByIdAndType(req.getId(), "1");
            projectExpertMapper.deleteProjectExpertByProjectId(req.getId());
            outProjectExpertMapper.update(new OutProjectExpert(),new LambdaUpdateWrapper<OutProjectExpert>()
                    .eq(OutProjectExpert::getProjectId, req.getId()).set(OutProjectExpert::getDelFlag, 1).set(OutProjectExpert::getUpdateTime, new Date())
                    .set(OutProjectExpert::getUpdateBy, SecurityUtils.getUsername()));
        }
        if (i > 0 && StringUtils.isNotEmpty(projectFile) && req.getState() == 3) {
            //删除之前的该类型
            projectFileMapper.deleteProjectFileByIdAndType(req.getId(), "1");
            String username = SecurityUtils.getUsername();
            projectFile.forEach(file -> {
                file.setCreateBy(username);
                file.setProjectId(req.getId());
            });
            //int i1 = projectFileMapper.insertProjectFiles(projectFile);
            Boolean b = projectFileService.saveBatchProjectFile(projectFile);
            if (!b) {
                throw new ServiceException("保存附件失败!");
            }
        }
    }
 
    @Override
    public void projectBuildCheck(ProjectExpertSaveBatchReqDto projectManagement) {
        ProjectManagement projectManagement1 = checkDataPer(projectManagement.getId());
        //专家选取
 
        ProjectManagement projectManagementSave = new ProjectManagement();
        projectManagementSave.setUpdateBy(SecurityUtils.getUsername());
        projectManagementSave.setUpdateTime(DateUtils.getNowDate());
        projectManagementSave.setId(projectManagement.getId());
        if (projectManagement1.getStep() == 2 && projectManagement.getStep() == 3) {
            projectManagementSave.setStep(2L);
            projectManagementSave.setState(2L);
            projectManagementSave.setProjectCheckTime(new Date());
 
        }else {
            throw new ServiceException("该步骤无法生成审批单!");
        }
        int i = projectManagementMapper.updateProjectManagement(projectManagementSave);
        if (i<0){
            throw new ServiceException("生成审批单失败!");
        }
 
    }
 
    @Override
    public void projectArchive(ProjectArchiveReq req) {
        ProjectManagement projectManagement = checkDataPer(req.getId());
 
        Long state = projectManagement.getState();
        if (state != 3L) {
            throw new ServiceException("此状态不可归档!");
        }
        List<ProjectFile> projectFile = req.getProjectFile();
        ProjectManagement projectManagementUp = new ProjectManagement();
        projectManagementUp.setId(req.getId());
        projectManagementUp.setState(4L);
        projectManagementUp.setStep(4L);
//        projectManagementUp.setDesc(req.getDesc());
        projectManagementUp.setProjectEndTime(req.getProjectEndTime());
        projectManagementUp.setUpdateBy(SecurityUtils.getUsername());
        int i = projectManagementMapper.updateProjectManagement(projectManagementUp);
        if (i > 0 && StringUtils.isNotEmpty(projectFile)) {
            String username = SecurityUtils.getUsername();
            projectFile.forEach(file -> {
                file.setCreateBy(username);
                file.setProjectId(req.getId());
            });
//            int i1 = projectFileMapper.insertProjectFiles(projectFile);
//            if (i1 < 1){
//                throw new ServiceException("保存附件失败!");
//            }
            Boolean b = projectFileService.saveBatchProjectFile(projectFile);
            if (!b) {
                throw new ServiceException("保存附件失败!");
            }
        }
 
    }
 
    @Override
    public List<ProjectExpertStateResp> getProjectExpertSate(ProjectExpertStateReq req) {
 
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            req.setDeptId(SecurityUtils.getDeptId());
        }
        return projectManagementMapper.getProjectExpertSate(req);
    }
 
    @Override
    public List<ProjectExpertEvaluationResp> projectExpertEvaluationList(Long projectId) {
        List<ProjectExpertEvaluationResp> projectExpertEvaluationResps = projectExpertMapper.projectExpertEvaluationList(projectId);
        projectExpertEvaluationResps.sort(Comparator
                    .comparing(ProjectExpertCheckResp::getTeamLeader, Comparator.nullsLast(Comparator.reverseOrder()))
                    .thenComparing(ProjectExpertCheckResp::getSelectionMode, Comparator.nullsLast(Comparator.reverseOrder()))
                    .thenComparing(ProjectExpertCheckResp::getExpertType, Comparator.nullsLast(Comparator.naturalOrder())));
        return projectExpertEvaluationResps;
    }
 
    @Override
    @Transactional
    public void projectExpertDetailSave(ProjectExpertScoreSaveReq req) {
 
 
        Long projectId = null;
        Long expertType = req.getExpertType();
        List<Long> delData = req.getDelData();
        List<ProjectExpertScoreSaveReq.Score> saveData = req.getSaveData();
        if (expertType == 1) {
            ProjectExpert projectExpert1 = projectExpertMapper.selectProjectExpertById(req.getProjectExpertId());
            if (projectExpert1 == null) {
                throw new ServiceException("项目专家不存在!");
            }
            ProjectManagement projectManagement1 = checkData(projectExpert1.getProjectId());
            if (projectManagement1.getState() != 4) {
                throw new ServiceException("项目未完成!");
            }
            projectId = projectExpert1.getProjectId();
 
            if (StringUtils.isNotEmpty(delData)) {
                projectExpertDetailMapper.deleteProjectExpertDetailByIds(delData.toArray(new Long[delData.size()]));
            }
            //更新项目专家表
            ProjectExpert projectExpert = new ProjectExpert();
            BeanUtil.copyProperties(req, projectExpert);
            projectExpert.setId(req.getProjectExpertId());
            projectExpert.setScore(req.getScore());
            projectExpert.setEvaluationState(1L);
            int i = projectExpertMapper.updateProjectExpert(projectExpert);
            if (i < 1) {
                throw new ServiceException("保存失败!");
            }
 
            if (StringUtils.isNotEmpty(saveData)) {
                List<ProjectExpertDetail> projectExpertDetails = dealData(saveData, req.getProjectExpertId());
                List<ProjectExpertDetail> saveList = projectExpertDetails.stream()
                        .filter(dto -> dto.getId() == null)
                        .collect(Collectors.toList());
                if (StringUtils.isNotEmpty(saveList)) {
                    int i12 = projectExpertDetailMapper.batchInsertProjectExpertDetail(saveList);
                    if (i12 < 1) {
                        throw new ServiceException("保存失败!");
                    }
                }
                List<ProjectExpertDetail> updateList = projectExpertDetails.stream()
                        .filter(dto -> dto.getId() != null)
                        .collect(Collectors.toList());
                if (StringUtils.isNotEmpty(updateList)) {
                    int i1 = projectExpertDetailMapper.batchUpdateProjectExpertDetail(updateList);
                    if (i1 < 1) {
                        throw new ServiceException("保存失败!");
                    }
                }
            }
 
        }else {
            OutProjectExpert outProjectExpert1 = outProjectExpertMapper.selectById(req.getProjectExpertId());
            if (outProjectExpert1 == null) {
                throw new ServiceException("项目专家不存在!");
            }
            ProjectManagement projectManagement1 = checkData(outProjectExpert1.getProjectId());
            if (projectManagement1.getState() != 4) {
                throw new ServiceException("项目未完成!");
            }
            projectId = outProjectExpert1.getProjectId();
            if (StringUtils.isNotEmpty(delData)){
                outProjectExpertDetailMapper.update(new OutProjectExpertDetail(),
                        new LambdaUpdateWrapper<OutProjectExpertDetail>().in(OutProjectExpertDetail::getId, delData)
                                .set(OutProjectExpertDetail::getDelFlag, 1).set(OutProjectExpertDetail::getUpdateBy, SecurityUtils.getUsername())
                                .set(OutProjectExpertDetail::getUpdateTime, new Date()));
            }
            OutProjectExpert outProjectExpert = new OutProjectExpert();
            BeanUtil.copyProperties(req, outProjectExpert);
            outProjectExpert.setId(req.getProjectExpertId());
            outProjectExpert.setScore(req.getScore().intValue());
            outProjectExpert.setEvaluationState(1);
            int update = outProjectExpertMapper.updateById(outProjectExpert);
            if (update < 1) {
                throw new ServiceException("保存失败!");
            }
            if (StringUtils.isNotEmpty(saveData)) {
                List<OutProjectExpertDetail> outProjectExpertDetails = dealOutData(saveData, req.getProjectExpertId());
                outProjectExpertDetails.forEach(outProjectExpertDetail -> {
                    if (outProjectExpertDetail.getId() == null){
                        outProjectExpertDetailMapper.insert(outProjectExpertDetail);
                    }else {
                        outProjectExpertDetailMapper.updateById(outProjectExpertDetail);
                    }
                });
            }
        }
 
 
        //验证是否都已考评
        int unEva = projectExpertMapper.getUnEva(projectId);
        Long l = outProjectExpertMapper.selectCount(new LambdaQueryWrapper<OutProjectExpert>().eq(OutProjectExpert::getProjectId, projectId)
                .eq(OutProjectExpert::getDelFlag, 0).eq(OutProjectExpert::getEvaluationState, 0));
        if (unEva+l == 0) {
            ProjectManagement projectManagement = new ProjectManagement();
            projectManagement.setId(projectId);
            projectManagement.setEvaluationState(1L);
            projectManagement.setUpdateBy(SecurityUtils.getUsername());
            int i1 = projectManagementMapper.updateProjectManagement(projectManagement);
            if (i1 < 1) {
                throw new ServiceException("保存失败!");
            }
        }
 
    }
 
    @Override
    public List<ProjectExpertResp> projectExpertEvaList(SysProjectExpertReq req) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            req.setDeptId(SecurityUtils.getDeptId());
        }
 
        return projectExpertMapper.projectExpertEvaList(req);
//        if (req.getExpertType() == 1){
//            return projectExpertMapper.projectExpertEvaList(req);
//        }else {
//            return projectExpertMapper.projectExpertEvaOutList(req);
//        }
 
    }
 
    @Override
    public List<ProjectExpertDetail> selectProjectExpertDetailList(Long projectExpertId) {
        ProjectExpertDetail projectExpertDetail = new ProjectExpertDetail();
        projectExpertDetail.setProjectExpertId(projectExpertId);
        return projectExpertDetailMapper.selectProjectExpertDetailList(projectExpertDetail);
    }
 
    @Override
    public List<ProjectFile> selectProjectFileList(Long projectId, String module) {
        ProjectFile projectFile = new ProjectFile();
        projectFile.setProjectId(projectId);
        projectFile.setModule(module);
        List<ProjectFile> projectFiles = projectFileMapper.selectProjectFileList(projectFile);
        if (StringUtils.isNotEmpty(projectFiles)) {
            return projectFiles;
        }
        return Collections.emptyList();
    }
 
    @Override
    public ProjectNumResp getProjectNum() {
        ProjectNumResp projectNumResp = new ProjectNumResp();
        List<Integer> dataNum = projectManagementMapper.getDataNum(SecurityUtils.isAdmin(SecurityUtils.getUserId()) ? null : SecurityUtils.getDeptId());
        if (!StringUtils.isEmpty(dataNum)) {
            Map<Integer, Long> statusCounts = dataNum.stream()
                    .collect(Collectors.groupingBy(
                            status -> status,
                            Collectors.counting()
                    ));
            projectNumResp.setTotal(dataNum.size());
            int i = statusCounts.get(2) == null ? 0 : statusCounts.get(2).intValue();
            projectNumResp.setApproval(statusCounts.get(3) == null ? 0 + i : statusCounts.get(3).intValue() + i);
            projectNumResp.setExpert(statusCounts.get(1) == null ? 0 : statusCounts.get(1).intValue());
            projectNumResp.setStaging(statusCounts.get(0) == null ? 0 : statusCounts.get(0).intValue());
            projectNumResp.setEnd(statusCounts.get(4) == null ? 0 : statusCounts.get(4).intValue());
        }
        return projectNumResp;
    }
 
    @Override
    public ProjectDetailResp selectProjectExpertDetail(Long projectExpertId,Long expertType) {
        ProjectDetailResp projectDetailResp = new ProjectDetailResp();
        if (expertType == 1){
            ProjectExpert projectExpert = projectExpertMapper.selectProjectExpertById(projectExpertId);
            BeanUtil.copyProperties(projectExpert, projectDetailResp);
            if (projectDetailResp != null) {
                ProjectExpertDetail projectExpertDetail = new ProjectExpertDetail();
                projectExpertDetail.setProjectExpertId(projectExpertId);
                List<ProjectExpertDetail> projectExpertDetails = projectExpertDetailMapper.selectProjectExpertDetailList(projectExpertDetail);
                if (StringUtils.isNotEmpty(projectExpertDetails)) {
                    projectDetailResp.setExpertDetails(projectExpertDetails);
                }
                Long projectId = projectDetailResp.getProjectId();
                ProjectManagement projectManagement = projectManagementMapper.selectProjectManagementById(projectId);
                projectDetailResp.setDeptId(projectManagement.getDeptId());
                projectDetailResp.setDeptName(projectDetailResp.getDeptName());
                projectDetailResp.setProjectName(projectManagement.getProjectName());
                projectDetailResp.setProjectDateStart(projectManagement.getProjectDateStart());
                projectDetailResp.setProjectDateEnd(projectManagement.getProjectDateEnd());
                if (StringUtils.isNotEmpty(projectManagement.getJobCategory())) {
                    projectDetailResp.setJobCategory(projectManagement.getJobCategory());
                }
                SysExpertInfo sysExpertInfo = sysExpertInfoMapper.selectById(projectDetailResp.getExpertId());
                if (sysExpertInfo != null) {
                    projectDetailResp.setTitle(sysExpertInfo.getTitle());
                    projectDetailResp.setJob(sysExpertInfo.getJob());
                }
 
            }
        }else if (expertType == 2){
 
            OutProjectExpert outProjectExpert = outProjectExpertMapper.selectById(projectExpertId);
            BeanUtil.copyProperties(outProjectExpert, projectDetailResp);
            if (projectDetailResp != null) {
 
                LambdaQueryWrapper<OutProjectExpertDetail> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(OutProjectExpertDetail::getOutProjectExpertId, projectExpertId);
                List<OutProjectExpertDetail> outProjectExpertDetails = outProjectExpertDetailMapper.selectList(queryWrapper);
                if (StringUtils.isNotEmpty(outProjectExpertDetails)) {
                    List<ProjectExpertDetail> projectExpertDetails = new ArrayList<>();
                    outProjectExpertDetails.forEach(outProjectExpertDetail -> {
                        ProjectExpertDetail projectExpertDetail = new ProjectExpertDetail();
                        BeanUtil.copyProperties(outProjectExpertDetail, projectExpertDetail);
                        projectExpertDetails.add(projectExpertDetail);
 
                    });
                    projectDetailResp.setExpertDetails(projectExpertDetails);
                }
                Long projectId = projectDetailResp.getProjectId();
                ProjectManagement projectManagement = projectManagementMapper.selectProjectManagementById(projectId);
                projectDetailResp.setDeptId(projectManagement.getDeptId());
                projectDetailResp.setDeptName(projectDetailResp.getDeptName());
                projectDetailResp.setProjectName(projectManagement.getProjectName());
                projectDetailResp.setProjectDateStart(projectManagement.getProjectDateStart());
                projectDetailResp.setProjectDateEnd(projectManagement.getProjectDateEnd());
                if (StringUtils.isNotEmpty(projectManagement.getJobCategory())) {
                    projectDetailResp.setJobCategory(projectManagement.getJobCategory());
                }
 
            }
        }
 
        return projectDetailResp;
    }
 
    @Override
    public List<ProjectExpertExportInfoRes> projectExpertExportList(Long projectId) {
        ProjectManagement projectManagement = checkData(projectId);
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId()) && projectManagement.getDeptId().intValue() != SecurityUtils.getDeptId().intValue()) {
            throw new ServiceException("非本部门数据,无权操作!");
        }
        if (projectManagement.getState() != 4) {
            throw new ServiceException("项目未完成,无权操作!");
        }
        if (projectManagement.getEvaluationState() == 0) {
            throw new ServiceException("有专家未完成评定与报销,请完善后再试");
        }
 
        return projectExpertMapper.projectExpertExportList(projectId);
 
    }
 
    @Override
    public List<ProjectDataResp> selectProjectData(ProjectManagement projectManagement) {
        return projectManagementMapper.selectProjectData(projectManagement);
    }
 
    private List<ProjectExpertDetail> dealData(List<ProjectExpertScoreSaveReq.Score> data, Long projectExpertId) {
        List<ProjectExpertDetail> dealData = new ArrayList<>();
        data.forEach(dto -> {
            ProjectExpertDetail projectExpertDetail = new ProjectExpertDetail();
            projectExpertDetail.setProjectExpertId(projectExpertId);
            projectExpertDetail.setContent(dto.getContent());
            projectExpertDetail.setScore(dto.getScore());
            projectExpertDetail.setScoreType(dto.getScoreType());
            if (dto.getId() == null) {
                projectExpertDetail.setCreateBy(SecurityUtils.getUsername());
            } else {
                projectExpertDetail.setId(dto.getId());
                projectExpertDetail.setUpdateBy(SecurityUtils.getUsername());
            }
 
            dealData.add(projectExpertDetail);
        });
        return dealData;
    }
 
 
    private List<OutProjectExpertDetail> dealOutData(List<ProjectExpertScoreSaveReq.Score> data, Long projectExpertId) {
        List<OutProjectExpertDetail> dealData = new ArrayList<>();
        data.forEach(dto -> {
            OutProjectExpertDetail projectExpertDetail = new OutProjectExpertDetail();
            projectExpertDetail.setOutProjectExpertId(projectExpertId);
            projectExpertDetail.setContent(dto.getContent());
            projectExpertDetail.setScore(dto.getScore().intValue());
            projectExpertDetail.setScoreType(dto.getScoreType());
            if (dto.getId() == null) {
                projectExpertDetail.setCreateBy(SecurityUtils.getUsername());
            } else {
                projectExpertDetail.setId(dto.getId());
                projectExpertDetail.setUpdateBy(SecurityUtils.getUsername());
            }
 
            dealData.add(projectExpertDetail);
        });
        return dealData;
    }
 
 
    public ProjectManagement checkData(Long id) {
        ProjectManagement projectManagement = projectManagementMapper.selectProjectManagementById(id);
        if (ObjectUtil.isNull(projectManagement)) {
            throw new ServiceException("数据不存在!");
        }
        return projectManagement;
    }
 
    public ProjectManagement checkDataPer(Long id) {
        ProjectManagement projectManagement = checkData(id);
        Long deptId = projectManagement.getDeptId();
        // 本部门审批?
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId()) && deptId.intValue() != SecurityUtils.getDeptId().intValue()) {
            throw new ServiceException("非本处室数据,无权操作!");
        }
        if (projectManagement.getState() == 4) {
            throw new ServiceException("已结项,无权操作!");
        }
        return projectManagement;
    }
 
 
    private void checkDelData(Long id) {
        ProjectManagement projectManagement = checkData(id);
        Long deptId = projectManagement.getDeptId();
        // 本部门审批?
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId()) && deptId.intValue() != SecurityUtils.getDeptId().intValue()) {
            throw new ServiceException("非本处室数据,无权操作!");
        }
    }
 
 
}