|
|
package com.yohoufo.order.service.proxy;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import com.yoho.core.rest.client.ServiceCaller;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yoho.service.model.request.UserAddressReqBO;
|
|
|
import com.yoho.service.model.response.UserAddressRspBO;
|
|
|
import com.yohoufo.common.ApiResponse;
|
|
|
import com.yohoufo.order.constants.OrderConstant;
|
|
|
import com.yohoufo.order.convert.AddressInfoConvertor;
|
|
|
import com.yohoufo.order.model.AddressInfo;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -15,27 +28,59 @@ import java.util.concurrent.TimeUnit; |
|
|
@Service
|
|
|
public class UserProxyService {
|
|
|
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
@Autowired
|
|
|
ServiceCaller serviceCaller;
|
|
|
|
|
|
@Value("${erp-gateway.url}")
|
|
|
private String erpGatewayUrl;
|
|
|
/**
|
|
|
* 获取用户信息
|
|
|
* @param uid
|
|
|
* @param addressId
|
|
|
* @return
|
|
|
*/
|
|
|
public UserAddressRspBO getAddressInfo(int uid, int addressId) {
|
|
|
public AddressInfo getAddressInfo(int uid, int addressId) {
|
|
|
// 收货地址信息, 入口参数:uid, address_id
|
|
|
UserAddressReqBO userAddressReqBO = new UserAddressReqBO();
|
|
|
userAddressReqBO.setUid(uid);
|
|
|
userAddressReqBO.setId(addressId);
|
|
|
|
|
|
//超时,使用默认地址,后续需要手动补充地址
|
|
|
UserAddressRspBO defaultResponse = new UserAddressRspBO();
|
|
|
defaultResponse.setAreaCode(OrderConstant.EMPTY_ADDRESS_CODE);
|
|
|
defaultResponse.setId(String.valueOf(addressId));
|
|
|
UserAddressRspBO userAddressBO = serviceCaller.asyncCall("users.getAddress", userAddressReqBO,
|
|
|
UserAddressRspBO.class, defaultResponse).get(500, TimeUnit.MILLISECONDS);
|
|
|
ApiResponse defaultResponse = new ApiResponse();
|
|
|
|
|
|
|
|
|
Map<String,Object> params = Maps.newHashMap();
|
|
|
params.put("uid", uid);
|
|
|
params.put("debug", "XYZ");
|
|
|
String url = erpGatewayUrl + "/erp/passport/getInnerAddress";
|
|
|
|
|
|
AddressInfo addressInfo ;
|
|
|
ApiResponse userAddressBO ;
|
|
|
try {
|
|
|
userAddressBO = serviceCaller.get("users.getAddress", url, params,
|
|
|
ApiResponse.class, defaultResponse).get(500, TimeUnit.MILLISECONDS);
|
|
|
|
|
|
return userAddressBO;
|
|
|
}catch (Exception ex){
|
|
|
logger.warn("in getAddressInfo fail, uid {}, addressId {}", uid, addressId, ex);
|
|
|
throw new ServiceException(ServiceError.ADDRESS_NULL);
|
|
|
}
|
|
|
JSONArray addressArray ;
|
|
|
if (userAddressBO == null || (addressArray = (JSONArray)userAddressBO.getData()) == null){
|
|
|
logger.warn("in getAddressInfo fail, uid {}, addressId {}", uid, addressId);
|
|
|
throw new ServiceException(ServiceError.ADDRESS_NULL);
|
|
|
}
|
|
|
JSONObject addressJsonObj = (JSONObject) addressArray.parallelStream().filter(addressJson -> {
|
|
|
Integer address_id = ((JSONObject)addressJson).getInteger("address_id");
|
|
|
return Objects.nonNull(address_id) && address_id == addressId;
|
|
|
}).findFirst().orElse(null);
|
|
|
if (addressJsonObj == null){
|
|
|
logger.warn("in getAddressInfo fail, uid {}, addressId {}, addressArray {}", uid, addressId, addressArray);
|
|
|
throw new ServiceException(ServiceError.ADDRESS_NULL);
|
|
|
}
|
|
|
addressInfo = AddressInfoConvertor.userAddressRsp2AddressInfo(addressJsonObj);
|
|
|
return addressInfo;
|
|
|
}
|
|
|
} |
...
|
...
|
|