package test.com.leanwo;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.leanwo.activiti.util.FormatUtil;


/**
 * 登陆资源
 * @author YangZhiJie
 *
 */
public class LoginResourceTest {
	
	private static Logger logger = LoggerFactory.getLogger(LoginResourceTest.class);

	
	/**
	 * 登录
	 * @param userName 用户名
	 * @param password 密码
	 * @return token 登陆的token
	 * @author ZhangHao
	 */
	public String login(String userName, String password) {
		BaseRestTemplateTest restfulResource = new BaseRestTemplateTest();
		String url = restfulResource.getProdogServerApiUrl("/authApi/LoginResource/login");
		RestTemplate restTemplate = restfulResource.getRestTemplate();
		
		HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		
		LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
		params.add("userName", userName);
		params.add("password", password);
		params.add("languageId", "zh_CN");
		params.add("accountDateTime", FormatUtil.formatDateTimeFormat(new Date()));
		
		HttpEntity<LinkedMultiValueMap<String, String>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, String>>(
				params, headers);

		ResponseEntity<LoginInfo> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, LoginInfo.class);

		LoginInfo loginInfo = response.getBody();
		return loginInfo.getToken();
	}
}
