package cn.iocoder.byzs.module.web.controller.admin.login;

import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.byzs.module.system.controller.admin.auth.vo.AuthLoginReqVO;
import cn.iocoder.byzs.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import cn.iocoder.byzs.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.byzs.module.system.service.auth.AdminAuthService;
import cn.iocoder.byzs.module.system.service.tenant.TenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

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

@Tag(name = "前端")
@RestController
@RequestMapping("/bjdxWeb/web")
public class WebLoginController {

    @Resource
    private TenantService tenantService;
    @Resource
    private AdminAuthService authService;


    @GetMapping("/getTenantIdByName")
    @PermitAll
    @TenantIgnore
    @Operation(summary = "使用租户名，获得租户编号", description = "登录界面，根据用户的租户名，获得租户编号")
    @Parameter(name = "name", description = "租户名", required = true, example = "1024")
    public CommonResult<Long> getTenantIdByName(@RequestParam("name") String name) {
        TenantDO tenant = tenantService.getTenantByName(name);
        return success(tenant != null ? tenant.getId() : null);
    }


    @PostMapping("/login")
    @PermitAll
    @Operation(summary = "使用账号密码登录")
    public CommonResult<AuthLoginRespVO> login(@RequestBody @Valid AuthLoginReqVO reqVO) {
        return success(authService.login(reqVO));
    }
}
