package com.trade.client.enterprise.customer.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.trade.client.enterprise.customer.constant.CustConstant;
import com.trade.client.enterprise.customer.domain.model.RegisterMo;
import com.trade.common.core.domain.CommonEntity;
import com.trade.common.security.EncryptUtils;
import com.trade.common.validate.ValidatorGroup;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import java.util.regex.Matcher;

/**
 * @description: 客户实体对象
 * @Author: wanglang
 * @Date: 2024/4/1 15:28
 * @Version: 1.0
 */
public class CustomerUser extends CommonEntity {

    /** 账号ID */
    @ApiModelProperty(name = "identifierNo", value = "账号ID")
    private String identifierNo;
    @ApiModelProperty(name = "nickname", value = "昵称/会员名称")
    /** 昵称/会员名称 */
    @Pattern(regexp = "[a-zA-Z0-9]{6,16}", message = "会员名称必须是6～16位数字和字母的组合", groups = {
            ValidatorGroup.Add.class, ValidatorGroup.Update.class
    })
    private String nickname;
    /** 密码 */
    @ApiModelProperty(name = "password", value = "密码")
    @NotEmpty(message = "密码不能为空", groups = {
            ValidatorGroup.Add.class, ValidatorGroup.Update.class
    })
    @Pattern(regexp = "^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[@#$%^&*_+-=]).{8,16}$", message = "密码必须是8～16位数字、字母以及特殊字符的组合", groups = {
            ValidatorGroup.Add.class, ValidatorGroup.Update.class
    })
    private String password;
    @ApiModelProperty(name = "phoneNumber", value = "手机号")
    /** 手机号 */
    @NotEmpty(message = "手机号不能为空", groups = {
            ValidatorGroup.Add.class, ValidatorGroup.Update.class
    })
    @Pattern(regexp = "\\d{11}", message = "手机号必须是11位数字", groups = {
            ValidatorGroup.Add.class, ValidatorGroup.Update.class
    })
    private String phoneNumber;
    @ApiModelProperty(name = "email", value = "电子邮箱")
    /** 电子邮箱 */
    @Length(max = 300, message = "邮箱长度超过限制", groups = {
            ValidatorGroup.Add.class, ValidatorGroup.Update.class
    })
    @Email(message = "邮箱格式不正确", groups = {
            ValidatorGroup.Add.class, ValidatorGroup.Update.class
    })
    private String email;
    @ApiModelProperty(name = "code", value = "验证码")
    /** 验证码 */
    @NotEmpty(message = "验证码不能为空", groups = {
            ValidatorGroup.Add.class, ValidatorGroup.Update.class
    })
    @Pattern(regexp = "\\d{6}", message = "验证码必须是6为数字", groups = {
            ValidatorGroup.Add.class, ValidatorGroup.Update.class
    })
    @TableField(exist = false)
    private String code;
    @ApiModelProperty(name = "referralCode", value = "推荐码")
    /** 辅助字段：不保存数据库，推荐码 */
    @TableField(exist = false)
    private String referralCode;
    /**
     * 小图用户id
     */
    private String xtUid;

    public CustomerUser(){
    }

    public CustomerUser(RegisterMo register){
        this.password = EncryptUtils.encryptPassword(register.getPassword());
        this.phoneNumber = register.getPhoneNumber();
        this.email = register.getEmail();
        this.code = register.getCode();
        this.referralCode = register.getReferralCode();
        this.nickname = register.getUserName();
    }

    /**
     * 重置密码
     * 
     * @param newPassword 新密码
     */
    public void resetPassword(String newPassword) {
        this.password = EncryptUtils.encryptPassword(newPassword);
    }

    /**
     * 校验密码规则
     * 
     * @param password 密码
     * @return true 校验通过，false 校验不通过
     */
    public static boolean verifyPasswordRules(String password) {
        java.util.regex.Pattern r = java.util.regex.Pattern.compile(CustConstant.CUST_PASSWORD_REGEXP);
        Matcher m = r.matcher(password);
        return m.find();
    }

    public String getIdentifierNo() {
        return identifierNo;
    }

    public void setIdentifierNo(String identifierNo) {
        this.identifierNo = identifierNo;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getReferralCode() {
        return referralCode;
    }

    public void setReferralCode(String referralCode) {
        this.referralCode = referralCode;
    }

    public String getXtUid() {
        return xtUid;
    }

    public void setXtUid(String xtUid) {
        this.xtUid = xtUid;
    }
}
