mitao
2025-01-20 93fec20f3cf9d7801eeaa10acef4687ed110d435
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
package com.zzg.system.service.state.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.zzg.common.core.domain.BaseEntity;
import com.zzg.common.core.domain.entity.state.*;
import com.zzg.common.enums.*;
import com.zzg.common.exception.GlobalException;
import com.zzg.common.utils.DateUtils;
import com.zzg.common.utils.FileUtil;
import com.zzg.common.utils.PageUtils;
import com.zzg.common.utils.SecurityUtils;
import com.zzg.system.convert.StateProjectConvert;
import com.zzg.system.domain.bo.*;
import com.zzg.system.domain.vo.ApplyHouseholdOwnerVO;
import com.zzg.system.domain.vo.ApplyOwnerVO;
import com.zzg.system.domain.vo.StateApplyRecordPageInfoLevelVO;
import com.zzg.system.domain.vo.StateApplyRecordVO;
import com.zzg.system.mapper.state.StateApplyRecordMapper;
import com.zzg.system.service.state.*;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
@Service
@RequiredArgsConstructor
public class StateApplyRecordServiceImpl extends ServiceImpl<StateApplyRecordMapper, StateApplyRecord> implements StateApplyRecordService {
    private final StateProjectService stateProjectService;
    private final StateApplyRecordItemService stateApplyRecordItemService;
    private final StateHouseholdService stateHouseholdService;
    private final StateHouseholdOwnerService stateHouseholdOwnerService;
    private final StateProjectCompensationService stateProjectCompensationService;
    private final StateProjectCompensateStandardService stateProjectCompensateStandardService;
    private final StateProcessTemplateService stateProcessTemplateService;
 
    @Lazy
    @Resource
    private StateApplyRecordService stateApplyRecordService;
 
    /**
     * 分页查询
     * @param stateApplyRecordPageBO
     * @return
     */
    @Override
    public PageInfo<StateApplyRecordVO> pageInfo(StateApplyRecordPageBO stateApplyRecordPageBO) {
        PageUtils.startPage();
        adjustEndTime(stateApplyRecordPageBO);
        List<StateApplyRecordVO> stateApplyRecordVOS = this.getBaseMapper().selectProjectWithApplyRecord(stateApplyRecordPageBO);
        PageInfo<StateApplyRecordVO> stateApplyRecordVOPageInfo = new PageInfo<>(stateApplyRecordVOS);
        if (CollectionUtils.isEmpty(stateApplyRecordVOPageInfo.getList())) {
            return new PageInfo<>();
        }
        for (StateApplyRecordVO e : stateApplyRecordVOPageInfo.getList()) {
            e.setApplyStatusStr(SubmitStatusEnum.getTextByValue(e.getApplyStatus()));
            e.setApplyTypeStr(ApplyTypeEnum.getTextByValue(e.getApplyType()));
            e.setDistributionAmount(e.getAmount());
        }
        stateApplyRecordVOPageInfo.getList().sort(Comparator.comparing(BaseEntity::getCreateTime).reversed());
        return stateApplyRecordVOPageInfo;
    }
 
    public void adjustEndTime(StateApplyRecordPageBO stateApplyRecordPageBO) {
        // 获取 applyStartTime 和 applyEndTime
        LocalDate applyStartTime = stateApplyRecordPageBO.getApplyStartTime();
        LocalDate applyEndTime = stateApplyRecordPageBO.getApplyEndTime();
 
        // 如果开始时间和结束时间相同,则延长结束时间一天
        if (applyStartTime != null && applyStartTime.equals(applyEndTime)) {
            applyEndTime = applyEndTime.plusDays(1);
            stateApplyRecordPageBO.setApplyEndTime(applyEndTime); // 设置新的结束时间
        }
    }
 
    /**
     * 保存请账
     * @param recordAndItemBO
     * @return
     */
    @Override
    @Transactional
    public boolean saveStateApplyRecordAndItem(SaveRecordAndItemBO recordAndItemBO) {
        StateApplyRecord stateApplyRecord = StateProjectConvert.INSTANCE.toStateApplyRecord(recordAndItemBO);
        List<StateApplyRecordItem> recordItem = StateProjectConvert.INSTANCE.toStateApplyRecordItem(recordAndItemBO.getApplyOwnerBOList());
        StateApplyRecordItem item;
        List<StateApplyRecordItem> newRecordItem = new ArrayList<>();
        for (StateApplyRecordItem stateApplyRecordItem : recordItem) {
            if (stateApplyRecordItem.getOwnerName().contains("|") && stateApplyRecordItem.getStateHouseholdOwnerId().contains("|")) {
 
                String[] splitOwnerName = stateApplyRecordItem.getOwnerName().split("\\|");
                String[] splitOwnerId = stateApplyRecordItem.getStateHouseholdOwnerId().split("\\|");
                for (int i = 0; i < splitOwnerName.length; i++) {
                    item = StateProjectConvert.INSTANCE.copyStateApplyRecordItem(stateApplyRecordItem);
                    item.setOwnerName(splitOwnerName[i]);
                    item.setStateHouseholdOwner(splitOwnerId[i]);
                    item.setStateHouseholdOwnerId(splitOwnerId[i]);
                    newRecordItem.add(item);
                }
            } else {
                newRecordItem.add(stateApplyRecordItem);
            }
        }
        stateApplyRecord.setFileUrl(JSON.toJSONString(recordAndItemBO.getFileBOList()));
        //审核中
        stateApplyRecord.setApplyStatus(SubmitStatusEnum.PENDING_REVIEW.getValue());
        if (SecurityUtils.getLoginUser() != null && SecurityUtils.getLoginUser().getUser() != null) {
            if (SecurityUtils.getLoginUser().getUser().getNickName() != null) {
                stateApplyRecord.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());
            }
 
            if (SecurityUtils.getLoginUser().getUser().getDept() != null &&
                    SecurityUtils.getLoginUser().getUser().getDept().getDeptName() != null) {
                stateApplyRecord.setApplyDepartment(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
            }
        }
        StateProject projectById = stateProjectService.getProjectById(recordAndItemBO.getProjectId());
        if (Objects.nonNull(projectById)) {
            stateApplyRecord.setRecordNo(projectById.getProjectNo());
        }
 
        stateApplyRecord.setHouseholdCount((int) recordAndItemBO.getApplyOwnerBOList()
                .stream()
                .map(ApplyOwnerBO::getStateHouseholdId).filter(Objects::nonNull).collect(Collectors.toList()).stream()
                .distinct().count());
 
        this.saveOrUpdate(stateApplyRecord);
 
        //发起请款流程
        ProcessStartBO processStartBO = new ProcessStartBO();
        if (Objects.equals(ApplyTypeEnum.TEMPORARY_RESIDENTIAL_SUBSIDY.getValue(), recordAndItemBO.getApplyType())) {
            processStartBO.setCategory(ProcessCategoryEnum.CATEGORY5.getValue().toString());
            processStartBO.setModuleName("住宅临时安置补助费");
        } else {
            processStartBO.setCategory(ProcessCategoryEnum.CATEGORY6.getValue().toString());
            processStartBO.setModuleName("停产停业经济损失补助费");
        }
 
        StateProject stateProject = stateProjectService.getProjectById(stateApplyRecord.getProjectId());
        String name = ObjectUtil.isEmpty(stateProject) ? "" : stateProject.getProjectName();
        processStartBO.setName(name);
 
        //http://122.9.167.128/zentao/bug-view-15459.html
        //需要显示发起申请人所在单位
        String cedName = SecurityUtils.getLoginUser().getUser().getDept().getDeptName();
        String remark = String.format("【镇/街】:%s,【征收实施单位】:%s,【申请金额】:%s万元", stateProject.getStreet(), cedName, stateApplyRecord.getAmount());
        processStartBO.setRemark(remark);
        Map<String, Object> variable = new HashMap<>();
        variable.put("recordId", stateApplyRecord.getId());
        variable.put("applyId", stateApplyRecord.getId());
        variable.put("projectId", stateApplyRecord.getProjectId());
        variable.put("applyType", stateApplyRecord.getApplyType());
        processStartBO.setVariable(variable);
        //开启工作流程
        stateProcessTemplateService.start(processStartBO);
 
        //保存请款子表
        newRecordItem.forEach(e -> e.setApplyId(stateApplyRecord.getId()));
        return stateApplyRecordItemService.saveOrUpdateBatch(newRecordItem);
    }
 
    /**
     * 通过项目id查询权利人
     * @param getApplyHouseholdOwnerBO
     * @return
     */
    @Override
    public ApplyHouseholdOwnerVO getApplyHouseholdOwner(GetApplyHouseholdOwnerBO getApplyHouseholdOwnerBO) {
        String projectId = getApplyHouseholdOwnerBO.getProjectId();
        String ownerName = getApplyHouseholdOwnerBO.getOwnerName();
        String street = getApplyHouseholdOwnerBO.getStreet();
 
        LambdaQueryWrapper<StateProject> lqwStateProject = new LambdaQueryWrapper<>();
        lqwStateProject.eq(StateProject::getId, projectId);
        //查询项目
        StateProject one = stateProjectService.getOne(lqwStateProject);
        if (Objects.isNull(one)) {
            return new ApplyHouseholdOwnerVO();
        }
        LambdaQueryWrapper<StateHousehold> lqwHousehold = new LambdaQueryWrapper<>();
        lqwHousehold.eq(StateHousehold::getStateProjectId, one.getId());
        if (org.apache.commons.lang3.StringUtils.isNotBlank(street)) {
            lqwHousehold.like(StateHousehold::getStreet, street);
        }
        //查询房产
        List<StateHousehold> householdList = stateHouseholdService.list(lqwHousehold);
        if (CollectionUtils.isEmpty(householdList)) {
            return new ApplyHouseholdOwnerVO();
        }
        LambdaQueryWrapper<StateHouseholdOwner> lqwHouseholdOwner = new LambdaQueryWrapper<>();
        List<String> houseIdList = householdList.stream().map(StateHousehold::getId).collect(Collectors.toList());
        lqwHouseholdOwner
//                .eq(StateHouseholdOwner::getOwnerType, ownerType)
                .in(StateHouseholdOwner::getStateHouseholdId, houseIdList);
        if (org.apache.commons.lang3.StringUtils.isNotBlank(ownerName)) {
            lqwHouseholdOwner.like(StateHouseholdOwner::getOwnerName, ownerName);
        }
        //查询权利人
        List<StateHouseholdOwner> ownerList = stateHouseholdOwnerService.list(lqwHouseholdOwner);
 
        if (CollectionUtils.isEmpty(ownerList)) {
            return new ApplyHouseholdOwnerVO();
        }
        //查询项目关联的补偿标准
        StateProjectCompensation compensation = stateProjectCompensationService.getById(one.getId());
 
        Map<String, StateHousehold> stateHouseholdMap = householdList.stream().collect(Collectors.toMap(StateHousehold::getId, Function.identity()));
        ApplyHouseholdOwnerVO applyHouseholdOwnerVO = new ApplyHouseholdOwnerVO();
        applyHouseholdOwnerVO.setProjectName(one.getProjectName());
        applyHouseholdOwnerVO.setProjectStartTime(one.getStartTime());
 
 
        if (Objects.nonNull(compensation)) {
            applyHouseholdOwnerVO.setTransitionFeeStandardName(compensation.getPolicySubsidy());
            Integer status = 0;
            if (ApplyTypeEnum.TEMPORARY_RESIDENTIAL_SUBSIDY.getValue().equals(getApplyHouseholdOwnerBO.getApplyType())) {
                status = CompensationCategoryEnum.TEMPORARY_HOUSING_ALLOWANCE.getCode();
            } else if (ApplyTypeEnum.BUSINESS_SHUTDOWN_SUBSIDY.getValue().equals(getApplyHouseholdOwnerBO.getApplyType())) {
                status = CompensationCategoryEnum.NON_RESIDENTIAL_ECONOMIC_LOSS_ALLOWANCE.getCode();
            }
            LambdaQueryWrapper<StateProjectCompensateStandard> lqwCompensateStandard = new LambdaQueryWrapper<>();
            lqwCompensateStandard.eq(StateProjectCompensateStandard::getStandardName, compensation.getPolicySubsidy())
                    .eq(StateProjectCompensateStandard::getCategory, status)
                    .eq(StateProjectCompensateStandard::getCompensateType, CompensateTypeEnum.POLICY_SUBSIDY_FEE.getCode());
            //查询项目补偿标准
            List<StateProjectCompensateStandard> list = stateProjectCompensateStandardService.list(lqwCompensateStandard);
            if (!CollectionUtils.isEmpty(list)) {
                StateProjectCompensateStandard stateProjectCompensateStandard = list.stream().findFirst().get();
                applyHouseholdOwnerVO.setDistributionStandard(stateProjectCompensateStandard.getCompensateStandard());
                applyHouseholdOwnerVO.setUnit(stateProjectCompensateStandard.getUnit());
            }
        }
        ownerList.sort(Comparator.comparing(BaseEntity::getCreateTime));
        List<ApplyOwnerBO> applyOwnerBOList = StateProjectConvert.INSTANCE.toApplyOwnerBOList(ownerList);
        Map<String, List<ApplyOwnerBO>> collect = applyOwnerBOList.stream().collect(Collectors.groupingBy(ApplyOwnerBO::getStateHouseholdId));
        applyOwnerBOList.forEach(e -> {
            StateHousehold stateHousehold = stateHouseholdMap.get(e.getStateHouseholdId());
            if (Objects.nonNull(stateHousehold)) {
                e.setStreet(stateHousehold.getStreet());
                e.setRealEstateCertificateNumber(stateHousehold.getRealEstateCertificateNumber());
            }
        });
        List<ApplyOwnerBO> newList = new ArrayList<>();
        collect.forEach((k, v) -> {
            ApplyOwnerBO applyOwnerBO = v.get(0);
            applyOwnerBO.setOwnerName(v.stream().map(ApplyOwnerBO::getOwnerName).collect(Collectors.joining("|")));
            applyOwnerBO.setStateHouseholdOwnerId(v.stream().map(ApplyOwnerBO::getStateHouseholdOwnerId).collect(Collectors.joining("|")));
            StateHousehold stateHousehold = stateHouseholdMap.get(applyOwnerBO.getStateHouseholdId());
            if (Objects.nonNull(stateHousehold)) {
                applyOwnerBO.setStreet(stateHousehold.getStreet());
                applyOwnerBO.setRealEstateCertificateNumber(stateHousehold.getRealEstateCertificateNumber());
            }
            newList.add(applyOwnerBO);
        });
        newList.sort(Comparator.comparing(ApplyOwnerBO::getRealEstateCertificateNumber));
        applyHouseholdOwnerVO.setApplyOwnerBOList(newList);
        return applyHouseholdOwnerVO;
    }
 
    /**
     * 分页查询一级台账
     * @param stateApplyRecordPageBO
     * @return
     */
    @Override
    public StateApplyRecordPageInfoLevelVO pageInfoLevel(StateApplyRecordPageInfoLevelBO stateApplyRecordPageBO) {
        List<StateApplyRecordVO> allStateApplyRecordVOList = this.getBaseMapper().selectApplyRecordWithProject(stateApplyRecordPageBO);
        if (CollectionUtils.isEmpty(allStateApplyRecordVOList)) {
            return new StateApplyRecordPageInfoLevelVO();
        }
        StateApplyRecordPageInfoLevelVO vo = new StateApplyRecordPageInfoLevelVO();
        int totalCount = allStateApplyRecordVOList.stream().mapToInt(StateApplyRecord::getHouseholdCount).sum();
        BigDecimal totalAmount = allStateApplyRecordVOList.stream()
                .filter(Objects::nonNull)
                .filter(data -> Objects.nonNull(data.getAmount()) && data.getApplyType().equals(stateApplyRecordPageBO.getApplyType()))
                .map(StateApplyRecord::getAmount)  // 将 mapToInt 替换为 map 以处理 BigDecimal
                .reduce(BigDecimal.ZERO, BigDecimal::add);  // 使用 reduce 方法累加
 
        vo.setTotalCount(totalCount);
 
        // 使用 BigDecimal 进行除法并转换为字符串,保留 "万" 的单位
        vo.setTotalAmount(totalAmount.setScale(4, BigDecimal.ROUND_HALF_UP) + "万");
 
        PageUtils.startPage();
        List<StateApplyRecordVO> stateApplyRecordVOS = this.getBaseMapper().selectApplyRecordWithProject(stateApplyRecordPageBO);
        if (CollectionUtils.isEmpty(stateApplyRecordVOS)) {
            return vo;
        }
        PageInfo<StateApplyRecordVO> stateApplyRecordVOPageInfo = new PageInfo<>(stateApplyRecordVOS);
        List<String> projectIdList = stateApplyRecordVOPageInfo.getList().stream().map(StateApplyRecordVO::getProjectId).collect(Collectors.toList());
 
        Map<String, StateProjectCompensateStandard> projectCompensationStandardMap = getProjectCompensationStandardMap(projectIdList);
 
        stateApplyRecordVOS.forEach(e -> {
            StateProjectCompensateStandard compensationStandard = projectCompensationStandardMap.get(e.getProjectId());
            if (Objects.nonNull(compensationStandard)) {
                e.setTransitionFee(compensationStandard.getCompensateStandard());
                e.setUnit(compensationStandard.getUnit());
            }
            if (Objects.nonNull(e.getTransitionFeeStartDate()) && Objects.nonNull(e.getTransitionFeeEndDate())) {
                int i = DateUtils.differentMonthsByDate(e.getTransitionFeeStartDate(), e.getTransitionFeeEndDate());
                e.setDistributionMonths(i);
            }
        });
        stateApplyRecordVOS.sort(Comparator.comparing(BaseEntity::getCreateTime));
        stateApplyRecordVOPageInfo.setList(stateApplyRecordVOS);
        vo.setPageInfo(stateApplyRecordVOPageInfo);
        return vo;
    }
 
    private Map<String, StateProjectCompensateStandard> getProjectCompensationStandardMap(List<String> projectIdList) {
        // 查询项目的补偿信息
        LambdaQueryWrapper<StateProjectCompensation> lqwCompensate = new LambdaQueryWrapper<>();
        lqwCompensate.in(StateProjectCompensation::getProjectId, projectIdList);
        List<StateProjectCompensation> compensationList = stateProjectCompensationService.list(lqwCompensate);
 
        if (CollectionUtils.isEmpty(compensationList)) {
            return new HashMap<>();
        }
 
        // 根据补偿信息查询对应的补偿标准
        List<String> policySubsidyList = compensationList.stream()
                .map(StateProjectCompensation::getPolicySubsidy)
                .collect(Collectors.toList());
 
        LambdaQueryWrapper<StateProjectCompensateStandard> lqwCompensateStandard = new LambdaQueryWrapper<>();
        lqwCompensateStandard.in(StateProjectCompensateStandard::getStandardName, policySubsidyList)
                .eq(StateProjectCompensateStandard::getCategory, CompensationCategoryEnum.TEMPORARY_HOUSING_ALLOWANCE.getCode())
                .eq(StateProjectCompensateStandard::getCompensateType, CompensateTypeEnum.POLICY_SUBSIDY_FEE.getCode());
 
        List<StateProjectCompensateStandard> standardList = stateProjectCompensateStandardService.list(lqwCompensateStandard);
 
        // 生成项目ID到补偿标准的映射表
        return compensationList.stream().collect(Collectors.toMap(
                StateProjectCompensation::getProjectId,
                compensation -> {
                    Optional<StateProjectCompensateStandard> standardOpt = standardList.stream()
                            .filter(standard -> standard.getStandardName().equals(compensation.getPolicySubsidy()))
                            .findFirst();
                    return standardOpt.get();
                }
        ));
    }
 
    /**
     * 住宅临时安置补助费户详情或者 或者 停产停业补助费详情
     * @param getApplyHouseholdOwnerBO
     * @return
     */
    @Override
    public PageInfo<ApplyOwnerBO> getApplyHouseholdOwnerInfo(GetApplyHouseholdOwnerBO getApplyHouseholdOwnerBO) {
        PageUtils.startPage();
        //TODO 查询请款记录 由于之前产品设计的问题是以权利人作为维度的即一个请款对应是 一个权利人, 后期又修改成了 以房产为 维度 即一个请款对应一个房产 导致之前数据库设计有问题
        List<ApplyOwnerBO> applyHouseholdOwnerVOS = this.getBaseMapper().selectApplyRecordWithOwnerDistinctPage(getApplyHouseholdOwnerBO);
 
        if (CollectionUtils.isEmpty(applyHouseholdOwnerVOS)) {
            return new PageInfo<>();
        }
        getApplyHouseholdOwnerBO.setStateHouseIdList(applyHouseholdOwnerVOS.stream().map(ApplyOwnerBO::getStateHouseholdId).collect(Collectors.toList()));
        PageInfo<ApplyOwnerBO> applyOwnerBOPageInfo = new PageInfo<>(applyHouseholdOwnerVOS);
        //TODO
        List<ApplyOwnerBO> applyOwnerBOS = this.getBaseMapper().selectApplyRecordWithOwnerPage(getApplyHouseholdOwnerBO);
        applyOwnerBOPageInfo.setList(applyOwnerBOS);
        List<String> projectIdList = applyOwnerBOPageInfo.getList().stream().map(ApplyOwnerBO::getProjectId).collect(Collectors.toList());
 
        List<String> houseIdList = applyOwnerBOPageInfo.getList().stream().map(ApplyOwnerBO::getStateHouseholdId).collect(Collectors.toList());
        List<String> ownerIdList = applyOwnerBOPageInfo.getList().stream().map(ApplyOwnerBO::getStateHouseholdOwnerId).collect(Collectors.toList());
 
        List<StateHousehold> stateHouseholds = stateHouseholdService.listByIds(houseIdList);
        Map<String, String> realNumber = stateHouseholds
                .stream().filter(Objects::nonNull)
                .collect(Collectors.toMap(StateHousehold::getId, StateHousehold::getRealEstateCertificateNumber));
        LambdaQueryWrapper<StateApplyRecordItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(StateApplyRecordItem::getStateHouseholdOwner, ownerIdList);
        List<StateApplyRecordItem> list = stateApplyRecordItemService.list(queryWrapper);
 
 
        LambdaQueryWrapper<StateApplyRecord> stateApplyRecordQueryWrapper = new LambdaQueryWrapper<>();
//        stateApplyRecordQueryWrapper.in(StateApplyRecord::getApplyStatus, SubmitStatusEnum.ACCEPT.getValue());
        stateApplyRecordQueryWrapper.eq(StateApplyRecord::getApplyType, getApplyHouseholdOwnerBO.getApplyType());
        List<StateApplyRecord> stateApplyRecordList = stateApplyRecordService.list(stateApplyRecordQueryWrapper);
        if (!CollectionUtils.isEmpty(stateApplyRecordList)) {
            list = list.stream().filter(e -> stateApplyRecordList.stream().map(StateApplyRecord::getId).collect(Collectors.toList()).contains(e.getApplyId())).collect(Collectors.toList());
        }
 
 
        Map<String, Map<String, List<StateApplyRecordItem>>> itemMap;
        if (CollectionUtils.isEmpty(list)) {
            itemMap = new HashMap<>();
        } else {
            itemMap = list.stream().collect(Collectors.groupingBy(StateApplyRecordItem::getApplyId, Collectors.groupingBy(StateApplyRecordItem::getStateHouseholdId)));
        }
//        List<StateProject> stateProjects = stateProjectService.listByIds(projectIdList);
 
//        Map<String, StateProject> stateProjectMap = stateProjects.stream().collect(Collectors.toMap(StateProject::getId, Function.identity()));
 
        Map<String, StateProjectCompensateStandard> projectCompensationStandardMap = getProjectCompensationStandardMap(projectIdList);
        Map<String, List<ApplyOwnerBO>> stateHouseRecordMap = applyOwnerBOS.stream().collect(Collectors.groupingBy(ApplyOwnerBO::getStateHouseholdId));
 
        List<ApplyOwnerBO> newList = new ArrayList<>();
        stateHouseRecordMap.forEach((k, v) -> {
            ApplyOwnerBO applyOwnerBO = v.get(0);
            applyOwnerBO.setOwnerName(v.stream().map(ApplyOwnerBO::getOwnerName).collect(Collectors.joining("|")));
            applyOwnerBO.setStateHouseholdOwnerId(v.stream().map(ApplyOwnerBO::getStateHouseholdOwnerId).collect(Collectors.joining("|")));
            newList.add(applyOwnerBO);
        });
        for (ApplyOwnerBO e : newList) {
            StateProjectCompensateStandard compensationStandard = projectCompensationStandardMap.get(e.getProjectId());
            if (Objects.nonNull(compensationStandard)) {
                e.setTransitionFee(compensationStandard.getCompensateStandard());
                e.setUnit(compensationStandard.getUnit());
            }
            String[] ownerIdArray = e.getStateHouseholdOwnerId().split("\\|");
            if (0 != ownerIdArray.length) {
                List<StateApplyRecordItem> stateApplyRecordItems = new ArrayList<>();
 
                itemMap.forEach((k, v) -> {
                    if (!CollectionUtils.isEmpty(v.get(e.getStateHouseholdId()))) {
                        stateApplyRecordItems.add(v.get(e.getStateHouseholdId()).get(0));
                    }
                });
 
                if (!CollectionUtils.isEmpty(stateApplyRecordItems)) {
                    e.setTotalDistributionAmount(String.valueOf(
                            stateApplyRecordItems.stream()
                                    .map(item -> new BigDecimal(item.getDistributionAmount())) // 将 String 转为 BigDecimal
                                    .reduce(BigDecimal.ZERO, BigDecimal::add) // 求和
                                    .setScale(2, RoundingMode.HALF_UP) // 四舍五入保留两位小数
                    ));
                }
            }
            //原本原型是已发放月数
//            if (Objects.nonNull(e.getStartDistributionTime())) {
//                int i = DateUtils.differentMonthsByDate(e.getStartDistributionTime(), new Date());
//                e.setAlreadyDistributionMonths(i);
//            }
            if (realNumber.containsKey(e.getStateHouseholdId())) {
                e.setRealEstateCertificateNumber(realNumber.get(e.getStateHouseholdId()));
            }
            e.setAlreadyDistributionMonths(Integer.valueOf(e.getDistributionMonths()));
 
//            if (stateProjectMap.containsKey(e.getProjectId())) {
//                e.setStreet(stateProjectMap.get(e.getProjectId()).getStreet());
//            }
        }
        applyOwnerBOPageInfo.setList(newList);
        return applyOwnerBOPageInfo;
    }
 
    /**
     * 任务中心-请帐详情
     * @param getApplyHouseholdOwnerBO
     * @return
     */
    @Override
    public ApplyBO stateApplyRecordDetail(GetApplyHouseholdOwnerBO getApplyHouseholdOwnerBO) {
        StateApplyRecord stateApplyRecord = this.getById(getApplyHouseholdOwnerBO.getApplyId());
        if (Objects.isNull(stateApplyRecord)) {
            return null;
        }
        ApplyBO applyBO = BeanUtil.toBean(stateApplyRecord, ApplyBO.class);
        StateProject stateProject = stateProjectService.getProjectById(stateApplyRecord.getProjectId());
        if (ObjectUtil.isNotEmpty(stateProject)) {
            applyBO.setProjectName(stateProject.getProjectName());
        }
        return applyBO;
    }
 
    /**
     * 导入权利人
     * @param importApplyRecordBO
     * @return
     */
    @Override
    public List<ApplyOwnerBO> importOwnerByFile(ImportApplyRecordBO importApplyRecordBO) {
        List<ApplyOwnerVO> dataList;
        try {
            dataList = EasyExcelFactory.read(FileUtil.convertToFile(importApplyRecordBO.getFile()))
                    .head(ApplyOwnerVO.class)
                    .sheet()
                    .doReadSync();
        } catch (IOException e) {
            throw new GlobalException("读取Excel文件失败");
        }
 
        if (CollectionUtils.isEmpty(dataList)) {
            throw new GlobalException("读取Excel文件失败");
        }
//        for (ApplyOwnerVO applyOwnerVO : dataList) {
//            if (applyOwnerVO.getOwnerName().contains("|")) {
//                throw new GlobalException("Excel文件不需要带上 | 写全名称,aa | bb 选择其一即可");
//            }
//        }
        LambdaQueryWrapper<StateProject> lqwStateProject = new LambdaQueryWrapper<>();
        lqwStateProject.eq(StateProject::getId, importApplyRecordBO.getProjectId());
        StateProject one = stateProjectService.getOne(lqwStateProject);
 
        if (Objects.isNull(one)) {
            throw new GlobalException("未查询到相应项目");
        }
        //查询项目下的所有房产
        LambdaQueryWrapper<StateHousehold> lqwHousehold = new LambdaQueryWrapper<>();
        lqwHousehold.eq(StateHousehold::getStateProjectId, one.getId());
 
        List<StateHousehold> householdList = stateHouseholdService.list(lqwHousehold);
 
        if (CollectionUtils.isEmpty(householdList)) {
            throw new GlobalException("未查询到相应房产");
        }
        List<String> realEstateCertificateNumberInDb = householdList.stream().map(StateHousehold::getRealEstateCertificateNumber).collect(Collectors.toList());
        List<String> realEstateCertificateNumberInExcel = dataList.stream().map(ApplyOwnerVO::getRealEstateCertificateNumber).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(realEstateCertificateNumberInExcel)) {
            throw new GlobalException("所有不动产号都为空");
        }
        realEstateCertificateNumberInExcel.removeAll(realEstateCertificateNumberInDb);
        if (!CollectionUtils.isEmpty(realEstateCertificateNumberInExcel)) {
            String number = realEstateCertificateNumberInExcel.stream().collect(Collectors.joining(","));
            throw new GlobalException(String.format("这些不动产号不存在:%s", number));
        }
 
        LambdaQueryWrapper<StateHouseholdOwner> lqwHouseholdOwner = new LambdaQueryWrapper<>();
        List<String> houseIdList = householdList.stream().map(StateHousehold::getId).collect(Collectors.toList());
        lqwHouseholdOwner.in(StateHouseholdOwner::getStateHouseholdId, houseIdList);
 
        List<StateHouseholdOwner> ownerList = stateHouseholdOwnerService.list(lqwHouseholdOwner);
 
        if (CollectionUtils.isEmpty(ownerList)) {
            throw new GlobalException("未查询到相应房产权利人");
        }
 
        //TODO 废弃代码开始
        StateApplyRecord stateApplyRecord;
        if (org.apache.commons.lang3.StringUtils.isBlank(importApplyRecordBO.getApplyId())) {
            stateApplyRecord = new StateApplyRecord();
            stateApplyRecord.setApplyType(importApplyRecordBO.getApplyType());
            stateApplyRecord.setApplyStatus(SubmitStatusEnum.PENDING_REVIEW.getValue());
            stateApplyRecord.setProjectId(importApplyRecordBO.getProjectId());
            stateApplyRecord.setAmount(importApplyRecordBO.getAppliedAmount());
            stateApplyRecord.setRecordNo(one.getProjectNo());
            stateApplyRecord.setTransitionFeeEndDate(importApplyRecordBO.getTransitionFeeEndDate());
            if (SecurityUtils.getLoginUser() != null && SecurityUtils.getLoginUser().getUser() != null) {
                if (SecurityUtils.getLoginUser().getUser().getNickName() != null) {
                    stateApplyRecord.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());
                }
 
                if (SecurityUtils.getLoginUser().getUser().getDept() != null &&
                        SecurityUtils.getLoginUser().getUser().getDept().getDeptName() != null) {
                    stateApplyRecord.setApplyDepartment(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
                }
            }
//            this.save(stateApplyRecord);
        } else {
            stateApplyRecord = new StateApplyRecord();
            stateApplyRecord.setId(importApplyRecordBO.getApplyId());
        }
        //废弃代码结束
        List<ApplyOwnerBO> itemList = new ArrayList<>();
        ApplyOwnerBO applyOwnerBO;
        StateApplyRecordItem item;
        //根据导入数据的不动产权证号筛选出同项目下的房产数据
        List<StateHousehold> stateHouseholds = householdList.stream().filter(e -> dataList.stream().map(ApplyOwnerVO::getRealEstateCertificateNumber)
                .collect(Collectors.toList()).contains(e.getRealEstateCertificateNumber())).collect(Collectors.toList());
        ownerList.sort(Comparator.comparing(BaseEntity::getCreateTime));
        //权利人根据房产id分组
        Map<String, List<StateHouseholdOwner>> ownerMap = ownerList.stream().collect(Collectors.groupingBy(StateHouseholdOwner::getStateHouseholdId));
        //导入数据根据不同不动产权证号分组
        Map<String, List<ApplyOwnerVO>> importMap = dataList.stream().collect(Collectors.groupingBy(ApplyOwnerVO::getRealEstateCertificateNumber));
        DecimalFormat decimalFormat = new DecimalFormat("##0.####");
        for (StateHousehold stateHousehold : stateHouseholds) {
            List<StateHouseholdOwner> owners = ownerMap.get(stateHousehold.getId());
            if (!CollectionUtils.isEmpty(owners)) {
                applyOwnerBO = StateProjectConvert.INSTANCE.toApplyOwnerBO(owners.get(0));//TODO 对象拷贝时new 了对象的
                applyOwnerBO.setOwnerName(owners.stream().map(StateHouseholdOwner::getOwnerName).collect(Collectors.joining("|"))); // 多个权利人用|分割
                applyOwnerBO.setStateHouseholdOwnerId(owners.stream().map(StateHouseholdOwner::getId).collect(Collectors.joining("|"))); //多个权利人id以|分割
 
                applyOwnerBO.setStreet(stateHousehold.getStreet());
                applyOwnerBO.setRealEstateCertificateNumber(stateHousehold.getRealEstateCertificateNumber());
                List<ApplyOwnerVO> applyOwnerVOS = importMap.get(applyOwnerBO.getRealEstateCertificateNumber());
                if (!CollectionUtils.isEmpty(applyOwnerVOS)) {
                    applyOwnerBO.setStartDistributionTime(applyOwnerVOS.get(0).getStartDistributionTime());
                    applyOwnerBO.setEstimatedEndDistributionTime(applyOwnerVOS.get(0).getEstimatedEndDistributionTime());
                    applyOwnerBO.setDistributionMonths(String.valueOf((int) Double.parseDouble(applyOwnerVOS.get(0).getDistributionMonths())));
                    applyOwnerBO.setDistributionAmount(decimalFormat.format(new BigDecimal(applyOwnerVOS.get(0).getDistributionAmount())));
                    applyOwnerBO.setAdditionalDistributionDays(String.valueOf((int) Double.parseDouble(applyOwnerVOS.get(0).getAdditionalDistributionDays())));
                }
                itemList.add(applyOwnerBO);
 
            }
        }
 
//        Map<String, List<ApplyOwnerVO>> dataMap = itemList.stream().collect(Collectors.groupingBy());
//        stateApplyRecordItemService.saveBatch(itemList);
        itemList.sort(Comparator.comparing(ApplyOwnerBO::getRealEstateCertificateNumber));
        return itemList;
    }
 
    @Override
    public void updateApplyStatus(String recordId, Integer status) {
        LambdaUpdateWrapper<StateApplyRecord> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StateApplyRecord::getId, recordId)
                .set(StateApplyRecord::getApplyStatus, status);
        this.update(updateWrapper);
    }
}