From 7e92d9c7f76777c48f76376c99ffaae2e16fe7ed Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期二, 01 七月 2025 14:37:08 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev' into dev

---
 ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/task/TaskUtil.java |  156 +++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 136 insertions(+), 20 deletions(-)

diff --git a/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/task/TaskUtil.java b/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/task/TaskUtil.java
index 495fdef..2e438e3 100644
--- a/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/task/TaskUtil.java
+++ b/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/task/TaskUtil.java
@@ -1,15 +1,28 @@
 package com.ruoyi.order.task;
 
 
+import com.alibaba.fastjson.JSONObject;
+import com.ruoyi.common.core.constant.WechatConstants;
+import com.ruoyi.common.core.exception.GlobalException;
+import com.ruoyi.common.core.vo.UserDto;
+import com.ruoyi.order.entity.Order;
+import com.ruoyi.order.entity.Withdraw;
 import com.ruoyi.order.entity.WithdrawDetail;
+import com.ruoyi.order.service.OrderService;
 import com.ruoyi.order.service.WithdrawDetailService;
+import com.ruoyi.order.service.WithdrawService;
 import com.ruoyi.order.vx.GetTransferBatchByOutNo;
+import com.ruoyi.order.vx.HttpUtil;
+import com.ruoyi.user.api.feignClient.UserClient;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
 
 import javax.annotation.Resource;
-import java.util.List;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.*;
 
 
 /**
@@ -20,35 +33,52 @@
 
     @Resource
     private WithdrawDetailService withdrawDetailService;
+    @Resource
+    private WithdrawService withdrawService;
 
-
+    @Resource
+    private OrderService orderService;
+    @Autowired
+    private UserClient userClient;
     /**
-     * 每隔一分钟去处理的定时任务
+     * 每隔一小时去处理的定时任务
      */
-    @Scheduled(fixedRate = 10000 * 60)
+    @Scheduled(fixedRate = 1000 * 3600)
     public void taskMinute(){
         try {
-
-            List<WithdrawDetail> list = withdrawDetailService.lambdaQuery().ne(WithdrawDetail::getStatus, "SUCCESS").ne(WithdrawDetail::getStatus, "FAIL").list();
+            List<Withdraw> list1 = withdrawService.lambdaQuery().eq(Withdraw::getId,"1938794177477832705").list();
+            List<WithdrawDetail> list = withdrawDetailService.lambdaQuery()
+                    .isNotNull(WithdrawDetail::getOutBatchNo).ne(WithdrawDetail::getStatus, "SUCCESS").list();
 
             for (WithdrawDetail withdrawDetail : list) {
-                String s = GetTransferBatchByOutNo.checkStatus(withdrawDetail.getOutBatchNo());
-                if (s.equals("SUCCESS")) {
-                    withdrawDetail.setStatus("SUCCESS");
-                    withdrawDetailService.updateById(withdrawDetail);
-                    //执行订单提现成功,增加提现成功金额
-                    //查询订单
+                Withdraw withdraw = list1.stream().filter(e -> e.getId().equals(withdrawDetail.getWithdrawId())).findFirst().orElse(null);
+                if (withdraw==null)continue;
+                Order order = orderService.getById(withdraw.getOrderId());
+                UserDto data = userClient.getUser(order.getUserId()).getData();
+                String s = withdrawDetail.getOutBatchNo();
+                String s1 = HttpUtil.queryTransBatRequest(WechatConstants.WE_CHAT_PAY_QUERY_URL_PRE + s,
+                        "7EEA04429B006E12AAA421C002EC48BBEED5BE94",
+                        "1665330417",
+                        "/usr/local/vx/apiclient_key.pem", WechatConstants.WE_CHAT_QUERY_URL_SUF + s);
+                System.err.println(s1);
+                JSONObject jsonObject = JSONObject.parseObject(s1);
+                String string = jsonObject.getString("state");
+                if (StringUtils.hasLength( string)){
+                    if (string.equals("SUCCESS")) {
+                        order.setIsWithdrawal(3);
+                        orderService.updateById(order);
+                        withdrawDetail.setStatus("SUCCESS");
+                        withdrawDetailService.updateById(withdrawDetail);
+                    } else if (string.equals("FAIL")||string.equals("CANCELING")||string.equals("CANCELLED")) {
+                        // 重新发起一笔转账
+                        withdrawDetail.setStatus("FAIL");
+                        withdrawDetailService.updateById(withdrawDetail);
 
-                    //增加已提现金额
+                        weChatPay(order.getOrderMoney(), data.getOpenId(),withdraw.getId(),order.getServeName());
 
-
-                } else if (s.equals("FAIL")) {
-                    withdrawDetail.setStatus("FAIL");
-                    withdrawDetailService.updateById(withdrawDetail);
-                }else {
-                    withdrawDetail.setStatus(s);
-                    withdrawDetailService.updateById(withdrawDetail);
+                    }
                 }
+
 
             }
 
@@ -57,5 +87,91 @@
         }
     }
 
+    private boolean weChatPay(BigDecimal orderMoney, String openId,String withdrawId,String serverName) {
+        if (com.ruoyi.common.core.utils.StringUtils.isBlank(openId)) {
+            return false;
+        }
+        BigDecimal maxTransferAmount = new BigDecimal("200000"); // 单次转账限额,单位为分
+        int totalTransfers = orderMoney.multiply(new BigDecimal("100")).divide(maxTransferAmount, 0, RoundingMode.UP).intValue();
+        boolean allTransfersSuccessful = true;
+        for (int i = 0; i < totalTransfers; i++) {
+            BigDecimal transferAmount;
+            if (i < totalTransfers - 1) {
+                transferAmount = maxTransferAmount;
+            } else {
+                // 最后一笔转账,金额为剩余金额
+                transferAmount = orderMoney.multiply(new BigDecimal("100")).subtract(maxTransferAmount.multiply(new BigDecimal(i))).setScale(0, RoundingMode.DOWN);
+            }
+
+            Map<String, Object> postMap = new HashMap<>(8);
+            postMap.put(WechatConstants.APP_ID, "wx98563d0ec9cf21c8");
+            // 订单号
+            postMap.put("out_bill_no", String.valueOf(UUID.randomUUID()).replaceAll("-", ""));
+            System.err.println("====="+postMap.get("out_bill_no"));
+            postMap.put(WechatConstants.OPEN_ID, openId);
+            // 转账金额
+            postMap.put("transfer_amount", transferAmount);
+            postMap.put("transfer_scene_id", "1010");
+            // 转账备注
+            postMap.put("transfer_remark", "二手回收提现确认收款");
+            // 回调地址
+            postMap.put("notify_url", "https://hyhsbqgc.com/api/ruoyi-order/wx/wxChatPay");
+            // 转账场景报备信息
+            List<Map<String, Object>> list = new ArrayList<>();
+            Map<String, Object> info = new HashMap<>();
+            info.put("info_type","回收商品名称");
+            info.put("info_content",serverName);
+            list.add(info);
+            postMap.put("transfer_scene_report_infos", list);
+            String result = HttpUtil.postTransBatRequest(
+                    WechatConstants.WE_CHAT_PAY_URL_PRE,
+                    com.alibaba.fastjson2.JSONObject.toJSONString(postMap),
+                    "7EEA04429B006E12AAA421C002EC48BBEED5BE94",
+                    "1665330417",
+                    "/usr/local/vx/apiclient_key.pem", WechatConstants.WE_CHAT_URL_SUF);
+            com.alibaba.fastjson2.JSONObject jsonObject = com.alibaba.fastjson2.JSONObject.parseObject(result);
+//            WithdrawDetail withdrawDetail = new WithdrawDetail();
+//            withdrawDetail.setWithdrawId(withdrawId);
+//            withdrawDetail.setMoney(transferAmount);
+//            withdrawDetail.setOutBatchNo((String) postMap.get(WechatConstants.OUT_BATCH_NO));
+//            withdrawDetailService.save(withdrawDetail);
+            System.err.println(jsonObject);
+//            WithdrawDetail withdrawDetail = new WithdrawDetail();
+//            withdrawDetail.setWithdrawId(withdrawId);
+//            withdrawDetail.setMoney(transferAmount);
+//            withdrawDetail.setOutBatchNo((String) postMap.get(WechatConstants.OUT_BATCH_NO));
+//            withdrawDetailService.save(withdrawDetail);
+            if (jsonObject.containsKey(WechatConstants.CREATE_TIME)) {
+                String string = jsonObject.getString("package_info");
+                Withdraw withdraw = withdrawService.getById(withdrawId);
+
+                Order order = orderService.getById(withdraw.getOrderId());
+                order.setIsWithdrawal(2);
+                order.setPackageInfo(string);
+                orderService.updateById(order);
+                // 转账成功
+                //保存转账明细
+                WithdrawDetail one = withdrawDetailService.lambdaQuery().eq(WithdrawDetail::getWithdrawId, withdrawId).last("limit 1")
+                        .one();
+                if(one!=null){
+                    one.setOutBatchNo(postMap.get("out_bill_no")+"");
+                    one.setStatus("PENDING");
+                    withdrawDetailService.updateById( one);
+                }else{
+                    WithdrawDetail withdrawDetail = new WithdrawDetail();
+                    withdrawDetail.setWithdrawId(withdrawId);
+                    withdrawDetail.setMoney(transferAmount);
+                    withdrawDetail.setOutBatchNo(postMap.get("out_bill_no")+"");
+                    withdrawDetailService.save(withdrawDetail);
+                }
+
+            } else {
+                throw new GlobalException("提现失败,失败原因:"+jsonObject.getString("message"));
+//                allTransfersSuccessful = false;
+//                break;
+            }
+        }
+        return allTransfersSuccessful;
+    }
 
 }

--
Gitblit v1.7.1