dcs 2 月之前
父节点
当前提交
34b2a5a1e4

+ 0 - 2
virgo.api/src/main/java/com/bosshand/virgo/api/Application.java

@@ -5,7 +5,6 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.openfeign.EnableFeignClients;
 import org.springframework.context.annotation.ComponentScan;
-import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
 
 @EnableFeignClients
@@ -14,7 +13,6 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
 @EnableTransactionManagement
 @MapperScan("com.bosshand.virgo.core.dao,com.bosshand.virgo.api.dao,com.bosshand.virgo.api.operate.dao,com.bosshand.virgo.api.test.dao,com.bosshand.virgo.api.workark.dao")
 @ComponentScan(basePackages = {"com.bosshand"})
-@EnableScheduling
 public class Application {
 	public static void main(String[] args) {
 		SpringApplication.run(Application.class, args);

+ 18 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/service/QuartzService.java

@@ -6,6 +6,7 @@ import com.bosshand.virgo.api.model.JobInfo;
 import com.bosshand.virgo.api.model.Project;
 import com.bosshand.virgo.api.model.Reminder;
 import com.bosshand.virgo.api.model.RemoteReminder;
+import com.bosshand.virgo.api.workark.model.OrderInfo;
 import org.quartz.*;
 import org.quartz.impl.matchers.GroupMatcher;
 import org.slf4j.Logger;
@@ -89,6 +90,23 @@ public class QuartzService {
         deleteCronJob(project.getId() + "_" + project.getName(), DEFAULT_JOB_GROUP, TRIGGER_PRE + project.getId() + "_" + project.getName(), DEFAULT_TRIGGER_GROUP);
     }
 
+    /**
+     * 关闭订单任务
+     */
+    public void executeWxPayCloseOrderTask(OrderInfo orderInfo){
+
+        String jobName = orderInfo.getId() + "_" + orderInfo.getOrderNo();
+        String jobClassName = "com.bosshand.virgo.api.workark.task.WxPayCloseOrderTask";
+
+        Instant futureTime = orderInfo.getCodeUrlFailureTime().toInstant();
+
+        // 计算延迟时间(毫秒)
+        long delay = Duration.between(Instant.now(), futureTime).toMillis();
+
+        // 执行任务
+        executeOnce(delay, jobName, jobClassName, JSONObject.toJSONString(orderInfo));
+    }
+
     /**
      * 提醒任务
      */

+ 0 - 1
virgo.api/src/main/java/com/bosshand/virgo/api/workark/dao/OrderInfoDao.java

@@ -33,5 +33,4 @@ public interface OrderInfoDao {
 
     OrderInfo getNoPayOrderByProductId(Long productId, Long userId, String orderStatus);
 
-    List<OrderInfo> selectList(OrderInfo orderInfo);
 }

+ 3 - 11
virgo.api/src/main/java/com/bosshand/virgo/api/workark/service/OrderInfoService.java

@@ -153,8 +153,9 @@ public class OrderInfoService {
      * 存储订单二维码
      * @param orderNo
      * @param codeUrl
+     * @return OrderInfo
      */
-    public void saveCodeUrl(String orderNo, String codeUrl) {
+    public OrderInfo saveCodeUrl(String orderNo, String codeUrl) {
         OrderInfo orderInfo = orderInfoDao.getOrderNo(orderNo);
         orderInfo.setCodeUrl(codeUrl);
         // 设置110分钟过期
@@ -163,6 +164,7 @@ public class OrderInfoService {
         Date date = Date.from(zonedDateTime.toInstant());
         orderInfo.setCodeUrlFailureTime(date);
         orderInfoDao.update(orderInfo);
+        return orderInfo;
     }
 
     /**
@@ -193,16 +195,6 @@ public class OrderInfoService {
         orderInfoDao.update(orderInfo);
     }
 
-    /**
-     * 查询支付二维码到达过期时间未支付的订单
-     */
-    public List<OrderInfo> getNoPayOrderByDuration() {
-        OrderInfo orderInfo = new OrderInfo();
-        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
-        List<OrderInfo> orderInfoList = orderInfoDao.selectList(orderInfo);
-        return orderInfoList;
-    }
-
     public JSONObject calculateDetails(long productId, List<ProductCoupon> productCouponList) {
         // 获取商品信息
         Product product = productDao.get(productId);

+ 9 - 2
virgo.api/src/main/java/com/bosshand/virgo/api/workark/service/WxPayService.java

@@ -1,6 +1,7 @@
 package com.bosshand.virgo.api.workark.service;
 
 import com.alibaba.fastjson.JSONObject;
+import com.bosshand.virgo.api.service.QuartzService;
 import com.bosshand.virgo.api.workark.config.WxPayConfig;
 import com.bosshand.virgo.api.workark.enums.OrderStatus;
 import com.bosshand.virgo.api.workark.enums.wxpay.WxNotifyType;
@@ -56,6 +57,9 @@ public class WxPayService {
     @Autowired
     private RefundInfoService refundInfoService;
 
+    @Autowired
+    private QuartzService quartzService;
+
     private final ReentrantLock lock = new ReentrantLock();
 
     private final static Log log = LogFactory.getLog(WxPayService.class);
@@ -144,12 +148,15 @@ public class WxPayService {
         codeUrl = response.getCodeUrl();
 
         //保存二维码
-        orderInfoService.saveCodeUrl(orderInfo.getOrderNo(), codeUrl);
+        OrderInfo orderInfo1 = orderInfoService.saveCodeUrl(orderInfo.getOrderNo(), codeUrl);
+
+        //支付超时关闭订单
+        quartzService.executeWxPayCloseOrderTask(orderInfo1);
 
         //返回二维码
         Map<String, Object> map = new HashMap<>();
         map.put("codeUrl", codeUrl);
-        map.put("codeUrlFailureTime", orderInfo.getCodeUrlFailureTime());
+        map.put("codeUrlFailureTime", orderInfo1.getCodeUrlFailureTime());
         map.put("orderNo", orderInfo.getOrderNo());
         map.put("base64", QrRcodeGenUtil.jumpToQRcodeGen(codeUrl));
         return map;

+ 48 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/workark/task/WxPayCloseOrderTask.java

@@ -0,0 +1,48 @@
+package com.bosshand.virgo.api.workark.task;
+
+import com.alibaba.fastjson.JSONObject;
+import com.bosshand.virgo.api.workark.enums.OrderStatus;
+import com.bosshand.virgo.api.workark.model.OrderInfo;
+import com.bosshand.virgo.api.workark.service.OrderInfoService;
+import com.bosshand.virgo.api.workark.service.WxPayService;
+import org.quartz.JobDataMap;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.quartz.QuartzJobBean;
+
+public class WxPayCloseOrderTask extends QuartzJobBean {
+
+    static Logger log = LoggerFactory.getLogger(WxPayCloseOrderTask.class);
+
+    @Autowired
+    private OrderInfoService orderInfoService;
+
+    @Autowired
+    private WxPayService wxPayService;
+
+    @Override
+    public void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+        log.info("orderConfirm 被执行......");
+
+        JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
+        String data = mergedJobDataMap.getString("data");
+        JSONObject json = JSONObject.parseObject(data);
+        long id = json.getLongValue("id");
+        OrderInfo orderInfo = orderInfoService.get(id);
+        if (orderInfo != null) {
+            if (OrderStatus.NOTPAY.getType().equals(orderInfo.getOrderStatus())) {
+                log.warn("超时订单 ===>" + orderInfo.getOrderNo());
+                //核实订单状态:调用微信支付查单接口
+                try {
+                    wxPayService.checkOrderClosed(orderInfo.getOrderNo());
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+}

+ 0 - 83
virgo.api/src/main/java/com/bosshand/virgo/api/workark/task/WxPayTask.java

@@ -1,83 +0,0 @@
-package com.bosshand.virgo.api.workark.task;
-
-import com.bosshand.virgo.api.workark.model.OrderInfo;
-import com.bosshand.virgo.api.workark.model.RefundInfo;
-import com.bosshand.virgo.api.workark.service.OrderInfoService;
-import com.bosshand.virgo.api.workark.service.RefundInfoService;
-import com.bosshand.virgo.api.workark.service.WxPayService;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.scheduling.annotation.Scheduled;
-import org.springframework.stereotype.Component;
-
-import java.util.List;
-
-@Component
-public class WxPayTask {
-
-    private final static Log log = LogFactory.getLog(WxPayTask.class);
-
-    @Autowired
-    private OrderInfoService orderInfoService;
-
-    @Autowired
-    private WxPayService wxPayService;
-
-    @Autowired
-    private RefundInfoService refundInfoService;
-
-    /**
-     * 秒 分 时 日 月 周
-     * 以秒为例
-     * *:每秒都执行
-     * 1-3:从第1秒开始执行,到第3秒结束执行
-     * 0/3:从第0秒开始,每隔3秒执行1次
-     * 1,2,3:在指定的第1、2、3秒执行
-     * ?:不指定
-     * 日和周不能同时制定,指定其中之一,则另一个设置为?
-     */
-    //@Scheduled(cron = "0/3 * * * * ?")
-    public void task1(){
-        log.info("task1 被执行......");
-    }
-
-    /**
-     * 从第0秒开始每隔30秒执行1次,查询到二维码过期时间,并且未支付的订单
-     */
-    @Scheduled(cron = "0/30 * * * * ?")
-    public void orderConfirm() throws Exception {
-        log.info("orderConfirm 被执行......");
-
-        List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration();
-
-        for (OrderInfo orderInfo : orderInfoList) {
-            String orderNo = orderInfo.getOrderNo();
-            log.warn("超时订单 ===>"+orderNo);
-
-            //核实订单状态:调用微信支付查单接口
-            wxPayService.checkOrderClosed(orderNo);
-        }
-    }
-
-
-    /**
-     * 从第0秒开始每隔30秒执行1次,查询创建超过5分钟,并且未成功的退款单
-     */
-    //@Scheduled(cron = "0/30 * * * * ?")
-    public void refundConfirm() throws Exception {
-        log.info("refundConfirm 被执行......");
-
-        //找出申请退款超过5分钟并且未成功的退款单
-        List<RefundInfo> refundInfoList = refundInfoService.getNoRefundOrderByDuration(1);
-
-        for (RefundInfo refundInfo : refundInfoList) {
-            String refundNo = refundInfo.getRefundNo();
-            log.warn("超时未退款的退款单号 ===>"+ refundNo);
-
-            //核实订单状态:调用微信支付查询退款接口
-            wxPayService.checkRefundStatus(refundNo);
-        }
-    }
-
-}

+ 0 - 4
virgo.api/src/main/resources/mapper/OrderInfoMapper.xml

@@ -37,10 +37,6 @@
         select * from workark_orderInfo where productId = #{productId} AND userId =#{userId} AND orderStatus=#{orderStatus}
     </select>
 
-    <select id="selectList" resultMap="result">
-        select * from workark_orderInfo where codeUrl IS NOT NULL and orderStatus = #{orderStatus} and codeUrlFailureTime &lt; now()
-    </select>
-
     <select id="getList" resultMap="result">
         select * from workark_orderInfo
         <where>