cloudroam
2024-10-28 f77c25900aac7231d006d8f4e08aaa12d72c62b4
src/main/java/com/mzl/flower/service/payment/OrderSettlementService.java
@@ -38,7 +38,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
@@ -676,11 +675,6 @@
            settlement.setUserId(s.getUserId());
            settlement.setStatus(Constants.SETTLEMENT_STATUS.PENDING.name());
            //2024-10-23
            //新增WalletBillRecordDO
            WalletBillRecordDO walletBillRecord =  new WalletBillRecordDO();
            walletBillRecord.setId(UUIDGenerator.getUUID());
            Integer flowerNum = 0;//商品数量
            BigDecimal totalAmount = new BigDecimal(0);//交易合计
            BigDecimal checkFee = new BigDecimal(0);//降级扣款
@@ -723,10 +717,10 @@
                //2024-10-23
                //copy信息到t_wallet_bill_record_detail
                WalletBillRecordDetail walletBillRecordDetail = new WalletBillRecordDetail();
                BeanUtils.copyProperties(detail, walletBillRecordDetail, "id");
                walletBillRecordDetail.setBillRecordId(walletBillRecord.getId());
                walletBillRecordDetailMapper.insert(walletBillRecordDetail);
//                WalletBillRecordDetail walletBillRecordDetail = new WalletBillRecordDetail();
//                BeanUtils.copyProperties(detail, walletBillRecordDetail, "id");
//                walletBillRecordDetail.setBillRecordId(walletBillRecord.getId());
//                walletBillRecordDetailMapper.insert(walletBillRecordDetail);
                flowerNum += oi.getNum();
                totalAmount = totalAmount.add(detail.getTotalAmount());
@@ -738,6 +732,48 @@
                serviceFee = serviceFee.add(ois.getServiceFee());
                serviceFeeRate = ois.getServiceFeeRate();
                settlementAmount = settlementAmount.add(ois.getIncomeSupplier());
                WalletDO walletDO = walletService.getBySupplierId(supplierId);
                RLock lock = redissonClient.getLock(String.format(LockConstants.WALLET_ID_KEY, walletDO.getId()));
                try {
                    if (lock.tryLock(10, 30, TimeUnit.SECONDS)) {
                        try {
                            //2024-10-28 直接保存到walletBillRecord
                            //新增WalletBillRecordDO
                            WalletBillRecordDO walletBillRecord = new WalletBillRecordDO();
                            walletBillRecord.setId(UUIDGenerator.getUUID());
                            WalletDO walletDOInfo = walletService.getBySupplierId(supplierId);
                            //增加供应商结算金额保存到钱包
                            walletBillRecord.setSupplierId(supplierId);
                            walletBillRecord.setWalletId(walletDOInfo.getId());
                            walletBillRecord.setSettlementId(settlement.getId());
                            walletBillRecord.setOrderItemId(detail.getOrderItemId());
                            //变动金额等于供应商收入
                            walletBillRecord.setTotalAmount(ois.getIncomeSupplier());
                            walletBillRecord.setType(Constants.BILL_CHANGE_TYPE.settlement.name());
                            walletBillRecord.setMethod(Constants.BILL_CHANGE_METHOD.add.name());
                            walletBillRecord.setOriginalAmount(walletDOInfo.getTotalAmount());
                            walletBillRecord.setChangeAmount(ois.getIncomeSupplier());
                            walletBillRecord.setBalance(walletDOInfo.getWithdrawableAmount().add(ois.getIncomeSupplier()));
                            Order order = orderMapper.selectById(detail.getOrderId());
                            if (!ObjectUtils.isEmpty(order)) {
                                walletBillRecord.setRemark("订单完成(订单号" + order.getOrderNo() + ")" + ",获得收入");
                                walletBillRecord.setOrderNo(order.getOrderNo());
                            }
                            //更新钱包
                            //可提现金额=钱包余额=结算金额
                            walletDOInfo.setWithdrawableAmount(walletDOInfo.getWithdrawableAmount().add(ois.getIncomeSupplier()));
                            walletDOInfo.setTotalAmount(walletDOInfo.getWithdrawableAmount());
                            //已结算金额
                            walletDOInfo.setSettledAmount(walletDOInfo.getSettledAmount().add(ois.getIncomeSupplier()));
                            walletMapper.updateById(walletDOInfo);
                            walletBillRecordMapper.insert(walletBillRecord);
                        } finally {
                            lock.unlock();
                        }
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            settlement.setFlowerNum(flowerNum);
@@ -771,40 +807,6 @@
            //新逻辑不是立即转账。不走结算后转账逻辑。此时没有转账时间、openID、转账ID、转账明细ID,直接标记结算状态完成,存金额到钱包中
            settlement.setStatus(Constants.SETTLEMENT_STATUS.COMPLETED.name());
            settlementMapper.insert(settlement);
            WalletDO walletDO = walletService.getCurrentSupplier();
            if (null == walletDO) {
                throw new ValidationException("钱包不能为空");
            }
            RLock lock = redissonClient.getLock(String.format(LockConstants.WALLET_ID_KEY, walletDO.getId()));
            try {
                if (lock.tryLock(10, 30, TimeUnit.SECONDS)) {
                    try {
                        WalletDO walletDOInfo = walletService.getCurrentSupplier();
                        //增加供应商结算金额保存到钱包
                        walletBillRecord.setSupplierId(s.getId());
                        walletBillRecord.setWalletId(walletDOInfo.getId());
                        walletBillRecord.setSettlementId(settlement.getId());
                        walletBillRecord.setType(Constants.BILL_CHANGE_TYPE.settlement.name());
                        walletBillRecord.setMethod(Constants.BILL_CHANGE_METHOD.add.name());
                        walletBillRecord.setOriginalAmount(walletDOInfo.getTotalAmount());
                        walletBillRecord.setChangeAmount(settlementAmount);
                        walletBillRecord.setBalance(walletDOInfo.getWithdrawableAmount().add(settlementAmount));
                        //更新钱包
                        //可提现金额=钱包余额=结算金额
                        walletDOInfo.setWithdrawableAmount(walletDOInfo.getWithdrawableAmount().add(settlementAmount));
                        walletDOInfo.setTotalAmount(walletDOInfo.getWithdrawableAmount());
                        //已结算金额
                        walletDOInfo.setSettledAmount(walletDOInfo.getSettledAmount().add(settlementAmount));
                        walletMapper.updateById(walletDOInfo);
                        walletBillRecordMapper.insert(walletBillRecord);
                    } finally {
                        lock.unlock();
                    }
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }