package com.cxyxiaomo.epp.user.controller;

import com.cxyxiaomo.epp.common.pojo.User;
import com.cxyxiaomo.epp.common.response.Res;
import com.cxyxiaomo.epp.common.vo.UserVO;
import com.cxyxiaomo.epp.user.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    // @RequestMapping("/login")
    @PostMapping("/login")
    @ResponseBody
    public Res login(String username, String password) {
        User user = userService.getUserByUsername(username);
        if (user != null) {
            String passwordHash = user.getPassword();
            if (passwordHash.equals(password)) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("userInfo", UserVO.convertFrom(user));
                return Res.success(map);
            } else {
                return Res.error("用户名或密码不正确");
            }
        } else {
            return Res.error("用户不存在");
        }
    }

    @RequestMapping("/person")
    @ResponseBody
    public User person(String username) {
        return null;
        // return userService.getUserByUsername(username);
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public Res addUser(String username, String name, Integer role) {
        // User user = new User();
        // user.setUsername(username);
        // user.setPassword(username);
        // user.setName(name);
        // if (role == 2) {
        //     user.setStu_id(username);
        // }
        // user.setRole(role);
        // return Res.success(userService.addUser(user));
        return null;
    }
}
