package cn.pluss.platform.entity;

import cn.pluss.platform.wx.*;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;

/**
 * <p>
 * 缓存信息表
 * </p>
 *
 * @author bzg
 * @since 2021-09-24
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tb_pluss_cache_info")
public class CacheInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 缓存最大差值
     * @date: 2021/11/30 16:27
     */
    public static final Long MAX_CRITICAL_VALUE = 200L;

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


    /**
     * 特殊规则字符串组成，用与获取token
     */
    @TableField("keyCode")
    private String keyCode;


    /**
     * appid
     */
    @TableField("appId")
    private String appId;


    /**
     * appsercet
     */
    @TableField("appSercet")
    private String appSercet;


    /**
     * accessToken
     */
    @TableField("accessToken")
    private String accessToken;


    /**
     * 刷新token
     */
    @TableField("refreshToken")
    private String refreshToken;


    /**
     * 过期时间
     */
    @TableField("expires_in")
    private Long expiresIn;


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

    /**
     * 创建时间 开始时间
     */
    @TableField(exist = false, condition = "createTime &gt; #{%s}")
    private Date createTimeStart;

    /**
     * 创建时间 截止时间
     */
    @TableField(exist = false, condition = "createTime &lt; #{%s}")
    private Date createTimeEnd;

    public CacheInfo(String appId, String appSercet, String keyCode,String token,Long expiresIn){
        this.appId = appId;
        this.appSercet = appSercet;
        this.keyCode = keyCode;
        this.accessToken = token;
        this.expiresIn = expiresIn;
    }

    public CacheInfo(WxAccessTokenResponse response, WxAccessTokenRequest request, String keyCode){
        this.appId = request.getAppid();
        this.appSercet = request.getSecret();
        this.keyCode = keyCode;
        this.accessToken = response.getAccess_token();
        this.expiresIn = (System.currentTimeMillis() / 1000) + response.getExpires_in();
    }

    public CacheInfo(WxOauth2AccessTokenResponse response, WxAccessTokenRequest request, String keyCode){
        this.appId = request.getAppid();
        this.appSercet = request.getSecret();
        this.keyCode = keyCode;
        this.accessToken = response.getAccess_token();
        this.expiresIn = (System.currentTimeMillis() / 1000) + response.getExpires_in();
        this.refreshToken = response.getRefresh_token();
    }

    public CacheInfo() {

    }

    public CacheInfo(WxOpenidResponse response, WxAccessTokenRequest request, String keyCode) {
        this.appId = request.getAppid();
        this.appSercet = request.getSecret();
        this.keyCode = keyCode;
        this.accessToken = response.getAccess_token();
        this.expiresIn = (System.currentTimeMillis() / 1000) + response.getExpires_in();
        this.refreshToken = response.getRefresh_token();
    }
    public CacheInfo(WxAccessTokenRequest request, String keyCode,String accessToken,Long expiresIn,String refreshToken) {
        this.appId = request.getAppid();
        this.appSercet = request.getSecret();
        this.keyCode = keyCode;
        this.accessToken = accessToken;
        this.expiresIn = expiresIn;
        this.refreshToken = refreshToken;
    }

    public CacheInfo(WxJsApiTicketResponse response, WxAccessTokenRequest request, String keyCode) {
        this.appId = request.getAppid();
        this.appSercet = request.getSecret();
        this.keyCode = keyCode;
        this.accessToken = response.getTicket();
        this.expiresIn = (System.currentTimeMillis() / 1000) + response.getExpires_in();
    }
}