package com.luozhihui.project.controller;
import com.luozhihui.project.common.LoginResponse;
import com.luozhihui.project.entity.Admin;
import com.luozhihui.project.mapper.AdminLoginMapper;
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 AdminLoginController {
    @Resource
    AdminLoginMapper adminLoginMapper;


    @PostMapping("/admin_login")
    public LoginResponse login(@RequestParam("AdminUser") String AdminUser, @RequestParam("AdminPassWord") String AdminPassWord) {
        List<Admin> userList = adminLoginMapper.findByPhone(AdminUser);
        System.out.println(userList);
        if (userList.isEmpty()) {
            return new LoginResponse(null, null,500, "用户不存在");
        }

        Admin admin = userList.get(0);
        if (admin.getAdminPassWord().equals(AdminPassWord)) {
            // 登录成功,加上 token
            JwtUtil jwtUtil = new JwtUtil();
            String token = jwtUtil.createToken(userList.get(0).getAdminUser());
            return new LoginResponse(token, null,200, "登录成功");
        } else {
            return new LoginResponse(null,null, 500, "密码错误");
        }
    }


}


