分布式事务一致性,事务补偿实战

 一、事务记录补偿表设计

 三、业务补偿函数

@Override
    public void compensation(BidPaymentDetailConfirmRecord confirmRecord, ProviderUserSession userSession) throws Exception {
        Long type = confirmRecord.getBusinessType();
        Long detailId = confirmRecord.getPaymentDetailId();
        if(type == null || detailId == null) {
            throw new Exception("事务补偿失败,必要参数为空,type:"+type+",detailId:"+detailId);
        }
        BidPaymentsDetail detail = bidPaymentsDetailService.selectByPrimaryKey(detailId);
        BidPayments payments = bidPaymentsService.selectByPrimaryKey(detail.getPaymentId());
        if(payments == null) {
            throw new Exception("事务补偿失败,收款申请已不存在:"+detail.getPaymentId());
        }
        TrcTrack trcTrack = trcTrackProvider.selectByPrimaryKey(payments.getTrackId());
        if(trcTrack == null) {
            throw new Exception("事务补偿失败,商机信息已不存在:"+payments.getTrackId());
        }
        CstCustomer customer = cstCustomerProvider.selectByPrimaryKey(trcTrack.getCustomerId());
        if(customer == null) {
            throw new Exception("事务补偿失败,客户信息已不存在:"+trcTrack.getCustomerId());
        }
        final BidContract bidContract = bidContractService.getEffectContract(trcTrack.getId());
        try {
            if (0 == confirmRecord.getStatus()) {
                switch (type.intValue()) {
                    case 10:
                        bizBidPaymentProvider.receiveDeposit(trcTrack, customer,
                                detail.getReceiveType(), detail.getReceiveAmount(), userSession);
                        break;
                    case 20:
                        //首期款20%,不足额
                        bizBidPaymentProvider.receiveContractAmount(trcTrack, customer, detail.getReceiveType(),
                                detail.getReceiveAmount(), bidContract.getContractAmount(), false, false, userSession);
                        break;
                    case 21:
                        //首期款20%足额,新建合同号
                        bizBidPaymentProvider.receiveContractAmount(trcTrack, customer, detail.getReceiveType(),
                                detail.getReceiveAmount(), bidContract.getContractAmount(), true, true, userSession);
                        break;
                    case 22:
                        //首期款20%足额,但不是新建合同号
                        bizBidPaymentProvider.receiveContractAmount(trcTrack, customer, detail.getReceiveType(),
                                detail.getReceiveAmount(), bidContract.getContractAmount(), true, false, userSession);
                        break;
                    case 23:
                        //1、创建EBS客户
                        Map<String, String> resultMap = bidPaymentsDetailService.import_customer(
                                payments, userSession.getEbsBizId(), bidContract.getContractNo());
                        //2、定金转首期款
                        if (resultMap != null &&
                                !EbsWebServiceConstants.RETURN_STATUS_ERROR.equals(resultMap.get("status"))) {
                            bidPaymentsDetailService.intentionToDownpayment(payments, trcTrack.getId(),
                                    userSession.getEbsBizId(), bidContract.getContractNo());
                        }
                        break;
                    case 24:
                        //1、定金首期款
                        bidPaymentsDetailService.intentionToDownpayment(payments, trcTrack.getId(),
                                userSession.getEbsBizId(), bidContract.getContractNo());
                        break;
                    case 30:
                        //创建收款确认跟进记录
                        bidPaymentsDetailService.insertReceivedAmountAction(trcTrack, customer, detail, userSession);
                        break;
                }
                confirmRecord.setStatus(2);        //设置为已补偿
                bidPaymentDetailConfirmRecordMapper.updateByPrimaryKeySelective(confirmRecord);
            } else {
                LogUtil.error("无效的事务补偿,status状态为:" + confirmRecord.getStatus());
            }
        } catch (Exception e) {
            throw new Exception("事务补偿失败,"+e.getMessage());
        }
    }

调用rest接口,传事务记录ID,进行事务补偿

原文地址:https://www.cnblogs.com/cocoat/p/6056810.html