package cn.qinys.platform.upms.api.controller;



import cn.qinys.platform.base.response.Result;
import cn.qinys.platform.base.service.ICaptchaService;
import cn.qinys.platform.upms.api.req.AdminTokenReq;
import cn.qinys.platform.upms.api.req.LoginSmsReq;
import cn.qinys.platform.upms.api.resp.AdminTokenResp;
import cn.qinys.platform.upms.api.service.ILoginService;
import cn.qinys.platform.upms.config.mybatisplus.TenantContextHolder;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;


import java.util.Map;

/**
 * 登录控制器
 *
 * @author tanlie
 * Date: 2024/2/4 17:18
 */
@RestController
@RequestMapping("/upms/login")
public class LoginController {

    @Resource
    private ICaptchaService captchaService;
    @Resource
    private ILoginService loginService;

    /**
     * 获取验证码
     *
     * @return
     */
    @GetMapping("/digit-captcha")
    public Result<Map<String, String>> getCaptcha(@RequestParam(required = false, defaultValue = "1") String type) {
        return new Result<>(captchaService.render(type));
    }


    /**
     * 获取短信验证码-登陆专用
     *
     * @return
     */
    @PostMapping("/sms-code")
    public Result<String> sendSmsCode(@RequestBody @Valid LoginSmsReq req) {
        TenantContextHolder.setTenantId(null);
        loginService.sendSmsCode(req);
        TenantContextHolder.clear();
        return new Result<>("短信已发送，请注意查收。");
    }


    /**
     * 获取短信验证码-登陆专用
     *
     * @return
     */
    @PostMapping("/admin-token")
    public Result<AdminTokenResp> getAdminToken(@RequestBody @Valid AdminTokenReq req) {
        TenantContextHolder.setTenantId(null);
        AdminTokenResp res = loginService.login(req);
        TenantContextHolder.clear();
        return new Result<>(res);
    }


}
