feat: 支持关联多标签、计划任务立即执行、标签相关操作优化

This commit is contained in:
meilin.huang
2023-12-05 23:03:51 +08:00
parent b347bd7ef5
commit 57361d8241
107 changed files with 1819 additions and 825 deletions

View File

@@ -3,7 +3,10 @@ package contextx
import (
"context"
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/stringx"
"gorm.io/gorm"
)
type CtxKey string
@@ -11,6 +14,7 @@ type CtxKey string
const (
LoginAccountKey CtxKey = "loginAccount"
TraceIdKey CtxKey = "traceId"
DbKey CtxKey = "db"
)
func NewLoginAccount(la *model.LoginAccount) context.Context {
@@ -44,3 +48,30 @@ func GetTraceId(ctx context.Context) string {
}
return ""
}
// 将事务db放置context中使用stack保存。以便多个方法调用实现方法内部各自的事务操作
func WithDb(ctx context.Context, db *gorm.DB) context.Context {
if dbStack, ok := ctx.Value(DbKey).(*collx.Stack[*gorm.DB]); ok {
dbStack.Push(db)
return ctx
}
dbStack := new(collx.Stack[*gorm.DB])
dbStack.Push(db)
return context.WithValue(ctx, DbKey, dbStack)
}
// 获取当前操作的栈顶事务数据库实例
func GetDb(ctx context.Context) *gorm.DB {
if dbStack, ok := ctx.Value(DbKey).(*collx.Stack[*gorm.DB]); ok {
return dbStack.Top()
}
return nil
}
func RmDb(ctx context.Context) *gorm.DB {
if dbStack, ok := ctx.Value(DbKey).(*collx.Stack[*gorm.DB]); ok {
return dbStack.Pop()
}
return nil
}