package com.ruoyi.ss.account.domain;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.annotation.Sensitive;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.enums.DesensitizedType;
import com.ruoyi.common.utils.RegexpUtils;
import com.ruoyi.common.valid.EnumValid;
import com.ruoyi.common.valid.bank.BankCardInfo;
import com.ruoyi.common.validRule.accountExist.AccountExist;
import com.ruoyi.ss.account.domain.enums.AccountType;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

/**
 * 用户账户对象 sm_account
 *
 * @author ruoyi
 * @date 2024-03-26
 */
@Data
public class Account extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 账户id */
    @NotNull(message = "账户ID不允许为空", groups = {ValidGroup.Update.class, ValidGroup.FrontUpdate.class})
    @AccountExist(message = "账户不存在", checkBelong = true, groups = {ValidGroup.FrontUpdate.class})
    @AccountExist(message = "账户不存在", groups = {ValidGroup.Update.class})
    private Long accountId;

    /** 用户id */
    @Excel(name = "用户id")
    @NotNull(message = "用户ID不允许为空", groups = {ValidGroup.Create.class})
    private Long userId;

    /** 账户类型：1银行卡、2微信、3支付宝 */
    @Excel(name = "账户类型：1银行卡、2微信、3支付宝、4线下收款码")
    @EnumValid(
            clazz = AccountType.class,
            message = "非法的账户类型",
            method = "getType"
    )
    @NotBlank(message = "账户类型不允许为空", groups = {ValidGroup.FrontCreate.class, ValidGroup.Create.class})
    private String accountType;

    /** 姓名 */
    @Excel(name = "姓名")
    @Size(max = 32, message = "姓名长度不能超过32个字符")
    private String name;

    /** 身份证 */
    @Excel(name = "身份证")
    @Pattern(regexp = RegexpUtils.IDENTITY_CARD_REGEXP, message = "身份证格式错误")
    @Sensitive(desensitizedType = DesensitizedType.ID_CARD)
    private String idCard;

    /** 卡号（微信号/支付宝账号） */
    @Excel(name = "账户")
    @NotBlank(message = "账户不允许为空", groups = {ValidGroup.FrontCreate.class, ValidGroup.Create.class})
    private String accountNo;

    /** 手机号 */
    @Excel(name = "手机号")
    @Pattern(regexp = RegexpUtils.MOBILE_PHONE_REGEXP, message = "手机号格式错误")
    private String mobile;

    @ApiModelProperty("卡面信息")
    private BankCardInfo cardInfo;

    @ApiModelProperty("是否默认账户")
    private Boolean isDefault;

    /**
     * 数据脱敏
     */
    public void desensitization() {
        if (this.accountNo != null && AccountType.BANK_CARD.getType().equals(this.accountType)) {
            this.accountNo = StringUtils.substring(this.accountNo, 0, 4)
                    + "****"
                    + StringUtils.substring(this.accountNo, this.accountNo.length() - 4, this.accountNo.length());
        }
    }
}
