InboxUserProxyService.java 2.31 KB
package com.yohoufo.inboxclient.sdk;

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.yohoufo.common.ApiResponse;
import org.apache.commons.lang3.StringUtils;
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.concurrent.TimeUnit;

/**
 * Created by chenchao on 2018/9/18.
 */
@Service
public class InboxUserProxyService {


    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    ServiceCaller serviceCaller;

    @Value("${erp-gateway.url}")
    private String erpGatewayUrl;

    @Value("${uic.url:http://uic.yohoops.org/uic}")
    private String uicUrl;


    /**
     * http://java-yoho-uic.test3.ingress.dev.yohocorp.com/uic
     * //profile/getProfile?uid=600032978
     * @param uid
     * @return
     */
    public static final String PRFILE_API = "/profile/getProfile";
    public String getMobile(int uid){
        String url = uicUrl + PRFILE_API;
        Map<String,Object> params = Maps.newHashMap();
        params.put("uid", uid);

        logger.info("InboxUserProxyService in getMobile enter, uid {}", uid);

        ApiResponse userInfo ;
        try {
            userInfo = serviceCaller.get("users.getAddress", url, params,
                    ApiResponse.class, null).get(500, TimeUnit.MILLISECONDS);

        }catch (Exception ex){
            logger.warn("InboxUserProxyService in getMobile fail, uid {}", uid,  ex);
            throw new ServiceException(ServiceError.USER_IS_NOT_EXIST);
        }

        JSONObject jsonObject;
        String mobile;
        if (userInfo == null || (jsonObject = (JSONObject)userInfo.getData()) == null
                || StringUtils.isBlank(mobile = jsonObject.getString("mobile_phone"))){
            logger.warn("InboxUserProxyService in getMobile fail, uid {}, userInfo {}", uid, userInfo);
            throw new ServiceException(ServiceError.PROFILE_IS_NULL);
        }
        return mobile;
    }

}