package com.itheima.controller;
import com.itheima.domain.UserInfo;
import com.itheima.service.UserInfoService;
import com.itheima.util.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/*****
 * @Author: http://www.itheima.com
 * @Description: com.itheima.controller.UserInfoController
 ****/
@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /****
     * 根据name获取用户信息
     * @param name
     * @return
     */
    @PostMapping(value = "/login")
    public String login(String name) throws Exception{
        //查询用户信息
        UserInfo userInfo = userInfoService.get(name);

        if(userInfo!=null){
            Map<String,Object> userMap = new HashMap<String,Object>();
            userMap.put("username",userInfo.getUsername());
            userMap.put("name",userInfo.getName());
            return JwtTokenUtil.generateTokenUser(UUID.randomUUID().toString(),userMap,1000000000L);
        }
        return "账号或密码错误";
    }

    /****
     * 根据name获取用户信息   http://192.168.211.141/usr/wangwu
     * @param name
     * @return
     */
    @GetMapping(value = "/userinfo/{name}")
    public UserInfo one(@PathVariable(value = "name")String name){
        //try {
        //    System.out.println("hello!");
        //    Thread.sleep(10000);
        //} catch (InterruptedException e) {
        //    e.printStackTrace();
        //}
        return userInfoService.get(name);
    }
}
