package com.ssyf.module.auth.controller.share;

import com.ssyf.module.auth.domain.entity.User;
import com.ssyf.module.auth.domain.query.*;
import com.ssyf.module.auth.service.UserService;
import com.ssyf.common.core.util.PatternUtil;
import com.ssyf.plugs.logger.Log;

import com.ssyf.common.web.apicode.ApiCodeUser;
import com.ssyf.common.web.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* com.ssyf.module.auth.entity.User模型开放接口
*
* @author: auth
* @date: 2022-10-30
*/
@Api(tags="权限-用户开放接口")
@RestController
@RequestMapping("/share/user")
public class UserShareController {

    @Autowired
    private UserService baseService;

    @Log("手机注册")
    @ApiOperation(value = "手机注册")
//    @CertificateValid(type = "captchaSms" ,contentVar = "phone")
    @PostMapping(value = "/register/sms")
    public Result registerPhone(@RequestParam String phone) {
        if(!PatternUtil.isPhone(phone)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        this.baseService.registerByPhone(phone);
        return Result.success();
    }

    @ApiOperation(value = "邮箱注册")
//    @CertificateValid(type="captchaMail" ,contentVar = "mail")
    @PostMapping(value = "/register/mail")
    public Result registerMail(@RequestParam String mail) {
        if(!PatternUtil.isMail(mail)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        this.baseService.registerByMail(mail);
        return Result.success();
    }


    @ApiOperation(value = "账户注册")
//    @CertificateValid(type="captcha")
    @PostMapping(value = "/register/username")
    public Result registerUsername(@Validated @RequestBody UsernameInfoQuery query) {
        this.baseService.registerByUsername(query.getUsername() ,query.getPassword());
        return Result.success();
    }

    @ApiOperation(value = "账户登录")
//    @CertificateValid(type="captcha")
    @PostMapping(value = "/login/username")
    public Result loginUsername(@Validated @RequestBody UsernameInfoQuery query){
        User user = this.baseService.loginByUsername(query.getUsername() ,query.getPassword());
        return Result.success(this.baseService.loginAndGetToken(user));
    }

    @ApiOperation(value = "邮件登录")
//    @CertificateValid(type="captchaMail",contentVar = "mail")
    @PostMapping(value = "/login/mail")
    public Result loginMail(@RequestParam String mail){
        if(!PatternUtil.isMail(mail)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        User user = this.baseService.loginByMail(mail);
        return Result.success(this.baseService.loginAndGetToken(user));
    }

    /**
     * CertificateValid 注解用于校验用户是否通过短信验证码校验
     * @param phone
     * @return
     */
    @ApiOperation(value = "手机登录")
//    @CertificateValid(type="captchaSms" ,contentVar = "phone")
    @PostMapping(value = "/login/phone")
    public Result loginPhone(@RequestParam String phone){
        if(!PatternUtil.isPhone(phone)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        User user = this.baseService.loginByPhone(phone);
        return Result.success(this.baseService.loginAndGetToken(user));
    }
}
