51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package model
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
|
||
"spark-server/internal/module/user/entity"
|
||
)
|
||
|
||
// UserLoginLog maps table user_login_logs: one row per successful login.
|
||
type UserLoginLog struct {
|
||
ID int64 `gorm:"column:id;primaryKey;autoIncrement;comment:行 id,表内自增"`
|
||
UserID int64 `gorm:"column:user_id;comment:登录用户"`
|
||
DeviceID uint64 `gorm:"column:device_id;comment:devices.device_id"`
|
||
AppID uint8 `gorm:"column:app_id;comment:当次登录 app_id"`
|
||
DeviceType string `gorm:"column:device_type;comment:设备类型"`
|
||
DeviceName string `gorm:"column:device_name;comment:设备展示名"`
|
||
AppVersion string `gorm:"column:app_version;comment:客户端版本"`
|
||
Language string `gorm:"column:language;comment:登录语言"`
|
||
IP string `gorm:"column:ip;comment:PROXY RealIP"`
|
||
CreatedAt time.Time `gorm:"column:created_at;comment:本次登录时间"`
|
||
}
|
||
|
||
func (UserLoginLog) TableName() string { return "t_user_login_logs" }
|
||
|
||
func (m *Repo) InsertLoginLog(ctx context.Context, log *entity.LoginLog) error {
|
||
if log == nil {
|
||
return nil
|
||
}
|
||
db, err := m.session(ctx)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
row := UserLoginLog{
|
||
UserID: log.UserID,
|
||
DeviceID: log.DeviceID,
|
||
AppID: log.AppID,
|
||
DeviceType: log.DeviceType,
|
||
DeviceName: log.DeviceName,
|
||
AppVersion: log.AppVersion,
|
||
Language: log.Language,
|
||
IP: log.IP,
|
||
CreatedAt: log.CreatedAt,
|
||
}
|
||
if err := db.Create(&row).Error; err != nil {
|
||
return mapDBErr(err)
|
||
}
|
||
log.ID = row.ID
|
||
return nil
|
||
}
|