diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/dto/RefundableWxPayOrderData.java b/jsowell-pile/src/main/java/com/jsowell/pile/dto/RefundableWxPayOrderData.java new file mode 100644 index 000000000..f352f9ce9 --- /dev/null +++ b/jsowell-pile/src/main/java/com/jsowell/pile/dto/RefundableWxPayOrderData.java @@ -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; +} diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/mapper/WxpayRefundCallbackMapper.java b/jsowell-pile/src/main/java/com/jsowell/pile/mapper/WxpayRefundCallbackMapper.java index 29dbd86e0..50bd19a41 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/mapper/WxpayRefundCallbackMapper.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/mapper/WxpayRefundCallbackMapper.java @@ -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 selectWxpayRefundCallbackList(List outTradeNoList); } \ No newline at end of file diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/WxpayRefundCallbackService.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/WxpayRefundCallbackService.java index 9c4112164..2986282a6 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/WxpayRefundCallbackService.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/WxpayRefundCallbackService.java @@ -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 selectWxpayRefundCallbackList(List outTradeNoList); } diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/OrderBasicInfoServiceImpl.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/OrderBasicInfoServiceImpl.java index 0956b2f14..71a46fa0f 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/OrderBasicInfoServiceImpl.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/OrderBasicInfoServiceImpl.java @@ -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 recordList = wxpayCallbackRecordService.queryBalanceRechargeRecordOfTheLatestYear(dto.getMemberId()); + + // 查询用户充值余额订单 过滤掉已经退款的充值订单 refundableOrder + List recordList = queryRefundableOrder(dto.getMemberId()); + // 也许需要多笔支付订单才够退款 List 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 queryRefundableOrder(String memberId) { + List resultList = Lists.newArrayList(); + // 查询最近一年余额充值订单 + List recordList = wxpayCallbackRecordService.queryBalanceRechargeRecordOfTheLatestYear(memberId); + if (CollectionUtils.isEmpty(recordList)) { + return resultList; + } + // 拿到微信商户单号list + List outTradeNoList = recordList.stream() + .map(WxpayCallbackRecord::getOutTradeNo) + .collect(Collectors.toList()); + List 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> refundMap = wxpayRefundCallbacks.stream() + .collect(Collectors.groupingBy(WxpayRefundCallback::getOutTradeNo)); + for (WxpayCallbackRecord wxpayCallbackRecord : recordList) { + List 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; + } + /** * 通过订单号查询订单详情 * diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/WxpayRefundCallbackServiceImpl.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/WxpayRefundCallbackServiceImpl.java index fa2eed663..f79b57da0 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/WxpayRefundCallbackServiceImpl.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/WxpayRefundCallbackServiceImpl.java @@ -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 selectWxpayRefundCallbackList(List 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); + // } } diff --git a/jsowell-pile/src/main/resources/mapper/pile/WxpayRefundCallbackMapper.xml b/jsowell-pile/src/main/resources/mapper/pile/WxpayRefundCallbackMapper.xml index c732fbbff..fac55e0f6 100644 --- a/jsowell-pile/src/main/resources/mapper/pile/WxpayRefundCallbackMapper.xml +++ b/jsowell-pile/src/main/resources/mapper/pile/WxpayRefundCallbackMapper.xml @@ -224,4 +224,14 @@ create_time = #{createTime,jdbcType=TIMESTAMP} where id = #{id,jdbcType=INTEGER} + + \ No newline at end of file