package com.platform.modules.foreign.utils;/**
											* Created by TROY on 2019/2/27.
											*/

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.google.gson.Gson;
import com.platform.common.utils.RedisKeys;
import com.platform.common.utils.RedisUtils;
import com.platform.modules.foreign.entity.AiJiNanUserInfoModel;

import net.sf.json.JSONObject;

/**
 * @author TROY
 * @version V1.0
 * @ClassName: AiJinanUtils
 * @Description: 爱济南客户端对接接口
 * @date 2019/2/27 17:19
 */
@Component
public class AiJinanUtils {

	@Autowired
	private RedisUtils redisUtils;

	@Autowired
	private RestTemplate restTemplate;

	/**
	 * 接口主域名
	 */
	private static final String API_DOMAIN = "appc.e23.cn";
	/**
	 * 用户信息接口路径
	 */
	private static final String USER_INFO_API = "/index.php?m=api&c=user&a=getUserInfo_pub";

	/**
	 * token过期时间：1天 单位s:24*60*60
	 */
	private static final Long TOKEN_EXPIRETIEM = 24 * 60 * 60L;

	public static String getUserInfoApi() {
		return getHttpApiUrl(API_DOMAIN, USER_INFO_API);
	}

	private static String getHttpApiUrl(String domain, String path) {
		return getApiUrl(null, domain, path);
	}

	private static String getApiUrl(String protocol, String domain, String path) {
		if (StringUtils.isBlank(protocol)) {
			protocol = "http";
		}
		return protocol + "://" + domain + path;
	}

	/**
	 * 根据爱济南用户token获取用户信息<br>
	 * redis中存在直接获取<br>
	 * 不存在通过接口获取并存入redis中
	 * 
	 * @param token
	 *            utoken
	 * @return AiJiNanUserInfoModel{userid、avatar、username}
	 */
	public AiJiNanUserInfoModel getUserInfoByToken(String token) throws IllegalAccessException {

		if (StringUtils.isBlank(token)) {
			throw new NullPointerException();
		}

		String key = RedisKeys.getForeignUserToken(token);
		boolean hasCache = redisUtils.hasKey(key);

		if (hasCache) {
			String userStr = redisUtils.get(key, TOKEN_EXPIRETIEM);
			Gson gson = new Gson();
			return gson.fromJson(userStr, AiJiNanUserInfoModel.class);
		}

		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.MULTIPART_FORM_DATA);
		MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
		params.add("token", token);
		HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
		String userStr;
		try {
			userStr = restTemplate.postForEntity(AiJinanUtils.getUserInfoApi(), requestEntity, String.class).getBody();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		if (StringUtils.isNotBlank(userStr)) {
			JSONObject userJson = JSONObject.fromObject(userStr);
			if (null != userJson && null != userJson.get("code") && userJson.get("code").equals(new Integer(200))) {
				JSONObject data = userJson.getJSONObject("data");
				AiJiNanUserInfoModel userInfoModel = new AiJiNanUserInfoModel();
				userInfoModel.setUserId(data.getLong("userid"));
				userInfoModel.setAvatar(data.getString("avatar"));
				userInfoModel.setUserName(data.getString("username"));
				redisUtils.set(key, userInfoModel.toJson(), TOKEN_EXPIRETIEM);
				return userInfoModel;
			}
		}
		throw new IllegalAccessException();
	}
}
