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

import com.alibaba.fastjson.JSONObject;
import com.netflix.config.DynamicLongProperty;
import com.netflix.config.DynamicPropertyFactory;
import com.yoho.core.common.utils.JsonUtil;
import com.yoho.core.rest.client.ServiceCaller;
import com.yoho.core.rest.client.hystrix.AsyncFuture;
import com.yoho.error.ServiceError;
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.service.model.response.UserInfoRspBO;
import com.yoho.service.model.union.request.UnionUicUserReqBO;
import com.yoho.unions.common.redis.RedisValueCache;
import com.yoho.unions.vo.ApiResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
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.*;
import java.util.concurrent.TimeUnit;

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

	static Logger log = LoggerFactory.getLogger(UserServiceHelper.class);

	@Resource
	ServiceCaller serviceCaller;

	@Resource
	RedisValueCache redisValueCache;

	@Resource
	ClientSecretHelper clientSecretHelper;

	@Value("${gateway.url}")
	private String gatewayUrl;

	private static final String USERINFO_KEY = "yh:union:share:userinfo:";


	/**
	 * 根据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;
	}

	public UserInfoRspBO getUserInfoFromUic(int uid) throws ServiceException {
		if (1 > uid) {
			log.warn("getUserInfoFromUic: param uid is null.");
			throw new ServiceException(ServiceError.UID_IS_NULL);
		}

		UserInfoRspBO userInfo = redisValueCache.get(USERINFO_KEY, UserInfoRspBO.class);

		if (userInfo != null) {
			log.info("getUserInfoFromUic: get userInfo from cache: uid is {}", uid);
			return userInfo;
		}

		try {
			UnionUicUserReqBO uicUserReqBO = new UnionUicUserReqBO();
			uicUserReqBO.setYohoUid(uid);
			uicUserReqBO.setChannel(1);
			userInfo = serviceCaller.call("uic.getUserInfoByYohoUid", uicUserReqBO, UserInfoRspBO.class);
			if (userInfo == null) {
				return null;
			}
			redisValueCache.set(USERINFO_KEY+uid,userInfo,180, TimeUnit.SECONDS);
			log.info("getUserInfoFromUic: end call uic.getUserInfo, uid is {}, userInfo is {}", uid, userInfo);
		} catch (Exception e) {
			log.warn("get user info from uic failed, uid is {}", uid);
			return new UserInfoRspBO();
		}
		return userInfo;
	}

	public List<UserInfoRspBO> getUserInfoListFromUic(Set<Integer> uidSet) throws ServiceException {
		if (CollectionUtils.isEmpty(uidSet)) {
			log.warn("getUserInfoFromUic: param uids is null.");
			throw new ServiceException(ServiceError.UID_IS_NULL);
		}

		String uids=StringUtils.join(uidSet, ',');
		List<UserInfoRspBO> result = new ArrayList<>();
		try {
			UnionUicUserReqBO uicUserReqBO = new UnionUicUserReqBO();
			uicUserReqBO.setUids(uids);
			uicUserReqBO.setChannel(1);
			List<JSONObject> userInfo = serviceCaller.asyncCall("uic.getUserInfoListByYohoUid", uicUserReqBO, List.class,new ArrayList()).get(100);
			log.info("getUserInfoFromUic: end call uic.getUserInfo, uids is {}, userInfo is {}", uids, userInfo);
			if (CollectionUtils.isEmpty(userInfo)) {
				return result;
			}

			userInfo.forEach(u->{
				result.add(JsonUtil.jsonToObject(u.toJSONString(), UserInfoRspBO.class));
			});
			return result;
		} catch (Exception e) {
			log.warn("get user info from uic failed, uids is {}", uids);
			return result;
		}
	}
}