package cn.iocoder.yudao.module.remote.controller.admin.login;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.remote.service.login.LoginService;
import cn.iocoder.yudao.module.system.api.remote.dto.LoginCheckDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 远遥登录")
@RestController
@RequestMapping("/remote/login")
@Validated
public class LoginController {

    @Resource
    private LoginService loginService;

    @PostMapping("/check")
    @Operation(summary = "测试通讯")
    @PermitAll
    public CommonResult<Boolean> checkCommunication(@Valid @RequestBody LoginCheckDTO loginCheckDTO) {
        return success(loginService.checkCommunication(loginCheckDTO));
    }

    @PostMapping("/saveIpAndPort")
    @Operation(summary = "保存IP和端口")
    @PermitAll
    public CommonResult<Boolean> saveIpAndPort(@Valid @RequestBody LoginCheckDTO loginCheckDTO,
                                               HttpServletRequest request) {
        loginService.saveIpAndPort(loginCheckDTO,request);
        return success(true);
    }

    @PostMapping("/getIpAndPort")
    @Operation(summary = "查询IP和端口")
    @PermitAll
    public CommonResult<LoginCheckDTO> getIpAndPort(HttpServletRequest request) {
        return success(loginService.getIpAndPort(request));
    }

}