修改余额退款逻辑

This commit is contained in:
2023-03-07 16:43:19 +08:00
parent f4fe4e74c2
commit 3035eb547b
6 changed files with 164 additions and 27 deletions

View File

@@ -0,0 +1,34 @@
package com.jsowell.pile.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class RefundableWxPayOrderData {
/**
* 微信商户订单号
*/
private String outTradeNo;
/**
* 微信支付订单号
*/
private String transactionId;
/**
* 原订单支付金额
*/
private BigDecimal payerAmount;
/**
* 可退金额
*/
private BigDecimal refundableAmount;
}

View File

@@ -2,6 +2,8 @@ package com.jsowell.pile.mapper;
import com.jsowell.pile.domain.WxpayRefundCallback;
import java.util.List;
public interface WxpayRefundCallbackMapper {
int deleteByPrimaryKey(Integer id);
@@ -13,5 +15,7 @@ public interface WxpayRefundCallbackMapper {
int updateByPrimaryKeySelective(WxpayRefundCallback record);
int updateByPrimaryKey(WxpayRefundCallback record);
// int updateByPrimaryKey(WxpayRefundCallback record);
List<WxpayRefundCallback> selectWxpayRefundCallbackList(List<String> outTradeNoList);
}

View File

@@ -2,14 +2,18 @@ package com.jsowell.pile.service;
import com.jsowell.pile.domain.WxpayRefundCallback;
import java.util.List;
public interface WxpayRefundCallbackService {
int deleteByPrimaryKey(Integer id);
// int deleteByPrimaryKey(Integer id);
int insertSelective(WxpayRefundCallback record);
WxpayRefundCallback selectByPrimaryKey(Integer id);
// WxpayRefundCallback selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(WxpayRefundCallback record);
// int updateByPrimaryKeySelective(WxpayRefundCallback record);
int updateByPrimaryKey(WxpayRefundCallback record);
// int updateByPrimaryKey(WxpayRefundCallback record);
List<WxpayRefundCallback> selectWxpayRefundCallbackList(List<String> outTradeNoList);
}

View File

@@ -27,9 +27,11 @@ import com.jsowell.pile.domain.OrderBasicInfo;
import com.jsowell.pile.domain.OrderDetail;
import com.jsowell.pile.domain.OrderPayRecord;
import com.jsowell.pile.domain.WxpayCallbackRecord;
import com.jsowell.pile.domain.WxpayRefundCallback;
import com.jsowell.pile.dto.IndexQueryDTO;
import com.jsowell.pile.dto.QueryOrderDTO;
import com.jsowell.pile.dto.QueryPersonPileDTO;
import com.jsowell.pile.dto.RefundableWxPayOrderData;
import com.jsowell.pile.mapper.OrderBasicInfoMapper;
import com.jsowell.pile.service.IMemberBasicInfoService;
import com.jsowell.pile.service.IOrderAbnormalRecordService;
@@ -38,9 +40,13 @@ import com.jsowell.pile.service.IOrderPayRecordService;
import com.jsowell.pile.service.IPileConnectorInfoService;
import com.jsowell.pile.service.WechatPayService;
import com.jsowell.pile.service.WxpayCallbackRecordService;
import com.jsowell.pile.service.WxpayRefundCallbackService;
import com.jsowell.pile.transaction.dto.OrderTransactionDTO;
import com.jsowell.pile.transaction.service.TransactionService;
import com.jsowell.pile.vo.uniapp.*;
import com.jsowell.pile.vo.uniapp.MemberVO;
import com.jsowell.pile.vo.uniapp.OrderVO;
import com.jsowell.pile.vo.uniapp.PersonPileConnectorSumInfoVO;
import com.jsowell.pile.vo.uniapp.SendMessageVO;
import com.jsowell.pile.vo.web.IndexOrderInfoVO;
import com.jsowell.pile.vo.web.OrderListVO;
import com.jsowell.pile.vo.web.OrderTotalDataVO;
@@ -110,6 +116,9 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
@Autowired
private IPileConnectorInfoService pileConnectorInfoService;
@Autowired
private WxpayRefundCallbackService wxpayRefundCallbackService;
/**
* 查询订单
*
@@ -768,14 +777,18 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
}
// 退款金额 元转分 123
int refundTotalCents = refundAmount.multiply(new BigDecimal(100)).intValue();
// 查询最近一年余额充值订单
List<WxpayCallbackRecord> recordList = wxpayCallbackRecordService.queryBalanceRechargeRecordOfTheLatestYear(dto.getMemberId());
// 查询用户充值余额订单 过滤掉已经退款的充值订单 refundableOrder
List<RefundableWxPayOrderData> recordList = queryRefundableOrder(dto.getMemberId());
// 也许需要多笔支付订单才够退款
List<WechatPayRefundRequest> requestList = Lists.newArrayList();
WechatPayRefundRequest request;
for (WxpayCallbackRecord record : recordList) {
int payerTotal = Integer.parseInt(record.getPayerTotal()); // 该笔支付订单的支付金额,单位分
refundTotalCents = refundTotalCents - payerTotal; // 123 - 100
for (RefundableWxPayOrderData record : recordList) {
int refundableTotal = record.getRefundableAmount().intValue(); // 该笔支付订单的可退金额,单位分
int payerTotal = record.getPayerAmount().intValue(); // 该笔支付订单的支付金额,单位分
// 用户申请退款金额-可退金额
refundTotalCents = refundTotalCents - refundableTotal; // 123 - 100
request = new WechatPayRefundRequest();
request.setTransaction_id(record.getTransactionId()); // 微信支付单号
request.setOut_trade_no(record.getOutTradeNo()); // 商户订单号
@@ -786,7 +799,7 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
if (refundTotalCents > 0) {
// 如果大于0说明这笔单退完也不够
WechatPayRefundRequest.Amount amount = new WechatPayRefundRequest.Amount();
amount.setRefund(payerTotal); // 退款金额
amount.setRefund(refundableTotal); // 退款金额
amount.setTotal(payerTotal); // 原订单金额
request.setAmount(amount);
requestList.add(request);
@@ -795,7 +808,7 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
// 生成退款单号
WechatPayRefundRequest.Amount amount = new WechatPayRefundRequest.Amount();
// 部分退
int i = payerTotal + refundTotalCents;
int i = refundableTotal + refundTotalCents;
amount.setRefund(i); // 退款金额
amount.setTotal(payerTotal); // 原订单金额
request.setAmount(amount);
@@ -817,6 +830,72 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
return null;
}
private List<RefundableWxPayOrderData> queryRefundableOrder(String memberId) {
List<RefundableWxPayOrderData> resultList = Lists.newArrayList();
// 查询最近一年余额充值订单
List<WxpayCallbackRecord> recordList = wxpayCallbackRecordService.queryBalanceRechargeRecordOfTheLatestYear(memberId);
if (CollectionUtils.isEmpty(recordList)) {
return resultList;
}
// 拿到微信商户单号list
List<String> outTradeNoList = recordList.stream()
.map(WxpayCallbackRecord::getOutTradeNo)
.collect(Collectors.toList());
List<WxpayRefundCallback> wxpayRefundCallbacks = wxpayRefundCallbackService.selectWxpayRefundCallbackList(outTradeNoList);
if (CollectionUtils.isEmpty(wxpayRefundCallbacks)) {
// 退款记录为空,表示没有一笔退款
for (WxpayCallbackRecord wxpayCallbackRecord : recordList) {
resultList.add(
RefundableWxPayOrderData.builder()
.outTradeNo(wxpayCallbackRecord.getOutTradeNo())
.transactionId(wxpayCallbackRecord.getTransactionId())
.payerAmount(new BigDecimal(wxpayCallbackRecord.getPayerTotal()))
.refundableAmount(new BigDecimal(wxpayCallbackRecord.getPayerTotal()))
.build()
);
}
return resultList;
}
// 退款记录转map key:outTradeNo value:退款记录
Map<String, List<WxpayRefundCallback>> refundMap = wxpayRefundCallbacks.stream()
.collect(Collectors.groupingBy(WxpayRefundCallback::getOutTradeNo));
for (WxpayCallbackRecord wxpayCallbackRecord : recordList) {
List<WxpayRefundCallback> refundCallbackList = refundMap.get(wxpayCallbackRecord.getOutTradeNo());
if (CollectionUtils.isEmpty(refundCallbackList)) {
// 这笔单没有申请过退款
resultList.add(
RefundableWxPayOrderData.builder()
.outTradeNo(wxpayCallbackRecord.getOutTradeNo())
.transactionId(wxpayCallbackRecord.getTransactionId())
.payerAmount(new BigDecimal(wxpayCallbackRecord.getPayerTotal()))
.refundableAmount(new BigDecimal(wxpayCallbackRecord.getPayerTotal()))
.build()
);
continue;
}
// 支付订单产生过退款
BigDecimal refundTotal = refundCallbackList.stream()
.map(x -> new BigDecimal(x.getAmountRefund()))
.reduce(BigDecimal::add)
.get();
// 可退金额
BigDecimal subtract = new BigDecimal(wxpayCallbackRecord.getPayerTotal()).subtract(refundTotal);
if (subtract.compareTo(BigDecimal.ZERO) > 0) {
resultList.add(
RefundableWxPayOrderData.builder()
.outTradeNo(wxpayCallbackRecord.getOutTradeNo())
.transactionId(wxpayCallbackRecord.getTransactionId())
.payerAmount(new BigDecimal(wxpayCallbackRecord.getPayerTotal()))
.refundableAmount(subtract)
.build()
);
}
}
return resultList;
}
/**
* 通过订单号查询订单详情
*

View File

@@ -6,6 +6,7 @@ import com.jsowell.pile.service.WxpayRefundCallbackService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class WxpayRefundCallbackServiceImpl implements WxpayRefundCallbackService {
@@ -13,10 +14,10 @@ public class WxpayRefundCallbackServiceImpl implements WxpayRefundCallbackServic
@Resource
private WxpayRefundCallbackMapper wxpayRefundCallbackMapper;
@Override
public int deleteByPrimaryKey(Integer id) {
return wxpayRefundCallbackMapper.deleteByPrimaryKey(id);
}
// @Override
// public int deleteByPrimaryKey(Integer id) {
// return wxpayRefundCallbackMapper.deleteByPrimaryKey(id);
// }
@Override
public int insertSelective(WxpayRefundCallback record) {
@@ -24,18 +25,23 @@ public class WxpayRefundCallbackServiceImpl implements WxpayRefundCallbackServic
}
@Override
public WxpayRefundCallback selectByPrimaryKey(Integer id) {
return wxpayRefundCallbackMapper.selectByPrimaryKey(id);
public List<WxpayRefundCallback> selectWxpayRefundCallbackList(List<String> outTradeNoList) {
return wxpayRefundCallbackMapper.selectWxpayRefundCallbackList(outTradeNoList);
}
@Override
public int updateByPrimaryKeySelective(WxpayRefundCallback record) {
return wxpayRefundCallbackMapper.updateByPrimaryKeySelective(record);
}
// @Override
// public WxpayRefundCallback selectByPrimaryKey(Integer id) {
// return wxpayRefundCallbackMapper.selectByPrimaryKey(id);
// }
@Override
public int updateByPrimaryKey(WxpayRefundCallback record) {
return wxpayRefundCallbackMapper.updateByPrimaryKey(record);
}
// @Override
// public int updateByPrimaryKeySelective(WxpayRefundCallback record) {
// return wxpayRefundCallbackMapper.updateByPrimaryKeySelective(record);
// }
// @Override
// public int updateByPrimaryKey(WxpayRefundCallback record) {
// return wxpayRefundCallbackMapper.updateByPrimaryKey(record);
// }
}