package com.test.controller;

import com.test.bean.Admin;
import com.test.bean.Result;
import com.test.mapper.AdminMapper;
import com.test.service.AdminService;
import com.test.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {
//    private static Logger log = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    AdminService adminService;
    @PostMapping
    public Result doLogin(String username, String password){
        Admin admin = adminService.doLogin(username, password);
        if (admin != null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", admin.getAdminId());
            claims.put("username", admin.getUsername());
            claims.put("name", admin.getName());
            claims.put("gender", admin.getGender());
            claims.put("phone", admin.getPhone());
            claims.put("department", admin.getDepartment());
            claims.put("email", admin.getEmail());
            String jwt = JwtUtils.createJwt(claims);
            return Result.success(jwt);
        }else{
            return Result.error("用户名或密码错误");
        }


    }

//    @GetMapping("/getAdmin/{id}")
//    public Result getAdmin(@PathVariable int id){
//        log.info("getAdmin");
//        return Result.success(adminService.getAdminById(id));
//    }

}
