From d21b473a1a9ecae5c6e98e849eaa491ccb4c845b Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期五, 08 十一月 2024 14:03:44 +0800
Subject: [PATCH] add:钱包功能调整1108

---
 src/main/java/com/mzl/flower/service/payment/UserPaymentV3Service.java |   56 +++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/mzl/flower/service/payment/UserPaymentV3Service.java b/src/main/java/com/mzl/flower/service/payment/UserPaymentV3Service.java
index 3640a32..911682e 100644
--- a/src/main/java/com/mzl/flower/service/payment/UserPaymentV3Service.java
+++ b/src/main/java/com/mzl/flower/service/payment/UserPaymentV3Service.java
@@ -9,6 +9,7 @@
 import com.mzl.flower.dto.request.payment.TransferDetailReqDTO;
 import com.mzl.flower.dto.request.payment.TransferReqDTO;
 import com.mzl.flower.dto.request.payment.UserPaymentDTO;
+import com.mzl.flower.entity.flower.Flower;
 import com.mzl.flower.entity.payment.*;
 import com.mzl.flower.entity.system.UserWechat;
 import com.mzl.flower.mapper.flower.FlowerMapper;
@@ -114,6 +115,9 @@
 
     @Autowired
     private CouponRecordService couponRecordService;
+
+    @Autowired
+    private OrderService orderService;
 
     /**
      * 微信预支付
@@ -474,6 +478,22 @@
     }
 
     public Map payAgain(String orderId){
+        // 获取订单里面的商品是否有限购的,如果有则判断是否已经超过限购数量
+        List<OrderItem> orderItemList = orderItemMapper.selectList(new QueryWrapper<OrderItem>()
+                .eq("order_id", orderId));
+
+        orderItemList.forEach(orderItem -> {
+            // 限购数量 鲜花数量校验
+            Integer completeNumToday=orderService.getFlowerCompleteNumToday(orderItem.getCreateBy(),orderItem.getFlowerId());
+            Integer tmp=completeNumToday+orderItem.getNum();
+            Flower flower=flowerMapper.selectById(orderItem.getFlowerId());
+            if(null!=flower.getLimited() && tmp.compareTo(flower.getLimited())>0){
+                throw new ValidationException("商品:'"+flower.getName()+"' 昨天17:00到今天17:00 超过限售数量:"+flower.getLimited()+"!");
+            }
+
+        });
+
+
         UserPayment up = userPaymentMapper.selectOne(
                 new QueryWrapper<UserPayment>().eq("order_id", orderId));
         if(StringUtils.isNotEmpty(up.getStatus())){
@@ -520,15 +540,6 @@
         request.setTransactionId(o.getPaymentTrId());
         request.setNotifyUrl(PyamentV3Configurer.notify_url_refund);
 
-        AmountReq amount = new AmountReq();
-        int oa = prepareAmount(o.getPaymentAmount());
-        amount.setTotal((long)oa);
-        amount.setRefund((long)oa);
-        amount.setCurrency("CNY");
-        request.setAmount(amount);
-        // 调用接口
-        refundService.create(request);
-
         o.setRefundAmount(o.getPaymentAmount());
         o.setRefundNo(request.getOutRefundNo());
         o.setRefundTime(LocalDateTime.now());
@@ -542,6 +553,16 @@
         deliveryOrderService.refundDelete(orderId);
 
         releasePrepayLock(o);
+
+        //最后调用微信退款接口,防止回滚
+        AmountReq amount = new AmountReq();
+        int oa = prepareAmount(o.getPaymentAmount());
+        amount.setTotal((long)oa);
+        amount.setRefund((long)oa);
+        amount.setCurrency("CNY");
+        request.setAmount(amount);
+        // 调用接口
+        refundService.create(request);
     }
 
     public String refundOrderSub(Order o, BigDecimal refundAmount){
@@ -682,8 +703,8 @@
         InitiateBatchTransferRequest request = new InitiateBatchTransferRequest();
         request.setAppid(dto.getAppId());
         request.setOutBatchNo(transfer.getId());//【商家批次单号】 商户系统内部的商家批次单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一
-        request.setBatchName(transfer.getName());//【批次名称】 该笔批量转账的名称
-        request.setBatchRemark(transfer.getRemarks());//【批次备注】 转账说明,UTF8编码,最多允许32个字符
+        request.setBatchName(subString(transfer.getName(), 32));//【批次名称】 该笔批量转账的名称
+        request.setBatchRemark(subString(transfer.getRemarks(), 32));//【批次备注】 转账说明,UTF8编码,最多允许32个字符
 
         List<TransferDetailReqDTO> details = dto.getDetails();
         transfer.setTotalNum(details.size());
@@ -728,6 +749,19 @@
         return transfer.getId();
     }
 
+    private String subString(String str, int len){
+        if(StringUtils.isEmpty(str)){
+            return str;
+        }
+
+        int length = str.length();
+        if(length > len){
+            return str.substring(0, len - 1);
+        }
+
+        return str;
+    }
+
     public void checkTransferStatus(Transfer transfer) {//定时任务验证转账状态
         GetTransferBatchByOutNoRequest request = new GetTransferBatchByOutNoRequest();
         request.setOutBatchNo(transfer.getId());

--
Gitblit v1.9.3