package com.wms_main.controller.wms;

import com.wms_main.model.dto.request.wms.LoginRequest;
import com.wms_main.model.vo.wms.UserLoginVo;
import com.wms_main.model.dto.response.wms.WmsApiResponse;
import com.wms_main.service.controller.ILoginControllerService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import static com.wms_main.repository.utils.StringUtils.getIpAddr;

/**
 * 登录控制类
 */
@RestController
@ResponseBody
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/wms/login")
public class LoginController {
    private final HttpServletRequest servletRequest;// 请求头部信息

    private final ILoginControllerService loginControllerService;

    /**
     * 带密码登录
     * @param request 请求
     * @return 登录结果
     */
    @PostMapping("/loginWithAuth")
    public WmsApiResponse<UserLoginVo> loginWithAuth(@RequestBody LoginRequest request) {
        return loginControllerService.loginWithAuth(request.getLoginAccount(), request.getLoginPassword(), getIpAddr(servletRequest));
    }

    /**
     * 无密码登录
     * @param request 请求
     * @return 登录结果
     */
    @PostMapping("/loginWithOutAuth")
    public WmsApiResponse<UserLoginVo> loginWithOutAuth(@RequestBody LoginRequest request) {
        return loginControllerService.loginWithAuth(request.getLoginAccount(), request.getLoginPassword(), getIpAddr(servletRequest));
    }
}
