From 49e81b765077fdba50c7c679e0abdb5f188f8037 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期三, 19 五月 2021 16:59:13 +0800
Subject: [PATCH] 优化代码

---
 service-front/src/main/java/com/java110/front/smo/payment/impl/ToPaySMOImpl.java |   79 +++++++++++++++++++++++++++++++++++----
 1 files changed, 71 insertions(+), 8 deletions(-)

diff --git a/service-front/src/main/java/com/java110/front/smo/payment/impl/ToPaySMOImpl.java b/service-front/src/main/java/com/java110/front/smo/payment/impl/ToPaySMOImpl.java
old mode 100644
new mode 100755
index f1afcd6..d81e222
--- a/service-front/src/main/java/com/java110/front/smo/payment/impl/ToPaySMOImpl.java
+++ b/service-front/src/main/java/com/java110/front/smo/payment/impl/ToPaySMOImpl.java
@@ -1,12 +1,23 @@
 package com.java110.front.smo.payment.impl;
 
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.java110.core.context.IPageData;
+import com.java110.core.context.PageData;
+import com.java110.dto.app.AppDto;
+import com.java110.dto.owner.OwnerAppUserDto;
+import com.java110.dto.smallWeChat.SmallWeChatDto;
 import com.java110.front.properties.WechatAuthProperties;
 import com.java110.front.smo.AppAbstractComponentSMO;
 import com.java110.front.smo.payment.IToPaySMO;
-import com.java110.core.context.IPageData;
+import com.java110.front.smo.payment.adapt.IPayAdapt;
+import com.java110.utils.cache.MappingCache;
 import com.java110.utils.constant.ServiceConstant;
+import com.java110.utils.constant.WechatConstant;
+import com.java110.utils.factory.ApplicationContextFactory;
 import com.java110.utils.util.Assert;
+import com.java110.utils.util.BeanConvertUtil;
+import com.java110.utils.util.StringUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -24,8 +35,14 @@
     private static final Logger logger = LoggerFactory.getLogger(AppAbstractComponentSMO.class);
 
 
+
+
     @Autowired
     private RestTemplate restTemplate;
+
+    @Autowired
+    private RestTemplate outRestTemplate;
+
 
     @Autowired
     private WechatAuthProperties wechatAuthProperties;
@@ -43,6 +60,7 @@
         Assert.jsonObjectHaveKey(paramIn, "receivedAmount", "璇锋眰鎶ユ枃涓湭鍖呭惈receivedAmount鑺傜偣");
         Assert.jsonObjectHaveKey(paramIn, "feeId", "璇锋眰鎶ユ枃涓湭鍖呭惈feeId鑺傜偣");
         Assert.jsonObjectHaveKey(paramIn, "feeName", "璇锋眰鎶ユ枃涓湭鍖呭惈feeName鑺傜偣");
+        Assert.jsonObjectHaveKey(paramIn, "appId", "璇锋眰鎶ユ枃涓湭鍖呭惈appId鑺傜偣");
 
     }
 
@@ -50,6 +68,17 @@
     protected ResponseEntity<String> doBusinessProcess(IPageData pd, JSONObject paramIn) throws Exception {
 
         ResponseEntity responseEntity = null;
+
+        SmallWeChatDto smallWeChatDto = getSmallWechat(pd, paramIn);
+
+        if (smallWeChatDto == null) { //浠庨厤缃枃浠朵腑鑾峰彇 灏忕▼搴忛厤缃俊鎭�
+            smallWeChatDto = new SmallWeChatDto();
+            smallWeChatDto.setAppId(wechatAuthProperties.getAppId());
+            smallWeChatDto.setAppSecret(wechatAuthProperties.getSecret());
+            smallWeChatDto.setMchId(wechatAuthProperties.getMchId());
+            smallWeChatDto.setPayPassword(wechatAuthProperties.getKey());
+        }
+
         //鏌ヨ鐢ㄦ埛ID
         paramIn.put("userId", pd.getUserId());
         String url = ServiceConstant.SERVICE_API_URL + "/api/fee.payFeePre";
@@ -60,10 +89,19 @@
         }
         JSONObject orderInfo = JSONObject.parseObject(responseEntity.getBody().toString());
         String orderId = orderInfo.getString("oId");
-        double money = Double.parseDouble(orderInfo.getString("receivableAmount"));
+        double money = Double.parseDouble(orderInfo.getString("receivedAmount"));
+        String appType = OwnerAppUserDto.APP_TYPE_WECHAT_MINA;
+        if (AppDto.WECHAT_OWNER_APP_ID.equals(pd.getAppId())) {
+            appType = OwnerAppUserDto.APP_TYPE_WECHAT;
+        } else if (AppDto.WECHAT_MINA_OWNER_APP_ID.equals(pd.getAppId())) {
+            appType = OwnerAppUserDto.APP_TYPE_WECHAT_MINA;
+        } else {
+            appType = OwnerAppUserDto.APP_TYPE_APP;
+        }
         Map tmpParamIn = new HashMap();
         tmpParamIn.put("userId", pd.getUserId());
-        responseEntity = super.getUserAndAttr(pd, restTemplate, tmpParamIn);
+        tmpParamIn.put("appType", appType);
+        responseEntity = super.getOwnerAppUser(pd, restTemplate, tmpParamIn);
         logger.debug("鏌ヨ鐢ㄦ埛淇℃伅杩斿洖鎶ユ枃锛�" + responseEntity);
         if (responseEntity.getStatusCode() != HttpStatus.OK) {
             throw new IllegalArgumentException("鏈煡璇㈢敤鎴蜂俊鎭紓甯�" + tmpParamIn);
@@ -76,17 +114,42 @@
             throw new IllegalArgumentException("鏈煡璇㈠井淇$敤鎴�");
         }
 
-        JSONObject realUserInfo = userResult.getJSONArray("users").getJSONObject(0);
-
+        JSONObject realUserInfo = userResult.getJSONArray("data").getJSONObject(0);
         String openId = realUserInfo.getString("openId");
-
-        //寰俊涓嬪崟PayUtil
-        Map result = super.java110Payment(restTemplate,paramIn.getString("feeName"),paramIn.getString("tradeType"), orderId, money, openId);
+        String payAdapt = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.PAY_ADAPT);
+        payAdapt = StringUtil.isEmpty(payAdapt) ? DEFAULT_PAY_ADAPT : payAdapt;
+        //鏀粯閫傞厤鍣�
+        IPayAdapt tPayAdapt = ApplicationContextFactory.getBean(payAdapt, IPayAdapt.class);
+        Map result = tPayAdapt.java110Payment(outRestTemplate, paramIn.getString("feeName"), paramIn.getString("tradeType"), orderId, money, openId, smallWeChatDto);
         responseEntity = new ResponseEntity(JSONObject.toJSONString(result), HttpStatus.OK);
 
         return responseEntity;
     }
 
 
+    private SmallWeChatDto getSmallWechat(IPageData pd, JSONObject paramIn) {
+
+        ResponseEntity responseEntity = null;
+
+        pd = PageData.newInstance().builder(pd.getUserId(), "", "", pd.getReqData(),
+                "", "", "", "",
+                pd.getAppId());
+        responseEntity = this.callCenterService(restTemplate, pd, "",
+                ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId="
+                        + paramIn.getString("appId") + "&page=1&row=1&communityId="+paramIn.getString("communityId"), HttpMethod.GET);
+
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            return null;
+        }
+        JSONObject smallWechatObj = JSONObject.parseObject(responseEntity.getBody().toString());
+        JSONArray smallWeChats = smallWechatObj.getJSONArray("smallWeChats");
+
+        if (smallWeChats == null || smallWeChats.size() < 1) {
+            return null;
+        }
+
+        return BeanConvertUtil.covertBean(smallWeChats.get(0), SmallWeChatDto.class);
+    }
+
 
 }

--
Gitblit v1.8.0