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,16 +54,17 @@ public class FastDeliveryProxyService { @@ -34,16 +54,17 @@ 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)){  
38 - try{  
39 - JSONObject jsonObject = JSONObject.parseObject(sellerAccessInfoJSON);  
40 - return jsonObject.getObject(String.valueOf(sellerUid), FastDeliverySellerAccessInfo.class);  
41 - }catch (Exception e){  
42 - logger.warn("getConfigBySellerUid error {}", e);  
43 - } 57 + if (StringUtils.isBlank(sellerAccessInfoJSON)){
  58 + return null;
44 } 59 }
45 60
46 - return null; 61 + try{
  62 + JSONObject jsonObject = JSONObject.parseObject(sellerAccessInfoJSON);
  63 + return jsonObject.getObject(String.valueOf(sellerUid), FastDeliverySellerAccessInfo.class);
  64 + }catch (Exception e){
  65 + logger.warn("getConfigBySellerUid error {}", e);
  66 + return null;
  67 + }
47 } 68 }
48 69
49 @Autowired 70 @Autowired
@@ -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
57 - /**  
58 - * 锁库存  
59 - * @param skup  
60 - * @return  
61 - */  
62 - public ApiResponse lockSkup(FastDeliveryReq fastDeliveryReq) {  
63 - if (!isOnToThird() || fastDeliveryReq.getSellerUid()==0 ||fastDeliveryReq.getSkup() == 0) {  
64 - return new ApiResponse(); 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 +
65 } 105 }
66 106
67 - FastDeliverySellerAccessInfo sellerAccessInfo = getConfigBySellerUid(fastDeliveryReq.getSellerUid());  
68 107
69 - if (sellerAccessInfo == null || StringUtils.isEmpty(sellerAccessInfo.getLockSkupUrl())) {  
70 - return new ApiResponse(); 108 + /**
  109 + * 执行-对异常进行封装
  110 + * @param fastDeliveryReq 请求参数
  111 + * @param serviceName 服务名称
  112 + * @param urlFunction 获取执行url的function
  113 + * @param exception 封装异常
  114 + * @return
  115 + */
  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 +
71 } 130 }
72 - fastDeliveryReq.setYhSecret(sellerAccessInfo.getYhSecret());  
73 131
74 - logger.info("[{}],[{}] start fast delivery lockSkup {}, body {}", fastDeliveryReq.getSellerUid(),  
75 - fastDeliveryReq.getSkup(), sellerAccessInfo.getLockSkupUrl(), fastDeliveryReq);  
76 - ApiResponse result = null;  
77 - try{  
78 - result = baseServiceCaller.doPost("fast.delivery.lockSkup", sellerAccessInfo.getLockSkupUrl(), fastDeliveryReq, Default_Time);  
79 - }catch (Exception e){  
80 - logger.info("[{}],[{}] error fast delivery lockSkup, {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(), e);  
81 - throw new ServiceException(403, "扣减库存失败"); 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);
  149 +
  150 + }
  151 +
  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 + };
82 } 161 }
83 - logger.info("[{}],[{}] end fast delivery lockSkup, result {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(), result);  
84 162
85 - return result;  
86 } 163 }
87 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) {
  179 + try{
  180 + return function.apply(t);
  181 + }catch (Exception e){
  182 + throw serviceException;
  183 + }
  184 + }
  185 + }
  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();  
103 - }  
104 - fastDeliveryReq.setYhSecret(sellerAccessInfo.getYhSecret()); 198 + return fastDeliveryTemplate.executeWrapperException(
  199 + fastDeliveryReq,
  200 + LOCK_SKUP_SERVICE_NAME,
  201 + (fastDeliveryConfig) -> getLockSkupUrl(fastDeliveryConfig),
  202 + new ServiceException(403, "扣减库存失败"));
105 203
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; 204 + }
111 205
  206 + /**
  207 + * 下单
  208 + * @return
  209 + */
  210 + public ApiResponse delivery(FastDeliveryReq fastDeliveryReq){
  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 - } 224 + return fastDeliveryTemplate.execute(
  225 + fastDeliveryReq,
  226 + UNLOCK_SKUP_SERVICE_NAME,
  227 + (fastDeliveryConfig) -> getUnlockSkupUrl(fastDeliveryConfig));
125 228
126 - FastDeliverySellerAccessInfo sellerAccessInfo = getConfigBySellerUid(fastDeliveryReq.getSellerUid());  
127 -  
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 /**