package session

import (
	"fmt"
	"time"
)

// Update 更新对应字段数据
func (session *Session) Update(updateCols []string) (err error) {
	err = getDb().Model(session).Select(updateCols).Updates(*session).Error
	return
}

// UpdateSession 更新session
func (session *Session) UpdateSession(userId int64, expireTime time.Time) (err error) {
	session.UserID = userId
	session.ExpireTime = expireTime
	var updateCols = []string{"UserID", "ExpireTime"}
	err = session.Update(updateCols)
	return
}

// ClearRepeatBindAccountToken 清除重复绑定账号的token
func ClearRepeatBindAccountToken(openIds []string) (err error) {
	if len(openIds) == 0 {
		return
	}
	sql := fmt.Sprintf(` DELETE FROM session WHERE open_id IN ? AND expire_time >= NOW() `)
	err = getDb().Exec(sql, openIds).Error
	return
}
