package com.baidu.sso.login.web;

import com.baidu.sso.login.entity.LoginDo;
import com.baidu.sso.login.entity.UserDo;
import com.baidu.sso.login.utils.LoginUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/login")
@RestController
//@CrossOrigin
public class LoginController {
    
    @Resource
    LoginUtils loginUtils;
    
    
    /**
     * 获取签名
     * @param userDo 用户信息
     * @return 登录信息
     * */
    @PostMapping("/getSign")
    public LoginDo getSign(@RequestBody UserDo userDo){
        if(userDo == null || userDo.getName() == null || userDo.getPsd() == null){
            return null;
        }
        
        // 检查用户名和密码是否正确
        boolean isLogin = loginUtils.checkPsd(userDo.getName(), userDo.getPsd());
        if(isLogin){
            LoginDo loginDo = new LoginDo();
            
            // 获取用户信息
            userDo = loginUtils.getUser(userDo.getName());
            
            // 获取时间戳
            Long timestamp = loginUtils.getTimestamp();
            
            // 设置登录信息
            loginDo.setAccount(loginUtils.getAccount());
            loginDo.setTimestamp(timestamp);
            loginDo.setSign(loginUtils.getSign(userDo, timestamp));
            loginDo.setQybh(loginUtils.getCardNo());
            loginDo.setYhm(userDo.getName());
            return loginDo;
        }
        
        return null;
    }
    
    @PostMapping("/getLoginAddress")
    public String getLoginAddress(){
       return loginUtils.getLoginAddress();
    }
}
