优化表结构,重构模型,重新实现基于白银网关的提取节点流程
This commit is contained in:
@@ -28,13 +28,23 @@ func newLogsLogin(db *gorm.DB, opts ...gen.DOOption) logsLogin {
|
||||
tableName := _logsLogin.logsLoginDo.TableName()
|
||||
_logsLogin.ALL = field.NewAsterisk(tableName)
|
||||
_logsLogin.ID = field.NewInt32(tableName, "id")
|
||||
_logsLogin.IP = field.NewString(tableName, "ip")
|
||||
_logsLogin.IP = field.NewField(tableName, "ip")
|
||||
_logsLogin.UA = field.NewString(tableName, "ua")
|
||||
_logsLogin.GrantType = field.NewString(tableName, "grant_type")
|
||||
_logsLogin.PasswordGrantType = field.NewString(tableName, "password_grant_type")
|
||||
_logsLogin.PasswordType = field.NewString(tableName, "password_type")
|
||||
_logsLogin.Success = field.NewBool(tableName, "success")
|
||||
_logsLogin.UserID = field.NewInt32(tableName, "user_id")
|
||||
_logsLogin.Time = field.NewField(tableName, "time")
|
||||
_logsLogin.Time = field.NewTime(tableName, "time")
|
||||
_logsLogin.User = logsLoginBelongsToUser{
|
||||
db: db.Session(&gorm.Session{}),
|
||||
|
||||
RelationField: field.NewRelation("User", "models.User"),
|
||||
Admin: struct {
|
||||
field.RelationField
|
||||
}{
|
||||
RelationField: field.NewRelation("User.Admin", "models.Admin"),
|
||||
},
|
||||
}
|
||||
|
||||
_logsLogin.fillFieldMap()
|
||||
|
||||
@@ -44,15 +54,16 @@ func newLogsLogin(db *gorm.DB, opts ...gen.DOOption) logsLogin {
|
||||
type logsLogin struct {
|
||||
logsLoginDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Int32 // 登录日志ID
|
||||
IP field.String // IP地址
|
||||
UA field.String // 用户代理
|
||||
GrantType field.String // 授权类型:authorization_code-授权码模式,client_credentials-客户端凭证模式,refresh_token-刷新令牌模式,password-密码模式
|
||||
PasswordGrantType field.String // 密码模式子授权类型:password-账号密码,phone_code-手机验证码,email_code-邮箱验证码
|
||||
Success field.Bool // 登录是否成功
|
||||
UserID field.Int32 // 用户ID
|
||||
Time field.Field // 登录时间
|
||||
ALL field.Asterisk
|
||||
ID field.Int32
|
||||
IP field.Field
|
||||
UA field.String
|
||||
GrantType field.String
|
||||
PasswordType field.String
|
||||
Success field.Bool
|
||||
UserID field.Int32
|
||||
Time field.Time
|
||||
User logsLoginBelongsToUser
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
@@ -70,13 +81,13 @@ func (l logsLogin) As(alias string) *logsLogin {
|
||||
func (l *logsLogin) updateTableName(table string) *logsLogin {
|
||||
l.ALL = field.NewAsterisk(table)
|
||||
l.ID = field.NewInt32(table, "id")
|
||||
l.IP = field.NewString(table, "ip")
|
||||
l.IP = field.NewField(table, "ip")
|
||||
l.UA = field.NewString(table, "ua")
|
||||
l.GrantType = field.NewString(table, "grant_type")
|
||||
l.PasswordGrantType = field.NewString(table, "password_grant_type")
|
||||
l.PasswordType = field.NewString(table, "password_type")
|
||||
l.Success = field.NewBool(table, "success")
|
||||
l.UserID = field.NewInt32(table, "user_id")
|
||||
l.Time = field.NewField(table, "time")
|
||||
l.Time = field.NewTime(table, "time")
|
||||
|
||||
l.fillFieldMap()
|
||||
|
||||
@@ -93,27 +104,116 @@ func (l *logsLogin) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
}
|
||||
|
||||
func (l *logsLogin) fillFieldMap() {
|
||||
l.fieldMap = make(map[string]field.Expr, 8)
|
||||
l.fieldMap = make(map[string]field.Expr, 9)
|
||||
l.fieldMap["id"] = l.ID
|
||||
l.fieldMap["ip"] = l.IP
|
||||
l.fieldMap["ua"] = l.UA
|
||||
l.fieldMap["grant_type"] = l.GrantType
|
||||
l.fieldMap["password_grant_type"] = l.PasswordGrantType
|
||||
l.fieldMap["password_type"] = l.PasswordType
|
||||
l.fieldMap["success"] = l.Success
|
||||
l.fieldMap["user_id"] = l.UserID
|
||||
l.fieldMap["time"] = l.Time
|
||||
|
||||
}
|
||||
|
||||
func (l logsLogin) clone(db *gorm.DB) logsLogin {
|
||||
l.logsLoginDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
l.User.db = db.Session(&gorm.Session{Initialized: true})
|
||||
l.User.db.Statement.ConnPool = db.Statement.ConnPool
|
||||
return l
|
||||
}
|
||||
|
||||
func (l logsLogin) replaceDB(db *gorm.DB) logsLogin {
|
||||
l.logsLoginDo.ReplaceDB(db)
|
||||
l.User.db = db.Session(&gorm.Session{})
|
||||
return l
|
||||
}
|
||||
|
||||
type logsLoginBelongsToUser struct {
|
||||
db *gorm.DB
|
||||
|
||||
field.RelationField
|
||||
|
||||
Admin struct {
|
||||
field.RelationField
|
||||
}
|
||||
}
|
||||
|
||||
func (a logsLoginBelongsToUser) Where(conds ...field.Expr) *logsLoginBelongsToUser {
|
||||
if len(conds) == 0 {
|
||||
return &a
|
||||
}
|
||||
|
||||
exprs := make([]clause.Expression, 0, len(conds))
|
||||
for _, cond := range conds {
|
||||
exprs = append(exprs, cond.BeCond().(clause.Expression))
|
||||
}
|
||||
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a logsLoginBelongsToUser) WithContext(ctx context.Context) *logsLoginBelongsToUser {
|
||||
a.db = a.db.WithContext(ctx)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a logsLoginBelongsToUser) Session(session *gorm.Session) *logsLoginBelongsToUser {
|
||||
a.db = a.db.Session(session)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a logsLoginBelongsToUser) Model(m *models.LogsLogin) *logsLoginBelongsToUserTx {
|
||||
return &logsLoginBelongsToUserTx{a.db.Model(m).Association(a.Name())}
|
||||
}
|
||||
|
||||
func (a logsLoginBelongsToUser) Unscoped() *logsLoginBelongsToUser {
|
||||
a.db = a.db.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type logsLoginBelongsToUserTx struct{ tx *gorm.Association }
|
||||
|
||||
func (a logsLoginBelongsToUserTx) Find() (result *models.User, err error) {
|
||||
return result, a.tx.Find(&result)
|
||||
}
|
||||
|
||||
func (a logsLoginBelongsToUserTx) Append(values ...*models.User) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Append(targetValues...)
|
||||
}
|
||||
|
||||
func (a logsLoginBelongsToUserTx) Replace(values ...*models.User) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Replace(targetValues...)
|
||||
}
|
||||
|
||||
func (a logsLoginBelongsToUserTx) Delete(values ...*models.User) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Delete(targetValues...)
|
||||
}
|
||||
|
||||
func (a logsLoginBelongsToUserTx) Clear() error {
|
||||
return a.tx.Clear()
|
||||
}
|
||||
|
||||
func (a logsLoginBelongsToUserTx) Count() int64 {
|
||||
return a.tx.Count()
|
||||
}
|
||||
|
||||
func (a logsLoginBelongsToUserTx) Unscoped() *logsLoginBelongsToUserTx {
|
||||
a.tx = a.tx.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type logsLoginDo struct{ gen.DO }
|
||||
|
||||
func (l logsLoginDo) Debug() *logsLoginDo {
|
||||
|
||||
Reference in New Issue
Block a user