yanghb
2025-04-25 39854ed874e1f38ce3f192ca8e0362a826cfdd55
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
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
package com.ruoyi.bussiness.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bussiness.domain.*;
import com.ruoyi.bussiness.enums.MessageTypeEnum;
import com.ruoyi.bussiness.mapper.PlacementBatchMapper;
import com.ruoyi.bussiness.object.request.placementBatch.*;
import com.ruoyi.bussiness.object.request.report.ReportRequest;
import com.ruoyi.bussiness.object.response.placementApply.ProblemExportResponse;
import com.ruoyi.bussiness.object.response.placementBatch.*;
import com.ruoyi.bussiness.object.response.screen.*;
import com.ruoyi.bussiness.service.*;
import com.ruoyi.bussiness.utils.BatchNumberUtils;
import com.ruoyi.bussiness.utils.PaymentCycleHelper;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
@Service
public class PlacementBatchServiceImpl extends ServiceImpl<PlacementBatchMapper, PlacementBatch> implements PlacementBatchService {
 
    @Autowired
    private PlacementBatchAssetService placementBatchAssetService;
    @Autowired
    private PlacementBatchHouseholdService placementBatchHouseholdService;
    @Autowired
    private ISysNoticeService sysNoticeService;
    @Autowired
    private PlacementApplyRecordService placementApplyRecordService;
    @Autowired
    private CompensateService compensateService;
    @Autowired
    private PlacementService placementService;
    @Autowired
    private PlacementErrorService placementErrorService;
 
    @Override
    public PlacementBatchPageResponse page(PlacementBatchPageRequest request) {
        Page<PlacementBatch> page = new Page<>(request.getPageNum(), request.getPageSize());
        LambdaQueryWrapper<PlacementBatch> wrapper = new LambdaQueryWrapper<>();
        if (ObjUtil.isNotEmpty(request.getBatchNumber())) {
            wrapper.like(PlacementBatch::getBatchNumber, request.getBatchNumber());
        }
        if(ObjUtil.isNotEmpty(request.getStatus())){
            wrapper.eq(PlacementBatch::getStatus,request.getStatus());
        }
        wrapper.orderByDesc(PlacementBatch::getId);
 
        Page<PlacementBatch> pageData = baseMapper.selectPage(page, wrapper);
 
        PlacementBatchPageResponse response = new PlacementBatchPageResponse();
        response.setRecords(pageData.getRecords());
        response.setTotal(pageData.getTotal());
        return response;
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void imports(PlacementBatchImportRequest request) {
        //判断数据
        if (ObjUtil.isEmpty(request.getId())) {
            if (ObjUtil.isEmpty(request.getAssetFile())) {
                throw new GlobalException("请上传资金表");
            }
            if (ObjUtil.isEmpty(request.getHouseholdFile())) {
                throw new GlobalException("请上传房产表");
            }
        } else {
            if (ObjUtil.isEmpty(request.getAssetFile())
                    && ObjUtil.isEmpty(request.getHouseholdFile())) {
                throw new GlobalException("请至少上传资金表或者房产表");
            }
        }
 
        //新增安置批次表信息
        PlacementBatch placementBatch;
        if (ObjUtil.isEmpty(request.getId())) {
            placementBatch = new PlacementBatch();
            String batchNumber = BatchNumberUtils.getBatchNumber(getLastBatchNumber());
            placementBatch.setBatchNumber(batchNumber);
            placementBatch.setCreateId(SecurityUtils.getUserId());
            placementBatch.setCreateUser(SecurityUtils.getLoginUser().getUser().getNickName());
            save(placementBatch);
        } else {
            placementBatch = baseMapper.selectById(request.getId());
        }
 
        if (ObjUtil.isEmpty(placementBatch)) {
            throw new GlobalException("安置批次信息不存在!");
        }
 
        //导入资金表
        if (ObjUtil.isNotEmpty(request.getAssetFile())) {
            importAsset(placementBatch.getId(), request.getAssetFile());
        }
        //导入房产表
        if (ObjUtil.isNotEmpty(request.getHouseholdFile())) {
            importHousehold(placementBatch.getId(), request.getHouseholdFile());
        }
 
        //汇总数据
        sumPlacementBatch(placementBatch);
 
        //同步待处理消息
        new Thread(() -> {
            //批注数据
            placementBatch.setStatus(0);
            dataTag(placementBatch);
            countError(placementBatch);
            //统计错误数据
            syncApplyMessage();
        }).start();
 
    }
 
    /**
     * 统计数据
     */
    public void countError(PlacementBatch placementBatch) {
//        List<Map<String, Object>> error = this.baseMapper.countError(placementBatch.getId());
 
        //查询当前批次资金信息
        LambdaQueryWrapper<PlacementBatchAsset> assetLambdaQueryWrapper = new LambdaQueryWrapper<>();
        assetLambdaQueryWrapper.eq(PlacementBatchAsset::getPlacementBatchId,placementBatch.getId());
        List<PlacementBatchAsset> assets = placementBatchAssetService.list(assetLambdaQueryWrapper);
 
        List<PlacementError> placementErrors = new ArrayList<>();
        if(ObjUtil.isNotEmpty(assets)){
            for(PlacementBatchAsset asset : assets){
                PlacementError placementError = new PlacementError();
                /**
                 * sum(household_head_warn+id_card_warn+wait_family_area_warn+two_price_warn+
                 *                          price_amount_warn+compensation_sum_warn+quarter_pay_amount_warn+subsidy_amount_warn) as errorNum,
                 *                      sum(household_head_warn+id_card_warn) as settleNum,
                 *                      sum(wait_family_area_warn) as areaNum,
                 *                      sum(two_price_warn+price_amount_warn+compensation_sum_warn+quarter_pay_amount_warn+subsidy_amount_warn) as compensationNum,
                 */
 
                Integer settleNum = asset.getHouseholdHeadWarn() + asset.getIdCardWarn();
                Integer areaNum = asset.getWaitFamilyAreaWarn();
                Integer compensationNum = asset.getTwoPriceWarn() + asset.getPriceAmountWarn()
                        + asset.getCompensationSumWarn()+asset.getQuarterPayAmountWarn()
                        + asset.getSubsidyAmountWarn() + asset.getDownPaymentAmountWarn();
                Integer errorNum = settleNum + areaNum + compensationNum;
 
                placementError.setStreet(asset.getStreet());
                placementError.setErrorNum(errorNum);
                placementError.setSettleNum(settleNum);
                placementError.setAreaNum(areaNum);
                placementError.setCompensationNum(compensationNum);
                placementErrors.add(placementError);
            }
        }
 
 
        //查询当前批次购房信息
        LambdaQueryWrapper<PlacementBatchHousehold> householdLambdaQueryWrapper = new LambdaQueryWrapper<>();
        householdLambdaQueryWrapper.eq(PlacementBatchHousehold::getPlacementBatchId,placementBatch.getId());
        List<PlacementBatchHousehold> households = placementBatchHouseholdService.list(householdLambdaQueryWrapper);
        if (ObjUtil.isNotEmpty(households)) {
            for(PlacementBatchHousehold household : households){
                PlacementError placementError = new PlacementError();
                /**
                 * sum(household_head_warn+id_card_warn+wait_family_names_warn+wait_family_names_no_warn) as settleNum,
                 *                         sum(wait_family_area_warn) as areaNum,
                 *                         sum(compensation_amount_warn+compensation_sum_warn+quarter_pay_amount_warn+subsidy_amount_warn) as compensationNum,
                 *                         sum(area_warn) as houseNum
                 */
                Integer settleNum = household.getHouseholdHeadWarn()+household.getIdCardWarn()
                        +household.getWaitFamilyNamesWarn() + household.getWaitFamilyNamesNoWarn();
                Integer areaNum = household.getWaitFamilyAreaWarn();
                Integer compensationNum = household.getCompensationAmountWarn()
                        + household.getCompensationSumWarn()
                        +household.getQuarterPayAmountWarn()+household.getSubsidyAmountWarn()
                        +household.getDownPaymentAmountWarn();
                Integer houseNum = household.getAreaWarn();
 
                Integer errorNum = settleNum + areaNum + compensationNum + houseNum;
 
                placementError.setStreet(household.getStreet());
                placementError.setErrorNum(errorNum);
                placementError.setSettleNum(settleNum);
                placementError.setAreaNum(areaNum);
                placementError.setCompensationNum(compensationNum);
                placementError.setHouseNum(houseNum);
                placementErrors.add(placementError);
            }
        }
        placementErrorService.saveBatch(placementErrors);
    }
 
    /**
     * 更新消息
     */
    private void syncApplyMessage() {
        SysNotice sysNotice = sysNoticeService.getById(2);
        LambdaQueryWrapper<PlacementBatch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlacementBatch::getStatus, 0);
        List<PlacementBatch> batches = this.list(queryWrapper);
        if (ObjUtil.isEmpty(batches)) {
            return;
        }
        List<Long> batchIds = batches.stream().map(PlacementBatch::getId).collect(Collectors.toList());
        LambdaQueryWrapper<PlacementBatchAsset> assetCountQuery = new LambdaQueryWrapper<>();
        assetCountQuery.eq(PlacementBatchAsset::getPlacementBatchId, batchIds);
        int assetCount = NumberUtil.parseInt(String.valueOf(placementBatchAssetService.count(assetCountQuery)));
 
        LambdaQueryWrapper<PlacementBatchHousehold> householdCountQuery = new LambdaQueryWrapper<>();
        householdCountQuery.eq(PlacementBatchHousehold::getPlacementBatchId, batchIds);
        int houseHoldCount = NumberUtil.parseInt(String.valueOf(placementBatchHouseholdService.count(householdCountQuery)));
 
        int count = assetCount + houseHoldCount;
        String msg = MessageTypeEnum.PLACEMENT_BATCH.getMessage();
        sysNotice.setNoticeTitle(StrUtil.format(msg, Integer.toString(count)));
        sysNotice.setCountNum(new BigDecimal(count));
        sysNoticeService.updateById(sysNotice);
 
        //当月支付补偿款
        SysNotice sysNotice2 = sysNoticeService.getById(3);
        BigDecimal currentMonthMoney = placementBatchHouseholdService.sumCurrentMonth();
        String payMsg = MessageTypeEnum.PLACEMENT_PAY.getMessage();
        sysNotice2.setNoticeTitle(StrUtil.format(payMsg, currentMonthMoney));
        sysNotice2.setCountNum(currentMonthMoney);
        sysNoticeService.updateById(sysNotice2);
 
        //审核通过的购房表的人,以及签订时间从当前日期算,不超过2个月的 合计个人数统计就行了
        SysNotice sysNotice3 = sysNoticeService.getById(4);
        BigDecimal sumPersonCount = placementBatchHouseholdService.countCurrentPerson();
        String personMsg = MessageTypeEnum.PLACEMENT_FIRST_PAY.getMessage();
        sysNotice3.setNoticeTitle(StrUtil.format(personMsg, sumPersonCount));
        sysNotice3.setCountNum(sumPersonCount);
        sysNoticeService.updateById(sysNotice3);
 
    }
 
    @Override
    public PlacementBatchDetailResponse detail(PlacementBatchDetailRequest request) {
        PlacementBatch placementBatch = this.getById(request.getPlacementBatchId());
        if (ObjUtil.isEmpty(placementBatch)) {
            throw new GlobalException("安置批次信息不存在!");
        }
 
        PlacementBatchDetailResponse response = new PlacementBatchDetailResponse();
        response.setPlacementBatch(placementBatch);
        //根据类型查询明细记录(资金)
        if (request.getType() == 1) {
            //分页
            Page<PlacementBatchAsset> assetPage = placementBatchAssetService.page(request);
            response.setAssetList(assetPage.getRecords());
            response.setTotal(assetPage.getTotal());
            return response;
        } else {
            Page<PlacementBatchHousehold> householdPage = placementBatchHouseholdService.page(request);
            //填充集体面积、非集体面积
            if (ObjUtil.isNotEmpty(householdPage.getRecords())) {
                for (PlacementBatchHousehold placementBatchHousehold : householdPage.getRecords()) {
                    PlacementApplyRecord record = placementApplyRecordService.getRecordsByCardId(placementBatchHousehold.getIdCard());
                    if (ObjUtil.isNotEmpty(record)) {
                        placementBatchHousehold.setOrgArea(record.getOrgArea());
                        placementBatchHousehold.setNoOrgArea(record.getNoOrgArea());
                    }
                }
            }
            response.setHouseholdList(householdPage.getRecords());
            response.setTotal(householdPage.getTotal());
            return response;
        }
    }
 
    @Override
    public void dataApprove(PlacementBatchIdRequest request) {
        PlacementBatch placementBatch = this.getById(request.getId());
        if (ObjUtil.isEmpty(placementBatch)) {
            throw new GlobalException("数据不存在");
        }
        if(placementBatch.getStatus() != 0){
            throw new GlobalException("已审核通过!");
        }
        dataTag(placementBatch);
    }
 
    @Override
    public void assetCheck(IdRequest request) {
        LambdaUpdateWrapper<PlacementBatchAsset>  updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(PlacementBatchAsset::getChecks,1);
        updateWrapper.set(PlacementBatchAsset::getHouseholdHeadWarn,0);
        updateWrapper.set(PlacementBatchAsset::getIdCardWarn,0);
        updateWrapper.set(PlacementBatchAsset::getWaitFamilyAreaWarn,0);
        updateWrapper.set(PlacementBatchAsset::getTwoPriceWarn,0);
        updateWrapper.set(PlacementBatchAsset::getPriceAmountWarn,0);
        updateWrapper.set(PlacementBatchAsset::getCompensationSumWarn,0);
        updateWrapper.set(PlacementBatchAsset::getQuarterPayAmountWarn,0);
        updateWrapper.set(PlacementBatchAsset::getSubsidyAmountWarn,0);
 
        updateWrapper.eq(PlacementBatchAsset::getId,request.getId());
        placementBatchAssetService.update(updateWrapper);
    }
 
    @Override
    public void householdCheck(IdRequest request) {
        LambdaUpdateWrapper<PlacementBatchHousehold>  updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(PlacementBatchHousehold::getChecks,1);
        updateWrapper.set(PlacementBatchHousehold::getHouseholdHeadWarn,0);
        updateWrapper.set(PlacementBatchHousehold::getIdCardWarn,0);
        updateWrapper.set(PlacementBatchHousehold::getWaitFamilyNamesWarn,0);
        updateWrapper.set(PlacementBatchHousehold::getWaitFamilyNamesNoWarn,0);
        updateWrapper.set(PlacementBatchHousehold::getWaitFamilyAreaWarn,0);
        updateWrapper.set(PlacementBatchHousehold::getCompensationAmountWarn,0);
        updateWrapper.set(PlacementBatchHousehold::getCompensationSumWarn,0);
        updateWrapper.set(PlacementBatchHousehold::getQuarterPayAmountWarn,0);
        updateWrapper.set(PlacementBatchHousehold::getSubsidyAmountWarn,0);
        updateWrapper.set(PlacementBatchHousehold::getAreaWarn,0);
 
        updateWrapper.eq(PlacementBatchHousehold::getId,request.getId());
        placementBatchHouseholdService.update(updateWrapper);
    }
 
    @Override
    public void del(PlacementBatchIdRequest request) {
        this.removeById(request.getId());
        placementBatchHouseholdService.remove(new LambdaQueryWrapper<PlacementBatchHousehold>().
                eq(PlacementBatchHousehold::getPlacementBatchId, request.getId()));
        placementBatchAssetService.remove(new LambdaQueryWrapper<PlacementBatchAsset>().
                eq(PlacementBatchAsset::getPlacementBatchId, request.getId()));
    }
 
    /**
     * 数据标注
     * @param placementBatch
     */
    private void dataTag(PlacementBatch placementBatch) {
        if (ObjUtil.isEmpty(placementBatch)) {
            return;
        }
        //数据已经审核过了,不需要标注
        if (placementBatch.getStatus() != 0) {
            return;
        }
        //资产表
        List<PlacementBatchAsset> assetRecords = placementBatchAssetService.allList(placementBatch.getId());
        if (ObjUtil.isNotEmpty(assetRecords)) {
            //去购房表取签订时间、拆迁时间
            List<String> idsCard = assetRecords.stream().map(PlacementBatchAsset::getIdCard).collect(Collectors.toList());
            //去申请表取安置人数类型
            List<PlacementApplyRecord> placementApplyRecords = placementApplyRecordService.getRecordsByCardIds(idsCard);
            //申请表
            Map<String, PlacementApplyRecord> placementApplyRecordMap = placementApplyRecords.stream()
                    .collect(Collectors.toMap(
                            PlacementApplyRecord::getIdCard,
                            Function.identity(),
                            (existing, replacement) -> existing
                    ));
            for (PlacementBatchAsset record : assetRecords) {
                if (record.getChecks() == 1) {
                    continue;
                }
                //未通过安置申请
                if (placementApplyRecordService.countPassIdCard(record.getIdCard()) == 0) {
                    record.setHouseholdHeadWarn(1);
                } else {
                    record.setHouseholdHeadWarn(0);
                }
                //赔偿金额(判断新购房还是二手房,这两个只能存在一个)
                if (ObjUtil.isNotEmpty(record.getPriceNewAmount())
                        && ObjUtil.isNotEmpty(record.getPriceOldAmount())) {
                    record.setPriceAmountWarn(1);
                } else if (ObjUtil.isEmpty(record.getPriceNewAmount())
                        && ObjUtil.isEmpty(record.getPriceOldAmount())) {
                    record.setPriceAmountWarn(1);
                } else {
                    record.setPriceAmountWarn(0);
                }
                //拆迁时间
                PlacementApplyRecord applyRecord = placementApplyRecordMap.get(record.getIdCard());
                if (ObjUtil.isEmpty(applyRecord)) {
                    continue;
                }
                //自主购房补贴、过渡补贴(如果购房情况异常则不判定自主购房补贴是否异常,自主购房补贴、过渡补贴 依赖赔偿金额信息)
                if (record.getPriceAmountWarn() == 0 && record.getPriceAmountWarn() == 0) {
                    boolean warnFlag = compensateService.compensateBuyCalculateV2(applyRecord.getDemolitionTime(),
                            record.getStreet(),
                            applyRecord.getOrgArea(), applyRecord.getNoOrgArea(),
                            record.getPriceNewAmount(), record.getPriceOldAmount(),
                            record.getCompensationAmount());
                    record.setCompensationSumWarn(warnFlag ? 0 : 1);
 
                    //过渡补贴
                    if (compensateService.compensateSubsidyCalculate(applyRecord.getCurrentCount(), record.getSubsidyAmount())) {
                        record.setSubsidyAmountWarn(0);
                    } else {
                        record.setSubsidyAmountWarn(1);
                    }
 
                    //首付款警告
                    boolean downPaymentAmountWarn = true;
                    BigDecimal cateAmount = record.getCompensationAmount().multiply(new BigDecimal("0.25"));
                    if (cateAmount.compareTo(record.getDownPaymentAmount()) == 0) {
                        downPaymentAmountWarn = false;
                    }
                    record.setDownPaymentAmountWarn(downPaymentAmountWarn ? 1 : 0);
                }
            }
            placementBatchAssetService.updateBatchById(assetRecords);
        }
 
        List<PlacementBatchHousehold> householdsRecords = placementBatchHouseholdService.allList(placementBatch.getId());
        if (ObjUtil.isNotEmpty(householdsRecords)) {
            for (PlacementBatchHousehold record : householdsRecords) {
                if(record.getChecks() == 1){
                    continue;
                }
                //未通过安置申请
                int pass = placementApplyRecordService.countPassIdCard(record.getIdCard());
                if (pass == 0) {
                    record.setHouseholdHeadWarn(1);
                }else{
                    record.setHouseholdHeadWarn(0);
                }
                //不在安置库
                int noSettle = placementService.countPlacementByIdCard(record.getIdCard());
                if ( noSettle == 0) {
                    record.setIdCardWarn(1);
                }else{
                    record.setIdCardWarn(0);
                }
                //判断家庭成员是否重复
                if (ObjUtil.isNotEmpty(record.getWaitFamilyNames())) {
                    boolean familyNameWarn = false;
                    boolean familyNoWarn = false;
                    String[] familyNames = record.getWaitFamilyNames().split("、");
                    if (ObjUtil.isNotEmpty(familyNames)) {
                        //判断是否在安置库 =0不在安置库
                        for (String familyName : familyNames) {
                            if (placementService.countNamesExists(familyName) ==0) {
                                familyNoWarn = true;
                                break;
                            }
                        }
                        //判断重复
                        for(String familyName : familyNames){
                            int exists = selectNameExists(familyName);
                            if (exists > 1) {
                                familyNameWarn = true;
                                break;
                            }
                        }
                        record.setWaitFamilyNamesWarn(familyNameWarn ? 1 : 0);
                        record.setWaitFamilyNamesNoWarn(familyNoWarn ? 1 : 0);
                    }
                }
                //应安置面积警告标注
                boolean warn = true;
                BigDecimal waitFamilyArea = record.getWaitFamilyArea();
//                Integer currentCollectiveNum = record.getCurrentCollectiveNum();
//                Integer currentNoCollectiveNum = record.getCurrentNoCollectiveNum();
                //查询申请表 面积
                PlacementApplyRecord placementApplyRecord = placementApplyRecordService.getRecordsByCardId(record.getIdCard());
                if (ObjUtil.isEmpty(placementApplyRecord)) {
                    warn = false;
                }else {
                    warn = compensateService.compensateSettleAreaCalculate(placementApplyRecord.getOrgArea(), placementApplyRecord.getNoOrgArea(), waitFamilyArea);
                }
                record.setWaitFamilyAreaWarn(warn ? 0 : 1);
 
                //判断是否两个都填写了
                if (ObjUtil.isNotEmpty(record.getCompensationOldAmount())
                        && ObjUtil.isNotEmpty(record.getCompensationNewAmount())) {
                    record.setCompensationAmountWarn(1);
                }else if(ObjUtil.isEmpty(record.getCompensationNewAmount())
                    && ObjUtil.isEmpty(record.getCompensationOldAmount())){
                    record.setCompensationAmountWarn(1);
                } else {
                    record.setCompensationAmountWarn(0);
                }
 
 
                BigDecimal orgArea = placementApplyRecord == null ? BigDecimal.ZERO : placementApplyRecord.getOrgArea();
                BigDecimal noOrgArea = placementApplyRecord == null ? BigDecimal.ZERO : placementApplyRecord.getNoOrgArea();
                //自主购房补贴、过渡补贴(如果购房情况异常则不判定自主购房补贴是否异常,自主购房补贴、过渡补贴 依赖赔偿金额信息)
                if (record.getCompensationAmountWarn() == 0) {
                    boolean warnFlag = compensateService.compensateBuyCalculateV2(record.getDemolitionTime(),
                            record.getStreet(),
                            orgArea, noOrgArea,
                            record.getCompensationNewAmount(), record.getCompensationOldAmount(),
                            record.getCompensationSum());
                    record.setCompensationSumWarn(warnFlag ? 0 : 1);
 
                    //过渡补贴
                    if (compensateService.compensateSubsidyCalculate(record.getCurrentCount(), record.getSubsidyAmount())) {
                        record.setSubsidyAmountWarn(0);
                    } else {
                        record.setSubsidyAmountWarn(1);
                    }
                }
 
                //每季度支付款项
                BigDecimal pay = record.getCompensationSum().multiply(new BigDecimal("0.25"));
                BigDecimal quarterPayAmount = (record.getCompensationSum().subtract(pay)).divide(new BigDecimal("20"), 10, RoundingMode.DOWN);
                if (quarterPayAmount.compareTo(record.getQuarterPayAmount()) != 0) {
                    record.setQuarterPayAmountWarn(1);
                }else{
                    record.setQuarterPayAmountWarn(0);
                }
                //首付款警告
                boolean downPaymentAmountWarn = true;
                BigDecimal cateAmount = record.getCompensationSum().multiply(new BigDecimal("0.25"));
                if(cateAmount.compareTo(record.getDownPaymentAmount()) == 0){
                    downPaymentAmountWarn = false;
                }
                record.setDownPaymentAmountWarn(downPaymentAmountWarn ? 1 : 0);
                //房源
                BigDecimal area;
                if(record.getOldHousingArea().compareTo(BigDecimal.ZERO) > 0){
                    area = record.getOldHousingArea();
                }else if(record.getBuildHousingArea().compareTo(BigDecimal.ZERO) > 0){
                    area = record.getBuildHousingArea();
                }else{
                    area = record.getNewHousingArea();
                }
                if(compensateService.houseCalculate(record.getCurrentCount(),area)){
                    record.setAreaWarn(1);
                }else{
                    record.setAreaWarn(0);
                }
            }
            placementBatchHouseholdService.updateBatchById(householdsRecords);
        }
    }
 
    public Integer selectNameExists(String name) {
        //排除驳回的数据
        LambdaQueryWrapper<PlacementBatch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(PlacementBatch::getStatus,2);
        List<PlacementBatch> batches = this.list(queryWrapper);
        if (ObjUtil.isEmpty(batches)) {
            return 0;
        }
        List<Long> batchIds = batches.stream().map(PlacementBatch::getId).collect(Collectors.toList());
        LambdaQueryWrapper<PlacementBatchHousehold> childQueryWrapper = new LambdaQueryWrapper<>();
        childQueryWrapper.in(PlacementBatchHousehold::getPlacementBatchId,batchIds);
        childQueryWrapper.and(query->query.like(PlacementBatchHousehold::getWaitFamilyNames,name));
        return NumberUtil.parseInt(String.valueOf(placementBatchHouseholdService.count(childQueryWrapper)));
    }
 
    @Override
    public void assetEdit(PlacementBatchAsset request) {
        PlacementBatch placementBatch = this.getById(request.getPlacementBatchId());
        if (ObjUtil.isEmpty(placementBatch)) {
            throw new GlobalException("安置批次不存在!");
        }
        placementBatchAssetService.updateById(request);
        //调整汇总信息
        sumPlacementBatch(placementBatch);
        dataTag(placementBatch);
    }
 
    @Override
    public void householdEdit(PlacementBatchHousehold request) {
        PlacementBatch placementBatch = this.getById(request.getPlacementBatchId());
        if (ObjUtil.isEmpty(placementBatch)) {
            throw new GlobalException("安置批次不存在!");
        }
        placementBatchHouseholdService.updateById(request);
        //调整汇总信息
        sumPlacementBatch(placementBatch);
        dataTag(placementBatch);
    }
 
    @Override
    public void assetDel(IdRequest request) {
        PlacementBatchAsset placementBatchAsset = placementBatchAssetService.getById(request.getId());
        if (ObjUtil.isEmpty(placementBatchAsset)) {
            throw new GlobalException("记录不存在!");
        }
        PlacementBatch placementBatch = this.getById(placementBatchAsset.getPlacementBatchId());
        if (ObjUtil.isEmpty(placementBatch)) {
            throw new GlobalException("安置批次不存在!");
        }
        placementBatchAssetService.removeById(request.getId());
        //调整汇总信息
        sumPlacementBatch(placementBatch);
    }
 
    @Override
    public void householdDel(IdRequest request) {
        PlacementBatchHousehold placementBatchHousehold = placementBatchHouseholdService.getById(request.getId());
        if(ObjUtil.isEmpty(placementBatchHousehold)){
            throw new GlobalException("记录不存在!");
        }
        PlacementBatch placementBatch = this.getById(placementBatchHousehold.getPlacementBatchId());
        if (ObjUtil.isEmpty(placementBatch)) {
            throw new GlobalException("安置批次不存在!");
        }
        placementBatchHouseholdService.removeById(request.getId());
        //调整汇总信息
        sumPlacementBatch(placementBatch);
    }
 
    @Override
    public void householdAdd(PlacementBatchHousehold request) {
        PlacementBatch placementBatch = this.getById(request.getPlacementBatchId());
        if (ObjUtil.isEmpty(placementBatch)) {
            throw new GlobalException("安置批次不存在!");
        }
        placementBatchHouseholdService.save(request);
        //调整汇总信息
        sumPlacementBatch(placementBatch);
    }
 
    @Override
    public void assetAdd(PlacementBatchAsset request) {
        PlacementBatch placementBatch = this.getById(request.getPlacementBatchId());
        if (ObjUtil.isEmpty(placementBatch)) {
            throw new GlobalException("安置批次不存在!");
        }
        placementBatchAssetService.save(request);
        //调整汇总信息
        sumPlacementBatch(placementBatch);
    }
 
    @Override
    public PlacementBatchAsset assetDetail(IdRequest request) {
        return placementBatchAssetService.getById(request.getId());
    }
 
    @Override
    public PlacementBatchHousehold householdDetail(IdRequest request) {
        return placementBatchHouseholdService.getById(request.getId());
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void approve(ApproveRequest request) {
        PlacementBatch placementBatch = this.getById(request.getId());
        if (ObjUtil.isEmpty(placementBatch)) {
            throw new GlobalException("安置批次不存在!");
        }
        if (placementBatch.getStatus() != 0) {
            throw new GlobalException("已经审核过了");
        }
        placementBatch.setStatus(request.getStatus());
        placementBatch.setReason(request.getReason());
        placementBatch.setApproveTime(new Date());
        placementBatch.setApproveId(SecurityUtils.getUserId());
        placementBatch.setApproveName(SecurityUtils.getLoginUser().getUser().getNickName());
        updateById(placementBatch);
        //记录审核周期
        buildCycleTime(placementBatch.getId());
 
        //审核通过,修改待安置库状态
        if(request.getStatus() == 1) {
            LambdaQueryWrapper<PlacementBatchHousehold> placementApplyRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
            placementApplyRecordLambdaQueryWrapper.eq(PlacementBatchHousehold::getPlacementBatchId, placementBatch.getId());
            //家庭成员
            List<PlacementBatchHousehold> households = placementBatchHouseholdService.list(placementApplyRecordLambdaQueryWrapper);
            //户主
            List<String> houseHead = households.stream().map(PlacementBatchHousehold::getHouseholdHead).collect(Collectors.toList());
            if (ObjUtil.isNotEmpty(households)) {
                List<String> familyNames = households.stream().map(PlacementBatchHousehold::getWaitFamilyNames).collect(Collectors.toList());
                List<String> allFamilyNames = new ArrayList<>();
                for(String familyName : familyNames) {
                    List<String> names = Arrays.asList(familyName.split("、"));
                    allFamilyNames.addAll(names);
                }
                LambdaUpdateWrapper<Placement> placementLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                placementLambdaUpdateWrapper.set(Placement::getStatus, 1);
                placementLambdaUpdateWrapper.and(query -> query.in(Placement::getFamilyName, allFamilyNames));
                placementService.update(placementLambdaUpdateWrapper);
 
                LambdaUpdateWrapper<Placement> placementHouseHeadUpdateWrapper = new LambdaUpdateWrapper<>();
                placementHouseHeadUpdateWrapper.set(Placement::getStatus, 1);
                placementHouseHeadUpdateWrapper.and(query -> query.in(Placement::getHouseholdHead, houseHead));
                placementService.update(placementHouseHeadUpdateWrapper);
            }
        }
 
        //同步待处理消息
        new Thread(() -> {
            syncApplyMessage();
        }).start();
    }
 
    /**
     * 生成付款周期
     * @param placementBatchId
     */
    private void buildCycleTime(Long placementBatchId){
        List<PlacementBatchHousehold> records = placementBatchHouseholdService.allList(placementBatchId);
        if (ObjUtil.isEmpty(records)) {
            return;
        }
        for(PlacementBatchHousehold placementBatchHousehold : records){
            if(ObjUtil.isEmpty(placementBatchHousehold.getCompensationPayTime())){
                continue;
            }
            String cycleTimes = PaymentCycleHelper.getQuarterlyPaymentCycles(placementBatchHousehold.getCompensationPayTime());
            placementBatchHousehold.setCycle(cycleTimes);
        }
        placementBatchHouseholdService.updateBatchById(records);
    }
 
    @Override
    public void problemExport(ProblemExportRequest request, HttpServletResponse response) {
        try {
            PlacementBatch placementBatch = getById(request.getPlacementBatchId());
            //查询安置表是否存在
            if (ObjUtil.isEmpty(placementBatch)) {
                throw new GlobalException("安置批次信息不存在");
            }
            FileUtils.setExcelResponseHeader(response, "问题数据.xlsx");
            //资金表
            if (request.getType() == 1) {
                ExcelWriterBuilder write = EasyExcelFactory.write(response.getOutputStream(), AssetExportResponse.class);
                List<PlacementBatchAsset> assets = placementBatchAssetService.problemList(request);
                List<AssetExportResponse> data = BeanUtil.copyToList(assets, AssetExportResponse.class);
                write.sheet("sheet").doWrite(data);
                response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            } else {
                ExcelWriterBuilder write = EasyExcelFactory.write(response.getOutputStream(), HouseholdExportResponse.class);
                //购房表
                List<PlacementBatchHousehold> households = placementBatchHouseholdService.problemList(request);
                List<HouseholdExportResponse> data = BeanUtil.copyToList(households, HouseholdExportResponse.class);
                write.sheet("sheet").doWrite(data);
                response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            }
        } catch (Exception e) {
            throw new GlobalException("导出问题数据失败");
        }
    }
 
    /**
     * 获取最近批次号
     *
     * @return
     */
    @Override
    public String getLastBatchNumber() {
        LambdaQueryWrapper<PlacementBatch> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(PlacementBatch::getId);
        wrapper.last("limit 1");
        PlacementBatch placementBatch = baseMapper.selectOne(wrapper);
        if (ObjUtil.isEmpty(placementBatch)) {
            return null;
        }
        return placementBatch.getBatchNumber();
    }
 
    @Override
    public SelfBuyResponse getSelfBuyResponse() {
        SelfBuyResponse selfBuyResponse = new SelfBuyResponse();
        LambdaQueryWrapper<PlacementBatch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlacementBatch::getStatus,1);
        List<PlacementBatch> batches = list(queryWrapper);
        if (ObjUtil.isEmpty(batches)) {
            return selfBuyResponse;
        }
        Integer householdCount = batches.stream().mapToInt(PlacementBatch::getHouseholdsNum).sum();
        Integer personCount = batches.stream().mapToInt(PlacementBatch::getPersonNum).sum();
        selfBuyResponse.setHouseholdNum(householdCount);
        selfBuyResponse.setPersonNum(personCount);
        return selfBuyResponse;
    }
 
    @Override
    public List<StreetResponse> getStreetResponse() {
        return placementBatchHouseholdService.getStreetResponse();
    }
 
    @Override
    public List<ImportErrorResponse> getImportErrorResponse() {
        return placementBatchHouseholdService.getImportErrorResponse();
    }
 
    @Override
    public List<MapResponse> getMapResponse() {
        return placementBatchHouseholdService.getMapResponse();
    }
 
    @Override
    public List<QuarterProcessResponse> getQuarterProcessResponse(ReportRequest request) {
        return placementBatchHouseholdService.getQuarterProcessResponse(request);
    }
 
    @Override
    public List<PlacementTypeResponse> getPlacementTypeResponse() {
        return placementBatchHouseholdService.getPlacementTypeResponse();
    }
 
    @Override
    public List<MonthCompensationResponse> getMonthCompensationResponses(String month) {
        return placementBatchHouseholdService.getMonthCompensationResponse(month);
    }
 
    @Override
    public List<Map<String,Object>> getQuarterPayResponse(List<String> quarters) {
        return placementBatchHouseholdService.getQuarterPayResponse(quarters);
    }
 
 
    /**
     * 资金表导入
     *
     * @param assetFile
     * @throws IOException
     */
    public void importAsset(Long placementBatchId, MultipartFile assetFile) {
        File file = FileUtils.convertToFile(assetFile);
        List<Map<Integer, String>> maps = FileUtils.readExcelHead(file);
        if (!Objects.equals(maps.get(0).get(0), "镇(街道)")
                || !Objects.equals(maps.get(0).get(1), "拆迁项目名称")
                || !Objects.equals(maps.get(0).get(2), "所在村(社区)")
                || !Objects.equals(maps.get(0).get(3), "户主姓名")
                || !Objects.equals(maps.get(0).get(4), "身份证号")
                || !Objects.equals(maps.get(0).get(5), "应安置人数(人)")
                || !Objects.equals(maps.get(0).get(6), "所有家庭人员应安置面积(m²)")
        ) {
            throw new GlobalException("Excel表头格式不对");
        }
        List<PlacementAssetTemplateResponse> dataList;
        try {
            dataList = EasyExcelFactory.read(file)
                    .head(PlacementAssetTemplateResponse.class)
                    .sheet()
                    .doReadSync();
        } catch (Exception e) {
            throw new GlobalException("读取Excel文件失败");
        }
 
        if (CollectionUtils.isEmpty(dataList)) {
            throw new GlobalException("未解析出数据");
        }
 
        //导入数据,直接导入库
        List<PlacementBatchAsset> records = BeanUtil.copyToList(dataList, PlacementBatchAsset.class);
        //校验数据(身份证必填)
        for (PlacementBatchAsset record : records) {
            record.setPlacementBatchId(placementBatchId);
            if (ObjUtil.isEmpty(record.getIdCard())) {
                throw new GlobalException(record.getHouseholdHead() + "身份证不能为空!");
            }
            if (ObjUtil.isEmpty(record.getProjectName())) {
                throw new GlobalException("请输入拆迁项目名称");
            }
            if (ObjUtil.isEmpty(record.getStreet())) {
                throw new GlobalException("请输入镇(街道)");
            }
            if (ObjUtil.isEmpty(record.getCommunity())) {
                throw new GlobalException("请输入社区");
            }
            if (ObjUtil.isEmpty(record.getHouseholdHead())) {
                throw new GlobalException("请输入户主");
            }
            if (ObjUtil.isEmpty(record.getResettledNum())) {
                throw new GlobalException("请输入应安置人数");
            }
            if (ObjUtil.isEmpty(record.getResettledArea())) {
                throw new GlobalException("请输入所有家庭人员应安置面积");
            }
            if (ObjUtil.isEmpty(record.getCompensationAmount())) {
                throw new GlobalException("请输入补偿总价");
            }
            if (ObjUtil.isEmpty(record.getDownPaymentAmount())) {
                throw new GlobalException("请输入25%首付款");
            }
            if (ObjUtil.isEmpty(record.getQuarterPayAmount())) {
                throw new GlobalException("请输入每季度需支付款项");
            }
            if (ObjUtil.isEmpty(record.getSubsidyAmount())) {
                throw new GlobalException("请输入过渡补贴");
            }
        }
        placementBatchAssetService.saveBatch(records);
    }
 
 
    /**
     * 购房表导入
     *
     * @param householdFile
     * @throws IOException
     */
    public void importHousehold(Long placementBatchId, MultipartFile householdFile) {
        File file = FileUtils.convertToFile(householdFile);
        List<Map<Integer, String>> maps = FileUtils.readExcelHead(file);
        if (!Objects.equals(maps.get(0).get(0), "镇(街道)")
                || !Objects.equals(maps.get(0).get(1), "拆迁项目名称")
                || !Objects.equals(maps.get(0).get(2), "所在村(社区)")
                || !Objects.equals(maps.get(0).get(3), "拆迁时间")
                || !Objects.equals(maps.get(0).get(4), "户主姓名")
                || !Objects.equals(maps.get(0).get(5), "身份证号")
                || !Objects.equals(maps.get(0).get(6), "联系电话")
        ) {
            throw new GlobalException("Excel表头格式不对");
        }
        List<PlacementHouseholdTemplateResponse> dataList = EasyExcelFactory.read(file)
                .head(PlacementHouseholdTemplateResponse.class)
                .sheet()
                .doReadSync();
 
        if (CollectionUtils.isEmpty(dataList)) {
            throw new GlobalException("未解析出数据");
        }
 
        //导入数据,直接导入库
        List<PlacementBatchHousehold> records = BeanUtil.copyToList(dataList, PlacementBatchHousehold.class);
        //校验数据(身份证必填)
        for (PlacementBatchHousehold record : records) {
            record.setPlacementBatchId(placementBatchId);
            if (ObjUtil.isEmpty(record.getIdCard())) {
                throw new GlobalException(record.getHouseholdHead() + "身份证不能为空!");
            }
            if (ObjUtil.isEmpty(record.getStreet())) {
                throw new GlobalException("请输入街道");
            }
            if (ObjUtil.isEmpty(record.getCommunity())) {
                throw new GlobalException("请输入所在村(社区)");
            }
            if (ObjUtil.isEmpty(record.getProjectName())) {
                throw new GlobalException("请输入项目名称");
            }
            if (ObjUtil.isEmpty(record.getDemolitionTime())) {
                throw new GlobalException("请输入拆迁时间");
            }
            if (ObjUtil.isEmpty(record.getHouseholdHead())) {
                throw new GlobalException("请输入户主名称");
            }
//            if (ObjUtil.isEmpty(record.getMobile())) {
//                throw new GlobalException("请输入联系电话");
//            }
            if(ObjUtil.isEmpty(record.getCurrentCount())){
                throw new GlobalException("请输入本次安置人数 - 合计");
            }
            if(ObjUtil.isEmpty(record.getWaitFamilyNames())){
                throw new GlobalException("请输入待安置家庭成员");
            }
            if(ObjUtil.isEmpty(record.getWaitFamilyArea())){
                throw new GlobalException("请输入待安置人员应安置面积合计");
            }
//            if(ObjUtil.isEmpty(record.getWaitFamilyArea())){
//                throw new GlobalException("请输入待安置人员应安置面积合计");
//            }
            if(ObjUtil.isEmpty(record.getCompensationSum())){
                throw new GlobalException("请输入补偿金额 - 合计");
            }
            if(ObjUtil.isEmpty(record.getDownPaymentAmount())){
                throw new GlobalException("25%首付款");
            }
            if(ObjUtil.isEmpty(record.getQuarterPayAmount())){
                throw new GlobalException("每季度需支付款项");
            }
            if(ObjUtil.isEmpty(record.getSubsidyAmount())){
                throw new GlobalException("过渡补贴");
            }
            if(ObjUtil.isEmpty(record.getCertificateTime())){
                throw new GlobalException("凭证发放时间");
            }
            if(ObjUtil.isEmpty(record.getBuyTime())){
                throw new GlobalException("购房时间");
            }
//            if(ObjUtil.isEmpty(record.getDealAmount())){
//                throw new GlobalException("成交金额");
//            }
            if(ObjUtil.isEmpty(record.getSignTime())){
                throw new GlobalException("自主购房签订时间");
            }
//            if(ObjUtil.isEmpty(record.getSignTime())){
//                throw new GlobalException("自主购房签订时间");
//            }
//            if(ObjUtil.isEmpty(record.getCompensationPayTime())){
//                throw new GlobalException("25%补偿款及过渡补贴支付时间");
//            }
        }
        placementBatchHouseholdService.saveBatch(records);
    }
 
 
    /**
     * 计算汇总安置批次信息
     */
    private void sumPlacementBatch(PlacementBatch placementBatch) {
        //汇总资金表
        LambdaQueryWrapper<PlacementBatchAsset> assetLambdaQueryWrapper = new LambdaQueryWrapper<>();
        assetLambdaQueryWrapper.eq(PlacementBatchAsset::getPlacementBatchId, placementBatch.getId());
        List<PlacementBatchAsset> assets = placementBatchAssetService.list(assetLambdaQueryWrapper);
        if (CollectionUtils.isNotEmpty(assets)) {
            // 申请总户数
            Integer householdsNum = Math.toIntExact(assets.stream().map(PlacementBatchAsset::getIdCard).distinct().count());
            //申请总人数
            Integer personNum = assets.stream().mapToInt(PlacementBatchAsset::getResettledNum).sum();
            //25%首付款
            BigDecimal downPaymentAmount = assets.stream().map(PlacementBatchAsset::getDownPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            //每季度需支付款项(万元)
            BigDecimal quarterPayAmount = assets.stream().map(PlacementBatchAsset::getQuarterPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add).multiply(new BigDecimal("20"));
            //过渡补贴(万元)
            BigDecimal subsidyAmount = assets.stream().map(PlacementBatchAsset::getSubsidyAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            //补偿金额(合计)
            BigDecimal totalAmount = assets.stream().map(PlacementBatchAsset::getCompensationAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
 
            placementBatch.setHouseholdsNum(householdsNum);
            placementBatch.setPersonNum(personNum);
            placementBatch.setDownPaymentAmount(downPaymentAmount);
            placementBatch.setQuarterPayAmount(quarterPayAmount);
            placementBatch.setSubsidyAmount(subsidyAmount);
            placementBatch.setTotalAmount(totalAmount);
        }
 
        //汇总房产表
        LambdaQueryWrapper<PlacementBatchHousehold> householdLambdaQueryWrapper = new LambdaQueryWrapper<>();
        householdLambdaQueryWrapper.eq(PlacementBatchHousehold::getPlacementBatchId, placementBatch.getId());
        List<PlacementBatchHousehold> households = placementBatchHouseholdService.list(householdLambdaQueryWrapper);
        if (CollectionUtils.isNotEmpty(households)) {
            //汇总面积
            //新建商品住房-面积
            BigDecimal newHousingArea = households.stream().map(PlacementBatchHousehold::getNewHousingArea).reduce(BigDecimal.ZERO, BigDecimal::add);
            //二手住房-面积
            BigDecimal oldHousingArea = households.stream().map(PlacementBatchHousehold::getOldHousingArea).reduce(BigDecimal.ZERO, BigDecimal::add);
            //新建商业住房
            BigDecimal buildHousingArea = households.stream().map(PlacementBatchHousehold::getBuildHousingArea).reduce(BigDecimal.ZERO, BigDecimal::add);
            //新建停车位-金额(万元)
            Integer newStopArea = households.stream().mapToInt(PlacementBatchHousehold::getNewStopNum).sum();
 
            placementBatch.setNewArea(newHousingArea);
            placementBatch.setOldArea(oldHousingArea);
            placementBatch.setBusinessArea(buildHousingArea);
            placementBatch.setParkingArea(newStopArea);
        }
        //修改
        updateById(placementBatch);
    }
 
}