package com.iformall.domain.po;

import com.baomidou.mybatisplus.annotation.TableName;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.utils.Constant;
import lombok.Data;
import lombok.EqualsAndHashCode;


import java.util.Date;
import java.util.UUID;

@TableName(value = "wx_merchant_b_user")
@Data
@EqualsAndHashCode(callSuper = true)
public class WxMerchantBUser extends BaseEntity {
    private static final long serialVersionUID = 1L;

    protected Long id;

    @io.swagger.annotations.ApiModelProperty(value = "租户ID", name = "tenantId")
    private String tenantId;
    @io.swagger.annotations.ApiModelProperty(value = "B端用户手机号", name = "phone")
    private String phone;
    @io.swagger.annotations.ApiModelProperty(value = "", name = "userPwd")
    private String userPwd;
    @io.swagger.annotations.ApiModelProperty(value = "商户id", name = "merchantId")
    private Long merchantId;
    @io.swagger.annotations.ApiModelProperty(value = "创建时间", name = "createDate")
    private Date createDate;
    @io.swagger.annotations.ApiModelProperty(value = "更新时间", name = "updateDate")
    private Date updateDate;
    @io.swagger.annotations.ApiModelProperty(value = "小程序的appId", name = "appId")
    private String appId;
    @io.swagger.annotations.ApiModelProperty(value = "用户登录用token", name = "token")
    private String token;
    @io.swagger.annotations.ApiModelProperty(value = "用户过期时间", name = "expireTime")
    private Date expireTime;
    @io.swagger.annotations.ApiModelProperty(value = "b端用户姓名", name = "name")
    private String name;
    @io.swagger.annotations.ApiModelProperty(value = "绑定状态1未绑定商户0已绑定商户", name = "status")
    private Integer status;

    private transient Long buserId;

    public String createToken(Date currentDate) {
        if (expireTime == null || expireTime.getTime() - Constant.H_EXPIRE < currentDate.getTime()) {
            //提前一小时， 新生成一个token
            String token = UUID.randomUUID().toString();
            //过期时间
            Date expireTime = new Date(currentDate.getTime() + Constant.EXPIRE);
            setToken(token);
            setExpireTime(expireTime);
        }
        return this.token;
    }
}
