yanghb
2025-02-18 1fc203f758ace853f4bfa900c422c2f741d09e79
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
package com.zzg.system.service.state.impl;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.zzg.common.core.domain.entity.state.StateHousehold;
import com.zzg.common.core.domain.entity.state.StateHouseholdOwner;
import com.zzg.common.core.domain.entity.state.StateProjectCompensateStandard;
import com.zzg.common.core.domain.entity.state.StateProjectCompensation;
import com.zzg.common.core.domain.entity.system.SysDictData;
import com.zzg.common.enums.CompensateTypeEnum;
import com.zzg.common.enums.CompensationCategoryEnum;
import com.zzg.common.enums.DeleteFlagEnum;
import com.zzg.common.enums.HouseOwnerTyeEnum;
import com.zzg.common.enums.HouseUsingTypeEnum;
import com.zzg.common.exception.GlobalException;
import com.zzg.common.utils.RoundingUtil;
import com.zzg.common.utils.StringUtils;
import com.zzg.system.convert.StateProjectConvert;
import com.zzg.system.domain.bo.AwardBO;
import com.zzg.system.domain.bo.CalculateCompensationBO;
import com.zzg.system.domain.bo.HomeCompensationBO;
import com.zzg.system.domain.bo.PolicyCompensationBO;
import com.zzg.system.domain.bo.PolicySubsidyBO;
import com.zzg.system.domain.bo.PolicySupplementBO;
import com.zzg.system.domain.vo.CalculateCompensationVO;
import com.zzg.system.domain.vo.OtherCompensationBO;
import com.zzg.system.domain.vo.StateHouseholdOwnerVO;
import com.zzg.system.mapper.state.StateHouseholdOwnerMapper;
import com.zzg.system.service.state.StateHouseholdOwnerService;
import com.zzg.system.service.state.StateHouseholdService;
import com.zzg.system.service.state.StateProjectCompensateStandardService;
import com.zzg.system.service.state.StateProjectCompensationService;
import com.zzg.system.service.system.ISysDictDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
 
@Service
@RequiredArgsConstructor
public class StateHouseholdOwnerServiceImpl
        extends ServiceImpl<StateHouseholdOwnerMapper, StateHouseholdOwner>
        implements StateHouseholdOwnerService {
 
    private final StateHouseholdService stateHouseholdService;
    private final StateProjectCompensationService stateProjectCompensationService;
    private final StateProjectCompensateStandardService stateProjectCompensateStandardService;
    private final ISysDictDataService dictDataService;
 
    public boolean areAllCompensationFieldsNotNull(CalculateCompensationBO compensationBO) {
        // 依次判断每个补偿字段是否为 null
        if (Objects.isNull(compensationBO)) {
            return false;
        }
 
        if (compensationBO.getHomeCompensationBO() == null) {
            return false;
        }
        if (compensationBO.getPolicyCompensationBO() == null) {
            return false;
        }
        if (compensationBO.getPolicySubsidyBO() == null) {
            return false;
        }
        if (compensationBO.getPolicySupplementBO() == null) {
            return false;
        }
//        if (compensationBO.getAwardBO() == null) {
//            return false;
//        }
//        if (compensationBO.getAttachmentBuildBO() == null || compensationBO.getAttachmentBuildBO().isEmpty()) {
//            return false;
//        }
//        if (compensationBO.getStructureBO() == null || compensationBO.getStructureBO().isEmpty()) {
//            return false;
//        }
//        if (compensationBO.getSurveyResultRegistrationBO() == null) {
//            return false;
//        }
//        if (compensationBO.getOtherCompensationBO() == null) {
//            return false;
//        }
 
        // 如果所有字段都不为 null,则返回 true
        return true;
    }
 
    //有些分住宅和非住宅,住宅是一种计算方式 非住宅又是另外一种,
    // 计算又分区间 如果是区间就只判断是否在区间内就行了,不在就报错
    //如果不是区间 就直接计算
    @Override
    public CalculateCompensationVO calculateCompensation(CalculateCompensationBO calculateCompensationBO) {
        //获取对应房产信息
        StateHousehold household = stateHouseholdService.getById(calculateCompensationBO.getStateHouseholdId());
        if (Objects.isNull(household)) {
            throw new GlobalException("未找到对应的房产");
        }
 
        String stateProjectId = household.getStateProjectId();
        //获取项目补偿标准信息
        StateProjectCompensation compensation = stateProjectCompensationService.getById(stateProjectId);
        if (Objects.isNull(compensation)) {
            throw new GlobalException("项目没有设置标准");
        }
        //通过标准类型名称查询对应的标准信息
        List<String> compensationNameList = new ArrayList<>();
        compensationNameList.add(compensation.getPolicyCompensationType());
        compensationNameList.add(compensation.getPolicySubsidy());
        compensationNameList.add(compensation.getAward());
        compensationNameList.add(compensation.getCompensationType());
        compensationNameList.add(compensation.getPolicySupplement());
        List<StateProjectCompensateStandard> enableCompensateStandard = stateProjectCompensateStandardService.getEnableCompensateStandard(compensationNameList);
        if (CollectionUtils.isEmpty(enableCompensateStandard)) {
            throw new GlobalException("未找到开启的标准");
        }
        enableCompensateStandard = enableCompensateStandard.stream().filter(e -> Objects.nonNull(e.getSubType())).collect(Collectors.toList());
        //根据类型分组后再根据子类型进行一次分组
        Map<Integer, Map<Integer, List<StateProjectCompensateStandard>>> enableCompensateStandardMap = enableCompensateStandard.stream()
                .collect(Collectors.groupingBy(StateProjectCompensateStandard::getCompensateType, Collectors.groupingBy(StateProjectCompensateStandard::getCategory)));
 
        //征收房屋补偿费
        Double homeAmount = null;
        // 从数据库查询的房产的预估价格附加信息不为空且calculateCompensationBO任意一个字段为空,则从数据库中房产的附加信息中获取
        if (org.apache.commons.lang3.StringUtils.isNotBlank(household.getAppendData()) && !areAllCompensationFieldsNotNull(calculateCompensationBO)) {
            calculateCompensationBO = JSON.parseObject(household.getAppendData(), CalculateCompensationBO.class);
        }
        CalculateCompensationVO compensationVO = StateProjectConvert.INSTANCE.boToVo(calculateCompensationBO);
        Integer houseUsingType = household.getHouseUsingType();
 
        HomeCompensationBO homeCompensationBO = calculateCompensationBO.getHomeCompensationBO();
        // 货币补偿
        if (Objects.nonNull(homeCompensationBO) && CompensationCategoryEnum.MONEY_COMPENSATION_1.getCode() == homeCompensationBO.getHomeCompensationType()) {
            Map<Integer, List<StateProjectCompensateStandard>> integerListMap = enableCompensateStandardMap.get(CompensateTypeEnum.HOUSE_ACQUISITION_COMPENSATION_FEE.getCode());
            if (!CollectionUtils.isEmpty(integerListMap)) {
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = integerListMap.get(CompensationCategoryEnum.MONEY_COMPENSATION.getCode());
                if (!CollectionUtils.isEmpty(stateProjectCompensateStandards)) {
                    StateProjectCompensateStandard stateProjectCompensateStandard = stateProjectCompensateStandards.stream().findFirst().get();
                    if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                        // Skip further calculations or set default values to avoid computation
//                        homeCompensationBO.setEvaluationPrice(0);
                    } else {
                        if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                            String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                            double start = Double.parseDouble(split[0]);
                            double end = Double.parseDouble(split[1]);
 
                            //评估单价
                            Double evaluationPrice = homeCompensationBO.getEvaluationPrice();
                            if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                                if (Objects.nonNull(evaluationPrice) && (evaluationPrice >= end || evaluationPrice <= start)) {
                                    throw new GlobalException("房屋补偿的评估单价不在区间内");
                                }
                                Double totalArea = homeCompensationBO.calculateTotalArea();
                                homeAmount = totalArea * evaluationPrice;
                                homeCompensationBO.setEvaluationPrice(evaluationPrice);
                            } else {
                                if (Objects.nonNull(evaluationPrice)) {
                                    Double totalArea = homeCompensationBO.calculateTotalArea();
                                    homeAmount = totalArea * evaluationPrice;
                                    homeCompensationBO.setEvaluationPrice(evaluationPrice);
                                }
                            }
                        } else {
                            Double specification = Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                            Double totalArea = homeCompensationBO.calculateTotalArea();
                            homeAmount = totalArea * specification;
                            homeCompensationBO.setEvaluationPrice(specification);
                        }
                    }
                }
            }
            //产权置换
        } else if (Objects.nonNull(homeCompensationBO)) {
            Map<Integer, List<StateProjectCompensateStandard>> integerListMap = enableCompensateStandardMap.get(CompensateTypeEnum.HOUSE_ACQUISITION_COMPENSATION_FEE.getCode());
            if (!CollectionUtils.isEmpty(integerListMap)) {
                //产权置换标准
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = integerListMap.get(CompensationCategoryEnum.PROPERTY_SWAP.getCode());
                if (!CollectionUtils.isEmpty(stateProjectCompensateStandards)) {
                    StateProjectCompensateStandard stateProjectCompensateStandard;
                    //根据房屋类型获取标准
                    if (HouseUsingTypeEnum.PERSONAL_HOUSE.getValue().equals(houseUsingType)) {
                        stateProjectCompensateStandard = stateProjectCompensateStandards.stream().filter(e -> e.getSpecification().equals("住宅")).findFirst().get();
                    } else {
                        stateProjectCompensateStandard = stateProjectCompensateStandards.stream().filter(e -> e.getSpecification().equals("非住宅")).findFirst().get();
                    }
                    if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                        // Skip further calculations or set default values to avoid computation
//                        homeCompensationBO.setEvaluationPrice(0);
                    } else {
                        if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                            String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                            double start = Double.parseDouble(split[0]);
                            double end = Double.parseDouble(split[1]);
 
                            //评估单价
                            Double evaluationPrice = homeCompensationBO.getEvaluationPrice();
                            if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                                if (Objects.nonNull(evaluationPrice) && (evaluationPrice > end || evaluationPrice < start)) {
                                    throw new GlobalException("房屋补偿的评估单价不在区间内");
                                }
                                Double totalArea = homeCompensationBO.calculateTotalArea();
                                homeAmount = totalArea * evaluationPrice;
                                homeCompensationBO.setEvaluationPrice(evaluationPrice);
                            } else {
                                Double totalArea = homeCompensationBO.calculateTotalArea();
                                homeAmount = totalArea * (Objects.nonNull(evaluationPrice)
                                        ? evaluationPrice
                                        : 0.0);
                                homeCompensationBO.setEvaluationPrice(evaluationPrice);
                            }
                            //@TODO区间不计算
//                            Integer totalArea = homeCompensationBO.calculateTotalArea();
//                            homeAmount = totalArea * homeCompensationBO.getEvaluationPrice();
                        } else {
                            Double totalArea = homeCompensationBO.calculateTotalArea();
                            homeAmount = totalArea * Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                            homeCompensationBO.setEvaluationPrice(Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard()));
                        }
                    }
                }
            }
        }
        if (Objects.nonNull(compensationVO.getHomeCompensationBO())) {
            compensationVO.getHomeCompensationBO().setTotalBuildingArea(compensationVO.getHomeCompensationBO().calculateTotalArea());
            compensationVO.getHomeCompensationBO().setTotalAmount(homeAmount);
        }
        //政策性补偿费
        //补偿费总金额
        Double policyAmount = 0.0;
        PolicyCompensationBO policyCompensationBO = calculateCompensationBO.getPolicyCompensationBO();
        Map<Integer, List<StateProjectCompensateStandard>> integerListMap = enableCompensateStandardMap.get(CompensateTypeEnum.POLICY_COMPENSATION_FEE.getCode());
        if (Objects.nonNull(policyCompensationBO) && !CollectionUtils.isEmpty(integerListMap)) {
            if (Objects.nonNull(policyCompensationBO.getTelephoneRelocation()) && Objects.nonNull(integerListMap.get(CompensationCategoryEnum.PHONE_RELOCATION.getCode()))) {
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = integerListMap.get(CompensationCategoryEnum.PHONE_RELOCATION.getCode());
                StateProjectCompensateStandard stateProjectCompensateStandard;
                if (HouseUsingTypeEnum.PERSONAL_HOUSE.getValue().equals(houseUsingType)) {
                    stateProjectCompensateStandard = stateProjectCompensateStandards.stream().filter(e -> e.getSpecification().equals("住宅")).findFirst().get();
                } else {
                    stateProjectCompensateStandard = stateProjectCompensateStandards.stream().filter(e -> e.getSpecification().equals("非住宅")).findFirst().get();
                }
                //电话移机费用
                Double telephoneRelocationAmount = 0.0;
                if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                    // Skip further calculations or set default values to avoid computation
                    policyAmount = policyAmount + telephoneRelocationAmount;
                } else {
                    if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                        String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                        double start = Double.parseDouble(split[0]);
                        double end = Double.parseDouble(split[1]);
                        if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                            if (Objects.nonNull(policyCompensationBO.getTelephoneRelocationAmount()) && policyCompensationBO.getTelephoneRelocationAmount() != 0 &&
                                    ((policyCompensationBO.getTelephoneRelocationAmount() / policyCompensationBO.getTelephoneRelocation()) > end
                                            || (policyCompensationBO.getTelephoneRelocationAmount() / policyCompensationBO.getTelephoneRelocation()) < start)) {
                                throw new GlobalException("政策性补偿的电话移机单价不在区间内");
                            }
                            telephoneRelocationAmount = policyCompensationBO.getTelephoneRelocationAmount();
                        } else {
                            telephoneRelocationAmount = policyCompensationBO.getTelephoneRelocationAmount();
                        }
                    } else {
                        telephoneRelocationAmount = policyCompensationBO.getTelephoneRelocation() * Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                    }
 
                    compensationVO.getPolicyCompensationBO().setTelephoneRelocationAmount(telephoneRelocationAmount);
                    policyAmount = policyAmount + (telephoneRelocationAmount == null ? 0.0 : telephoneRelocationAmount);
                    ;
                }
            }
            // 有线电视迁装
            if (Objects.nonNull(policyCompensationBO.getCableTVRelocation()) && Objects.nonNull(integerListMap.get(CompensationCategoryEnum.CABLE_TV_RELOCATION.getCode()))) {
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = integerListMap.get(CompensationCategoryEnum.CABLE_TV_RELOCATION.getCode());
                StateProjectCompensateStandard stateProjectCompensateStandard = stateProjectCompensateStandards.stream().findFirst().get();
                // 有线电视迁装金额
                Double cableTVRelocationAmount = 0.0;
                if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                    // Skip further calculations or set default values to avoid computation
                    policyAmount = policyAmount + cableTVRelocationAmount;
                } else {
                    if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                        String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                        double start = Double.parseDouble(split[0]);
                        double end = Double.parseDouble(split[1]);
                        if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                            if (Objects.nonNull(policyCompensationBO.getCableTVRelocationAmount()) && policyCompensationBO.getCableTVRelocationAmount() != 0 && (
                                    (policyCompensationBO.getCableTVRelocationAmount() / policyCompensationBO.getCableTVRelocation()) > end
                                            || (policyCompensationBO.getCableTVRelocationAmount() / policyCompensationBO.getCableTVRelocation()) < start)) {
                                throw new GlobalException("政策性补偿的有线电视迁装费用单价不在区间内");
                            }
                            cableTVRelocationAmount = policyCompensationBO.getCableTVRelocationAmount();
                        } else {
                            cableTVRelocationAmount = policyCompensationBO.getCableTVRelocationAmount();
                        }
                    } else {
                        cableTVRelocationAmount = policyCompensationBO.getCableTVRelocation() * Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                    }
 
                    compensationVO.getPolicyCompensationBO().setCableTVRelocationAmount(cableTVRelocationAmount);
                    policyAmount = policyAmount + (cableTVRelocationAmount == null ? 0.0 : cableTVRelocationAmount);
                    ;
                }
            }
            // 宽带迁装
            if (Objects.nonNull(policyCompensationBO.getBroadbandRelocation()) && Objects.nonNull(integerListMap.get(CompensationCategoryEnum.BROADBAND_RELOCATION.getCode()))) {
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = integerListMap.get(CompensationCategoryEnum.BROADBAND_RELOCATION.getCode());
                StateProjectCompensateStandard stateProjectCompensateStandard = stateProjectCompensateStandards.stream().findFirst().get();
                // 宽带迁装金额
                Double broadbandRelocationAmount = 0.0;
                if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                    // Skip further calculations or set default values to avoid computation
                    policyAmount = policyAmount + broadbandRelocationAmount;
                } else {
                    if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                        String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                        double start = Double.parseDouble(split[0]);
                        double end = Double.parseDouble(split[1]);
                        if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                            if (Objects.nonNull(policyCompensationBO.getBroadbandRelocationAmount()) && policyCompensationBO.getBroadbandRelocationAmount() != 0 && (
                                    (policyCompensationBO.getBroadbandRelocationAmount() / policyCompensationBO.getBroadbandRelocation()) > end
                                            || (policyCompensationBO.getBroadbandRelocationAmount() / policyCompensationBO.getBroadbandRelocation()) < start)) {
                                throw new GlobalException("政策性补偿的宽带迁装费用单价不在区间内");
                            }
                            broadbandRelocationAmount = policyCompensationBO.getBroadbandRelocationAmount();
                        } else {
                            broadbandRelocationAmount = policyCompensationBO.getBroadbandRelocationAmount();
                        }
                    } else {
                        broadbandRelocationAmount = policyCompensationBO.getBroadbandRelocation() * Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                    }
 
                    compensationVO.getPolicyCompensationBO().setBroadbandRelocationAmount(broadbandRelocationAmount);
                    policyAmount = policyAmount + (broadbandRelocationAmount == null ? 0.0 : broadbandRelocationAmount);
                    ;
                }
            }
            // 空调移机
            if (Objects.nonNull(policyCompensationBO.getAirConditionerRelocation()) && Objects.nonNull(integerListMap.get(CompensationCategoryEnum.AIR_CONDITIONER_RELOCATION.getCode()))) {
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = integerListMap.get(CompensationCategoryEnum.AIR_CONDITIONER_RELOCATION.getCode());
                StateProjectCompensateStandard stateProjectCompensateStandard = stateProjectCompensateStandards.stream().findFirst().get();
                // 空调移机金额
                Double airConditionerRelocationAmount = 0.0;
                if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                    // Skip further calculations or set default values to avoid computation
                    policyAmount = policyAmount + airConditionerRelocationAmount;
                } else {
                    if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                        String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                        double start = Double.parseDouble(split[0]);
                        double end = Double.parseDouble(split[1]);
                        if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                            if (Objects.nonNull(policyCompensationBO.getAirConditionerRelocationAmount()) && policyCompensationBO.getAirConditionerRelocationAmount() != 0 && (
                                    (policyCompensationBO.getAirConditionerRelocationAmount() / policyCompensationBO.getAirConditionerRelocation()) > end
                                            || (policyCompensationBO.getAirConditionerRelocationAmount() / policyCompensationBO.getAirConditionerRelocation()) < start)) {
                                throw new GlobalException("政策性补偿的空调移机费用单价不在区间内");
                            }
                            airConditionerRelocationAmount = policyCompensationBO.getAirConditionerRelocationAmount();
                        } else {
                            airConditionerRelocationAmount = policyCompensationBO.getAirConditionerRelocationAmount();
                        }
                    } else {
                        airConditionerRelocationAmount = policyCompensationBO.getAirConditionerRelocation() * Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                    }
 
                    compensationVO.getPolicyCompensationBO().setAirConditionerRelocationAmount(airConditionerRelocationAmount);
                    policyAmount = policyAmount + (airConditionerRelocationAmount == null ? 0.0 : airConditionerRelocationAmount);
                    ;
                }
            }
            // 一户一表
            if (Objects.nonNull(policyCompensationBO.getIndividualMeterInstallation()) && Objects.nonNull(integerListMap.get(CompensationCategoryEnum.ONE_HOUSE_ONE_METER.getCode()))) {
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = integerListMap.get(CompensationCategoryEnum.ONE_HOUSE_ONE_METER.getCode());
                StateProjectCompensateStandard stateProjectCompensateStandard = stateProjectCompensateStandards.stream().findFirst().get();
                // 一户一表金额
                Double individualMeterInstallationAmount = 0.0;
                if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                    // Skip further calculations or set default values to avoid computation
                    policyAmount = policyAmount + individualMeterInstallationAmount;
                } else {
                    if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                        String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                        double start = Double.parseDouble(split[0]);
                        double end = Double.parseDouble(split[1]);
                        if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                            if (Objects.nonNull(policyCompensationBO.getIndividualMeterInstallationAmount()) && policyCompensationBO.getIndividualMeterInstallationAmount() != 0 && (
                                    (policyCompensationBO.getIndividualMeterInstallationAmount() / policyCompensationBO.getIndividualMeterInstallation()) > end
                                            || (policyCompensationBO.getIndividualMeterInstallationAmount() / policyCompensationBO.getIndividualMeterInstallation()) < start)) {
                                throw new GlobalException("政策性补偿的一户一表费用单价不在区间内");
                            }
                            individualMeterInstallationAmount = policyCompensationBO.getIndividualMeterInstallationAmount();
                        } else {
                            individualMeterInstallationAmount = policyCompensationBO.getIndividualMeterInstallationAmount();
                        }
                    } else {
                        individualMeterInstallationAmount = policyCompensationBO.getIndividualMeterInstallation() * Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                    }
 
 
                    compensationVO.getPolicyCompensationBO().setIndividualMeterInstallationAmount(individualMeterInstallationAmount);
                    policyAmount = policyAmount + (individualMeterInstallationAmount == null ? 0.0 : individualMeterInstallationAmount);
                    ;
                }
            }
            // 天然气初装
            if (Objects.nonNull(policyCompensationBO.getGasInitialInstallation()) && Objects.nonNull(integerListMap.get(CompensationCategoryEnum.GAS_INITIAL_INSTALLATION.getCode()))) {
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = integerListMap.get(CompensationCategoryEnum.GAS_INITIAL_INSTALLATION.getCode());
                StateProjectCompensateStandard stateProjectCompensateStandard = stateProjectCompensateStandards.stream().findFirst().get();
                Double gasInitialInstallationAmount = 0.0;
                if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                    // Skip further calculations or set default values to avoid computation
                    policyAmount = policyAmount + gasInitialInstallationAmount;
                } else {
                    if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                        String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                        double start = Double.parseDouble(split[0]);
                        double end = Double.parseDouble(split[1]);
                        if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                            if (Objects.nonNull(policyCompensationBO.getGasInitialInstallationAmount()) && policyCompensationBO.getGasInitialInstallationAmount() != 0 && (
                                    (policyCompensationBO.getGasInitialInstallationAmount() / policyCompensationBO.getGasInitialInstallation()) > end
                                            || (policyCompensationBO.getGasInitialInstallationAmount() / policyCompensationBO.getGasInitialInstallation()) < start)) {
                                throw new GlobalException("政策性补偿的天然气初装费用单价不在区间内");
                            }
                            gasInitialInstallationAmount = policyCompensationBO.getGasInitialInstallationAmount();
                        } else {
                            gasInitialInstallationAmount = policyCompensationBO.getGasInitialInstallationAmount();
                        }
                    } else {
                        gasInitialInstallationAmount = policyCompensationBO.getGasInitialInstallation() * Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                    }
 
                    compensationVO.getPolicyCompensationBO().setGasInitialInstallationAmount(gasInitialInstallationAmount);
                    policyAmount = policyAmount + (gasInitialInstallationAmount == null ? 0.0 : gasInitialInstallationAmount);
                    ;
                }
            }
        }
        compensationVO.getPolicyCompensationBO().calculateTotalAmount();
        //政策性补助费
        // 住宅临时安置补助费
        Double policySubsidyAmount = null;
        //非住宅停产,停业经济损失补偿费
        Double nonResidentialEconomicLossAllowanceAmount = null;
        // 搬家补助费
        Double movingAllowanceAmount = null;
        PolicySubsidyBO policySubsidyBO = calculateCompensationBO.getPolicySubsidyBO();
        //货币补偿
        if (Objects.nonNull(policySubsidyBO) && CompensationCategoryEnum.MONEY_COMPENSATION_1.getCode() == policySubsidyBO.getCompensationType()) {
            Map<Integer, List<StateProjectCompensateStandard>> policySubsidyMap = enableCompensateStandardMap.get(CompensateTypeEnum.POLICY_SUBSIDY_FEE.getCode());
            if (!CollectionUtils.isEmpty(policySubsidyMap)) {
                // 住宅临时安置补助费
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = policySubsidyMap.get(CompensationCategoryEnum.TEMPORARY_HOUSING_ALLOWANCE.getCode());
                if (!CollectionUtils.isEmpty(stateProjectCompensateStandards)) {
                    StateProjectCompensateStandard stateProjectCompensateStandard = stateProjectCompensateStandards.stream().findFirst().get();
                    if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                        // Skip further calculations or set default values to avoid computation
                    } else {
                        if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                            String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                            double start = Double.parseDouble(split[0]);
                            double end = Double.parseDouble(split[1]);
                            if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst()) && StringUtils.isNotBlank(policySubsidyBO.getUnitPrice())) {
                                if (Double.parseDouble(policySubsidyBO.getUnitPrice()) < start || Double.parseDouble(policySubsidyBO.getUnitPrice()) > end) {
                                    throw new GlobalException("政策性补助的住宅临时安置补助费用单价不在区间内");
                                }
                                Double specification = Double.parseDouble(policySubsidyBO.getUnitPrice());
                                policySubsidyAmount = Double.parseDouble(policySubsidyBO.getUnitPrice()) * policySubsidyBO.getIssuanceMonths() * homeCompensationBO.calculateTotalArea();
                                compensationVO.getPolicySubsidyBO().setUnitPrice(String.valueOf(specification));
                            } else {
                                if (StringUtils.isNotBlank(policySubsidyBO.getUnitPrice()) && !policySubsidyBO.getUnitPrice().contains("-")) {
                                    policySubsidyAmount = Double.parseDouble(policySubsidyBO.getUnitPrice()) * policySubsidyBO.getIssuanceMonths() * homeCompensationBO.calculateTotalArea();
                                    compensationVO.getPolicySubsidyBO().setUnitPrice(policySubsidyBO.getUnitPrice());
                                }
                            }
                        } else {
 
                            policySubsidyAmount = Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard()) * policySubsidyBO.getIssuanceMonths() * homeCompensationBO.calculateTotalArea();
                            compensationVO.getPolicySubsidyBO().setUnitPrice(stateProjectCompensateStandard.getCompensateStandard());
 
                        }
                    }
                }
                // 非住宅停产、停业经济损失补助费
                List<StateProjectCompensateStandard> nonResidentialEconomicLossAllowance = policySubsidyMap.get(CompensationCategoryEnum.NON_RESIDENTIAL_ECONOMIC_LOSS_ALLOWANCE.getCode());
                if (!CollectionUtils.isEmpty(nonResidentialEconomicLossAllowance)) {
                    StateProjectCompensateStandard stateProjectCompensateStandard = nonResidentialEconomicLossAllowance.stream().findFirst().get();
                    if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                        // Skip further calculations or set default values to avoid computation
                    } else {
                        if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                            String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                            double start = Double.parseDouble(split[0]);
                            double end = Double.parseDouble(split[1]);
                            if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                                if (Objects.nonNull(policySubsidyBO.getNonResidentialEconomicLossAllowanceAmount()) && (policySubsidyBO.getNonResidentialEconomicLossAllowanceAmount() < start || policySubsidyBO.getNonResidentialEconomicLossAllowanceAmount() > end)) {
                                    throw new GlobalException("政策性补助的 非住宅停产、停业经济损失补助费用金额不在区间内");
                                }
                                nonResidentialEconomicLossAllowanceAmount = policySubsidyBO.getNonResidentialEconomicLossAllowanceAmount();
                                compensationVO.getPolicySubsidyBO().setNonResidentialEconomicLossAllowanceAmount(nonResidentialEconomicLossAllowanceAmount);
                            } else {
                                nonResidentialEconomicLossAllowanceAmount = policySubsidyBO.getNonResidentialEconomicLossAllowanceAmount();
                                compensationVO.getPolicySubsidyBO().setNonResidentialEconomicLossAllowanceAmount(nonResidentialEconomicLossAllowanceAmount);
                            }
                        } else {
                            nonResidentialEconomicLossAllowanceAmount = Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                            compensationVO.getPolicySubsidyBO().setNonResidentialEconomicLossAllowanceAmount(nonResidentialEconomicLossAllowanceAmount);
                        }
                    }
                }
                // 搬家补助费
                List<StateProjectCompensateStandard> movingAllowance = policySubsidyMap.get(CompensationCategoryEnum.MOVING_ALLOWANCE.getCode());
                if (!CollectionUtils.isEmpty(movingAllowance)) {
                    StateProjectCompensateStandard stateProjectCompensateStandard = movingAllowance.stream().findFirst().get();
                    if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                        // Skip further calculations or set default values to avoid computation
                    } else {
                        if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                            String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                            double start = Double.parseDouble(split[0]);
                            double end = Double.parseDouble(split[1]);
                            if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                                if (Objects.nonNull(policySubsidyBO.getMovingAllowanceAmount()) && (policySubsidyBO.getMovingAllowanceAmount() < start || policySubsidyBO.getMovingAllowanceAmount() > end)) {
                                    throw new GlobalException("政策性补助的搬家补助费用不在区间内");
                                }
                                movingAllowanceAmount = policySubsidyBO.getMovingAllowanceAmount();
                                compensationVO.getPolicySubsidyBO().setMovingAllowanceAmount(movingAllowanceAmount);
                            } else {
                                movingAllowanceAmount = policySubsidyBO.getMovingAllowanceAmount();
                                compensationVO.getPolicySubsidyBO().setMovingAllowanceAmount(movingAllowanceAmount);
                            }
                        } else {
                            movingAllowanceAmount = Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                            compensationVO.getPolicySubsidyBO().setMovingAllowanceAmount(movingAllowanceAmount);
                        }
                    }
                }
            }
        } else if (Objects.nonNull(policySubsidyBO)) { //产权置换
            Map<Integer, List<StateProjectCompensateStandard>> policySubsidyMap = enableCompensateStandardMap.get(CompensateTypeEnum.POLICY_SUBSIDY_FEE.getCode());
            if (!CollectionUtils.isEmpty(policySubsidyMap)) {
                // 住宅临时安置补助费
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = policySubsidyMap.get(CompensationCategoryEnum.TEMPORARY_HOUSING_ALLOWANCE.getCode());
                if (!CollectionUtils.isEmpty(stateProjectCompensateStandards)) {
                    StateProjectCompensateStandard stateProjectCompensateStandard = stateProjectCompensateStandards.stream().findFirst().get();
                    if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                        // Skip further calculations or set default values to avoid computation
//                        compensationVO.getPolicySubsidyBO().setUnitPrice(String.valueOf(0));
                    } else {
                        if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                            String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                            double start = Double.parseDouble(split[0]);
                            double end = Double.parseDouble(split[1]);
                            if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst()) && StringUtils.isNotBlank(policySubsidyBO.getUnitPrice())) {
                                if (Double.parseDouble(policySubsidyBO.getUnitPrice()) < start || Double.parseDouble(policySubsidyBO.getUnitPrice()) > end) {
                                    throw new GlobalException("政策性补助的住宅临时安置补助费用单价不在区间内");
                                }
                                Double specification = Double.parseDouble(policySubsidyBO.getUnitPrice());
                                policySubsidyAmount = homeCompensationBO.calculateTotalArea() * specification;
                                compensationVO.getPolicySubsidyBO().setUnitPrice(String.valueOf(specification));
                            } else {
                                if (StringUtils.isNotBlank(policySubsidyBO.getUnitPrice()) &&!policySubsidyBO.getUnitPrice().contains("-") ) {
                                    Double specification = Double.parseDouble(policySubsidyBO.getUnitPrice());
                                    policySubsidyAmount = specification * homeCompensationBO.calculateTotalArea();
                                    compensationVO.getPolicySubsidyBO().setUnitPrice(String.valueOf(specification));
                                }
                            }
                        } else {
                            Double specification = Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                            policySubsidyAmount = homeCompensationBO.calculateTotalArea() * specification;
                            compensationVO.getPolicySubsidyBO().setUnitPrice(String.valueOf(specification));
                        }
                    }
                }
                // 非住宅停产、停业经济损失补助费
                List<StateProjectCompensateStandard> nonResidentialEconomicLossAllowance = policySubsidyMap.get(CompensationCategoryEnum.NON_RESIDENTIAL_ECONOMIC_LOSS_ALLOWANCE.getCode());
                if (!CollectionUtils.isEmpty(nonResidentialEconomicLossAllowance)) {
                    StateProjectCompensateStandard stateProjectCompensateStandard = nonResidentialEconomicLossAllowance.stream().findFirst().get();
                    if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                        // Skip further calculations or set default values to avoid computation
//                        compensationVO.getPolicySubsidyBO().setNonResidentialEconomicLossAllowanceAmount(0);
                    } else {
                        if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                            String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                            double start = Double.parseDouble(split[0]);
                            double end = Double.parseDouble(split[1]);
                            if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                                if (Objects.nonNull(policySubsidyBO.getNonResidentialEconomicLossAllowanceAmount())
                                        && (policySubsidyBO.getNonResidentialEconomicLossAllowanceAmount() < start || policySubsidyBO.getNonResidentialEconomicLossAllowanceAmount() > end)) {
                                    throw new GlobalException("政策性补助的 非住宅停产、停业经济损失补助费用不在区间内");
                                }
                                nonResidentialEconomicLossAllowanceAmount = policySubsidyBO.getNonResidentialEconomicLossAllowanceAmount();
                                compensationVO.getPolicySubsidyBO().setNonResidentialEconomicLossAllowanceAmount(nonResidentialEconomicLossAllowanceAmount);
                            } else {
                                nonResidentialEconomicLossAllowanceAmount = policySubsidyBO.getNonResidentialEconomicLossAllowanceAmount();
                                compensationVO.getPolicySubsidyBO().setNonResidentialEconomicLossAllowanceAmount(nonResidentialEconomicLossAllowanceAmount);
                            }
                        } else {
                            nonResidentialEconomicLossAllowanceAmount = Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                            compensationVO.getPolicySubsidyBO().setNonResidentialEconomicLossAllowanceAmount(nonResidentialEconomicLossAllowanceAmount);
                        }
                    }
                }
                // 搬家补助费
                List<StateProjectCompensateStandard> movingAllowance = policySubsidyMap.get(CompensationCategoryEnum.MOVING_ALLOWANCE.getCode());
                if (!CollectionUtils.isEmpty(movingAllowance)) {
                    StateProjectCompensateStandard stateProjectCompensateStandard = movingAllowance.stream().findFirst().get();
                    if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                        // Skip further calculations or set default values to avoid computation
//                        compensationVO.getPolicySubsidyBO().setMovingAllowanceAmount(0);
                    } else {
                        if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                            String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                            double start = Double.parseDouble(split[0]);
                            double end = Double.parseDouble(split[1]);
                            if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                                if (Objects.nonNull(policySubsidyBO.getMovingAllowanceAmount())
                                        && (policySubsidyBO.getMovingAllowanceAmount() < start || policySubsidyBO.getMovingAllowanceAmount() > end)) {
                                    throw new GlobalException("政策性补助的搬家补助费用不在区间内");
                                }
                                movingAllowanceAmount = policySubsidyBO.getMovingAllowanceAmount();
                                compensationVO.getPolicySubsidyBO().setMovingAllowanceAmount(movingAllowanceAmount);
                            } else {
                                movingAllowanceAmount = policySubsidyBO.getMovingAllowanceAmount();
                                compensationVO.getPolicySubsidyBO().setMovingAllowanceAmount(movingAllowanceAmount);
                            }
                        } else {
                            movingAllowanceAmount = Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                            compensationVO.getPolicySubsidyBO().setMovingAllowanceAmount(movingAllowanceAmount);
                        }
                    }
                }
            }
        }
 
        //政策性补贴
        //购房补贴
        Double propertyManagementSubsidyAmount = null;
        //物管费补贴
        Double housingSubsidyAmount = null;
        PolicySupplementBO policySupplementBO = calculateCompensationBO.getPolicySupplementBO();
        if (Objects.isNull(calculateCompensationBO.getPolicySupplementBO())) {
            calculateCompensationBO.setPolicySupplementBO(new PolicySupplementBO());
            compensationVO.setPolicySupplementBO(new PolicySupplementBO());
            policySupplementBO = calculateCompensationBO.getPolicySupplementBO();
        }
        Map<Integer, List<StateProjectCompensateStandard>> policyAllowanceMap = enableCompensateStandardMap.get(CompensateTypeEnum.POLICY_ALLOWANCE.getCode());
        if (!CollectionUtils.isEmpty(policyAllowanceMap)) {
            // 物管费补贴
            if (Objects.nonNull(policyAllowanceMap.get(CompensationCategoryEnum.PROPERTY_MANAGEMENT_SUBSIDY.getCode()))) {
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = policyAllowanceMap.get(CompensationCategoryEnum.PROPERTY_MANAGEMENT_SUBSIDY.getCode());
                StateProjectCompensateStandard stateProjectCompensateStandard;
                if (!CollectionUtils.isEmpty(stateProjectCompensateStandards)) {
                    stateProjectCompensateStandard = stateProjectCompensateStandards.stream().findFirst().get();
                    if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                        // Skip further calculations or set default values to avoid computation
//                        compensationVO.getPolicySupplementBO().setPropertyManagementSubsidy(propertyManagementSubsidyAmount);
                    }
                    if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                        if (Objects.isNull(policySupplementBO.getPropertyManagementSubsidy())) {
 
                        } else {
                            String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                            double start = Double.parseDouble(split[0]);
                            double end = Double.parseDouble(split[1]);
                            if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
 
                                if (Objects.nonNull(policySupplementBO.getPropertyManagementSubsidy()) && ((policySupplementBO.getPropertyManagementSubsidy() / 5) / 12 > end
                                        || (policySupplementBO.getPropertyManagementSubsidy() / 5) / 12 < start)) {
                                    throw new GlobalException("物管费补贴金额单价不在区间内");
                                }
                                double totalArea = homeCompensationBO.calculateTotalArea();
                                if (totalArea > 90) {
                                    propertyManagementSubsidyAmount = policySupplementBO.getPropertyManagementSubsidy();
                                } else {
                                    propertyManagementSubsidyAmount = policySupplementBO.getPropertyManagementSubsidy();
                                }
                            } else {
                                propertyManagementSubsidyAmount = policySupplementBO.getPropertyManagementSubsidy();
                            }
                        }
                    } else {
                        double totalArea = homeCompensationBO.calculateTotalArea();
                        if (totalArea > 90) {
                            propertyManagementSubsidyAmount = totalArea * 5 * 12 * Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                        } else {
                            propertyManagementSubsidyAmount = 90 * 5 * 12 * Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                        }
                    }
 
                    compensationVO.getPolicySupplementBO().setPropertyManagementSubsidy(propertyManagementSubsidyAmount);
                }
            }
            // 购房补贴
            if (Objects.nonNull(policyAllowanceMap.get(CompensationCategoryEnum.HOUSE_PURCHASE_SUBSIDY.getCode()))) {
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = policyAllowanceMap.get(CompensationCategoryEnum.HOUSE_PURCHASE_SUBSIDY.getCode());
                StateProjectCompensateStandard stateProjectCompensateStandard;
                stateProjectCompensateStandard = stateProjectCompensateStandards.stream().findFirst().get();
                if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                    // Skip further calculations or set default values to avoid computation
                    compensationVO.getPolicySupplementBO().setHousingSubsidy(housingSubsidyAmount);
                }
                if (HouseUsingTypeEnum.PERSONAL_HOUSE.getValue().equals(houseUsingType)) {
                    stateProjectCompensateStandard = stateProjectCompensateStandards.stream().filter(e -> e.getSpecification().equals("住宅")).findFirst().get();
                } else {
                    stateProjectCompensateStandard = stateProjectCompensateStandards.stream().filter(e -> e.getSpecification().equals("非住宅")).findFirst().get();
                }
                if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                    if (Objects.isNull(policySupplementBO.getHousingSubsidy())) {
 
                    } else {
                        String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                        double start = Double.parseDouble(split[0]);
                        double end = Double.parseDouble(split[1]);
                        if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                            if (Objects.nonNull(policySupplementBO.getHousingSubsidy()) && (policySupplementBO.getHousingSubsidy() > end || policySupplementBO.getHousingSubsidy() < start)) {
                                throw new GlobalException("购房补贴金额单价不在区间内");
                            }
                            housingSubsidyAmount = policySupplementBO.getHousingSubsidy();
                        } else {
                            housingSubsidyAmount = policySupplementBO.getHousingSubsidy();
                        }
                    }
                } else {
                    housingSubsidyAmount = Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                }
 
                compensationVO.getPolicySupplementBO().setHousingSubsidy(housingSubsidyAmount);
 
            }
        }
        // 提前搬迁奖励
        Double earlyRelocationRewardAmount = null;
        AwardBO awardBO = compensationVO.getAwardBO();
        Map<Integer, List<StateProjectCompensateStandard>> earlyRelocationRewardMap = enableCompensateStandardMap.get(CompensateTypeEnum.EARLY_RELOCATION_REWARD.getCode());
        if (Objects.isNull(compensationVO.getAwardBO())) {
            calculateCompensationBO.setAwardBO(new AwardBO());
            compensationVO.setAwardBO(new AwardBO());
            awardBO = calculateCompensationBO.getAwardBO();
        }
 
        if (Objects.nonNull(awardBO) && !CollectionUtils.isEmpty(earlyRelocationRewardMap)) {
            if (Objects.nonNull(policyCompensationBO.getTelephoneRelocation()) && Objects.nonNull(earlyRelocationRewardMap.get(CompensationCategoryEnum.EARLY_RELOCATION_BONUS.getCode()))) {
                List<StateProjectCompensateStandard> stateProjectCompensateStandards = earlyRelocationRewardMap.get(CompensationCategoryEnum.EARLY_RELOCATION_BONUS.getCode());
                StateProjectCompensateStandard stateProjectCompensateStandard;
 
                stateProjectCompensateStandard = stateProjectCompensateStandards.stream().findFirst().get();
                if (!checkCompensateStandard(stateProjectCompensateStandard)) {
                    // Skip further calculations or set default values to avoid computation
                    compensationVO.getAwardBO().setEarlyRelocationAwardAmount(earlyRelocationRewardAmount);
                }
                if (stateProjectCompensateStandard.getSubType().equals(2) && stateProjectCompensateStandard.getCompensateStandard().contains("-")) {
                    String[] split = stateProjectCompensateStandard.getCompensateStandard().split("-");
                    double start = Double.parseDouble(split[0]);
                    double end = Double.parseDouble(split[1]);
                    if (Objects.nonNull(calculateCompensationBO.getIsFirst()) && Boolean.TRUE.equals(calculateCompensationBO.getIsFirst())) {
                        if (Objects.nonNull(awardBO.getEarlyRelocationAwardAmount()) && (awardBO.getEarlyRelocationAwardAmount() > end || awardBO.getEarlyRelocationAwardAmount() < start)) {
                            throw new GlobalException("提前搬迁奖励的提前搬迁奖励单价不在区间内");
                        }
                        earlyRelocationRewardAmount = awardBO.getEarlyRelocationAwardAmount();
                    } else {
                        earlyRelocationRewardAmount = awardBO.getEarlyRelocationAwardAmount();
                    }
                } else {
                    earlyRelocationRewardAmount = Double.parseDouble(stateProjectCompensateStandard.getCompensateStandard());
                }
 
                compensationVO.getAwardBO().setEarlyRelocationAwardAmount(earlyRelocationRewardAmount);
            }
        }
 
        calculateCompensationBO.setPolicySubsidyBO(policySubsidyBO);
 
        compensationVO.setTotalAmount(
                safeAdd(policySubsidyAmount) +
                        safeAdd(policyAmount) +
                        safeAdd(homeAmount) +
                        safeAdd(nonResidentialEconomicLossAllowanceAmount) +
                        safeAdd(movingAllowanceAmount) +
                        safeAdd(earlyRelocationRewardAmount) +
                        safeAdd(housingSubsidyAmount) +
                        safeAdd(propertyManagementSubsidyAmount)
        );
        if (Objects.nonNull(compensationVO.getPolicySubsidyBO())) {
            compensationVO.getPolicySubsidyBO().setAmount(policySubsidyAmount);
        }
        compensationVO.setTotalAmount(calculateTotalAmount(compensationVO));
        RoundingUtil.roundDoubleFields(compensationVO);
        return compensationVO;
    }
 
    private static boolean checkCompensateStandard(StateProjectCompensateStandard stateProjectCompensateStandard) {
        if (Objects.isNull(stateProjectCompensateStandard) ||
                Objects.isNull(stateProjectCompensateStandard.getCompensateStandard()) ||
                Objects.isNull(stateProjectCompensateStandard.getSubType())) {
            // Log a warning message instead of throwing an exception
            System.out.println("Warning: 当前选择的标准未填写具体的单价或区间");
            return false;
        }
        return true;
    }
 
    public double calculateTotalAmount(CalculateCompensationVO compensationVO) {
        Double totalAmount = 0.0;
 
        // 房屋补偿信息
        if (compensationVO.getHomeCompensationBO() != null) {
            HomeCompensationBO homeCompensation = compensationVO.getHomeCompensationBO();
            if (homeCompensation.getTotalAmount() != null) {
                totalAmount += homeCompensation.getTotalAmount();
            }
        }
 
        // 政策性补偿信息
        if (compensationVO.getPolicyCompensationBO() != null) {
            PolicyCompensationBO policyCompensation = compensationVO.getPolicyCompensationBO();
            totalAmount += safeAdd(policyCompensation.getTelephoneRelocationAmount());
            totalAmount += safeAdd(policyCompensation.getCableTVRelocationAmount());
            totalAmount += safeAdd(policyCompensation.getAirConditionerRelocationAmount());
            totalAmount += safeAdd(policyCompensation.getBroadbandRelocationAmount());
            totalAmount += safeAdd(policyCompensation.getIndividualMeterInstallationAmount());
            totalAmount += safeAdd(policyCompensation.getGasInitialInstallationAmount());
        }
 
        // 政策性补助信息
        if (compensationVO.getPolicySubsidyBO() != null) {
            PolicySubsidyBO policySubsidy = compensationVO.getPolicySubsidyBO();
            totalAmount += safeAdd(policySubsidy.getNonResidentialEconomicLossAllowanceAmount());
            totalAmount += safeAdd(policySubsidy.getMovingAllowanceAmount());
            totalAmount += safeAdd(policySubsidy.getAmount());
        }
 
        // 政策性补贴信息
        if (compensationVO.getPolicySupplementBO() != null) {
            PolicySupplementBO policySupplement = compensationVO.getPolicySupplementBO();
            totalAmount += safeAdd(policySupplement.getHousingSubsidy());
            totalAmount += safeAdd(policySupplement.getPropertyManagementSubsidy());
        }
 
        // 提前搬迁奖励信息
        if (compensationVO.getAwardBO() != null) {
            AwardBO award = compensationVO.getAwardBO();
            totalAmount += safeAdd(award.getEarlyRelocationAwardAmount());
        }
 
        // 处理 attachmentBuildBO 和 structureBO 中的金额
        if (compensationVO.getAttachmentBuildBO() != null && !compensationVO.getAttachmentBuildBO().isEmpty()) {
            for (JSONObject attachment : compensationVO.getAttachmentBuildBO()) {
                Double amount = attachment.getDouble("amount");
                if (amount != null) {
                    totalAmount += amount;
                }
            }
        }
 
        if (compensationVO.getStructureBO() != null && !compensationVO.getStructureBO().isEmpty()) {
            for (JSONObject structure : compensationVO.getStructureBO()) {
                Double amount = structure.getDouble("amount");
                if (amount != null) {
                    totalAmount += amount;
                }
            }
        }
 
        // 其他补偿
        if (compensationVO.getOtherCompensationBO() != null) {
            OtherCompensationBO otherCompensation = compensationVO.getOtherCompensationBO();
            if (otherCompensation.getTemporaryConstructionMaterialCompensation() != null) {
                totalAmount += otherCompensation.getTemporaryConstructionMaterialCompensation();
            }
            if (otherCompensation.getParkingSpaceGarageCompensation() != null) {
                totalAmount += otherCompensation.getParkingSpaceGarageCompensation();
            }
            if (otherCompensation.getAttachmentCompensation() != null) {
                totalAmount += otherCompensation.getAttachmentCompensation();
            }
            if (otherCompensation.getBusinessCompensation() != null) {
                totalAmount += otherCompensation.getBusinessCompensation();
            }
            if (otherCompensation.getOtherExpenses() != null) {
                totalAmount += otherCompensation.getOtherExpenses();
            }
        }
        return totalAmount;
    }
 
    private double safeAdd(Double value) {
        return Objects.nonNull(value) ? value : 0.0;
    }
 
 
    @Override
    public JSONObject getPersonsInfo(String stateHouseholdId) {
        StateHousehold stateHousehold = stateHouseholdService.getById(stateHouseholdId);
        List<StateHouseholdOwner> stateHouseholdOwners = this.list(Wrappers.lambdaQuery(StateHouseholdOwner.class)
                .eq(StateHouseholdOwner::getStateHouseholdId, stateHouseholdId).eq(StateHouseholdOwner::getDelFlag, DeleteFlagEnum.NOT_DELETED.getKey()));
        if (CollectionUtils.isEmpty(stateHouseholdOwners)) {
            return new JSONObject();
        }
        if (org.apache.commons.lang3.StringUtils.isNoneBlank(stateHousehold.getAppendData())) {
            CalculateCompensationBO calculateCompensationBO = JSON.parseObject(stateHousehold.getAppendData(), CalculateCompensationBO.class);
            calculateCompensationBO.setStateHouseholdId(stateHouseholdId);
            stateHousehold.setAppendData(JSON.toJSONString(calculateCompensation(calculateCompensationBO)));
        }
 
        Long personalOwnerNum = stateHouseholdOwners.stream().filter(stateHouseholdOwner -> Objects.nonNull(stateHouseholdOwner) && Objects.equals(stateHouseholdOwner.getOwnerType(), HouseOwnerTyeEnum.PERSONAL.getValue())).count();
        List<StateHouseholdOwnerVO> voList = StateProjectConvert.INSTANCE.toStateHouseholdOwnerVOList(stateHouseholdOwners);
 
        Map<String, SysDictData> sysDictDataMap = getStringSysDictDataMap();
 
        voList.forEach(e -> {
            String helpType = e.getHelpType();
            String helpTypeStr = "";
 
            if (helpType != null) {
                helpTypeStr = Arrays.stream(helpType.split(","))
                        .map(sysDictDataMap::get)
                        .filter(Objects::nonNull)
                        .map(SysDictData::getDictLabel)
                        .collect(Collectors.joining("|"));
            }
 
            e.setHelpTypeStr(helpTypeStr);
        });
 
        String jsonString = JSON.toJSONString(new PageInfo<>(voList));
        JSONObject jsonObject = JSON.parseObject(jsonString);
 
        // 新增 helpType 和 isHelp 到外部
        stateHouseholdOwners.stream()
                .filter(e -> HouseOwnerTyeEnum.PERSONAL.getValue().equals(e.getOwnerType())
                        && StringUtils.isNotBlank(e.getHelpType())
                        && Objects.nonNull(e.getIsHelp()))
                .findFirst()
                .ifPresent(stateHouseholdOwner -> {
                    jsonObject.put("helpType", stateHouseholdOwner.getHelpType());
                    jsonObject.put("isHelp", stateHouseholdOwner.getIsHelp());
 
                    // 处理 helpTypeStr
                    String helpTypeStr = Arrays.stream(stateHouseholdOwner.getHelpType().split(","))
                            .map(sysDictDataMap::get)
                            .filter(Objects::nonNull)
                            .map(SysDictData::getDictLabel)
                            .collect(Collectors.joining("|"));
                    jsonObject.put("helpTypeStr", helpTypeStr);
                });
 
 
        // 将其他数据放入 data 对象
        jsonObject.put("compensationType", stateHousehold.getCompensationType());
        jsonObject.put("compensationTypeStr", CompensationCategoryEnum.getDescription(stateHousehold.getCompensationType()));
        jsonObject.put("isAgreeToMove", stateHousehold.getAgreeMove());
        jsonObject.put("fileBOList", JSON.parseArray(stateHousehold.getFileUrl()));
        //补充备注
        if (Objects.nonNull(stateHousehold.getAppendData()) && StringUtils.isNotBlank(stateHousehold.getAppendData())) {
            String appendDataStr = stateHousehold.getAppendData();
            CalculateCompensationVO calculateCompensationVO = JSON.parseObject(appendDataStr, CalculateCompensationVO.class);
            jsonObject.put("remark", calculateCompensationVO.getRemark());
        } else {
            jsonObject.put("remark", "");
        }
        jsonObject.put("personalOwnerNum", personalOwnerNum);
        return jsonObject;
    }
 
    @Override
    public List<StateProjectCompensateStandard> getCalculateCompensation(String projectId) {
        StateProjectCompensation compensation = stateProjectCompensationService.getById(projectId);
        if (Objects.isNull(compensation)) {
            throw new GlobalException("项目没有设置标准");
        }
        List<String> compensationNameList = new ArrayList<>();
        compensationNameList.add(compensation.getPolicyCompensationType());
        compensationNameList.add(compensation.getPolicySubsidy());
        compensationNameList.add(compensation.getAward());
        compensationNameList.add(compensation.getCompensationType());
        compensationNameList.add(compensation.getPolicySupplement());
        List<StateProjectCompensateStandard> enableCompensateStandard = stateProjectCompensateStandardService.getEnableCompensateStandard(compensationNameList);
        if (CollectionUtils.isEmpty(enableCompensateStandard)) {
            throw new GlobalException("未找到开启的标准");
        }
        return enableCompensateStandard;
    }
 
    private String getFullName(List<StateHouseholdOwner> householdVOS) {
        if (CollectionUtils.isEmpty(householdVOS)) {
            return "";
        }
        List<String> uniqueOwnerNames = householdVOS.stream().filter(data -> Objects.nonNull(data)
                        && Objects.nonNull(data.getOwnerName()))
                .map(StateHouseholdOwner::getOwnerName)
                .distinct()
                .collect(Collectors.toList());
        return String.join("|", uniqueOwnerNames);
    }
 
    private Map<String, SysDictData> getStringSysDictDataMap() {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("help_type");
        List<SysDictData> list = dictDataService.getList(sysDictData);
        Map<String, SysDictData> sysDictDataMap;
        if (!CollectionUtils.isEmpty(list)) {
            sysDictDataMap = list.stream().collect(Collectors.toMap(SysDictData::getDictValue, Function.identity()));
        } else {
            sysDictDataMap = new HashMap<>();
        }
        return sysDictDataMap;
    }
 
}