package cn.pluss.platform.entity;

import cn.hutool.core.util.DesensitizedUtil;
import cn.pluss.platform.config.mybatis.SqlConditionExtra;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.Date;

/**
 * <p>
 * 银行卡信息
 * </p>
 *
 * @author Djh
 * @since 2020-10-16
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tb_pluss_bank_card")
public class BankCard implements Serializable, Cloneable {

    /**
     * 对私账户
     */
    public static final String ACCOUNT_TYPE_PRIVATE = "01";

    /**
     * 对公账户
     */
    public static final String ACCOUNT_TYPE_CORPORATE = "02";

    /**
     * 实名认证
     */
    public static final String ACCOUNT_TYPE_CERT = "03";

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;


    /**
     * 用户id
     */
    @NotEmpty(message = "缺少userId", groups = {BankCard.class})
    @TableField("userId")
    private String userId;

    /**
     * 银行卡持有人/开户名
     */
    @TableField("bankHolder")
    private String bankHolder;

    /**
     * 银行卡号
     */
    @NotEmpty(message = "请输入银行卡", groups = {BankCard.class})
    @TableField("bankCardNo")
    private String bankCardNo;


    /**
     * 开户行
     */
    @NotEmpty(message = "请输入开户行", groups = {BankCard.class})
    @TableField("bankName")
    private String bankName;


    /**
     * 开户支行名称
     */
    @NotEmpty(message = "请输入开户支行名称", groups = {BankCard.class})
    @TableField("branchName")
    private String branchName;


    /**
     * 01：对私账户（自然人只对私、个体工商户可选）; 02：对公账户; 03 实名认证
     */
    @NotEmpty(message = "请选择账户类型", groups = {BankCard.class})
    @TableField("accountType")
    private String accountType;


    /**
     * 联行号
     */
    @NotEmpty(message = "请输入开户支行联行号", groups = {BankCard.class})
    @TableField("contactLine")
    private String contactLine;


    /**
     * 开户支行所在省
     */
    @NotEmpty(message = "请选择开户省", groups = {BankCard.class})
    @TableField("branchProvince")
    private String branchProvince;


    /**
     * 开户支行所在市
     */
    @NotEmpty(message = "请选择开户市", groups = {BankCard.class})
    @TableField("branchCity")
    private String branchCity;


    /**
     * 开户支行所在区
     */
    @NotEmpty(message = "请选择开户区", groups = {BankCard.class})
    @TableField("branchArea")
    private String branchArea;

    /**
     * 开户省市区编码
     */
    @TableField("bankAddressNo")
    private String bankAddressNo;


    /**
     * 银行卡预留手机号，企业名称
     */
    @NotEmpty(message = "银行卡预留手机号", groups = {BankCard.class})
    @TableField("phone")
    private String phone;


    /**
     * 创建时间
     */
    @TableField(value = "createTime", fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 创建时间 开始时间
     */
    @TableField(select = false, value = "createTime", condition = SqlConditionExtra.GT)
    private Date createTimeStart;

    /**
     * 创建时间 截止时间
     */
    @TableField(select = false, value = "createTime", condition = SqlConditionExtra.LT)
    private Date createTimeEnd;

    /**
     * 更新时间
     */
    @TableField(value = "updateTime", fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    /**
     * 更新时间 开始时间
     */
    @TableField(select = false, value = "updateTime", condition = SqlConditionExtra.GT)
    private Date updateTimeStart;

    /**
     * 更新时间 截止时间
     */
    @TableField(select = false, value = "updateTime", condition = SqlConditionExtra.LT)
    private Date updateTimeEnd;

    /**
     * 银行卡卡号面图片url
     */
    @NotEmpty(message = "银行卡卡号面图片", groups = {BankCard.class})
    @TableField(value = "imgUrl", insertStrategy = FieldStrategy.NOT_EMPTY, updateStrategy = FieldStrategy.NOT_EMPTY)
    private String imgUrl;

    /**
     * 开户许可证图片url
     */
    @TableField(value = "licenseUrl", insertStrategy = FieldStrategy.NOT_EMPTY, updateStrategy = FieldStrategy.NOT_EMPTY)
    private String licenseUrl;

    public static BankCard desensitized(BankCard bankCard) {
        try {
            BankCard newBankCard = (BankCard) bankCard.clone();
            newBankCard.setPhone(DesensitizedUtil.mobilePhone(newBankCard.getPhone()));
            newBankCard.setBankCardNo(DesensitizedUtil.bankCard(newBankCard.getBankCardNo()));
            newBankCard.setBankHolder(DesensitizedUtil.chineseName(newBankCard.getBankHolder()));

            return newBankCard;
        } catch (CloneNotSupportedException ignored) {
            return null;
        }

    }
}