PaymentServiceImpl.java 2.67 KB
package vion.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vion.dto.PaymentDTO;
import vion.mapper.PaymentMapper;
import vion.model.Contract;
import vion.model.Payment;
import vion.service.IContractPaymentService;
import vion.service.IContractService;
import vion.service.IPaymentService;
import vion.vo.PaymentVO;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author HlQ
 * @date 2023/12/5
 */
@Service
@RequiredArgsConstructor
public class PaymentServiceImpl extends MPJBaseServiceImpl<PaymentMapper, Payment> implements IPaymentService {

    private final IContractService contractService;
    private final IContractPaymentService contractPaymentService;
    private final Converter converter;

    @Override
    public Page<PaymentVO> list(PaymentDTO dto) {
        Page<Payment> recPage = this.lambdaQuery(converter.convert(dto, new Payment())).page(Page.of(dto.getPageNum(), dto.getPageSize()));
        return Page.<PaymentVO>of(recPage.getCurrent(), recPage.getSize(), recPage.getTotal()).setRecords(converter.convert(recPage.getRecords(), PaymentVO.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String save(List<PaymentDTO> dto) {
        List<Payment> paymentList = converter.convert(dto, Payment.class);
        paymentList.forEach(p -> {
            Payment payment = this.lambdaQuery().eq(Payment::getSerialNo, p.getSerialNo()).one();
            if (ObjUtil.isNull(payment)) {
                this.save(p);
            } else {
                p.setId(payment.getId());
                this.updateById(p);
            }
        });
        Map<String, List<Payment>> no2PaymentMap = paymentList.stream().collect(Collectors.groupingBy(Payment::getContractNo));
        no2PaymentMap.forEach((no, list) -> {
            BigDecimal sumAmount = list.stream().map(Payment::getPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

            Contract exitContract = contractService.lambdaQuery().eq(Contract::getContractNo, no).one();
            // 最新的应收款去计算
            Contract updDto = new Contract();
            updDto.setId(exitContract.getId());
            updDto.setPaidAmount(sumAmount);
            contractPaymentService.calMoney(exitContract, updDto);
            contractService.updateById(updDto);
        });
        return "成功";
    }
}