2023-06-09 08:59:00 +00:00
|
|
|
package model
|
|
|
|
|
2023-06-10 08:04:04 +00:00
|
|
|
import (
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"one-api/common"
|
|
|
|
)
|
2023-06-09 08:59:00 +00:00
|
|
|
|
|
|
|
type Log struct {
|
|
|
|
Id int `json:"id"`
|
|
|
|
UserId int `json:"user_id" gorm:"index"`
|
|
|
|
CreatedAt int64 `json:"created_at" gorm:"bigint"`
|
|
|
|
Type int `json:"type" gorm:"index"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
}
|
|
|
|
|
2023-06-10 08:04:04 +00:00
|
|
|
const (
|
|
|
|
LogTypeUnknown = iota
|
|
|
|
LogTypeTopup
|
|
|
|
LogTypeConsume
|
2023-06-10 08:31:40 +00:00
|
|
|
LogTypeManage
|
|
|
|
LogTypeSystem
|
2023-06-10 08:04:04 +00:00
|
|
|
)
|
|
|
|
|
2023-06-09 08:59:00 +00:00
|
|
|
func RecordLog(userId int, logType int, content string) {
|
|
|
|
log := &Log{
|
|
|
|
UserId: userId,
|
|
|
|
CreatedAt: common.GetTimestamp(),
|
|
|
|
Type: logType,
|
|
|
|
Content: content,
|
|
|
|
}
|
|
|
|
err := DB.Create(log).Error
|
|
|
|
if err != nil {
|
|
|
|
common.SysError("failed to record log: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllLogs(logType int, startIdx int, num int) (logs []*Log, err error) {
|
|
|
|
err = DB.Where("type = ?", logType).Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error
|
|
|
|
return logs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUserLogs(userId int, logType int, startIdx int, num int) (logs []*Log, err error) {
|
2023-06-10 08:04:04 +00:00
|
|
|
var tx *gorm.DB
|
|
|
|
if logType == LogTypeUnknown {
|
|
|
|
tx = DB.Where("user_id = ?", userId)
|
|
|
|
} else {
|
|
|
|
tx = DB.Where("user_id = ? and type = ?", userId, logType)
|
|
|
|
}
|
|
|
|
err = tx.Order("id desc").Limit(num).Offset(startIdx).Omit("id").Find(&logs).Error
|
2023-06-09 08:59:00 +00:00
|
|
|
return logs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchAllLogs(keyword string) (logs []*Log, err error) {
|
|
|
|
err = DB.Where("type = ? or content LIKE ?", keyword, keyword+"%").Order("id desc").Limit(common.MaxRecentItems).Find(&logs).Error
|
|
|
|
return logs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchUserLogs(userId int, keyword string) (logs []*Log, err error) {
|
2023-06-10 08:04:04 +00:00
|
|
|
err = DB.Where("user_id = ? and type = ?", userId, keyword).Order("id desc").Limit(common.MaxRecentItems).Omit("id").Find(&logs).Error
|
2023-06-09 08:59:00 +00:00
|
|
|
return logs, err
|
|
|
|
}
|