Browse Source

优化微信支付HIS入库前的查询

lighter 2 years ago
parent
commit
dd92b9a6d0

+ 1 - 1
pom.xml

@@ -10,7 +10,7 @@
     </parent>
     <groupId>thyyxxk</groupId>
     <artifactId>wxservice-server</artifactId>
-    <version>9.8</version>
+    <version>9.9</version>
     <name>wxservice-server</name>
     <description>server for wxservice-web</description>
 

+ 1 - 2
src/main/java/thyyxxk/wxservice_server/controller/WxApiController.java

@@ -4,7 +4,6 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
-import thyyxxk.wxservice_server.constant.QuerySource;
 import thyyxxk.wxservice_server.entity.ResultVo;
 import thyyxxk.wxservice_server.entity.appointment.DoctorInfo;
 import thyyxxk.wxservice_server.entity.appointment.WeChatPayParam;
@@ -58,7 +57,7 @@ public class WxApiController {
 
     @GetMapping("/queryOrderState")
     public ResultVo<Object> queryOrderState(@RequestParam("tradeNo") String tradeNo) {
-        return service.queryOrderState(tradeNo, QuerySource.INTERFACE);
+        return service.queryOrderState(tradeNo);
     }
 
     @PostMapping("/genMzPayQrcode")

+ 3 - 0
src/main/java/thyyxxk/wxservice_server/dao/WxApiDao.java

@@ -71,6 +71,9 @@ public interface WxApiDao {
             "his_status=0,refund_op_datetime=getdate() where trade_no=#{tradeNo}")
     void refundOrder(@Param("tradeNo") String tradeNo, @Param("rea") String rea);
 
+    @Update("update t_wechat_pay_order set pay_status=4 where trade_no=#{tradeNo}")
+    void updateRefundStatus(@Param("tradeNo") String tradeNo);
+
     @Update("update t_wechat_pay_order set his_status=1,pay_status=1 where trade_no=#{tradeNo}")
     void updateSuccessHisStatus(@Param("tradeNo") String tradeNo);
 

+ 1 - 2
src/main/java/thyyxxk/wxservice_server/scheduled/QueryOrderStateTask.java

@@ -8,7 +8,6 @@ import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
-import thyyxxk.wxservice_server.constant.QuerySource;
 import thyyxxk.wxservice_server.dao.WxApiDao;
 import thyyxxk.wxservice_server.entity.ResultVo;
 import thyyxxk.wxservice_server.entity.scheduled.TradeNo;
@@ -59,7 +58,7 @@ public class QueryOrderStateTask {
                     order.setTradeNo(tradeNo.getTradeNo());
                     service.closeWxOrder(order);
                 } else {
-                    service.queryOrderState(tradeNo.getTradeNo(), QuerySource.SCHEDULE);
+                    service.queryOrderState(tradeNo.getTradeNo());
                 }
             }
         }

+ 6 - 0
src/main/java/thyyxxk/wxservice_server/scheduled/RefreshTask.java

@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 import thyyxxk.wxservice_server.service.AppointmentService;
+import thyyxxk.wxservice_server.utils.TradeVectorUtil;
 
 /**
  * @description: 各种定时刷新任务
@@ -25,4 +26,9 @@ public class RefreshTask {
     public void autoRefreshMzClasses() {
         appointmentService.refreshMzClasses();
     }
+
+    @Scheduled(cron = "0 0 3 * * ?")
+    public void clearRefundedVector() {
+        TradeVectorUtil.clearRefundedVector();
+    }
 }

+ 6 - 1
src/main/java/thyyxxk/wxservice_server/service/SavePayResultService.java

@@ -86,7 +86,7 @@ public class SavePayResultService {
         return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存挂号信息失败,自动退款失败,请联系服务中心进行退款。");
     }
 
-    public ResultVo<Object> saveMzChargeInfo(WxPayOrder order, String successTime) throws InterruptedException {
+    public ResultVo<Object> saveMzChargeInfo(WxPayOrder order, String successTime) throws Exception {
         while (TradeVectorUtil.tradeNoBeingRefund(order.getTradeNo())) {
             TimeUnit.SECONDS.sleep(3);
         }
@@ -97,6 +97,11 @@ public class SavePayResultService {
         }
         Integer status = dao.selectPayStatus(order.getTradeNo());
         if (null != status && status == TradeState.REFUND.getCode()) {
+            TradeVectorUtil.removeRefunded(order.getTradeNo());
+            return ResultVoUtil.success("因系统原因,订单已退款。请稍后重新缴费或前往一楼收费窗口缴费。");
+        }
+        if (TradeVectorUtil.tradeNoRefunded(order.getTradeNo())) {
+            dao.updateRefundStatus(order.getTradeNo());
             return ResultVoUtil.success("因系统原因,订单已退款。请稍后重新缴费或前往一楼收费窗口缴费。");
         }
         JSONObject hrgParam = new JSONObject();

+ 2 - 2
src/main/java/thyyxxk/wxservice_server/service/SpringRetryService.java

@@ -24,7 +24,7 @@ import java.io.IOException;
 @Service
 public class SpringRetryService {
     @Retryable(value = Exception.class, maxAttempts = 5, backoff = @Backoff(delay = 1000, multiplier = 1.5))
-    public JSONObject queryOrderStateFromTencent(String tradeNo, OrderType orderType, QuerySource source) throws IOException {
+    public JSONObject queryOrderStateFromTencent(String tradeNo, OrderType orderType) throws IOException {
         String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" +
                 tradeNo + "?mchid=" + PropertiesUtil.getProperty("mchId");
         HttpGet httpGet = new HttpGet(url);
@@ -34,7 +34,7 @@ public class SpringRetryService {
         String ret = EntityUtils.toString(response.getEntity());
         httpClient.close();
         JSONObject obj = JSONObject.parseObject(ret);
-        log.info("【{}】【{}】查询订单状态结果: {}", source, orderType.getLabel(), obj);
+        log.info("【{}】查询订单状态结果: {}", orderType.getLabel(), obj);
         if (null == obj || null == obj.getString("trade_state")) {
             throw new NullPointerException("spring-retry任务:服务异常,未查询到内容。");
         }

+ 11 - 9
src/main/java/thyyxxk/wxservice_server/service/WxApiService.java

@@ -74,14 +74,16 @@ public class WxApiService {
             if (existOrder.getPayStatus() == TradeState.SUCCESS.getCode()) {
                 return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "您已支付过一笔相同金额的订单,请勿重复支付。");
             }
-            if (existOrder.getPayStatus() == TradeState.NEWORDER.getCode() ||
-                    existOrder.getPayStatus() == TradeState.NOTPAY.getCode()) {
-                if (DateUtil.orderValid(existOrder.getCreateDatetime())) {
-                    return ResultVoUtil.success(existOrder);
+            if (StringUtil.notBlank(existOrder.getPaySign()) && StringUtil.notBlank(existOrder.getPrepayId())) {
+                if (existOrder.getPayStatus() == TradeState.NEWORDER.getCode() ||
+                        existOrder.getPayStatus() == TradeState.NOTPAY.getCode()) {
+                    if (DateUtil.orderValid(existOrder.getCreateDatetime())) {
+                        return ResultVoUtil.success(existOrder);
+                    }
+                }
+                if (null == param.getYjReqNo()) {
+                    param.setYjReqNo(existOrder.getYjReqNo());
                 }
-            }
-            if (null == param.getYjReqNo()) {
-                param.setYjReqNo(existOrder.getYjReqNo());
             }
         }
         String appId = PropertiesUtil.getProperty("appId");
@@ -182,7 +184,7 @@ public class WxApiService {
         }
     }
 
-    public ResultVo<Object> queryOrderState(String tradeNo, QuerySource source) {
+    public ResultVo<Object> queryOrderState(String tradeNo) {
         try {
             while (TradeVectorUtil.tradeNoBeingQuery(tradeNo) || TradeVectorUtil.tradeNoBeingRefund(tradeNo)) {
                 log.info("订单号:{} 正在查询状态中,进入等待区。", tradeNo);
@@ -206,7 +208,7 @@ public class WxApiService {
                 log.info("订单号:{} 的门诊缴费信息已保存,无需再次查询订单状态。", order.getTradeNo());
                 return ResultVoUtil.success("保存门诊缴费信息成功。");
             }
-            JSONObject obj = retryService.queryOrderStateFromTencent(tradeNo, orderType, source);
+            JSONObject obj = retryService.queryOrderStateFromTencent(tradeNo, orderType);
             TradeState tradeState = TradeState.get(obj.getString("trade_state"));
             if (tradeState.equals(TradeState.SUCCESS)) {
                 String successTime = obj.getString("success_time")

+ 1 - 0
src/main/java/thyyxxk/wxservice_server/service/WxRefundService.java

@@ -118,6 +118,7 @@ public class WxRefundService {
         Document document = DocumentHelper.parseText(str);
         Element root = document.getRootElement();
         if ("SUCCESS".equals(root.element("return_code").getStringValue())) {
+            TradeVectorUtil.addRefunded(order.getTradeNo());
             Element refundIdEle = root.element("refund_id");
             if (null == refundIdEle) {
                 String msg = root.element("err_code_des").getStringValue();

+ 17 - 0
src/main/java/thyyxxk/wxservice_server/utils/TradeVectorUtil.java

@@ -5,6 +5,7 @@ import java.util.Vector;
 public class TradeVectorUtil {
     private final static Vector<String> tradeNosBeingQuery = new Vector<>();
     private final static Vector<String> tradeNoBeingRefund = new Vector<>();
+    private final static Vector<String> tradeNoRefunded = new Vector<>();
 
     public static void addBeingQuery(String tradeNo) {
         tradeNosBeingQuery.add(tradeNo);
@@ -29,4 +30,20 @@ public class TradeVectorUtil {
     public static boolean tradeNoBeingRefund(String tradeNo) {
         return tradeNoBeingRefund.contains(tradeNo);
     }
+
+    public static void addRefunded(String tradeNo) {
+        tradeNoRefunded.add(tradeNo);
+    }
+
+    public static void removeRefunded(String tradeNo) {
+        tradeNoRefunded.remove(tradeNo);
+    }
+
+    public static boolean tradeNoRefunded(String tradeNo) {
+        return tradeNoRefunded.contains(tradeNo);
+    }
+
+    public static void clearRefundedVector() {
+        tradeNoRefunded.clear();
+    }
 }