package com.uas.eis.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.uas.eis.entity.ErrorMsg;
import com.uas.eis.exception.SystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.uas.eis.service.QueryService;
import com.uas.eis.utils.BaseUtil;

@RestController
public class LoginController {

	@Autowired
	private QueryService queryService;
	
	/**
	 * 首次登录请求token
	 */
	@RequestMapping("/login")
	public Object login(HttpServletRequest request, String username, String password){
		Map<String, Object> res = new HashMap<String, Object>();
		String token = queryService.login(username, password);
		request.getSession().removeAttribute("token");
		if(token != null) {
			request.getSession().setAttribute("token", token);
			res.put("success", true);
			res.put("token", token);
		}else {
			throw new SystemException(ErrorMsg.BAD_USERINFO);
		}
		return res;
	}
	
	/**
	 * 退出登录
	 */
	@RequestMapping("/logout")
	public Object logout(HttpServletRequest request){
		Map<String, Object> res = new HashMap<String, Object>();
		request.getSession().removeAttribute("token");
		res.put("success", true);
		return res;
	}
}
