Authored by TANLING

optimize

@@ -3,18 +3,29 @@ package com.yohoufo.order.service.proxy; @@ -3,18 +3,29 @@ package com.yohoufo.order.service.proxy;
3 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
4 import com.yoho.core.config.ConfigReader; 4 import com.yoho.core.config.ConfigReader;
5 import com.yoho.error.exception.ServiceException; 5 import com.yoho.error.exception.ServiceException;
  6 +import com.yoho.tools.docs.Api;
6 import com.yohobuy.ufo.model.order.common.OrderAttributes; 7 import com.yohobuy.ufo.model.order.common.OrderAttributes;
7 import com.yohobuy.ufo.model.order.resp.FastDeliveryReq; 8 import com.yohobuy.ufo.model.order.resp.FastDeliveryReq;
8 import com.yohobuy.ufo.model.order.resp.FastDeliverySellerAccessInfo; 9 import com.yohobuy.ufo.model.order.resp.FastDeliverySellerAccessInfo;
9 import com.yohoufo.common.ApiResponse; 10 import com.yohoufo.common.ApiResponse;
  11 +import com.yohoufo.common.utils.JavaUtils;
  12 +import com.yohoufo.common.utils.PropertyMapper;
  13 +import com.yohoufo.common.utils.StringUtil;
10 import com.yohoufo.dal.order.SellerOrderGoodsMapper; 14 import com.yohoufo.dal.order.SellerOrderGoodsMapper;
11 import com.yohoufo.dal.order.model.SellerOrderGoods; 15 import com.yohoufo.dal.order.model.SellerOrderGoods;
12 import org.apache.commons.lang3.StringUtils; 16 import org.apache.commons.lang3.StringUtils;
13 import org.slf4j.Logger; 17 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory; 18 import org.slf4j.LoggerFactory;
15 import org.springframework.beans.factory.annotation.Autowired; 19 import org.springframework.beans.factory.annotation.Autowired;
  20 +import org.springframework.stereotype.Component;
16 import org.springframework.stereotype.Service; 21 import org.springframework.stereotype.Service;
17 22
  23 +import java.util.Objects;
  24 +import java.util.function.BiFunction;
  25 +import java.util.function.Consumer;
  26 +import java.util.function.Function;
  27 +import java.util.function.Supplier;
  28 +
18 @Service 29 @Service
19 public class FastDeliveryProxyService { 30 public class FastDeliveryProxyService {
20 31
@@ -25,6 +36,15 @@ public class FastDeliveryProxyService { @@ -25,6 +36,15 @@ public class FastDeliveryProxyService {
25 36
26 int Default_Time = 1000; 37 int Default_Time = 1000;
27 38
  39 +
  40 + public static final String LOCK_SKUP_SERVICE_NAME = "fast.delivery.lockSkup";
  41 +
  42 + public static final String DELIVERY_SERVICE_NAME = "fast.delivery.delivery";
  43 +
  44 + public static final String UNLOCK_SKUP_SERVICE_NAME = "fast.delivery.cancel";
  45 +
  46 + public static final String VR_WAYBILL_CODE_FIX = "VR";
  47 +
28 @Autowired 48 @Autowired
29 private SellerOrderGoodsMapper sellerOrderGoodsMapper; 49 private SellerOrderGoodsMapper sellerOrderGoodsMapper;
30 50
@@ -34,17 +54,18 @@ public class FastDeliveryProxyService { @@ -34,17 +54,18 @@ public class FastDeliveryProxyService {
34 public FastDeliverySellerAccessInfo getConfigBySellerUid(int sellerUid){ 54 public FastDeliverySellerAccessInfo getConfigBySellerUid(int sellerUid){
35 55
36 String sellerAccessInfoJSON = configReader.getString("ufo.fast.delivery.seller.access.info", ""); 56 String sellerAccessInfoJSON = configReader.getString("ufo.fast.delivery.seller.access.info", "");
37 - if (StringUtils.isNotBlank(sellerAccessInfoJSON)){ 57 + if (StringUtils.isBlank(sellerAccessInfoJSON)){
  58 + return null;
  59 + }
  60 +
38 try{ 61 try{
39 JSONObject jsonObject = JSONObject.parseObject(sellerAccessInfoJSON); 62 JSONObject jsonObject = JSONObject.parseObject(sellerAccessInfoJSON);
40 return jsonObject.getObject(String.valueOf(sellerUid), FastDeliverySellerAccessInfo.class); 63 return jsonObject.getObject(String.valueOf(sellerUid), FastDeliverySellerAccessInfo.class);
41 }catch (Exception e){ 64 }catch (Exception e){
42 logger.warn("getConfigBySellerUid error {}", e); 65 logger.warn("getConfigBySellerUid error {}", e);
43 - }  
44 - }  
45 -  
46 return null; 66 return null;
47 } 67 }
  68 + }
48 69
49 @Autowired 70 @Autowired
50 private ConfigReader configReader; 71 private ConfigReader configReader;
@@ -54,95 +75,173 @@ public class FastDeliveryProxyService { @@ -54,95 +75,173 @@ public class FastDeliveryProxyService {
54 return configReader.getBoolean("ufo.fast.delivery.toThird", false); 75 return configReader.getBoolean("ufo.fast.delivery.toThird", false);
55 } 76 }
56 77
  78 +
  79 + @Component
  80 + private class FastDeliveryTemplate{
  81 +
  82 +
  83 + private ApiResponse basicExecute(FastDeliveryReq fastDeliveryReq,
  84 + Function<FastDeliverySellerAccessInfo, String> urlFunction,
  85 + Function<String, ApiResponse> caller){
  86 +
  87 + // 检查参数
  88 + if (!isOnToThird()
  89 + || fastDeliveryReq.getSellerUid()==0
  90 + ||fastDeliveryReq.getSkup() == 0) {
  91 + return new ApiResponse();
  92 + }
  93 +
  94 + // 获取配置url
  95 + FastDeliverySellerAccessInfo fastDeliveryConfig = getConfigBySellerUid(fastDeliveryReq.getSellerUid());
  96 + String url;
  97 + if (fastDeliveryConfig == null || StringUtils.isBlank(url = urlFunction.apply(fastDeliveryConfig))) {
  98 + return new ApiResponse();
  99 + }
  100 + fastDeliveryReq.setYhSecret(fastDeliveryConfig.getYhSecret());
  101 +
  102 + // 执行
  103 + return caller.apply(url);
  104 +
  105 + }
  106 +
  107 +
57 /** 108 /**
58 - * 锁库存  
59 - * @param skup 109 + * 执行-对异常进行封装
  110 + * @param fastDeliveryReq 请求参数
  111 + * @param serviceName 服务名称
  112 + * @param urlFunction 获取执行url的function
  113 + * @param exception 封装异常
60 * @return 114 * @return
61 */ 115 */
62 - public ApiResponse lockSkup(FastDeliveryReq fastDeliveryReq) {  
63 - if (!isOnToThird() || fastDeliveryReq.getSellerUid()==0 ||fastDeliveryReq.getSkup() == 0) {  
64 - return new ApiResponse(); 116 + public ApiResponse executeWrapperException(FastDeliveryReq fastDeliveryReq,
  117 + String serviceName,
  118 + Function<FastDeliverySellerAccessInfo, String> urlFunction,
  119 + ServiceException exception){
  120 +
  121 +
  122 + // 获取执行caller
  123 + Function<String, ApiResponse> caller = getCaller(fastDeliveryReq, serviceName);
  124 +
  125 + // 模板化执行 -- 对caller的异常进行分装
  126 + return basicExecute(fastDeliveryReq,
  127 + urlFunction,
  128 + new ExceptionWrapFunction(caller, exception));
  129 +
65 } 130 }
66 131
67 - FastDeliverySellerAccessInfo sellerAccessInfo = getConfigBySellerUid(fastDeliveryReq.getSellerUid()); 132 + /**
  133 + * 执行
  134 + * @param fastDeliveryReq 请求参数
  135 + * @param serviceName 服务名称
  136 + * @param urlFunction 获取执行url的function
  137 + * @return
  138 + */
  139 + public ApiResponse execute(FastDeliveryReq fastDeliveryReq,
  140 + String serviceName,
  141 + Function<FastDeliverySellerAccessInfo, String> urlFunction){
  142 + // 获取执行caller
  143 + Function<String, ApiResponse> caller = getCaller(fastDeliveryReq, serviceName);
  144 +
  145 + // 模板化执行
  146 + return basicExecute(fastDeliveryReq,
  147 + urlFunction,
  148 + caller);
68 149
69 - if (sellerAccessInfo == null || StringUtils.isEmpty(sellerAccessInfo.getLockSkupUrl())) {  
70 - return new ApiResponse();  
71 } 150 }
72 - fastDeliveryReq.setYhSecret(sellerAccessInfo.getYhSecret());  
73 151
74 - logger.info("[{}],[{}] start fast delivery lockSkup {}, body {}", fastDeliveryReq.getSellerUid(),  
75 - fastDeliveryReq.getSkup(), sellerAccessInfo.getLockSkupUrl(), fastDeliveryReq);  
76 - ApiResponse result = null; 152 + private Function<String, ApiResponse> getCaller(FastDeliveryReq fastDeliveryReq, String serviceName) {
  153 + return (url) -> {
  154 + logger.info("[{}],[{}] start fast delivery "+serviceName+" {}, body {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(),
  155 + url, fastDeliveryReq);
  156 +
  157 + ApiResponse result = baseServiceCaller.doPost(serviceName, url, fastDeliveryReq, Default_Time);
  158 + logger.info("[{}],[{}] end fast delivery "+serviceName+", result {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(), result);
  159 + return result;
  160 + };
  161 + }
  162 +
  163 + }
  164 +
  165 +
  166 + private static class ExceptionWrapFunction<T,R> implements Function<T,R> {
  167 +
  168 + private final Function<T, R> function;
  169 +
  170 + private final ServiceException serviceException;
  171 +
  172 + ExceptionWrapFunction(Function<T, R> function, ServiceException e) {
  173 + this.function = function;
  174 + this.serviceException = e;
  175 + }
  176 +
  177 + @Override
  178 + public R apply(T t) {
77 try{ 179 try{
78 - result = baseServiceCaller.doPost("fast.delivery.lockSkup", sellerAccessInfo.getLockSkupUrl(), fastDeliveryReq, Default_Time); 180 + return function.apply(t);
79 }catch (Exception e){ 181 }catch (Exception e){
80 - logger.info("[{}],[{}] error fast delivery lockSkup, {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(), e);  
81 - throw new ServiceException(403, "扣减库存失败"); 182 + throw serviceException;
  183 + }
82 } 184 }
83 - logger.info("[{}],[{}] end fast delivery lockSkup, result {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(), result);  
84 -  
85 - return result;  
86 } 185 }
87 186
  187 +
  188 + @Autowired
  189 + FastDeliveryTemplate fastDeliveryTemplate;
  190 +
88 /** 191 /**
89 - * 下单  
90 - * @param skup  
91 - * @param waybillCode 192 + * 锁库存
92 * @return 193 * @return
93 */ 194 */
94 - public ApiResponse delivery(FastDeliveryReq fastDeliveryReq){  
95 - if (!isOnToThird()|| fastDeliveryReq.getSellerUid()==0 ||fastDeliveryReq.getSkup() == 0){  
96 - return new ApiResponse();  
97 - } 195 + public ApiResponse lockSkup(FastDeliveryReq fastDeliveryReq) {
98 196
99 - FastDeliverySellerAccessInfo sellerAccessInfo = getConfigBySellerUid(fastDeliveryReq.getSellerUid());  
100 197
101 - if (sellerAccessInfo == null || StringUtils.isEmpty(sellerAccessInfo.getDeliveryUrl()) ){  
102 - return new ApiResponse(); 198 + return fastDeliveryTemplate.executeWrapperException(
  199 + fastDeliveryReq,
  200 + LOCK_SKUP_SERVICE_NAME,
  201 + (fastDeliveryConfig) -> getLockSkupUrl(fastDeliveryConfig),
  202 + new ServiceException(403, "扣减库存失败"));
  203 +
103 } 204 }
104 - fastDeliveryReq.setYhSecret(sellerAccessInfo.getYhSecret());  
105 205
106 - logger.info("[{}],[{}] start fast delivery delivery {}, body {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(),  
107 - sellerAccessInfo.getDeliveryUrl(), fastDeliveryReq);  
108 - ApiResponse result = baseServiceCaller.doPost("fast.delivery.delivery", sellerAccessInfo.getDeliveryUrl(), fastDeliveryReq, Default_Time);  
109 - logger.info("[{}],[{}] end fast delivery delivery, result {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(), result);  
110 - return result; 206 + /**
  207 + * 下单
  208 + * @return
  209 + */
  210 + public ApiResponse delivery(FastDeliveryReq fastDeliveryReq){
111 211
  212 + return fastDeliveryTemplate.execute(
  213 + fastDeliveryReq,
  214 + DELIVERY_SERVICE_NAME,
  215 + (fastDeliveryConfig) -> getDeliveryUrl(fastDeliveryConfig));
112 } 216 }
113 217
114 218
115 /** 219 /**
116 * 取消 220 * 取消
117 - * @param skup  
118 - * @param needReShelves 是否需要重新上架  
119 * @return 221 * @return
120 */ 222 */
121 public ApiResponse cancel(FastDeliveryReq fastDeliveryReq){ 223 public ApiResponse cancel(FastDeliveryReq fastDeliveryReq){
122 - if (!isOnToThird() || fastDeliveryReq.getSellerUid()==0 ||fastDeliveryReq.getSkup() == 0){  
123 - return new ApiResponse();  
124 - }  
125 -  
126 - FastDeliverySellerAccessInfo sellerAccessInfo = getConfigBySellerUid(fastDeliveryReq.getSellerUid()); 224 + return fastDeliveryTemplate.execute(
  225 + fastDeliveryReq,
  226 + UNLOCK_SKUP_SERVICE_NAME,
  227 + (fastDeliveryConfig) -> getUnlockSkupUrl(fastDeliveryConfig));
127 228
128 - if (sellerAccessInfo == null || StringUtils.isEmpty(sellerAccessInfo.getUnlockSkupUrl())){  
129 - return new ApiResponse();  
130 } 229 }
131 230
132 - fastDeliveryReq.setYhSecret(sellerAccessInfo.getYhSecret());  
133 231
134 - logger.info("[{}],[{}] start fast delivery cancel {}, body {}", fastDeliveryReq.getSellerUid(),fastDeliveryReq.getSkup(),  
135 - sellerAccessInfo.getUnlockSkupUrl(),sellerAccessInfo); 232 + private String getLockSkupUrl(FastDeliverySellerAccessInfo fastDeliveryConfig){
  233 + return fastDeliveryConfig.getLockSkupUrl();
  234 + }
136 235
137 - ApiResponse result = baseServiceCaller.doPost("fast.delivery.cancel", sellerAccessInfo.getUnlockSkupUrl(),  
138 - fastDeliveryReq, Default_Time);  
139 - logger.info("[{}],[{}] end fast delivery cancel, result {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(), result);  
140 - return result;  
141 236
  237 + private String getDeliveryUrl(FastDeliverySellerAccessInfo fastDeliveryConfig){
  238 + return fastDeliveryConfig.getDeliveryUrl();
142 } 239 }
143 240
144 241
145 - public static final String VR_WAYBILL_CODE_FIX = "VR"; 242 + private String getUnlockSkupUrl(FastDeliverySellerAccessInfo fastDeliveryConfig){
  243 + return fastDeliveryConfig.getUnlockSkupUrl();
  244 + }
146 245
147 246
148 /** 247 /**