package com.ruoyi.web.controller.system;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.lang.LangUnit;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.web.service.EmailService;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 注册验证
 *
 * @author ruoyi
 */
@Api(tags = "1 注册验证")
@ApiSupport(order = 1)
@RestController
public class SysRegisterController extends BaseController
{

    @Autowired
    private EmailService emailService;

    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private LangUnit langUnit;

    @ApiOperation(value = "1.1 发送注册验证码")
    @ApiOperationSupport(order = 1)
    @PostMapping("/sendRegisterEmailCode")
    public AjaxResult sendRegisterEmailCode(@RequestParam(required = true) String email)
    {
        AjaxResult ajax = AjaxResult.success();
        String uuid = IdUtils.simpleUUID();
        emailService.sendEmailCode(email, uuid, "注册验证码");
        ajax.put("uuid", uuid);
        return ajax;
    }

    @ApiOperation(value = "1.2 提交注册申请")
    @ApiOperationSupport(order = 2)
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error(langUnit.getValueTag("sysReg.1001", "当前系统没有开启注册功能！") );
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success("注册成功，等待管理员审批") : error(msg);
    }
}
