ContractPaymentServiceImpl.java
3 KB
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
package vion.service.impl;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vion.mapper.ContractPaymentMapper;
import vion.mapper.SettlementDiffMapper;
import vion.model.Contract;
import vion.model.ContractPayment;
import vion.model.SettlementDiff;
import vion.service.IContractPaymentService;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author HlQ
* @date 2023/11/29
*/
@Service
@RequiredArgsConstructor
public class ContractPaymentServiceImpl extends MPJBaseServiceImpl<ContractPaymentMapper, ContractPayment> implements IContractPaymentService {
private final SettlementDiffMapper settlementDiffMapper;
/**
* 当合同状态变化或者应收款变化时,重新计算款项
*
* @param existContract
* @param dto
*/
@Override
public void calMoney(Contract existContract, Contract dto) {
List<ContractPayment> contractPaymentList = this.lambdaQuery().eq(ContractPayment::getContractId, existContract.getId()).list();
List<SettlementDiff> diffList = settlementDiffMapper.selectList(Wrappers.<SettlementDiff>lambdaQuery().eq(SettlementDiff::getContractId, existContract.getId()));
BigDecimal diffAmount1 = diffList.stream().filter(diff -> diff.getOffsetItem() == 1).map(SettlementDiff::getSettlementDiff).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal diffAmount2 = diffList.stream().filter(diff -> diff.getOffsetItem() == 2).map(SettlementDiff::getSettlementDiff).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal diffAmount3 = diffList.stream().filter(diff -> diff.getOffsetItem() == 3).map(SettlementDiff::getSettlementDiff).reduce(BigDecimal.ZERO, BigDecimal::add);
Map<Integer, BigDecimal> type2RatioMap = contractPaymentList.stream().collect(Collectors.toMap(ContractPayment::getPaymentType, ContractPayment::getPaymentRatio));
Integer status = dto.getStatus() == null ? existContract.getStatus() : dto.getStatus();
BigDecimal sumRatio = new BigDecimal(0);
for (int i = 1; i <= status; i++) {
BigDecimal ratio = type2RatioMap.get(i);
sumRatio = NumberUtil.add(ratio, sumRatio);
}
BigDecimal totalAmount = existContract.getTotalAmount();
BigDecimal curAmount = NumberUtil.mul(totalAmount, sumRatio);
BigDecimal paidAmount = ObjUtil.isNull(dto.getPaidAmount()) ? existContract.getPaidAmount() : dto.getPaidAmount();
BigDecimal recAmount = NumberUtil.add(NumberUtil.add(NumberUtil.sub(curAmount, paidAmount), diffAmount2), diffAmount3);
BigDecimal outsideAmount = NumberUtil.add(NumberUtil.add(NumberUtil.sub(totalAmount, paidAmount), diffAmount1), diffAmount3);
dto.setReceivableAmount(recAmount);
dto.setOutstandingAmount(outsideAmount);
}
}