Pu Zhibing
2025-04-22 d138293736414a314467a2641e6116ff263ead48
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
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.ObjectUtil;
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.PlacementApplyMapper;
import com.ruoyi.bussiness.object.request.placementApply.*;
import com.ruoyi.bussiness.object.response.placementApply.*;
import com.ruoyi.bussiness.service.*;
import com.ruoyi.bussiness.utils.BatchNumberUtils;
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.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
 
 
@Service
public class PlacementApplyServiceImpl extends ServiceImpl<PlacementApplyMapper, PlacementApply> implements PlacementApplyService {
 
    @Autowired
    private PlacementApplyRecordService placementApplyRecordService;
    @Autowired
    private PlacementService placementService;;
    @Autowired
    private CompensateService compensateService;
    @Autowired
    private ISysNoticeService sysNoticeService;
    @Autowired
    private PlacementErrorService placementErrorService;
 
    @Override
    public PlacementApplyPageResponse page(PlacementApplyPageRequest request) {
        Page<PlacementApply> page = new Page<>(request.getPageNum(), request.getPageSize());
        LambdaQueryWrapper<PlacementApply> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(request.getBatchNumber())) {
            queryWrapper.like(PlacementApply::getBatchNumber, request.getBatchNumber());
        }
        if(ObjUtil.isNotEmpty(request.getStatus())){
            queryWrapper.eq(PlacementApply::getStatus,request.getStatus());
        }
        Page<PlacementApply> applyPage = this.page(page, queryWrapper);
 
        PlacementApplyPageResponse response = new PlacementApplyPageResponse();
        response.setTotal(applyPage.getTotal());
        response.setRecords(applyPage.getRecords());
        return response;
    }
 
    @Override
    public PlacementApplyRecordPageResponse detail(PlacementApplyRecordPageRequest request) {
        //查询申请表
        PlacementApply placementApply = this.getById(request.getApplyId());
        if (ObjUtil.isEmpty(placementApply)) {
            throw new GlobalException("申请表不存在!");
        }
 
        //查询详情
        Page<PlacementApplyRecord> applies = placementApplyRecordService.detail(request);
        PlacementApplyRecordPageResponse response = new PlacementApplyRecordPageResponse();
        response.setPlacementApply(placementApply);
        response.setRecords(applies.getRecords());
        response.setTotal(applies.getTotal());
        return response;
    }
 
    @Override
    public void del(PlacementApplyIdRequest request) {
        this.removeById(request.getId());
        placementApplyRecordService.remove(new LambdaQueryWrapper<PlacementApplyRecord>().
                eq(PlacementApplyRecord::getPlacementApplyId, request.getId()));
    }
 
    @Override
    public void dataApprove(PlacementApplyIdRequest request) {
        PlacementApply placementApply = this.getById(request.getId());
        if (ObjUtil.isEmpty(placementApply)) {
            throw new GlobalException("数据不存在");
        }
        if(placementApply.getStatus() != 0){
            throw new GlobalException("数据已审核通过!");
        }
        dataTag(placementApply);
    }
 
    @Override
    public void recordCheck(PlacementApplyRecordIdRequest request) {
        LambdaUpdateWrapper<PlacementApplyRecord>  updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(PlacementApplyRecord::getChecks,1);
        updateWrapper.set(PlacementApplyRecord::getIdCardExistsWarn,0);
        updateWrapper.set(PlacementApplyRecord::getIdCardNoWarn,0);
        updateWrapper.set(PlacementApplyRecord::getWaitFamilyNamesWarn,0);
        updateWrapper.set(PlacementApplyRecord::getWaitFamilyNamesNoWarn,0);
        updateWrapper.set(PlacementApplyRecord::getWaitFamilyAreaWarn,0);
        updateWrapper.set(PlacementApplyRecord::getCompensationSumWarn,0);
        updateWrapper.set(PlacementApplyRecord::getCompensationAmountWarn,0);
        updateWrapper.set(PlacementApplyRecord::getQuarterPayAmountWarn,0);
        updateWrapper.set(PlacementApplyRecord::getSubsidyAmountWarn,0);
 
        updateWrapper.eq(PlacementApplyRecord::getId,request.getId());
        placementApplyRecordService.update(updateWrapper);
 
    }
 
    /**
     * 数据标注
     * @param placementApply
     */
    private void dataTag(PlacementApply placementApply) {
        if (ObjUtil.isEmpty(placementApply)) {
            return;
        }
        //数据已经审核过了,不需要标注
        if (placementApply.getStatus() != 0) {
            return;
        }
        List<PlacementApplyRecord> records = placementApplyRecordService.allData(placementApply.getId());
        if (ObjUtil.isEmpty(records)) {
            return;
        }
        for (PlacementApplyRecord record : records) {
            if(record.getChecks() == 1){
                //已核对直接跳过
                continue;
            }
            //户主身份证查询
            int count = placementService.countPlacementByIdCard(record.getIdCard());
            if (count == 0) {
                //安置表户主信息不存在
                record.setIdCardExistsWarn(1);
            }else{
                record.setIdCardExistsWarn(0);
            }
            //判断户主是否重名
            if (ObjUtil.isNotEmpty(record.getIdCard())) {
                boolean exists = false;
                int counts = selectNameExists(record.getHouseholdHead(), record.getIdCard());
                if (counts > 1) {
                    exists = true;
                }
                record.setIdCardNoWarn(exists ? 1 : 0);
            }
            //判断家庭成员是否重复
            if (ObjUtil.isNotEmpty(record.getWaitFamilyNames())) {
                String[] familyNames = record.getWaitFamilyNames().split("、");
                if (ObjUtil.isEmpty(familyNames)) {
                    record.setIdCardNoWarn(0);
                } else {
                    boolean exists = false;
                    boolean familyWarn = false;
                    for (String familyName : familyNames) {
                        //查询待安置成员是否在安置库
                        if (placementService.countNamesExists(familyName) == 0) {
                            familyWarn = true;
                            break;
                        }
                    }
                    //重复
                    for (String familyName : familyNames) {
                        int counts = selectFamilyNameExists(familyName);
                        if (counts > 1) {
                            exists = true;
                            break;
                        }
                    }
                    record.setWaitFamilyNamesWarn(exists ? 1 : 0);
                    record.setWaitFamilyNamesNoWarn(familyWarn ? 1 : 0);
                }
            }
            //应安置面积警告标注(新版本警告)
            BigDecimal waitFamilyArea = record.getWaitFamilyArea();
//            Integer currentCollectiveNum = record.getCurrentCollectiveNum();
//            Integer currentNoCollectiveNum = record.getCurrentNoCollectiveNum();
            boolean warn = compensateService.compensateSettleAreaCalculate(record.getOrgArea(),record.getNoOrgArea(), waitFamilyArea);
            if (warn) {
                record.setWaitFamilyAreaWarn(0);
            }else{
                record.setWaitFamilyAreaWarn(1);
            }
 
            //赔偿金额(判断新购房还是二手房,这两个只能存在一个)
            if (record.getCompensationNewAmount().compareTo(BigDecimal.ZERO) == 0 &&
                    record.getCompensationOldAmount().compareTo(BigDecimal.ZERO) == 0) {
                record.setCompensationAmountWarn(1);
            }else{
                record.setCompensationAmountWarn(0);
            }
            if (record.getCompensationNewAmount().compareTo(BigDecimal.ZERO) > 0 &&
                    record.getCompensationOldAmount().compareTo(BigDecimal.ZERO) > 0) {
                record.setCompensationAmountWarn(1);
            }else{
                record.setCompensationAmountWarn(0);
            }
 
            //自主购房补贴、过渡补贴(如果购房情况异常则不判定自主购房补贴是否异常,自主购房补贴、过渡补贴 依赖赔偿金额信息)
            if (record.getCompensationAmountWarn() == 0) {
                boolean warnFlag = compensateService.compensateBuyCalculateV2(record.getDemolitionTime(),
                        record.getStreet(),
                        record.getOrgArea(), record.getNoOrgArea(),
                        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);
        }
        placementApplyRecordService.saveOrUpdateBatch(records);
    }
 
 
    /**
     * 统计数据
     */
    public void countError(PlacementApply placementApply) {
//        List<Map<String, Object>> error = placementApplyRecordService.countError(placementApply.getId());
//        if (ObjUtil.isEmpty(error)) {
//            return;
//        }
//        for (Map<String, Object> map : error) {
//            String street = map.get("street").toString();
//            PlacementError placementError = placementErrorService.getErrorByStreet(street);
//            placementError.setErrorNum(placementError.getErrorNum() + NumberUtil.parseInt(map.get("errorNum").toString()));
//            placementError.setSettleNum(placementError.getSettleNum() + NumberUtil.parseInt(map.get("settleNum").toString()));
//            placementError.setAreaNum(placementError.getAreaNum() + NumberUtil.parseInt(map.get("areaNum").toString()));
//            placementError.setCompensationNum(placementError.getCompensationNum() + NumberUtil.parseInt(map.get("compensationNum").toString()));
//            placementErrorService.updateById(placementError);
//        }
        //查询当前批次信息
        List<PlacementApplyRecord> placementApplies = placementApplyRecordService.allData(placementApply.getId());
 
        if(ObjUtil.isNotEmpty(placementApplies)){
            List<PlacementError> placementErrors = new ArrayList<>();
            /**
             * sum(id_card_exists_warn+id_card_no_warn+wait_family_names_warn+wait_family_names_no_warn) as settleNum,
             *                sum(wait_family_area_warn) as areaNum,
             *                sum(compensation_sum_warn+quarter_pay_amount_warn+subsidy_amount_warn) compensationNum
             */
            for(PlacementApplyRecord applyRecord : placementApplies){
                PlacementError placementError = new PlacementError();
 
                Integer settleNum = applyRecord.getIdCardExistsWarn() + applyRecord.getIdCardNoWarn()
                        + applyRecord.getWaitFamilyNamesNoWarn()+applyRecord.getWaitFamilyNamesWarn();
 
                Integer areaNum = applyRecord.getWaitFamilyAreaWarn();
 
                Integer compensationNum = applyRecord.getSubsidyAmountWarn()+
                        applyRecord.getCompensationSumWarn()+applyRecord.getQuarterPayAmountWarn()
                        +applyRecord.getDownPaymentAmountWarn();
 
                Integer errorNum = settleNum + areaNum + compensationNum;
 
                placementError.setStreet(applyRecord.getStreet());
                placementError.setErrorNum(errorNum);
                placementError.setSettleNum(settleNum);
                placementError.setAreaNum(areaNum);
                placementError.setCompensationNum(compensationNum);
                placementErrors.add(placementError);
            }
            placementErrorService.saveBatch(placementErrors);
        }
 
 
 
 
    }
 
    @Override
    public void approve(PlacementApplyApproveRequest request) {
        //查询申请表
        PlacementApply placementApply = this.getById(request.getApplyId());
        if (ObjUtil.isEmpty(placementApply)) {
            throw new GlobalException("申请表不存在!");
        }
        if (request.getStatus() != 1 && request.getStatus() != 2) {
            throw new GlobalException("审核状态不正确");
        }
        if (placementApply.getStatus() != 0) {
            throw new GlobalException("已经审核过了");
        }
 
        //修改审核状态
        placementApply.setStatus(request.getStatus());
        //原因
        placementApply.setReason(request.getReason());
        placementApply.setApproveTime(new Date());
        placementApply.setApproveName(SecurityUtils.getLoginUser().getUser().getNickName());
        placementApply.setApproveId(SecurityUtils.getUserId());
        this.updateById(placementApply);
 
        //同步待审核消息
        new Thread(() -> {
            syncApplyMessage();
        }).start();
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void imports(PlacementImportsRequest request) {
        File file = FileUtils.convertToFile(request.getFile());
        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<PlacementApplyTemplateResponse> dataList;
        try {
            dataList = EasyExcelFactory.read(file)
                    .head(PlacementApplyTemplateResponse.class)
                    .sheet()
                    .doReadSync();
        } catch (Exception e) {
            throw new GlobalException("读取Excel文件失败:" + e.getCause());
        }
 
        if (CollectionUtils.isEmpty(dataList)) {
            throw new GlobalException("未解析出数据");
        }
 
        //导入数据,直接导入库
        List<PlacementApplyRecord> records = BeanUtil.copyToList(dataList, PlacementApplyRecord.class);
        //校验数据(身份证必填)
        for(PlacementApplyRecord record : records) {
            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.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("请输入过渡补贴");
            }
        }
 
        //汇总数据准备
        PlacementApply placementApply;
        if (ObjUtil.isEmpty(request.getApplyId())) {
            //保存批次信息
            placementApply = new PlacementApply();
            String lastBatchNumber = selectLastBatchNumber();
            String batchNumber = BatchNumberUtils.getBatchNumber(lastBatchNumber);
            placementApply.setBatchNumber(batchNumber);
            placementApply.setCreateId(SecurityUtils.getUserId());
            placementApply.setCreateUser(SecurityUtils.getLoginUser().getUser().getNickName());
            this.save(placementApply);
        } else {
            //获取旧的批次信息
            placementApply = this.getById(request.getApplyId());
        }
 
        if (ObjUtil.isEmpty(placementApply)) {
            throw new GlobalException("申请表信息不存在!");
        }
 
        //添加数据
        for(PlacementApplyRecord record : records) {
            record.setPlacementApplyId(placementApply.getId());
            record.setCreateId(SecurityUtils.getUserId());
            record.setCreateUser(SecurityUtils.getLoginUser().getUser().getNickName());
        }
        //保存数据
        placementApplyRecordService.saveBatch(records);
        //计算汇总数据
        buildSumApplyRecord(placementApply);
        //同步待审核消息
        new Thread(() -> {
            //标注数据(数据太多有风险)
            placementApply.setStatus(0);
            dataTag(placementApply);
            //统计数据
            countError(placementApply);
            syncApplyMessage();
        }).start();
    }
 
    @Override
    public void recordAdd(PlacementApplyRecord request) {
        placementApplyRecordService.save(request);
    }
 
    @Override
    public PlacementApplyRecord recordDetail(PlacementApplyIdRequest request) {
        return placementApplyRecordService.getById(request.getId());
    }
 
 
    /**
     * 更新消息
     */
    private void syncApplyMessage() {
        SysNotice sysNotice = sysNoticeService.getById(1);
        if (ObjUtil.isEmpty(sysNotice)) {
            return;
        }
        LambdaQueryWrapper<PlacementApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlacementApply::getStatus, 0);
        List<PlacementApply> applies = this.list(queryWrapper);
        if (ObjUtil.isEmpty(applies)) {
            return;
        }
        List<Long> applyIds = applies.stream().map(PlacementApply::getId).collect(Collectors.toList());
 
        LambdaQueryWrapper<PlacementApplyRecord> countQuery = new LambdaQueryWrapper<>();
        countQuery.in(PlacementApplyRecord::getPlacementApplyId, applyIds);
        int count = NumberUtil.parseInt(String.valueOf(placementApplyRecordService.count(countQuery)));
        String msg = MessageTypeEnum.PLACEMENT_APPLY.getMessage();
        sysNotice.setNoticeTitle(StrUtil.format(msg, Integer.toString(count)));
        sysNotice.setCountNum(new BigDecimal(count));
        sysNoticeService.updateById(sysNotice);
    }
 
    @Override
    public void recordDel(PlacementApplyRecordIdRequest request) {
        PlacementApplyRecord placementApplyRecord = placementApplyRecordService.getById(request.getId());
        if(ObjUtil.isEmpty(placementApplyRecord)){
            throw new GlobalException("记录不存在");
        }
        //查询当前记录是否已经审核
        PlacementApply placementApply = this.getById(placementApplyRecord.getPlacementApplyId());
        if(ObjUtil.isEmpty(placementApply)){
            throw new GlobalException("自主购房安置申请批次表不存在");
        }
        if(placementApply.getStatus() == 1){
            throw new GlobalException("自主购房安置申请批次表已审核通过!");
        }
        placementApplyRecordService.removeById(request.getId());
        //重新汇总数据
        buildSumApplyRecord(placementApply);
    }
 
    @Override
    public void recordEdit(PlacementApplyRecord request) {
        //查询当前记录是否已经审核
        PlacementApply placementApply = this.getById(request.getPlacementApplyId());
        if (ObjUtil.isEmpty(placementApply)) {
            throw new GlobalException("自主购房安置申请批次表不存在");
        }
        if (placementApply.getStatus() == 1) {
            throw new GlobalException("自主购房安置申请批次表已审核通过!");
        }
        placementApplyRecordService.updateById(request);
        //重新汇总数据
        buildSumApplyRecord(placementApply);
        dataTag(placementApply);
    }
 
    @Override
    public void problemExport(ApplyProblemExportRequest request, HttpServletResponse response) {
        try {
            //查询记录明细
            List<PlacementApplyRecord> applyRecords = placementApplyRecordService.problemList(request);
            List<ProblemExportResponse> data = BeanUtil.copyToList(applyRecords, ProblemExportResponse.class);
 
            FileUtils.setExcelResponseHeader(response, "问题数据.xlsx");
            ExcelWriterBuilder write = EasyExcelFactory.write(response.getOutputStream(), ProblemExportResponse.class);
            write.sheet("sheet").doWrite(data);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        } catch (Exception e) {
            throw new GlobalException("导出问题数据失败");
        }
    }
 
    @Override
    public void allExport(ApplyAllExportRequest request, HttpServletResponse response) {
        try {
            //查询记录明细
            List<PlacementApplyRecord> applyRecords = placementApplyRecordService.passList(request);
            List<ProblemExportResponse> data = BeanUtil.copyToList(applyRecords, ProblemExportResponse.class);
 
            FileUtils.setExcelResponseHeader(response, "问题数据.xlsx");
            ExcelWriterBuilder write = EasyExcelFactory.write(response.getOutputStream(), ProblemExportResponse.class);
            write.sheet("sheet").doWrite(data);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        } catch (Exception e) {
            throw new GlobalException("导出问题数据失败");
        }
    }
 
 
    /**
     * 数据汇总
     */
    private void buildSumApplyRecord(PlacementApply placementApply) {
        LambdaQueryWrapper<PlacementApplyRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlacementApplyRecord::getPlacementApplyId, placementApply.getId());
        List<PlacementApplyRecord> records = placementApplyRecordService.list(queryWrapper);
        if (ObjUtil.isEmpty(records)) {
            return;
        }
        //汇总数据
 
        // 申请总户数
        Integer householdsNum = Math.toIntExact(records.stream().map(PlacementApplyRecord::getIdCard).distinct().count());
        //申请总人数
        Integer personNum = records.stream().mapToInt(PlacementApplyRecord::getCurrentCount).sum();
 
        //25%首付款
        BigDecimal downPaymentAmount = records.stream().map(PlacementApplyRecord::getDownPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        //每季度需支付款项(万元)
        BigDecimal quarterPayAmount = records.stream().map(PlacementApplyRecord::getQuarterPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        //过渡补贴(万元)
        BigDecimal subsidyAmount = records.stream().map(PlacementApplyRecord::getSubsidyAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        //待安置面积
        BigDecimal waitFamilyArea = records.stream().map(PlacementApplyRecord::getWaitFamilyArea).reduce(BigDecimal.ZERO,BigDecimal::add);
 
        //补偿金额(合计)
        BigDecimal totalAmount = records.stream().map(PlacementApplyRecord::getCompensationSum).reduce(BigDecimal.ZERO, BigDecimal::add);
//        //补偿总金额(万元) = 补偿金额(合计)+ 过渡补贴(万元)+ 每季度需支付款项(万元)+ 25%首付款
//        totalAmount = totalAmount.add(downPaymentAmount).add(quarterPayAmount).add(subsidyAmount);
 
        placementApply.setWaitFamilyArea(waitFamilyArea);
        placementApply.setHouseholdsNum(householdsNum);
        placementApply.setPersonNum(personNum);
        placementApply.setDownPaymentAmount(downPaymentAmount);
        placementApply.setQuarterPayAmount(quarterPayAmount);
        placementApply.setSubsidyAmount(subsidyAmount);
        placementApply.setTotalAmount(totalAmount);
        this.updateById(placementApply);
    }
 
    @Override
    public String selectLastBatchNumber() {
        LambdaQueryWrapper<PlacementApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(PlacementApply::getId);
        queryWrapper.last("limit 1");
        PlacementApply placementApply = this.getOne(queryWrapper);
        if (ObjUtil.isEmpty(placementApply)) {
            return null;
        }
        return placementApply.getBatchNumber();
    }
 
    @Override
    public Integer selectNameExists(String name,String idCard) {
        LambdaQueryWrapper<PlacementApply> placementApplyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        placementApplyLambdaQueryWrapper.ne(PlacementApply::getStatus, 2);
        List<PlacementApply> applies = list(placementApplyLambdaQueryWrapper);
        if (ObjUtil.isEmpty(applies)) {
            return 0;
        }
 
        List<Long> applyIds = applies.stream().map(PlacementApply::getId).collect(Collectors.toList());
        LambdaQueryWrapper<PlacementApplyRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PlacementApplyRecord::getPlacementApplyId, applyIds);
        queryWrapper.and(query -> query.eq(PlacementApplyRecord::getIdCard,idCard));
        return NumberUtil.parseInt(String.valueOf(placementApplyRecordService.count(queryWrapper)));
    }
 
    public Integer selectFamilyNameExists(String familyName) {
        LambdaQueryWrapper<PlacementApply> placementApplyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        placementApplyLambdaQueryWrapper.ne(PlacementApply::getStatus, 2);
        List<PlacementApply> applies = list(placementApplyLambdaQueryWrapper);
        if (ObjUtil.isEmpty(applies)) {
            return 0;
        }
 
        List<Long> applyIds = applies.stream().map(PlacementApply::getId).collect(Collectors.toList());
        LambdaQueryWrapper<PlacementApplyRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PlacementApplyRecord::getPlacementApplyId, applyIds);
        queryWrapper.apply(" FIND_IN_SET('"+familyName.trim()+"',REPLACE(wait_family_names,\"、\",\",\"))");
        return NumberUtil.parseInt(String.valueOf(placementApplyRecordService.count(queryWrapper)));
    }
 
 
}