UserServiceHelper.java 2.44 KB
/**
 * 
 */
package com.yoho.unions.helper;

import com.yoho.core.rest.client.ServiceCaller;
import com.yoho.core.rest.client.hystrix.AsyncFuture;
import com.yoho.error.exception.ServiceException;
import com.yoho.service.model.order.request.OrdersStatusStatisticsRequest;
import com.yoho.service.model.order.response.CountBO;
import com.yoho.unions.vo.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ping.huang
 * 2016年12月29日
 */
@Component
public class UserServiceHelper {

	static Logger log = LoggerFactory.getLogger(UserServiceHelper.class);
	
	@Resource
	ServiceCaller serviceCaller;

	@Resource
	ClientSecretHelper clientSecretHelper;

	@Value("${gateway.url}")
	private String gatewayUrl;
	
	/**
	 * 根据uid,查询订单数
	 * @param uid
	 * @return
	 * @throws ServiceException
	 */
	public CountBO getOrderCountByUid(int uid) throws ServiceException {
		log.info("start with getOrderCountByUid. uid is {}", uid);
		
		if (uid == 0) {
			log.warn("getOrderCountByUid error with uid={}", uid);
			return null;
		}
		OrdersStatusStatisticsRequest request = new OrdersStatusStatisticsRequest();
        request.setUid(uid);
        CountBO result = serviceCaller.call("order.getOrdersCountByUidAndStatus", request, CountBO.class);
        log.info("getOrderCountByUid success with response={}. uid is {}", result, uid);
		return result;
	}

	public ApiResponse getProfileByUid(int uid) throws ServiceException {
		log.info("start with getProfileByUid. uid is {}", uid);

		if (uid == 0) {
			log.warn("getProfileByUid error with uid={}", uid);
			return null;
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("method", "app.passport.profile");
		map.put("uid", String.valueOf(uid));
		String param = clientSecretHelper.createClientSecret(map);

		// 调用gateway的获取用户信息
		AsyncFuture<ApiResponse> response = serviceCaller.get("user.getProfileByUid", gatewayUrl + "?" + param, null, ApiResponse.class, null);

		ApiResponse apiResponse = response.get();
		if(apiResponse.getCode() != 200){
			log.warn("call app.passport.profile error e={}", apiResponse.getMessage());
			return new ApiResponse();
		}
		log.info("getProfileByUid success with response={}. uid is {}", apiResponse, uid);
		return apiResponse;
	}
}