Authored by TANLING

optimize

... ... @@ -3,18 +3,29 @@ package com.yohoufo.order.service.proxy;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.config.ConfigReader;
import com.yoho.error.exception.ServiceException;
import com.yoho.tools.docs.Api;
import com.yohobuy.ufo.model.order.common.OrderAttributes;
import com.yohobuy.ufo.model.order.resp.FastDeliveryReq;
import com.yohobuy.ufo.model.order.resp.FastDeliverySellerAccessInfo;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.common.utils.JavaUtils;
import com.yohoufo.common.utils.PropertyMapper;
import com.yohoufo.common.utils.StringUtil;
import com.yohoufo.dal.order.SellerOrderGoodsMapper;
import com.yohoufo.dal.order.model.SellerOrderGoods;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
@Service
public class FastDeliveryProxyService {
... ... @@ -25,6 +36,15 @@ public class FastDeliveryProxyService {
int Default_Time = 1000;
public static final String LOCK_SKUP_SERVICE_NAME = "fast.delivery.lockSkup";
public static final String DELIVERY_SERVICE_NAME = "fast.delivery.delivery";
public static final String UNLOCK_SKUP_SERVICE_NAME = "fast.delivery.cancel";
public static final String VR_WAYBILL_CODE_FIX = "VR";
@Autowired
private SellerOrderGoodsMapper sellerOrderGoodsMapper;
... ... @@ -34,16 +54,17 @@ public class FastDeliveryProxyService {
public FastDeliverySellerAccessInfo getConfigBySellerUid(int sellerUid){
String sellerAccessInfoJSON = configReader.getString("ufo.fast.delivery.seller.access.info", "");
if (StringUtils.isNotBlank(sellerAccessInfoJSON)){
try{
JSONObject jsonObject = JSONObject.parseObject(sellerAccessInfoJSON);
return jsonObject.getObject(String.valueOf(sellerUid), FastDeliverySellerAccessInfo.class);
}catch (Exception e){
logger.warn("getConfigBySellerUid error {}", e);
}
if (StringUtils.isBlank(sellerAccessInfoJSON)){
return null;
}
return null;
try{
JSONObject jsonObject = JSONObject.parseObject(sellerAccessInfoJSON);
return jsonObject.getObject(String.valueOf(sellerUid), FastDeliverySellerAccessInfo.class);
}catch (Exception e){
logger.warn("getConfigBySellerUid error {}", e);
return null;
}
}
@Autowired
... ... @@ -54,95 +75,173 @@ public class FastDeliveryProxyService {
return configReader.getBoolean("ufo.fast.delivery.toThird", false);
}
/**
* 锁库存
* @param skup
* @return
*/
public ApiResponse lockSkup(FastDeliveryReq fastDeliveryReq) {
if (!isOnToThird() || fastDeliveryReq.getSellerUid()==0 ||fastDeliveryReq.getSkup() == 0) {
return new ApiResponse();
@Component
private class FastDeliveryTemplate{
private ApiResponse basicExecute(FastDeliveryReq fastDeliveryReq,
Function<FastDeliverySellerAccessInfo, String> urlFunction,
Function<String, ApiResponse> caller){
// 检查参数
if (!isOnToThird()
|| fastDeliveryReq.getSellerUid()==0
||fastDeliveryReq.getSkup() == 0) {
return new ApiResponse();
}
// 获取配置url
FastDeliverySellerAccessInfo fastDeliveryConfig = getConfigBySellerUid(fastDeliveryReq.getSellerUid());
String url;
if (fastDeliveryConfig == null || StringUtils.isBlank(url = urlFunction.apply(fastDeliveryConfig))) {
return new ApiResponse();
}
fastDeliveryReq.setYhSecret(fastDeliveryConfig.getYhSecret());
// 执行
return caller.apply(url);
}
FastDeliverySellerAccessInfo sellerAccessInfo = getConfigBySellerUid(fastDeliveryReq.getSellerUid());
if (sellerAccessInfo == null || StringUtils.isEmpty(sellerAccessInfo.getLockSkupUrl())) {
return new ApiResponse();
/**
* 执行-对异常进行封装
* @param fastDeliveryReq 请求参数
* @param serviceName 服务名称
* @param urlFunction 获取执行url的function
* @param exception 封装异常
* @return
*/
public ApiResponse executeWrapperException(FastDeliveryReq fastDeliveryReq,
String serviceName,
Function<FastDeliverySellerAccessInfo, String> urlFunction,
ServiceException exception){
// 获取执行caller
Function<String, ApiResponse> caller = getCaller(fastDeliveryReq, serviceName);
// 模板化执行 -- 对caller的异常进行分装
return basicExecute(fastDeliveryReq,
urlFunction,
new ExceptionWrapFunction(caller, exception));
}
fastDeliveryReq.setYhSecret(sellerAccessInfo.getYhSecret());
logger.info("[{}],[{}] start fast delivery lockSkup {}, body {}", fastDeliveryReq.getSellerUid(),
fastDeliveryReq.getSkup(), sellerAccessInfo.getLockSkupUrl(), fastDeliveryReq);
ApiResponse result = null;
try{
result = baseServiceCaller.doPost("fast.delivery.lockSkup", sellerAccessInfo.getLockSkupUrl(), fastDeliveryReq, Default_Time);
}catch (Exception e){
logger.info("[{}],[{}] error fast delivery lockSkup, {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(), e);
throw new ServiceException(403, "扣减库存失败");
/**
* 执行
* @param fastDeliveryReq 请求参数
* @param serviceName 服务名称
* @param urlFunction 获取执行url的function
* @return
*/
public ApiResponse execute(FastDeliveryReq fastDeliveryReq,
String serviceName,
Function<FastDeliverySellerAccessInfo, String> urlFunction){
// 获取执行caller
Function<String, ApiResponse> caller = getCaller(fastDeliveryReq, serviceName);
// 模板化执行
return basicExecute(fastDeliveryReq,
urlFunction,
caller);
}
private Function<String, ApiResponse> getCaller(FastDeliveryReq fastDeliveryReq, String serviceName) {
return (url) -> {
logger.info("[{}],[{}] start fast delivery "+serviceName+" {}, body {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(),
url, fastDeliveryReq);
ApiResponse result = baseServiceCaller.doPost(serviceName, url, fastDeliveryReq, Default_Time);
logger.info("[{}],[{}] end fast delivery "+serviceName+", result {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(), result);
return result;
};
}
logger.info("[{}],[{}] end fast delivery lockSkup, result {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(), result);
return result;
}
private static class ExceptionWrapFunction<T,R> implements Function<T,R> {
private final Function<T, R> function;
private final ServiceException serviceException;
ExceptionWrapFunction(Function<T, R> function, ServiceException e) {
this.function = function;
this.serviceException = e;
}
@Override
public R apply(T t) {
try{
return function.apply(t);
}catch (Exception e){
throw serviceException;
}
}
}
@Autowired
FastDeliveryTemplate fastDeliveryTemplate;
/**
* 下单
* @param skup
* @param waybillCode
* 锁库存
* @return
*/
public ApiResponse delivery(FastDeliveryReq fastDeliveryReq){
if (!isOnToThird()|| fastDeliveryReq.getSellerUid()==0 ||fastDeliveryReq.getSkup() == 0){
return new ApiResponse();
}
public ApiResponse lockSkup(FastDeliveryReq fastDeliveryReq) {
FastDeliverySellerAccessInfo sellerAccessInfo = getConfigBySellerUid(fastDeliveryReq.getSellerUid());
if (sellerAccessInfo == null || StringUtils.isEmpty(sellerAccessInfo.getDeliveryUrl()) ){
return new ApiResponse();
}
fastDeliveryReq.setYhSecret(sellerAccessInfo.getYhSecret());
return fastDeliveryTemplate.executeWrapperException(
fastDeliveryReq,
LOCK_SKUP_SERVICE_NAME,
(fastDeliveryConfig) -> getLockSkupUrl(fastDeliveryConfig),
new ServiceException(403, "扣减库存失败"));
logger.info("[{}],[{}] start fast delivery delivery {}, body {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(),
sellerAccessInfo.getDeliveryUrl(), fastDeliveryReq);
ApiResponse result = baseServiceCaller.doPost("fast.delivery.delivery", sellerAccessInfo.getDeliveryUrl(), fastDeliveryReq, Default_Time);
logger.info("[{}],[{}] end fast delivery delivery, result {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(), result);
return result;
}
/**
* 下单
* @return
*/
public ApiResponse delivery(FastDeliveryReq fastDeliveryReq){
return fastDeliveryTemplate.execute(
fastDeliveryReq,
DELIVERY_SERVICE_NAME,
(fastDeliveryConfig) -> getDeliveryUrl(fastDeliveryConfig));
}
/**
* 取消
* @param skup
* @param needReShelves 是否需要重新上架
* @return
*/
public ApiResponse cancel(FastDeliveryReq fastDeliveryReq){
if (!isOnToThird() || fastDeliveryReq.getSellerUid()==0 ||fastDeliveryReq.getSkup() == 0){
return new ApiResponse();
}
return fastDeliveryTemplate.execute(
fastDeliveryReq,
UNLOCK_SKUP_SERVICE_NAME,
(fastDeliveryConfig) -> getUnlockSkupUrl(fastDeliveryConfig));
FastDeliverySellerAccessInfo sellerAccessInfo = getConfigBySellerUid(fastDeliveryReq.getSellerUid());
if (sellerAccessInfo == null || StringUtils.isEmpty(sellerAccessInfo.getUnlockSkupUrl())){
return new ApiResponse();
}
}
fastDeliveryReq.setYhSecret(sellerAccessInfo.getYhSecret());
logger.info("[{}],[{}] start fast delivery cancel {}, body {}", fastDeliveryReq.getSellerUid(),fastDeliveryReq.getSkup(),
sellerAccessInfo.getUnlockSkupUrl(),sellerAccessInfo);
private String getLockSkupUrl(FastDeliverySellerAccessInfo fastDeliveryConfig){
return fastDeliveryConfig.getLockSkupUrl();
}
ApiResponse result = baseServiceCaller.doPost("fast.delivery.cancel", sellerAccessInfo.getUnlockSkupUrl(),
fastDeliveryReq, Default_Time);
logger.info("[{}],[{}] end fast delivery cancel, result {}", fastDeliveryReq.getSellerUid(), fastDeliveryReq.getSkup(), result);
return result;
private String getDeliveryUrl(FastDeliverySellerAccessInfo fastDeliveryConfig){
return fastDeliveryConfig.getDeliveryUrl();
}
public static final String VR_WAYBILL_CODE_FIX = "VR";
private String getUnlockSkupUrl(FastDeliverySellerAccessInfo fastDeliveryConfig){
return fastDeliveryConfig.getUnlockSkupUrl();
}
/**
... ...