package com.luozhihui.project.controller;
import com.luozhihui.project.common.LoginResponse;
import com.luozhihui.project.entity.User;
import com.luozhihui.project.mapper.UserLoginMapper;
import com.luozhihui.project.util.JwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/api")
@CrossOrigin
public class UserLoginController {
    @Resource
    UserLoginMapper userLoginMapper;
//用户登录接口
    @PostMapping("/login")
    public LoginResponse login(@RequestParam("Phone") String Phone, @RequestParam("PassWord") String PassWord) {
        List<User> userList = userLoginMapper.findByPhone(Phone);
        System.out.println(userList);
        if (userList.isEmpty()) {
            return new LoginResponse(null, null,500, "用户不存在");
        }

        User user = userList.get(0);
        if (user.getPassWord().equals(PassWord)) {
            // 登录成功,加上 token
            JwtUtil jwtUtil = new JwtUtil();
            String token = jwtUtil.createToken(userList.get(0).getPhone());
            return new LoginResponse(token,user.getUserId(), 200, "登录成功");
        } else {
            return new LoginResponse(null, null,500, "账号或密码错误");
        }
    }
//通过token查询用户信息

}


