shenyijian
2024-09-05 42968703ee1ce4e34991bc0f1e189d2245cbd600
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
package cn.gistack.sm.sg.service.impl;
 
import cn.gistack.sm.sg.DO.*;
import cn.gistack.sm.sg.DTO.ProjectSearchDTO;
import cn.gistack.sm.sg.VO.*;
import cn.gistack.sm.sg.excel.ProjectExcel;
import cn.gistack.sm.sg.mapper.SgGxStepMapper;
import cn.gistack.sm.sg.mapper.SgProgressReportMapper;
import cn.gistack.sm.sg.mapper.SgProjectInfoMapper;
import cn.gistack.sm.sg.service.SgProjectInfoService;
import cn.gistack.sm.sjztmd.entity.AttAdBase;
import cn.gistack.sm.sjztmd.service.IAttAdBaseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 施工项目表 服务实现类
 * </p>
 *
 * @author shenyijian
 * @since 2024-06-13 11:51:20
 */
@Service
public class SgProjectInfoServiceImpl extends ServiceImpl<SgProjectInfoMapper, SgProjectInfoDO> implements SgProjectInfoService {
    @Autowired
    private SgProjectInfoMapper sgProjectInfoMapper;
    @Autowired
    private IAttAdBaseService iAttAdBaseService;
 
    @Autowired
    private SgProgressReportMapper sgProgressReportMapper;
 
    @Autowired
    private SgGxStepMapper sgGxStepMapper;
 
    @Override
    public Object queryPageList(IPage<SgProjectInfoDO> page, ProjectSearchDTO searchDTO) {
        List<String> sgCodeS = getSgCodeByUser();
        searchDTO.setSgCodeS(sgCodeS);
        Boolean isPage = searchDTO.getIsPage();
        if (isPage==null) isPage = true;
        if (isPage ){
            List<SgProjectInfoDO> sgProjectInfoDOS = baseMapper.queryPageList(page,searchDTO);
            List<SgProjectInfoDO> projectProgress = getProjectProgress(sgProjectInfoMapper.queryStepByProject(searchDTO));
            if (CollectionUtils.isNotEmpty(projectProgress)){
                Map<Long, String> progressMap = projectProgress.stream().filter(s->s.getProjectProgress()!=null).collect(Collectors.toMap(SgProjectInfoDO::getId, SgProjectInfoDO::getProjectProgress));
                sgProjectInfoDOS.forEach(s->{
                    s.setProjectProgress(progressMap.get(s.getId()));
                });
            }
            return page.setRecords(sgProjectInfoDOS);
        }else {
            List<SgAllDeptOrUser> allDepts = sgProjectInfoMapper.queryDept();
            Map<Long, String> deptMap = allDepts.stream().collect(Collectors.toMap(SgAllDeptOrUser::getId, SgAllDeptOrUser::getName));
            List<SgAllDeptOrUser> allUser = sgProjectInfoMapper.queryUser();
            Map<Long, String> userMap = allUser.stream().collect(Collectors.toMap(SgAllDeptOrUser::getId, SgAllDeptOrUser::getName));
            List<SgProjectInfoDO> sgProjectInfoDOS = baseMapper.queryPageList(null,searchDTO);
            List<SgProjectInfoDO> projectProgress = getProjectProgress(sgProjectInfoMapper.queryStepByProject(searchDTO));
            if (CollectionUtils.isNotEmpty(projectProgress)){
                Map<Long, String> progressMap = projectProgress.stream().filter(s->s.getProjectProgress()!=null).collect(Collectors.toMap(SgProjectInfoDO::getId, SgProjectInfoDO::getProjectProgress));
                sgProjectInfoDOS.forEach(s->{
                    s.setProjectProgress(progressMap.get(s.getId()));
                });
            }
            sgProjectInfoDOS.forEach(s->{
                //土建
                if (StringUtils.isNotBlank(s.getConstructionUnit())){
                    s.setConstructionUnit(deptMap.get(Long.valueOf(s.getConstructionUnit())));
                }
                s.setConstructionPerson(getUserNames(userMap, s.getConstructionPerson()));
 
                //设备
                if (StringUtils.isNotBlank(s.getSelfUnit())){
                    s.setSelfUnit(deptMap.get(Long.valueOf(s.getSelfUnit())));
                }
                s.setSelfPerson(getUserNames(userMap, s.getSelfPerson()));
 
                //设计
                if (StringUtils.isNotBlank(s.getDesignUnit())){
                    s.setDesignUnit(deptMap.get(Long.valueOf(s.getDesignUnit())));
                }
                s.setDesignPerson(getUserNames(userMap, s.getDesignPerson()));
 
                //监理
                if (StringUtils.isNotBlank(s.getControlUnit())){
                    s.setControlUnit(deptMap.get(Long.valueOf(s.getControlUnit())));
                }
                s.setControlPerson(getUserNames(userMap, s.getControlPerson()));
 
                //检测
                if (StringUtils.isNotBlank(s.getAcceptanceUnit())){
                    s.setAcceptanceUnit(deptMap.get(Long.valueOf(s.getAcceptanceUnit())));
                }
                s.setAcceptancePerson(getUserNames(userMap, s.getAcceptancePerson()));
            });
            return sgProjectInfoDOS;
        }
 
    }
 
    /**
     * 获取用户姓名
     * @param userMap
     * @return
     */
    private  String getUserNames(Map<Long, String> userMap,String person ) {
        String userNames = "";
        if (StringUtils.isNotBlank(person)){
            // 使用Stream API来处理分割和映射
             userNames = Arrays.stream(person.split(",")) // 将字符串按逗号分割成数组,并转为Stream
                .map(Long::valueOf) // 将每个字符串元素转换为Long
                .map(userMap::get) // 使用userMap将Long映射为对应的用户名
                .collect(Collectors.joining(",")); // 使用逗号将用户名拼接成一个字符串
            return userNames;
        }
        return userNames;
    }
 
    /**
     * 获取当前用户的工程编码
     */
    public List<String> getSgCodeByUser(){
        Long userId = AuthUtil.getUserId();
        String sgCode =  sgProjectInfoMapper.getSgCodeByUser(userId.toString());
        if (StringUtils.isNotBlank(sgCode)){
            List<String> list = List.of(sgCode.split(","));
            return list;
        }
        return null;
    }
    /**
     * 获取项目及项目下设备及设备下工序进度
     * @param searchDTO
     * @return
     */
    @Override
    public List<SgProjectInfoDO> queryStepByProject(ProjectSearchDTO searchDTO) {
        List<PreAcceVO>  palist = new ArrayList<>();
        List<SgProjectInfoVO> list = sgProjectInfoMapper.queryStepByProject( searchDTO);
        palist.add(handleQuery(25, list.get(0).getId()));
        palist.add(handleQuery(26, list.get(0).getId()));
        List<SgProjectInfoDO> projectProgress = getProjectProgress(list);
        projectProgress.forEach(s->{
            s.setPreAcce(palist);
        });
 
 
        return projectProgress;
    }
 
    /**
     * 获取项目进度
     * @param list
     * @return
     */
    @NotNull
    public List<SgProjectInfoDO> getProjectProgress(List<SgProjectInfoVO> list) {
        //项目分组
        Map<Long, List<SgProjectInfoVO>> projectMap = list.stream().collect(Collectors.groupingBy(SgProjectInfoVO::getId));
        List<SgProjectInfoDO> result = new ArrayList<>();
        for (Map.Entry<Long, List<SgProjectInfoVO>> entry : projectMap.entrySet()) {
            List<SgDeviceDO> allDevice = new ArrayList<>();
            Long porId = entry.getKey();
            List<SgProjectInfoVO> value = entry.getValue();
            if (CollectionUtils.isEmpty(value)){
                continue;
            }
            SgProjectInfoVO sgProjectInfoVO = value.get(0);
            SgProjectInfoDO sgProjectInfoDO = SgProjectInfoDO.builder().id(porId).lonLat(sgProjectInfoVO.getLonLat()).status(sgProjectInfoVO.getProjectStatus()).projectName(sgProjectInfoVO.getProjectName()).build();
            //父类型分组
            List<PtypeVO> pTypes = new ArrayList();
            Map<String, List<SgProjectInfoVO>> pTypeMap = value.stream().filter(s->s.getPtype()!=null).collect(Collectors.groupingBy(SgProjectInfoVO::getPtype));
            for (Map.Entry<String, List<SgProjectInfoVO>> pty : pTypeMap.entrySet()) {
                List<SgProjectInfoVO> ptyValue = pty.getValue();
                if (CollectionUtils.isEmpty(ptyValue)){
                    continue;
                }
                PtypeVO ptypeVO = PtypeVO.builder().pTypeName(ptyValue.get(0).getPtypeName()).build();
                List<SgTypeVO> sgGxDOS = new ArrayList<>();
                //类型分组
                Map<String, List<SgProjectInfoVO>> GxMap = ptyValue.stream().filter(s->s.getDeviceType()!=null).collect(Collectors.groupingBy(SgProjectInfoVO::getDeviceType));
                for (Map.Entry<String, List<SgProjectInfoVO>> ty : GxMap.entrySet()) {
                    String type = ty.getKey();
                    List<SgProjectInfoVO> gxList = ty.getValue();
                    if (CollectionUtils.isEmpty(gxList)){
                        continue;
                    }
                    SgTypeVO typeVO = SgTypeVO.builder().typeName(gxList.get(0).getTypeName()).build();
                    List<SgDeviceDO> deviceDOS = new ArrayList<>();
                    //设备分组
                    Map<Long, List<SgProjectInfoVO>> dMap = gxList.stream().collect(Collectors.groupingBy(SgProjectInfoVO::getDId));
                    for (Map.Entry<Long, List<SgProjectInfoVO>> device : dMap.entrySet()) {
                        Long dId = device.getKey();
                        List<SgProjectInfoVO> deList = device.getValue();
                        SgDeviceDO sgDeviceDO = new SgDeviceDO();
                        sgDeviceDO.setId(dId);
                        sgDeviceDO.setDeviceName(deList.get(0).getDeviceName());
                        List<SgGxStepDO> stepDOS = deList.stream().filter(s -> s.getReportStatus() == null || (s.getReportStatus() != 4 && !(s.getReportStatus() == 3 && s.getResubmit() != 0))).map(s -> SgGxStepDO.builder().id(s.getStepId()).gxStepNum(s.getGxStepNum()).gxStepName(s.getGxStepName()).deviceId(s.getDId()).approvalStatus(s.getReportStatus()).build()).collect(Collectors.toList());
                        List<SgGxStepDO> stepOver = stepDOS.stream().filter(s -> s.getApprovalStatus() != null && s.getApprovalStatus() == 2).collect(Collectors.toList());
                        BigDecimal stepSize = BigDecimal.valueOf(stepDOS.size());
                        BigDecimal stepOverSize = BigDecimal.ZERO;
                        if (!stepOver.isEmpty()){
                            stepOverSize = BigDecimal.valueOf(stepOver.size());
                        }
                        BigDecimal divide = stepOverSize.divide(stepSize, 2, RoundingMode.HALF_UP);
                        sgDeviceDO.setStatus(divide.compareTo(new BigDecimal(1)) == 0?2:deList.get(0).getDeviceStatus());
                        sgDeviceDO.setProgress(divide.toString());
                        sgDeviceDO.setDeviceProgress(getBigDecimalPercent(divide));
                        sgDeviceDO.setStepDOList(stepDOS);
                        deviceDOS.add(sgDeviceDO);
                        allDevice.add(sgDeviceDO);
                    }
                    BigDecimal deviceSize = BigDecimal.valueOf(deviceDOS.size());
                    double sum = deviceDOS.stream().mapToDouble(s -> Double.parseDouble(s.getProgress())).sum();
                    BigDecimal deviceOver = BigDecimal.valueOf(sum);
                    BigDecimal divideDevice = deviceOver.divide(deviceSize, 2, RoundingMode.HALF_UP);
                    typeVO.setDeviceDOS(deviceDOS);
                    typeVO.setTypeProgress(getBigDecimalPercent(divideDevice));
                    typeVO.setProgress(divideDevice.toString());
                    sgGxDOS.add(typeVO);
                }
                BigDecimal sgGxSize = BigDecimal.valueOf(sgGxDOS.size());
                double sum = sgGxDOS.stream().mapToDouble(s -> Double.parseDouble(s.getProgress())).sum();
                BigDecimal sgGxOver = BigDecimal.valueOf(sum);
                BigDecimal divideSgGx = sgGxOver.divide(sgGxSize, 2, RoundingMode.HALF_UP);
                ptypeVO.setPTypeProgress(getBigDecimalPercent(divideSgGx));
                ptypeVO.setSgTypeVOS(sgGxDOS);
                pTypes.add(ptypeVO);
            }
            BigDecimal allDeviceSize = BigDecimal.valueOf(allDevice.size());
            if (!allDeviceSize.equals(BigDecimal.ZERO)){
                double sum = allDevice.stream().mapToDouble(s -> Double.parseDouble(s.getProgress())).sum();
                BigDecimal allDeviceOver = BigDecimal.valueOf(sum);
                BigDecimal allDivideDevice = allDeviceOver.divide(allDeviceSize, 2, RoundingMode.HALF_UP);
                sgProjectInfoDO.setPtypeVOS(pTypes);
                sgProjectInfoDO.setProjectProgress( getBigDecimalPercent(allDivideDevice));
                sgProjectInfoDO.setProjectProgressNum(allDivideDevice);
            }
            result.add(sgProjectInfoDO);
        }
        return result;
    }
 
    @Override
    public List<SgProjectInfoVO> progressList(ProjectSearchDTO searchDTO) {
        List<SgProjectInfoVO> list = new ArrayList<>();
        List<SgProjectInfoDO> projectProgress = new ArrayList<>();
        if(StringUtils.isNotBlank(searchDTO.getAdCode())){
            projectProgress = getProjectProgress(sgProjectInfoMapper.queryStepByProject1(searchDTO));
        }else {
             projectProgress = getProjectProgress(sgProjectInfoMapper.queryStepByProject(searchDTO));
        }
 
        if (CollectionUtils.isNotEmpty(projectProgress)){
        projectProgress.forEach(s->{
            SgProjectInfoVO sgProjectInfoVO = new SgProjectInfoVO();
            sgProjectInfoVO.setId(s.getId());
            sgProjectInfoVO.setProjectName(s.getProjectName());
            sgProjectInfoVO.setProjectProgress(s.getProjectProgress());
            sgProjectInfoVO.setLonLat(s.getLonLat());
            sgProjectInfoVO.setProjectProgressNum(s.getProjectProgressNum());
            sgProjectInfoVO.setStatus(s.getStatus());
            list.add(sgProjectInfoVO);
        });}
        return list;
    }
 
    /**
     * 项目进度列表(分页)
     * @param page
     * @param searchDTO
     * @return
     */
    @Override
    public IPage<SgProjectInfoVO> progressPage(IPage<SgProjectInfoVO> page, ProjectSearchDTO searchDTO) {
        List<SgProjectInfoVO> list = new ArrayList<>();
            if(StringUtils.isNotBlank(searchDTO.getAdCode())){
                list =    sgProjectInfoMapper.progressList1(page, searchDTO);
            }else {
                list = sgProjectInfoMapper.progressList(page, searchDTO);
            }
 
        List<Long> ids = list.stream().map(SgProjectInfoVO::getId).collect(Collectors.toList());
        searchDTO.setProjectIds(ids);
        List<SgProjectInfoDO> projectProgress = new ArrayList<>();
        if(StringUtils.isNotBlank(searchDTO.getAdCode())){
            projectProgress = getProjectProgress(sgProjectInfoMapper.queryStepByProject1(searchDTO));
        }else {
            projectProgress = getProjectProgress(sgProjectInfoMapper.queryStepByProject(searchDTO));
        }
 
        if (CollectionUtils.isNotEmpty(projectProgress)){
        Map<Long, String> progressMap = projectProgress.stream().filter(s->s!=null && s.getProjectProgress() !=null).collect(Collectors.toMap(SgProjectInfoDO::getId, SgProjectInfoDO::getProjectProgress));
        list.forEach(s->{
            s.setProjectProgress(progressMap.get(s.getId()));
        });}
        return page.setRecords(list);
    }
 
    @Override
    public List<SgProjectInfoDO> listAll(ProjectSearchDTO searchDTO) {
        List<String> sgCodeS = getSgCodeByUser();
        searchDTO.setSgCodeS(sgCodeS);
        return sgProjectInfoMapper.queryPageList(null,searchDTO);
    }
 
    /**
     * 项目统计饼状图
     * @param code
     * @return
     */
    @Override
    public SgStatisticsVO getsgStatistics(String code) {
        AttAdBase ywxtAdDetail = iAttAdBaseService.getYwxtAdDetail(code);
 
        if (ywxtAdDetail == null){
            return sgProjectInfoMapper.getSgstatiss("0",code);
        }
            return sgProjectInfoMapper.getSgstatiss(ywxtAdDetail.getAdGrad(),code);
    }
 
    /**
     * 项目统计表格
     * @param code
     * @return
     */
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SgStatisProVO> getsgStatisticsList(String code) {
        AttAdBase ywxtAdDetail = iAttAdBaseService.getYwxtAdDetail(code);
        List<SgStatisProVO> sgStatisProVOS;
        //片区
        if (ywxtAdDetail == null) {
            sgStatisProVOS = sgProjectInfoMapper.getsgStatisticsZero(code);
            getDeviceTypes(sgStatisProVOS);
//            String getname = sgProjectInfoMapper.getname(code);
            String getname = code;
            updateProjectProgress(sgStatisProVOS, code, getname, "2");
        } else if (ywxtAdDetail.getAdGrad().equals("1")) {//默认
            sgStatisProVOS = sgProjectInfoMapper.getsgStatisticsOne(code);
            sgStatisProVOS = getSgStatisProVOS(sgStatisProVOS);
            getDeviceTypes(sgStatisProVOS);
            updateProjectProgress(sgStatisProVOS, ywxtAdDetail.getPAdCode(), ywxtAdDetail.getPAdName(), "0");
        } else {//区县
            sgStatisProVOS = sgProjectInfoMapper.getsgStatisticsList(code);
            getDeviceTypes(sgStatisProVOS);
            updateProjectProgress(sgStatisProVOS, ywxtAdDetail.getAdCode(), ywxtAdDetail.getAdName(), "3");
        }
        return sgStatisProVOS;
    }
 
    /**
     * 标段排序
     * @param sgStatisProVOS
     * @return
     */
    private  List<SgStatisProVO> getSgStatisProVOS(List<SgStatisProVO> sgStatisProVOS) {
        sgStatisProVOS = sgStatisProVOS.stream()
            .sorted(Comparator.comparing(s -> {
                String scode = s.getCode();
                if (scode.equals("第一标段")) {
                    return 1;
                } else if (scode.equals("第二标段")) {
                    return 2;
                } else if (scode.equals("第三标段")) {
                    return 3;
                } else if (scode.equals("第四标段")) {
                    return 4;
                } else if (scode.equals("第五标段")) {
                    return 5;
                } else {
                    return Integer.MAX_VALUE;
                }
            }))
            .collect(Collectors.toList());
        return sgStatisProVOS;
    }
 
    /**
     * 统计设备类型开工率
     * @param sgStatisProVOS
     */
    private void getDeviceTypes(List<SgStatisProVO> sgStatisProVOS) {
        if (CollectionUtils.isNotEmpty(sgStatisProVOS)){
            for (SgStatisProVO vo : sgStatisProVOS) {
                String proIds = vo.getProIds();
                if (StringUtils.isNotBlank(proIds)){
                List<SgStatisDeviceVO> deviceVOS =    sgProjectInfoMapper.getDeviceTypes("("+proIds+")");
                if(CollectionUtils.isNotEmpty(deviceVOS)){
                    Map<String, SgStatisDeviceVO> map = deviceVOS.stream().collect(Collectors.toMap(SgStatisDeviceVO::getPType, Function.identity()));
                    //表面变形
                    SgStatisDeviceVO sg108 = map.get("sg108");
                    if (Objects.nonNull(sg108)){
                        Integer deviceCount = sg108.getDeviceCount();
                        Integer deviceStart = sg108.getDeviceStart();
                        vo.setSurfaceNum(deviceCount);
                        if (deviceCount != 0){
                            BigDecimal divide = BigDecimal.valueOf(deviceStart).divide(BigDecimal.valueOf(deviceCount), 2, BigDecimal.ROUND_UP);
                            vo.setSeepageProgress(getBigDecimalPercent(divide));
                        }else {
                            vo.setSeepageProgress("0%");
                        }
                    }
                    //视频
                    SgStatisDeviceVO sg104 = map.get("sg104");
                    if (Objects.nonNull(sg104)){
                        Integer deviceCount = sg104.getDeviceCount();
                        Integer deviceStart = sg104.getDeviceStart();
                        vo.setVideoNum(deviceCount);
                        if (deviceCount != 0){
                            BigDecimal divide = BigDecimal.valueOf(deviceStart).divide(BigDecimal.valueOf(deviceCount), 2, BigDecimal.ROUND_UP);
                            vo.setVideoProgress(getBigDecimalPercent(divide));
                        }else {
                            vo.setVideoProgress("0%");
                        }
                    }
                    //渗压
                    SgStatisDeviceVO sg101 = map.get("sg101");
                    if (Objects.nonNull(sg101)){
                        Integer deviceCount = sg101.getDeviceCount();
                        Integer deviceStart = sg101.getDeviceStart();
                        vo.setTransNum(deviceCount);
                        if (deviceCount != 0){
                            BigDecimal divide = BigDecimal.valueOf(deviceStart).divide(BigDecimal.valueOf(deviceCount), 2, BigDecimal.ROUND_UP);
                            vo.setTransProgress(getBigDecimalPercent(divide));
                        }else {
                            vo.setTransProgress("0%");
                        }
                    }
                    //渗流
                    SgStatisDeviceVO sg102 = map.get("sg102");
                    if (Objects.nonNull(sg102)){
                        Integer deviceCount = sg102.getDeviceCount();
                        Integer deviceStart = sg102.getDeviceStart();
                        vo.setSeepageNum(deviceCount);
                        if (deviceCount != 0){
                            BigDecimal divide = BigDecimal.valueOf(deviceStart).divide(BigDecimal.valueOf(deviceCount), 2, BigDecimal.ROUND_UP);
                            vo.setSeepageProgress(getBigDecimalPercent(divide));
                        }else {
                            vo.setSeepageProgress("0%");
                        }
                    }
                    }
                }
            }
        }
    }
 
    private void updateProjectProgress(List<SgStatisProVO> sgStatisProVOS, String pcode, String pname, String adGrad) {
        ProjectSearchDTO projectSearchDTO = new ProjectSearchDTO();
        for (SgStatisProVO s : sgStatisProVOS) {
            if (s.getCommenceNum() == 0) {
                s.setProjectProgress("0%");
                s.setPcode(pcode);
                s.setPname(pname);
                s.setAdGrad(adGrad);
            } else {
                List<String> ids = sgProjectInfoMapper.getProjectId(adGrad, s.getCode());
                List<BigDecimal> bigDecimals = new ArrayList<>();
 
                for (String id : ids) {
                    projectSearchDTO.setProjectId(id);
                    List<SgProjectInfoVO> list = sgProjectInfoMapper.queryStepByProject(projectSearchDTO);
                    getProjectProgress(list).forEach(ss -> bigDecimals.add(ss.getProjectProgressNum()));
                }
 
                BigDecimal reduce = bigDecimals.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
                String bigDecimalPercent1 = getBigDecimalPercent(reduce.divide(BigDecimal.valueOf(s.getProjectSum()), 2, RoundingMode.HALF_UP));
                s.setProjectProgress(s.getCommenceNum() == 0? "0%" : bigDecimalPercent1);
                s.setPcode(pcode);
                s.setPname(pname);
                s.setAdGrad(adGrad);
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SgStatistccVO> getsgStatisticscc(String code) {
        AttAdBase ywxtAdDetail = iAttAdBaseService.getYwxtAdDetail(code);
 
 
        List<String> ids = sgProjectInfoMapper.getProjectId(ywxtAdDetail == null ? "0" : ywxtAdDetail.getAdGrad(), code);
        Map<String, Integer> mun = new HashMap<>();
        List<SgStatistccVO> sgdevices = sgProjectInfoMapper.getSgdevices(code, ywxtAdDetail == null ? "0" : ywxtAdDetail.getAdGrad());
        sgdevices.forEach(sgStatistccVO -> {
                mun.put(sgStatistccVO.getTypeName(), sgStatistccVO.getDeviceNum());
            }
        );
        Map<String, BigDecimal> bigDecimals = new HashMap<>();
        ProjectSearchDTO projectSearchDTO = new ProjectSearchDTO();
        ids.forEach(id -> {
 
            projectSearchDTO.setProjectId(id);
            List<SgProjectInfoVO> list = sgProjectInfoMapper.queryStepByProject(projectSearchDTO);
            List<SgProjectInfoDO> projectProgress = getProjectProgress(list);
            projectProgress.forEach(p -> {
 
                List<PtypeVO> ptypeVOS = p.getPtypeVOS();
                ptypeVOS.forEach(sgTypeVO -> {
                    String typeProgress = sgTypeVO.getPTypeProgress().replace("%", "");
                    BigDecimal percentageBigDecimal = new BigDecimal(typeProgress).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
                    bigDecimals.merge(sgTypeVO.getPTypeName(), percentageBigDecimal, BigDecimal::add);
                });
 
            });
 
        });
 
        for (String name : bigDecimals.keySet()) {
 
            if (mun.containsKey(name)) {
 
                BigDecimal bigDecimal = bigDecimals.get(name);
                Integer intValue = mun.get(name);
                BigDecimal result = bigDecimal.divide(new BigDecimal(intValue), 2, RoundingMode.HALF_UP);
 
                sgdevices.forEach(sgStatistccVO -> {
                    if (sgStatistccVO.getTypeName().equals(name)) {
                        sgStatistccVO.setTypeProgress(getBigDecimalPercent(result));
                    }
                });
            }
 
        }
 
        return sgdevices;
    }
 
    /**
     * 获取百分比
     * @param bigDecimal
     * @return
     */
    private  String getBigDecimalPercent(BigDecimal bigDecimal) {
        BigDecimal percentage = bigDecimal.multiply(new BigDecimal("100"));
        // 如果不需要保留小数位,可以设置一个scale为0
        BigDecimal percentageRounded = percentage.setScale(0, RoundingMode.HALF_UP);
        return percentageRounded+"%";
    }
 
    @Override
    public Map<String, Object> projectCount(ProjectSearchDTO searchDTO) {
        Map<String, Object> map  = new HashMap<>();
        List<SgProjectInfoVO> progressList = progressList(searchDTO);
        BigDecimal reduce = progressList.stream().filter(s->s.getProjectProgressNum()!=null).map(SgProjectInfoDO::getProjectProgressNum).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal divide = reduce.divide(BigDecimal.valueOf(progressList.size()==0?1:progressList.size()), 2, RoundingMode.HALF_UP);
        List<SgProjectInfoDO> pending = progressList.stream().filter(s -> s.getStatus() == 0).collect(Collectors.toList());
        List<SgProjectInfoDO> progress = progressList.stream().filter(s -> s.getStatus() == 1).collect(Collectors.toList());
        List<SgProjectInfoDO> over = progressList.stream().filter(s -> s.getStatus() == 2).collect(Collectors.toList());
        map.put("all",  progressList.size());
        map.put("pending", pending.size());
        map.put("progress",  progress.size());
        map.put("over",  over.size());
        map.put("projectProgress",  getBigDecimalPercent(divide));
        return map;
    }
 
    @Override
    public void importProject(List<ProjectExcel> data, Boolean isCovered) {
 
    }
 
    // 处理查询和添加到列表的方法
    private  PreAcceVO handleQuery( int stepIdForNull, Long programId) {
        QueryWrapper<SgProgressReportDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gx_step_id", stepIdForNull)
            .eq("profram_id", programId);
        SgProgressReportDO sgProgressReportDO = null;
 
        List<SgProgressReportDO> sgProgressReportDOS = sgProgressReportMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(sgProgressReportDOS)) {
            List<SgProgressReportDO> collect = sgProgressReportDOS.stream().filter(s -> s.getStatus() == null || (s.getStatus() != 4 && !(s.getStatus() == 3 && s.getResubmit() != 0))).collect(Collectors.toList());
          if (!collect.isEmpty()){
              sgProgressReportDO = collect.get(0);
          }
        }
        PreAcceVO preAcceVO = new PreAcceVO();
        if (sgProgressReportDO!= null) {
            preAcceVO.setId(sgProgressReportDO.getId().toString());
            preAcceVO.setName(sgProgressReportDO.getGxStepName());
            preAcceVO.setStatus((long) sgProgressReportDO.getStatus());
            preAcceVO.setProgramId(programId);
            preAcceVO.setGxDeviceId(sgProgressReportDO.getGxDeviceId());
            preAcceVO.setGxStepId(sgProgressReportDO.getGxStepId());
        } else {
            SgGxStepDO sgGxStepDO = sgGxStepMapper.selectById(stepIdForNull);
            preAcceVO.setName(sgGxStepDO.getGxStepName());
            preAcceVO.setProgramId(programId);
            preAcceVO.setGxDeviceId(stepIdForNull != 25? (long)998:(long)999);
            preAcceVO.setGxStepId(sgGxStepDO.getId());
 
        }
        return  preAcceVO;
 
    }
 
 
    @Override
    public List<SgProjectTreeVO> projectTree(ProjectSearchDTO dto) {
        List<SgProjectTreeVO> list =     sgProjectInfoMapper.projectTree(dto);
        if (CollectionUtils.isNotEmpty(list)){
        // 按省、市、县分组
        Map<String, Map<String, Map<String, List<SgProjectTreeVO>>>> groupedProjects = list.stream()
            .collect(Collectors.groupingBy(SgProjectTreeVO::getProvinceNm,
                Collectors.groupingBy(SgProjectTreeVO::getCityNm,
                    Collectors.groupingBy(SgProjectTreeVO::getCountyNm))));
 
        // 构建树结构
        List<SgProjectTreeVO> result = new ArrayList<>();
        for (Map.Entry<String, Map<String, Map<String, List<SgProjectTreeVO>>>> provinceEntry : groupedProjects.entrySet()) {
            String province = provinceEntry.getKey();
            Map<String, Map<String, List<SgProjectTreeVO>>> cityMap = provinceEntry.getValue();
 
            SgProjectTreeVO provinceNode = new SgProjectTreeVO();
            provinceNode.setProvinceNm(province);
            provinceNode.setId(UUID.randomUUID().toString().replace("-",""));
            provinceNode.setRegionName(province);
            provinceNode.setChildList(new ArrayList<>());
            long provinceNum = 0;
            for (Map.Entry<String, Map<String, List<SgProjectTreeVO>>> cityEntry : cityMap.entrySet()) {
                String city = cityEntry.getKey();
                Map<String, List<SgProjectTreeVO>> countyMap = cityEntry.getValue();
 
                SgProjectTreeVO cityNode = new SgProjectTreeVO();
                List<String> cityIds = new ArrayList<>();
                cityNode.setCityNm(city);
                cityNode.setId(UUID.randomUUID().toString().replace("-",""));
                cityNode.setRegionName(city);
                cityNode.setChildList(new ArrayList<>());
 
                for (Map.Entry<String, List<SgProjectTreeVO>> countyEntry : countyMap.entrySet()) {
                    String county = countyEntry.getKey();
                    List<SgProjectTreeVO> countyProjects = countyEntry.getValue();
                    for (SgProjectTreeVO countyProject : countyProjects) {
                        countyProject.setRegionName(countyProject.getResNm());
                    }
                    SgProjectTreeVO countyNode = new SgProjectTreeVO();
                    countyNode.setCountyNm(county);
                    countyNode.setRegionName(county);
                    countyNode.setId(UUID.randomUUID().toString().replace("-",""));
                    countyNode.setChildList(new ArrayList<>(countyProjects));
                    countyNode.setChildNum((long) countyProjects.size());
                    countyNode.setProjectIds(countyProjects.stream().map(SgProjectTreeVO::getId).collect(Collectors.toList()));
                    cityIds.addAll(countyNode.getProjectIds());
                    cityNode.getChildList().add(countyNode);
                }
 
                cityNode.setChildNum((long) cityIds.size());
                cityNode.setProjectIds(cityIds);
                provinceNode.getChildList().add(cityNode);
                provinceNum+= (long) cityIds.size();
            }
 
            provinceNode.setChildNum(provinceNum);
            result.add(provinceNode);
        }
        return result;
        }
        return new ArrayList<>();
    }
 
}