mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-06-19 22:59:46 +08:00
修改余额退款逻辑
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@ package com.jsowell.pile.mapper;
|
|||||||
|
|
||||||
import com.jsowell.pile.domain.WxpayRefundCallback;
|
import com.jsowell.pile.domain.WxpayRefundCallback;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface WxpayRefundCallbackMapper {
|
public interface WxpayRefundCallbackMapper {
|
||||||
int deleteByPrimaryKey(Integer id);
|
int deleteByPrimaryKey(Integer id);
|
||||||
|
|
||||||
@@ -13,5 +15,7 @@ public interface WxpayRefundCallbackMapper {
|
|||||||
|
|
||||||
int updateByPrimaryKeySelective(WxpayRefundCallback record);
|
int updateByPrimaryKeySelective(WxpayRefundCallback record);
|
||||||
|
|
||||||
int updateByPrimaryKey(WxpayRefundCallback record);
|
// int updateByPrimaryKey(WxpayRefundCallback record);
|
||||||
|
|
||||||
|
List<WxpayRefundCallback> selectWxpayRefundCallbackList(List<String> outTradeNoList);
|
||||||
}
|
}
|
||||||
@@ -2,14 +2,18 @@ package com.jsowell.pile.service;
|
|||||||
|
|
||||||
import com.jsowell.pile.domain.WxpayRefundCallback;
|
import com.jsowell.pile.domain.WxpayRefundCallback;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface WxpayRefundCallbackService {
|
public interface WxpayRefundCallbackService {
|
||||||
int deleteByPrimaryKey(Integer id);
|
// int deleteByPrimaryKey(Integer id);
|
||||||
|
|
||||||
int insertSelective(WxpayRefundCallback record);
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,9 +27,11 @@ import com.jsowell.pile.domain.OrderBasicInfo;
|
|||||||
import com.jsowell.pile.domain.OrderDetail;
|
import com.jsowell.pile.domain.OrderDetail;
|
||||||
import com.jsowell.pile.domain.OrderPayRecord;
|
import com.jsowell.pile.domain.OrderPayRecord;
|
||||||
import com.jsowell.pile.domain.WxpayCallbackRecord;
|
import com.jsowell.pile.domain.WxpayCallbackRecord;
|
||||||
|
import com.jsowell.pile.domain.WxpayRefundCallback;
|
||||||
import com.jsowell.pile.dto.IndexQueryDTO;
|
import com.jsowell.pile.dto.IndexQueryDTO;
|
||||||
import com.jsowell.pile.dto.QueryOrderDTO;
|
import com.jsowell.pile.dto.QueryOrderDTO;
|
||||||
import com.jsowell.pile.dto.QueryPersonPileDTO;
|
import com.jsowell.pile.dto.QueryPersonPileDTO;
|
||||||
|
import com.jsowell.pile.dto.RefundableWxPayOrderData;
|
||||||
import com.jsowell.pile.mapper.OrderBasicInfoMapper;
|
import com.jsowell.pile.mapper.OrderBasicInfoMapper;
|
||||||
import com.jsowell.pile.service.IMemberBasicInfoService;
|
import com.jsowell.pile.service.IMemberBasicInfoService;
|
||||||
import com.jsowell.pile.service.IOrderAbnormalRecordService;
|
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.IPileConnectorInfoService;
|
||||||
import com.jsowell.pile.service.WechatPayService;
|
import com.jsowell.pile.service.WechatPayService;
|
||||||
import com.jsowell.pile.service.WxpayCallbackRecordService;
|
import com.jsowell.pile.service.WxpayCallbackRecordService;
|
||||||
|
import com.jsowell.pile.service.WxpayRefundCallbackService;
|
||||||
import com.jsowell.pile.transaction.dto.OrderTransactionDTO;
|
import com.jsowell.pile.transaction.dto.OrderTransactionDTO;
|
||||||
import com.jsowell.pile.transaction.service.TransactionService;
|
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.IndexOrderInfoVO;
|
||||||
import com.jsowell.pile.vo.web.OrderListVO;
|
import com.jsowell.pile.vo.web.OrderListVO;
|
||||||
import com.jsowell.pile.vo.web.OrderTotalDataVO;
|
import com.jsowell.pile.vo.web.OrderTotalDataVO;
|
||||||
@@ -110,6 +116,9 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private IPileConnectorInfoService pileConnectorInfoService;
|
private IPileConnectorInfoService pileConnectorInfoService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WxpayRefundCallbackService wxpayRefundCallbackService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询订单
|
* 查询订单
|
||||||
*
|
*
|
||||||
@@ -768,14 +777,18 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
|||||||
}
|
}
|
||||||
// 退款金额 元转分 123
|
// 退款金额 元转分 123
|
||||||
int refundTotalCents = refundAmount.multiply(new BigDecimal(100)).intValue();
|
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();
|
List<WechatPayRefundRequest> requestList = Lists.newArrayList();
|
||||||
WechatPayRefundRequest request;
|
WechatPayRefundRequest request;
|
||||||
for (WxpayCallbackRecord record : recordList) {
|
for (RefundableWxPayOrderData record : recordList) {
|
||||||
int payerTotal = Integer.parseInt(record.getPayerTotal()); // 该笔支付订单的支付金额,单位分
|
int refundableTotal = record.getRefundableAmount().intValue(); // 该笔支付订单的可退金额,单位分
|
||||||
refundTotalCents = refundTotalCents - payerTotal; // 123 - 100
|
int payerTotal = record.getPayerAmount().intValue(); // 该笔支付订单的支付金额,单位分
|
||||||
|
// 用户申请退款金额-可退金额
|
||||||
|
refundTotalCents = refundTotalCents - refundableTotal; // 123 - 100
|
||||||
request = new WechatPayRefundRequest();
|
request = new WechatPayRefundRequest();
|
||||||
request.setTransaction_id(record.getTransactionId()); // 微信支付单号
|
request.setTransaction_id(record.getTransactionId()); // 微信支付单号
|
||||||
request.setOut_trade_no(record.getOutTradeNo()); // 商户订单号
|
request.setOut_trade_no(record.getOutTradeNo()); // 商户订单号
|
||||||
@@ -786,7 +799,7 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
|||||||
if (refundTotalCents > 0) {
|
if (refundTotalCents > 0) {
|
||||||
// 如果大于0说明,这笔单退完也不够
|
// 如果大于0说明,这笔单退完也不够
|
||||||
WechatPayRefundRequest.Amount amount = new WechatPayRefundRequest.Amount();
|
WechatPayRefundRequest.Amount amount = new WechatPayRefundRequest.Amount();
|
||||||
amount.setRefund(payerTotal); // 退款金额
|
amount.setRefund(refundableTotal); // 退款金额
|
||||||
amount.setTotal(payerTotal); // 原订单金额
|
amount.setTotal(payerTotal); // 原订单金额
|
||||||
request.setAmount(amount);
|
request.setAmount(amount);
|
||||||
requestList.add(request);
|
requestList.add(request);
|
||||||
@@ -795,7 +808,7 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
|||||||
// 生成退款单号
|
// 生成退款单号
|
||||||
WechatPayRefundRequest.Amount amount = new WechatPayRefundRequest.Amount();
|
WechatPayRefundRequest.Amount amount = new WechatPayRefundRequest.Amount();
|
||||||
// 部分退
|
// 部分退
|
||||||
int i = payerTotal + refundTotalCents;
|
int i = refundableTotal + refundTotalCents;
|
||||||
amount.setRefund(i); // 退款金额
|
amount.setRefund(i); // 退款金额
|
||||||
amount.setTotal(payerTotal); // 原订单金额
|
amount.setTotal(payerTotal); // 原订单金额
|
||||||
request.setAmount(amount);
|
request.setAmount(amount);
|
||||||
@@ -817,6 +830,72 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
|||||||
return null;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过订单号查询订单详情
|
* 通过订单号查询订单详情
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.jsowell.pile.service.WxpayRefundCallbackService;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class WxpayRefundCallbackServiceImpl implements WxpayRefundCallbackService {
|
public class WxpayRefundCallbackServiceImpl implements WxpayRefundCallbackService {
|
||||||
@@ -13,10 +14,10 @@ public class WxpayRefundCallbackServiceImpl implements WxpayRefundCallbackServic
|
|||||||
@Resource
|
@Resource
|
||||||
private WxpayRefundCallbackMapper wxpayRefundCallbackMapper;
|
private WxpayRefundCallbackMapper wxpayRefundCallbackMapper;
|
||||||
|
|
||||||
@Override
|
// @Override
|
||||||
public int deleteByPrimaryKey(Integer id) {
|
// public int deleteByPrimaryKey(Integer id) {
|
||||||
return wxpayRefundCallbackMapper.deleteByPrimaryKey(id);
|
// return wxpayRefundCallbackMapper.deleteByPrimaryKey(id);
|
||||||
}
|
// }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int insertSelective(WxpayRefundCallback record) {
|
public int insertSelective(WxpayRefundCallback record) {
|
||||||
@@ -24,18 +25,23 @@ public class WxpayRefundCallbackServiceImpl implements WxpayRefundCallbackServic
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WxpayRefundCallback selectByPrimaryKey(Integer id) {
|
public List<WxpayRefundCallback> selectWxpayRefundCallbackList(List<String> outTradeNoList) {
|
||||||
return wxpayRefundCallbackMapper.selectByPrimaryKey(id);
|
return wxpayRefundCallbackMapper.selectWxpayRefundCallbackList(outTradeNoList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// @Override
|
||||||
public int updateByPrimaryKeySelective(WxpayRefundCallback record) {
|
// public WxpayRefundCallback selectByPrimaryKey(Integer id) {
|
||||||
return wxpayRefundCallbackMapper.updateByPrimaryKeySelective(record);
|
// return wxpayRefundCallbackMapper.selectByPrimaryKey(id);
|
||||||
}
|
// }
|
||||||
|
|
||||||
@Override
|
// @Override
|
||||||
public int updateByPrimaryKey(WxpayRefundCallback record) {
|
// public int updateByPrimaryKeySelective(WxpayRefundCallback record) {
|
||||||
return wxpayRefundCallbackMapper.updateByPrimaryKey(record);
|
// return wxpayRefundCallbackMapper.updateByPrimaryKeySelective(record);
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// public int updateByPrimaryKey(WxpayRefundCallback record) {
|
||||||
|
// return wxpayRefundCallbackMapper.updateByPrimaryKey(record);
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -224,4 +224,14 @@
|
|||||||
create_time = #{createTime,jdbcType=TIMESTAMP}
|
create_time = #{createTime,jdbcType=TIMESTAMP}
|
||||||
where id = #{id,jdbcType=INTEGER}
|
where id = #{id,jdbcType=INTEGER}
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
|
<select id="selectWxpayRefundCallbackList" resultMap="BaseResultMap">
|
||||||
|
select
|
||||||
|
<include refid="Base_Column_List" />
|
||||||
|
from wxpay_refund_callback
|
||||||
|
where out_trade_no in
|
||||||
|
<foreach collection="list" item="item" open="(" separator="," close=")">
|
||||||
|
#{item,jdbcType=VARCHAR}
|
||||||
|
</foreach>
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
Reference in New Issue
Block a user