ContractPaymentServiceImpl.java 2.02 KB
package vion.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vion.mapper.ContractPaymentMapper;
import vion.model.Contract;
import vion.model.ContractPayment;
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 {

    /**
     * 当合同状态变化或者应收款变化时,重新计算款项
     *
     * @param existContract
     * @param dto
     */
    @Override
    public void calMoney(Contract existContract, Contract dto) {
        List<ContractPayment> contractPaymentList = this.lambdaQuery().eq(ContractPayment::getContractId, existContract.getId()).list();
        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.sub(curAmount, paidAmount);
        BigDecimal outsideAmount = NumberUtil.sub(totalAmount, paidAmount);
        dto.setReceivableAmount(recAmount);
        dto.setOutstandingAmount(outsideAmount);
    }
}