package io.omengye.ws.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import io.omengye.ws.entity.UserEntity;
import io.omengye.ws.service.TokenService;
import io.omengye.ws.service.UserService;
import io.omengye.ws.utils.StrUtil;

@RestController
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private TokenService tokenService;
	
	@PostMapping("/getUserid")
	public UserEntity getUserId(HttpServletRequest request) {
		String userip = request.getParameter("userip");
		UserEntity entity = new UserEntity();
		if (StrUtil.snull(userip)==null) {
			return entity;
		}
		//
		try {
			if (userService.getUserByIp(userip) != null) {
				// existed visitor
			}
			else {
				// new visitor
				userService.addUser(userip);
			}
			entity = userService.getUserByIp(userip);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return entity;
	}
	
	@PostMapping("/getToken")
	public String getToken(HttpServletRequest request) {
		String token = null;
		String userip = request.getParameter("userip");
		if (StrUtil.snull(userip)==null) {
			return null;
		} 
		try {
			if (tokenService.getToken(userip)!=null) {
				token = tokenService.getToken(userip);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return token;
	}
	
}

@@ -26,24 +26,76 @@
 troller;%0D%0A%0D%0A
+import java.util.HashMap;%0D%0Aimport java.util.Map;%0D%0A%0D%0A
 import javax
@@ -1277,22 +1277,34 @@
 %09public 
+Map%3C
 String
+,Object%3E
  getToke
@@ -1338,22 +1338,34 @@
 t) %7B%0D%0A%09%09
+Map%3C
 String
+,Object%3E
  token =
@@ -1366,19 +1366,76 @@
 oken = n
-ull
+ew HashMap%3CString, Object%3E();%0D%0A%09%09token.put(%22success%22, false)
 ;%0D%0A%09%09Str
@@ -1601,21 +1601,32 @@
  %7B%0D%0A%09%09%09%09
+String 
 token
+Data
  = token
@@ -1644,32 +1644,100 @@
 Token(userip);%0D%0A
+%09%09%09%09token.put(%22success%22, true);%0D%0A%09%09%09%09token.put(%22data%22, tokenData);%0D%0A
 %09%09%09%7D%0D%0A%09%09%7D catch 

add token controller