package com.reyun.saas.mob.openapi.dto;

import com.reyun.saas.common.exception.CustomException;
import com.reyun.saas.common.restResult.ResultCode;
import com.reyun.saas.common.util.PatternUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;

/**
 * @author: hanzepeng
 * @Date: 2022/02/09/14:48
 * @Description: 用户注册DTO
 */
@Data
@ApiModel(value="SignupDTO",description="OpenApi注册DTO对象")
public class SignupDTO implements Serializable {


    @ApiModelProperty(value="用户名")
    private String username;

    @ApiModelProperty(value="密码")
    private String password;

    @ApiModelProperty(value="公司")
    private String company;

    @ApiModelProperty(value="用户姓名")
    private String actualName;

    @ApiModelProperty(value="电话")
    private String phone;

    @ApiModelProperty(value="QQ")
    private String qq;

    @ApiModelProperty(value="微信")
    private String wechat;

    /**
     * 校验参数
     */
    public void checkParam() {
        //去除空格
        this.paramTrim();

        //校验必传字段
        if(StringUtils.isEmpty(this.username) ||
                StringUtils.isEmpty(this.password) ||
                StringUtils.isEmpty(this.company)){
            throw new CustomException(ResultCode.REQUEST_PARAM_ERROR);
        }

        //校验用户名  目前只校验邮箱
        if (!PatternUtil.isEmail(this.username)) {
            throw new CustomException(ResultCode.USERNAME_FORMAT_ERROR);
        }else{
            //校验长度
            if (this.username.length() > 64){
                throw new CustomException(ResultCode.USERNAME_FORMAT_ERROR);
            }
        }

        //校验密码格式
        String passrowd = this.password;
        if(!PatternUtil.checkSimplePassword(passrowd,6,20)){
            throw new CustomException(ResultCode.PASSWORD_FORMAT_ERROR);
        }

        //校验手机号
        if(!StringUtils.isEmpty(this.phone)){
            if (!PatternUtil.isMobileNo(this.phone)) {
                throw new CustomException(ResultCode.OPENAPI_PHONE_FORMAT_ERROR);
            }
        }
    }

    /**
     * 参数去空格
     */
    public void paramTrim(){
        if(!StringUtils.isEmpty(this.username)){
            this.username = this.username.trim();
        }

        if(!StringUtils.isEmpty(this.password)){
            this.password = this.password.trim();
        }

        if(!StringUtils.isEmpty(this.company)){
            this.company = this.company.trim();
        }

        if(!StringUtils.isEmpty(this.actualName)){
            this.actualName = this.actualName.trim();
        }

        if(!StringUtils.isEmpty(this.phone)){
            this.phone = this.phone.trim();
        }

        if(!StringUtils.isEmpty(this.qq)){
            this.qq = this.qq.trim();
        }

    }
}
