package session

import (
	"gorm.io/gorm"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/utils"
	"time"
)

// GetTokenByOpenId 根据用户id获取token
func GetTokenByOpenId(openID string) (item *Session, err error) {
	err = getDb().Where("open_id = ? ", openID).First(&item).Error
	return
}

// GetTokenByToken 根据token获取信息
func GetTokenByToken(token string) (item *Session, err error) {
	err = getDb().Where("access_token = ? and expire_time> ? ", token, time.Now()).Order("session_id DESC").First(&item).Error
	return
}

// 获取数据库链接
func getDb() *gorm.DB {
	return global.MYSQL["rddp"]
}

// GetTokenByUid 根据用户id获取token
func GetTokenByUid(userId int) (item *Session, err error) {
	err = getDb().Where("user_id = ? AND expire_time > ? ", userId, time.Now().Format(utils.FormatDateTime)).Order("session_id DESC").First(&item).Error
	return
}
