package com.iformall.domain.po;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.utils.Constant;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author gongbiao
 */
@TableName(value = "wx_buser")
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class WxBuser extends BaseEntity {

    protected Long id;

    @io.swagger.annotations.ApiModelProperty(value = "租户ID", name = "tenantId")
    private String tenantId;
    @io.swagger.annotations.ApiModelProperty(value = "微信openId", name = "openId")
    private String openId;
    @io.swagger.annotations.ApiModelProperty(value = "微信unionId", name = "unionId")
    private String unionId;
    @io.swagger.annotations.ApiModelProperty(value = "用户昵称", name = "nickName")
    private String nickName;
    @io.swagger.annotations.ApiModelProperty(value = "用户性别", name = "gender")
    private Integer gender;
    @io.swagger.annotations.ApiModelProperty(value = "用户头像地址", name = "avatarUrl")
    private String avatarUrl;
    @io.swagger.annotations.ApiModelProperty(value = "用户绑定的手机号", name = "phone")
    private String phone;
    @io.swagger.annotations.ApiModelProperty(value = "用户没有区号的手机号", name = "purePhone")
    private String purePhone;
    @io.swagger.annotations.ApiModelProperty(value = "用户所在城市", name = "city")
    private String city;
    @io.swagger.annotations.ApiModelProperty(value = "用户所在省份", name = "province")
    private String province;
    @io.swagger.annotations.ApiModelProperty(value = "用户所在语言", name = "language")
    private String language;
    @io.swagger.annotations.ApiModelProperty(value = "用户手机区号", name = "countryCode")
    private String countryCode;
    @io.swagger.annotations.ApiModelProperty(value = "用户注册IP", name = "registerIp")
    private String registerIp;
    @io.swagger.annotations.ApiModelProperty(value = "验证码手机", name = "verifyCodePhone")
    private String verifyCodePhone;
    @io.swagger.annotations.ApiModelProperty(value = "注册时记录用户扫码渠道，如朋友圈广告", name = "qrcodeSource")
    private String qrcodeSource;
    @io.swagger.annotations.ApiModelProperty(value = "二维码来源，小程序", name = "scene")
    private String scene;
    @io.swagger.annotations.ApiModelProperty(value = "渠道，小程序", name = "sceneAddress")
    private String sceneAddress;
    @io.swagger.annotations.ApiModelProperty(value = "session key for 微信小程序", name = "sessionKey")
    private String sessionKey;
    @io.swagger.annotations.ApiModelProperty(value = "成长值", name = "score")
    private Integer score;
    @io.swagger.annotations.ApiModelProperty(value = "更新时间", name = "updateDate")
    private Date updateDate;
    @io.swagger.annotations.ApiModelProperty(value = "上次活跃时间", name = "activeTime")
    private Date activeTime;
    @io.swagger.annotations.ApiModelProperty(value = "创建时间", name = "createDate")
    private Date createDate;
    @io.swagger.annotations.ApiModelProperty(value = "小程序的appId", name = "appId")
    private String appId;
    /*用户登录用token**/
    @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 = "经度", name = "longitude")
    private BigDecimal longitude;
    /*纬度**/
    @io.swagger.annotations.ApiModelProperty(value = "纬度", name = "latitude")
    private BigDecimal latitude;
    /*登录次数**/
    @io.swagger.annotations.ApiModelProperty(value = "登录次数", name = "loginCount")
    private Integer loginCount;
    /*附加信息**/
    @io.swagger.annotations.ApiModelProperty(value = "附加信息", name = "extraInfo")
    private String extraInfo;
    /**
     * 是否关注公众号
     **/
    @io.swagger.annotations.ApiModelProperty(value = "是否关注公众号", name = "isSubscribe")
    private Integer isSubscribe;

    @io.swagger.annotations.ApiModelProperty(value = "微信开放平台appId", name = "openAppId")
    private String openAppId;
    @io.swagger.annotations.ApiModelProperty(value = "服务号openId", name = "mpOpenId")
    private String mpOpenId;
    @io.swagger.annotations.ApiModelProperty(value = "服务号appId", name = "mpAppId")
    private String mpAppId;
    @io.swagger.annotations.ApiModelProperty(value = "是否关注服务号", name = "mpSubscribe")
    private Integer mpSubscribe;
    @io.swagger.annotations.ApiModelProperty(value = "关注服务号时间", name = "mpSubscribeTime")
    private Date mpSubscribeTime;
    @io.swagger.annotations.ApiModelProperty(value = "关注服务号Scene", name = "mpSubscribeScene")
    private String mpSubscribeScene;
    @io.swagger.annotations.ApiModelProperty(value = "订阅号openId", name = "subsOpenId")
    private String subsOpenId;
    @io.swagger.annotations.ApiModelProperty(value = "订阅号appId", name = "subsAppId")
    private String subsAppId;
    @io.swagger.annotations.ApiModelProperty(value = "是否关注订阅号", name = "subsSubscribe")
    private Integer subsSubscribe;
    @io.swagger.annotations.ApiModelProperty(value = "关注订阅号时间", name = "subsSubscribeTime")
    private Date subsSubscribeTime;
    @io.swagger.annotations.ApiModelProperty(value = "关注订阅号Scene", name = "subsSubscribeScene")
    private String subsSubscribeScene;

    @io.swagger.annotations.ApiModelProperty(value = "积分", name = "credit")
    private Integer credit;

    @TableField(exist = false)
    @io.swagger.annotations.ApiModelProperty(value = "渠道名称", name = "channelName")
    private String channelName;

    @TableField(exist = false)
    @io.swagger.annotations.ApiModelProperty(value = "渠道描述", name = "sceneDescription")
    protected String sceneDescription;

    @TableField(exist = false)
    List<String> sceneList;

    @TableField(exist = false)
    protected Date startDate;

    @TableField(exist = false)
    protected Date endDate;

    @TableField(exist = false)
    @io.swagger.annotations.ApiModelProperty(value = "操作人类型 枚举：EnumUserType", name = "operatorType")
    private Integer operatorType;

    @TableField(exist = false)
    @io.swagger.annotations.ApiModelProperty(value = "操作人ID", name = "operatorId")
    private Long operatorId;

    @io.swagger.annotations.ApiModelProperty(value = "登录用户ID", name = "userId")
    private Long userId;

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