jiangping
2024-12-09 c274fab446ab273da8e8c721c344211e72c61813
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
package com.doumee.service.business.impl;
 
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.*;
import com.doumee.dao.business.dao.CompanyMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.business.vo.EditRecordDataVO;
import com.doumee.dao.system.MultifileMapper;
import com.doumee.dao.system.SystemUserMapper;
import com.doumee.dao.system.model.Multifile;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.YwContractRevenueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 运维合同收支流水Service实现
 * @author 江蹄蹄
 * @date 2024/11/25 10:29
 */
@Service
public class YwContractRevenueServiceImpl implements YwContractRevenueService {
 
    @Autowired
    private YwContractRevenueMapper ywContractRevenueMapper;
 
    @Autowired
    private YwContractBillMapper ywContractBillMapper;
 
    @Autowired
    private YwContractMapper ywContractMapper;
 
    @Autowired
    private CompanyMapper companyMapper;
 
    @Autowired
    private YwAccountMapper ywAccountMapper;
 
    @Autowired
    private MultifileMapper multifileMapper;
 
    @Autowired
    private YwContractRoomMapper ywContractRoomMapper;
 
    @Autowired
    private SystemUserMapper systemUserMapper;
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
 
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public Integer create(YwContractRevenue ywContractRevenue) {
        if(Objects.isNull(ywContractRevenue)
        || Objects.isNull(ywContractRevenue.getActReceivableFee())
                || Objects.isNull(ywContractRevenue.getActPayDate())
                || Objects.isNull(ywContractRevenue.getPayType())
                || Objects.isNull(ywContractRevenue.getCompanyId())
                || Objects.isNull(ywContractRevenue.getAccountId())
                || Objects.isNull(ywContractRevenue.getBillId())
                || ywContractRevenue.getActReceivableFee().compareTo(BigDecimal.ZERO) <= Constants.ZERO
        ){
             throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        YwContractBill ywContractBill = ywContractBillMapper.selectJoinOne(YwContractBill.class,
                new MPJLambdaWrapper<YwContractBill>()
                        .selectAll(YwContractBill.class)
                        .selectAs(YwContract::getStatus, YwContractBill::getContractStatus)
                        .leftJoin(YwContract.class,YwContract::getId,YwContractBill::getContractId)
                        .eq(YwContractBill::getId,ywContractRevenue.getBillId()));
        if(Objects.isNull(ywContractBill)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到账单信息");
        }
        if(!Constants.equalsInteger(ywContractBill.getStatus(),Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"账单状态已流转,无法进行该操作");
        }
        if(!(Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.ZERO)
        || Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.TWO)
                || Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.THREE)
                || Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.FOUR)
        )){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"账单结清状态已流转,无法进行该操作");
        }
        Company company =companyMapper.selectById(ywContractRevenue.getCompanyId());
        if(Objects.isNull(company)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到所属公司信息");
        }
        YwAccount ywAccount =ywAccountMapper.selectById(ywContractRevenue.getAccountId());
        if(Objects.isNull(ywAccount)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到收支账户信息");
        }
        LoginUserInfo loginUserInfo = ywContractRevenue.getLoginUserInfo();
        ywContractRevenue.setCreateDate(new Date());
        ywContractRevenue.setCreator(loginUserInfo.getId());
        ywContractRevenue.setIsdeleted(Constants.ZERO);
        ywContractRevenue.setStatus(Constants.ZERO);
        ywContractRevenue.setContractId(ywContractBill.getContractId());
        //根据收支情况 更新账单数据
        //查询当前账单下 已支付的费用
        List<YwContractRevenue> ywContractRevenueList = ywContractRevenueMapper.selectList(new QueryWrapper<YwContractRevenue>()
                .lambda().eq(YwContractRevenue::getIsdeleted,Constants.ZERO)
                .eq(YwContractRevenue::getStatus,Constants.ZERO)
                .eq(YwContractRevenue::getBillId,ywContractRevenue.getBillId())
        );
        //已支付金额
        BigDecimal payTotal = BigDecimal.ZERO;
        //待支付金额
        BigDecimal waitPayTotal = BigDecimal.ZERO;
        //无付款记录 则为初次支付 根据账单类型 判断是支出 / 收入
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(ywContractRevenueList)){
            ywContractRevenue.setRevenueType(ywContractBill.getBillType());
            if(ywContractRevenue.getActReceivableFee().compareTo(ywContractBill.getReceivableFee())>Constants.ZERO){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"请输入正确的金额!");
            }else  if(ywContractRevenue.getActReceivableFee().compareTo(ywContractBill.getReceivableFee())==Constants.ZERO){
                ywContractBill.setPayStatus(Constants.ONE);
            }else{
                ywContractBill.setPayStatus(Constants.TWO);
            }
        }else{
            //获取已支付的总金额 (可能有收入 有支出)
            if(Constants.equalsInteger(ywContractBill.getBillType(),Constants.ZERO)){
                payTotal = ywContractRevenueList.stream().map(i->
                        i.getActReceivableFee().multiply(
                                BigDecimal.valueOf(Constants.equalsInteger(i.getRevenueType(),Constants.ZERO)?Constants.ONE:-Constants.ONE))
                ).reduce(BigDecimal.ZERO,BigDecimal::add);
                //获取待支付款金额 如果账单类型为支出 或 (账单类型为收入 且 待支付金额 小于 0) 则为支付
                waitPayTotal = ywContractBill.getReceivableFee().subtract(payTotal);
                //如果待支付金额 大于 0  则是 收入 否则是支出 其他状态 为异常!
                if(waitPayTotal.compareTo(BigDecimal.ZERO)>Constants.ZERO){
                    ywContractRevenue.setRevenueType(Constants.ZERO);
                }else if(waitPayTotal.compareTo(BigDecimal.ZERO)<Constants.ZERO){
                    ywContractRevenue.setRevenueType(Constants.ONE);
                }else{
                    throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"收支金额异常!请联系管理员");
                }
            }else{
                payTotal = ywContractRevenueList.stream().map(i->
                        i.getActReceivableFee().multiply(
                                BigDecimal.valueOf(Constants.equalsInteger(i.getRevenueType(),Constants.ZERO)?-Constants.ONE:Constants.ONE))
                ).reduce(BigDecimal.ZERO,BigDecimal::add);
                //获取待支付款金额 如果账单类型为支出 或 (账单类型为收入 且 待支付金额 小于 0) 则为支付
                waitPayTotal = ywContractBill.getReceivableFee().subtract(payTotal);
                //如果待支付金额 大于 0  则是 收入 否则是支出 其他状态 为异常!
                if(waitPayTotal.compareTo(BigDecimal.ZERO)>Constants.ZERO){
                    ywContractRevenue.setRevenueType(Constants.ONE);
                }else if(waitPayTotal.compareTo(BigDecimal.ZERO)<Constants.ZERO){
                    ywContractRevenue.setRevenueType(Constants.ZERO);
                }else{
                    throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"收支金额异常!请联系管理员");
                }
            }
 
 
            //待支付的流水 如果为收入 则比对 金额值  如果是支出 则获取绝对值 进行对比
            if(Constants.equalsInteger(ywContractRevenue.getRevenueType(),Constants.ZERO)){
                //如果支付金额 大于 待支付金额 则提示异常 如果支付金额小于 待支付金额 则状态不变化 其他状态 异常
                if(waitPayTotal.compareTo(ywContractRevenue.getActReceivableFee())<Constants.ZERO){
                    throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"请输入正确的金额!");
                }else if(waitPayTotal.compareTo(ywContractRevenue.getActReceivableFee())==Constants.ZERO){
                    ywContractBill.setPayStatus(Constants.ONE);
                }
            }else{
                waitPayTotal = waitPayTotal.abs();
                if(waitPayTotal.compareTo(ywContractRevenue.getActReceivableFee())<Constants.ZERO){
                    throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"请输入正确的金额!");
                }else if(waitPayTotal.compareTo(ywContractRevenue.getActReceivableFee())==Constants.ZERO){
                    ywContractBill.setPayStatus(Constants.ONE);
                }
            }
        }
        ywContractRevenueMapper.insert(ywContractRevenue);
        ywContractBillMapper.updateById(ywContractBill);
        //如果账单完结,则查询合同下开启中的账单是否存在退款中 如果不存在则标记合同已退款
        if(Constants.equalsInteger(ywContractBill.getContractStatus(),Constants.THREE)
                && Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.ONE)){
            if( ywContractBillMapper
                    .selectCount(new QueryWrapper<YwContractBill>().lambda().eq(YwContractBill::getContractId,ywContractBill.getContractId())
                            .ne(YwContractBill::getId,ywContractBill.getId())
                    .eq(YwContractBill::getStatus,Constants.ZERO)
                                    .in(YwContractBill::getPayStatus,Constants.ZERO,Constants.FOUR, Constants.TWO,Constants.THREE)
                    ) == Constants.ZERO){
                ywContractMapper.update(new UpdateWrapper<YwContract>().lambda()
                        .set(YwContract::getStatus,Constants.FOUR)
                        .set(YwContract::getEditDate,DateUtil.getCurrDateTime())
                        .eq(YwContract::getId,ywContractBill.getContractId())
                );
            }
        }
        //存储附件信息
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywContractRevenue.getMultifileList())){
            for (Multifile multifile:ywContractRevenue.getMultifileList()) {
                if(Objects.isNull(multifile)
                        || StringUtils.isBlank(multifile.getFileurl())
                        || StringUtils.isBlank(multifile.getName())){
                    throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"附件信息错误");
                }
                multifile.setCreator(loginUserInfo.getId());
                multifile.setCreateDate(new Date());
                multifile.setIsdeleted(Constants.ZERO);
                multifile.setObjType(Constants.MultiFile.FN_CONTRACT_REVENUE_FILE.getKey());
                multifile.setObjId(ywContractRevenue.getId());
            }
            multifileMapper.insert(ywContractRevenue.getMultifileList());
        }
        return ywContractRevenue.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        ywContractRevenueMapper.deleteById(id);
    }
 
    @Override
    public void delete(YwContractRevenue ywContractRevenue) {
        UpdateWrapper<YwContractRevenue> deleteWrapper = new UpdateWrapper<>(ywContractRevenue);
        ywContractRevenueMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        ywContractRevenueMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(YwContractRevenue ywContractRevenue) {
        ywContractRevenueMapper.updateById(ywContractRevenue);
    }
 
    @Override
    public void updateByIdInBatch(List<YwContractRevenue> ywContractRevenues) {
        if (CollectionUtils.isEmpty(ywContractRevenues)) {
            return;
        }
        for (YwContractRevenue ywContractRevenue: ywContractRevenues) {
            this.updateById(ywContractRevenue);
        }
    }
 
    @Override
    public YwContractRevenue findById(Integer id) {
        return ywContractRevenueMapper.selectById(id);
    }
 
    @Override
    public YwContractRevenue getDetail(Integer id) {
        YwContractRevenue ywContractRevenue = ywContractRevenueMapper.selectJoinOne(
                YwContractRevenue.class,
                new MPJLambdaWrapper<YwContractRevenue>().selectAll(YwContractRevenue.class)
                        .selectAs(YwContractBill::getCode,YwContractRevenue::getBillCode)
                        .selectAs(YwContract::getCode,YwContractRevenue::getContractCode)
                        .selectAs(YwContract::getStatus,YwContractRevenue::getContractStatus)
                        .selectAs(YwCustomer::getName,YwContractRevenue::getCustomerName)
                        .selectAs(SystemUser::getRealname,YwContractRevenue::getRealname)
                        .selectAs(YwAccount::getTitle,YwContractRevenue::getAccountTitle)
                        .leftJoin(YwContractBill.class,YwContractBill::getId,YwContractRevenue::getBillId)
                        .leftJoin(YwContract.class,YwContract::getId,YwContractBill::getContractId)
                        .leftJoin(YwCustomer.class,YwCustomer::getId,YwContract::getRenterId)
                        .leftJoin(SystemUser.class,SystemUser::getId,YwContractRevenue::getCreator)
                        .leftJoin(YwAccount.class,YwAccount::getId,YwContractRevenue::getAccountId)
                        .eq(YwContractRevenue::getId,id)
        );
 
       this.getRecordData(ywContractRevenue);
 
        //附件数据
        List<Multifile> multifileList = multifileMapper.selectJoinList(Multifile.class,new MPJLambdaWrapper<Multifile>()
                .selectAll(Multifile.class)
                .selectAs(SystemUser::getRealname,Multifile::getUserName)
                .leftJoin(SystemUser.class,SystemUser::getId,Multifile::getCreator)
                .eq(Multifile::getObjId,id)
                .eq(Multifile::getIsdeleted,Constants.ZERO)
                .eq(Multifile::getObjType,Constants.MultiFile.FN_CONTRACT_REVENUE_FILE.getKey()));
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(multifileList)){
            String path = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode()
                    +systemDictDataBiz.queryByCode(Constants.FTP,Constants.YW_CONTRACT_BILL).getCode();
            for (Multifile multifile:multifileList) {
                if(StringUtils.isNotBlank(multifile.getFileurl())){
                    multifile.setFileurlFull(path + multifile.getFileurl());
                }
            }
            ywContractRevenue.setMultifileList(multifileList);
        }
 
 
        return ywContractRevenue;
    }
 
    /**
     * 操作记录
     * @param ywContractRevenue
     */
    public void getRecordData(YwContractRevenue ywContractRevenue){
        List<EditRecordDataVO> editRecordDataVOList = new ArrayList();
        //模拟流水记录
        if(Objects.nonNull(ywContractRevenue.getCreateDate())){
            EditRecordDataVO editRecordDataVO = new EditRecordDataVO();
            editRecordDataVO.setEditRemark("创建流水");
            editRecordDataVO.setEditTime(ywContractRevenue.getCreateDate());
            SystemUser createUser = systemUserMapper.selectById(ywContractRevenue.getCreator());
            if(Objects.nonNull(createUser)){
                editRecordDataVO.setEditUserName(createUser.getRealname());
            }
            editRecordDataVOList.add(editRecordDataVO);
        }
        if(Objects.nonNull(ywContractRevenue.getEditor())){
            EditRecordDataVO editRecordDataVO = new EditRecordDataVO();
            editRecordDataVO.setEditRemark("关闭流水");
            editRecordDataVO.setEditTime(ywContractRevenue.getEditDate());
            SystemUser closeUser = systemUserMapper.selectById(ywContractRevenue.getEditor());
            if(Objects.nonNull(closeUser)){
                editRecordDataVO.setEditUserName(closeUser.getRealname());
            }
            editRecordDataVOList.add(editRecordDataVO);
        }
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(editRecordDataVOList)){
            ywContractRevenue.setEditRecordDataVOList(editRecordDataVOList);
        }
    }
 
 
    @Override
    public void closeRevenue(Integer id,LoginUserInfo loginUserInfo) {
        YwContractRevenue ywContractRevenue = ywContractRevenueMapper.selectById(id);
        if(Objects.isNull(ywContractRevenue)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        YwContractBill ywContractBill = ywContractBillMapper.selectById(ywContractRevenue.getBillId());
        if(Objects.isNull(ywContractBill)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        YwContract ywContract = ywContractMapper.selectById(ywContractBill.getContractId());
        if(Objects.isNull(ywContract)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(Constants.equalsInteger(ywContract.getStatus(),Constants.FOUR)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"合同状态已流转,无法操作");
        }
        if(Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.ONE)
            || Constants.equalsInteger(ywContractBill.getPayStatus(),Constants.FIVE)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"账单状态已流转,无法操作");
        }
        //查询账单下 所有的账单
        List<YwContractRevenue> ywContractRevenueList =  ywContractRevenueMapper.selectList(new QueryWrapper<YwContractRevenue>().lambda().eq(YwContractRevenue::getStatus,Constants.ZERO)
                        .eq(YwContractRevenue::getIsdeleted,Constants.ZERO)
                .eq(YwContractRevenue::getBillId,ywContractRevenue.getBillId())
                .ne(YwContractRevenue::getId,id)
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isEmpty(ywContractRevenueList)){
            //无其他流水记录 根据账单创建类型  变更状态
            //自建账单 根据账单收支类型进行 变更状态
            if(Constants.equalsInteger(ywContractBill.getType(),Constants.ONE)){
                if(Constants.equalsInteger(ywContractBill.getBillType(),Constants.ZERO)){
                    ywContractBill.setPayStatus(Constants.ZERO);
                }else{
                    ywContractBill.setPayStatus(Constants.THREE);
                }
            }else{
                //合同账单 变为 待收款
                ywContractBill.setPayStatus(Constants.ZERO);
            }
        }else{
            //存在其他流水 则统一变更为 部分结算
            ywContractBill.setPayStatus(Constants.TWO);
        }
 
        ywContractBillMapper.update(null,new UpdateWrapper<YwContractBill>().lambda()
                .set(YwContractBill::getPayStatus,ywContractBill.getPayStatus())
                .set(YwContractBill::getEditor,loginUserInfo.getId())
                .set(YwContractBill::getEditDate, DateUtil.getCurrDateTime())
                .eq(YwContractBill::getId,ywContractBill.getId())
        );
 
        ywContractRevenueMapper.update(null,new UpdateWrapper<YwContractRevenue>().lambda()
                .set(YwContractRevenue::getStatus,Constants.ONE)
                .set(YwContractRevenue::getEditor,loginUserInfo.getId())
                .set(YwContractRevenue::getEditDate,DateUtil.getCurrDateTime())
                .eq(YwContractRevenue::getId,id));
    }
 
 
    @Override
    public YwContractRevenue findOne(YwContractRevenue ywContractRevenue) {
        QueryWrapper<YwContractRevenue> wrapper = new QueryWrapper<>(ywContractRevenue);
        return ywContractRevenueMapper.selectOne(wrapper);
    }
 
    @Override
    public List<YwContractRevenue> findList(YwContractRevenue ywContractRevenue) {
        List<YwContractRevenue> ywContractRevenueList = ywContractRevenueMapper.selectJoinList(YwContractRevenue.class,new MPJLambdaWrapper<YwContractRevenue>()
                .selectAll(YwContractRevenue.class)
                .selectAs(YwCustomer::getName,YwContractRevenue::getCustomerName)
                .leftJoin(YwContract.class,YwContract::getId,YwContractRevenue::getContractId)
                .leftJoin(YwCustomer.class,YwCustomer::getId,YwContract::getRenterId)
                .eq(YwContractRevenue::getStatus,Constants.ZERO)
                .eq(Objects.nonNull(ywContractRevenue) && Objects.nonNull(ywContractRevenue.getBillId()),YwContractRevenue::getBillId,ywContractRevenue.getBillId())
        );
        return ywContractRevenueList;
    }
  
    @Override
    public PageData<YwContractRevenue> findPage(PageWrap<YwContractRevenue> pageWrap) {
        IPage<YwContractRevenue> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<YwContractRevenue> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        YwContractRevenue model = pageWrap.getModel();
        IPage<YwContractRevenue> iPage = ywContractRevenueMapper.selectJoinPage(page,YwContractRevenue.class,
                queryWrapper.selectAll(YwContractRevenue.class)
                        .selectAs(YwContractBill::getCode,YwContractRevenue::getBillCode)
                        .selectAs(YwContractBill::getType,YwContractRevenue::getBillType)
                         .selectAs(YwContract::getCode,YwContractRevenue::getContractCode)
                        .selectAs(YwContract::getStatus,YwContractRevenue::getContractStatus)
                        .selectAs(YwCustomer::getName,YwContractRevenue::getCustomerName)
                        .selectAs(SystemUser::getRealname,YwContractRevenue::getRealname)
                        .leftJoin(YwContractBill.class,YwContractBill::getId,YwContractRevenue::getBillId)
                        .leftJoin(YwContract.class,YwContract::getId,YwContractBill::getContractId)
                        .leftJoin(YwCustomer.class,YwCustomer::getId,YwContract::getRenterId)
                        .leftJoin(SystemUser.class,SystemUser::getId,YwContractRevenue::getCreator)
                        .eq(YwContractRevenue::getIsdeleted,Constants.ZERO)
                        .like(Objects.nonNull(model)&&StringUtils.isNotBlank(model.getCustomerName()),
                                YwCustomer::getName,model.getCustomerName())
                        .eq(Objects.nonNull(model)&&Objects.nonNull(model.getPayType()),
                                YwContractRevenue::getPayType,model.getPayType())
                        .eq(Objects.nonNull(model)&&Objects.nonNull(model.getRevenueType()),
                                YwContractRevenue::getRevenueType,model.getRevenueType())
                        .ge(Objects.nonNull(model)&&Objects.nonNull(model.getPayDateStart()),YwContractRevenue::getActPayDate, Utils.Date.getStart(model.getPayDateStart()))
                        .le(Objects.nonNull(model)&&Objects.nonNull(model.getPayDateEnd()),YwContractRevenue::getActPayDate, Utils.Date.getEnd(model.getPayDateEnd()))
                        .orderByDesc(YwContractRevenue::getId))
                ;
 
        this.dealRoomDetail(iPage.getRecords());
 
        for (YwContractRevenue ywContractRevenue:iPage.getRecords()) {
            //楼宇名称
            List<YwContractRoom> ywContractRoomList = ywContractRevenue.getYwContractRoomList();
            if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywContractRoomList)){
                StringBuilder roomPathName = new StringBuilder();
                for (YwContractRoom ywContractRoom:ywContractRoomList) {
                    if(Constants.equalsInteger(ywContractRevenue.getBillType(),Constants.ZERO)){
                        if(!(Constants.equalsInteger(ywContractRevenue.getContractId(),ywContractRoom.getContractId())
                        && Constants.equalsInteger(ywContractRoom.getType(),Constants.ZERO))){
                            continue;
                        }
                    }else{
                        if(!(Constants.equalsInteger(ywContractRevenue.getBillId(),ywContractRoom.getContractId())
                                && Constants.equalsInteger(ywContractRoom.getType(),Constants.ONE))){
                            continue;
                        }
                    }
                    if(StringUtils.isNotBlank(ywContractRoom.getProjectName())){
                        roomPathName.append(ywContractRoom.getProjectName()+"/");
                    }
                    if(StringUtils.isNotBlank(ywContractRoom.getBuildingName())){
                        roomPathName.append(ywContractRoom.getBuildingName()+"/");
                    }
                    if(StringUtils.isNotBlank(ywContractRoom.getFloorName())){
                        roomPathName.append(ywContractRoom.getFloorName()+"/");
                    }
                    if(StringUtils.isNotBlank(ywContractRoom.getRoomName())){
                        roomPathName.append(ywContractRoom.getRoomName());
                    }
                    if(StringUtils.isNotBlank(roomPathName)){
                        roomPathName.append(";");
                    }
                }
                ywContractRevenue.setRoomPathName(roomPathName.toString());
            }
 
        }
        return PageData.from(iPage);
    }
 
 
    public void dealRoomDetail(List<YwContractRevenue> ywContractRevenueList){
        //查询账单下的楼宇数据
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywContractRevenueList)){
            //获取所有数据
            List<Integer> billIdList = ywContractRevenueList.stream().map(i->i.getBillId()).collect(Collectors.toList());
            List<Integer> contractIdList = ywContractRevenueList.stream().map(i->i.getContractId()).collect(Collectors.toList());
            if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(billIdList)){
                List<YwContractRoom> ywContractRoomList  = ywContractRoomMapper.selectJoinList(YwContractRoom.class,new MPJLambdaWrapper<YwContractRoom>()
                        .selectAll(YwContractRoom.class)
                        .selectAs(YwProject::getName,YwRoom::getProjectName)
                        .selectAs(YwFloor::getName,YwRoom::getFloorName)
                        .selectAs(YwBuilding::getName,YwRoom::getBuildingName)
                        .selectAs(YwRoom::getCode,YwContractRoom::getRoomName)
                        .leftJoin(YwRoom.class,YwRoom::getId,YwContractRoom::getRoomId)
                        .leftJoin(YwFloor.class,YwFloor::getId,YwRoom::getFloor)
                        .leftJoin(YwProject.class,YwProject::getId,YwRoom::getProjectId)
                        .leftJoin(YwBuilding.class,YwBuilding::getId,YwRoom::getBuildingId)
                        .and(i->i.in(YwContractRoom::getContractId,billIdList).eq(YwContractRoom::getType,Constants.ONE)
                                .or().in(YwContractRoom::getContractId,contractIdList).eq(YwContractRoom::getType,Constants.ZERO)
                        )
//                        .in(YwContractRoom::getContractId,billIdList)
//                        .eq(YwContractRoom::getType,Constants.ONE)
                );
                if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywContractRoomList)){
                    for (YwContractRevenue ywContractRevenue:ywContractRevenueList) {
                        ywContractRevenue.setYwContractRoomList(
                                ywContractRoomList.stream().filter(i->
                                        Constants.equalsInteger(i.getContractId(),ywContractRevenue.getBillId())||Constants.equalsInteger(i.getContractId(),ywContractRevenue.getContractId()))
                                        .collect(Collectors.toList())
                        );
                    }
                }
            }
        }
    }
 
 
    @Override
    public long count(YwContractRevenue ywContractRevenue) {
        QueryWrapper<YwContractRevenue> wrapper = new QueryWrapper<>(ywContractRevenue);
        return ywContractRevenueMapper.selectCount(wrapper);
    }
}