增加es归档 分布式和constants变量
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/consts"
|
||||
"gitee.com/red-future---jilin-g/common/do"
|
||||
"gitee.com/red-future---jilin-g/common/redis"
|
||||
"gitee.com/red-future---jilin-g/common/utils"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
@@ -21,7 +22,12 @@ import (
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
var db = new(mongo.Database)
|
||||
var db *mongo.Database
|
||||
|
||||
// GetDB 获取 MongoDB 数据库实例
|
||||
func GetDB() *mongo.Database {
|
||||
return db
|
||||
}
|
||||
|
||||
func init() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
@@ -88,12 +94,49 @@ func oneOptionsToMap(ctx context.Context, opts ...options.Lister[options.FindOne
|
||||
return
|
||||
}
|
||||
|
||||
// GetTenantInfo 获取租户信息
|
||||
// 优先从 token 获取,失败则从请求参数 customerServiceId 查询 customer_service_account 表
|
||||
func GetTenantInfo(ctx context.Context) (user do.User, err error) {
|
||||
// 1. 优先从 token 获取
|
||||
user, err = utils.GetUserInfo(ctx)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 2. token 获取失败,尝试从请求参数获取 customerServiceId
|
||||
req := g.RequestFromCtx(ctx)
|
||||
if req == nil {
|
||||
return user, gerror.New("无法获取租户信息:无 token 且无 request")
|
||||
}
|
||||
|
||||
customerServiceId := req.Get("customerServiceId").String()
|
||||
if customerServiceId == "" {
|
||||
customerServiceId = req.Get("customer_service_id").String()
|
||||
}
|
||||
if customerServiceId == "" {
|
||||
return user, gerror.New("无法获取租户信息:无 token 且无 customerServiceId 参数")
|
||||
}
|
||||
|
||||
// 3. 直接查询 customer_service_account 表获取 tenantId
|
||||
filter := bson.M{"customerServiceId": customerServiceId, "isDeleted": false}
|
||||
var account struct {
|
||||
TenantId interface{} `bson:"tenantId"`
|
||||
}
|
||||
if findErr := db.Collection("customer_service_account").FindOne(ctx, filter).Decode(&account); findErr != nil {
|
||||
return user, gerror.Newf("通过 customerServiceId 查询租户失败: %v", findErr)
|
||||
}
|
||||
|
||||
user.TenantId = account.TenantId
|
||||
user.UserName = customerServiceId
|
||||
return
|
||||
}
|
||||
|
||||
// Find 查询多条记录
|
||||
func Find(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.Lister[options.FindOptions]) (err error) {
|
||||
if err = utils.ValidStructPtr(result); err != nil {
|
||||
return
|
||||
}
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
user, err := GetTenantInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -135,7 +178,7 @@ func FindOne(ctx context.Context, filter bson.M, result interface{}, collection
|
||||
if err = utils.ValidStructPtr(result); err != nil {
|
||||
return
|
||||
}
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
user, err := GetTenantInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -198,7 +241,7 @@ func Delete(ctx context.Context, filter bson.M, collection string, opts ...optio
|
||||
err = gerror.New("缺少查询条件")
|
||||
return
|
||||
}
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
user, err := GetTenantInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -219,7 +262,7 @@ func Update(ctx context.Context, filter bson.M, update bson.M, collection string
|
||||
return
|
||||
}
|
||||
filter["isDeleted"] = false
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
user, err := GetTenantInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -238,7 +281,7 @@ func Update(ctx context.Context, filter bson.M, update bson.M, collection string
|
||||
|
||||
// Insert 插入多条记录
|
||||
func Insert(ctx context.Context, documents []interface{}, collection string, opts ...options.Lister[options.InsertManyOptions]) (ids []interface{}, err error) {
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
user, err := GetTenantInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -265,7 +308,7 @@ func Insert(ctx context.Context, documents []interface{}, collection string, opt
|
||||
|
||||
// Count 查询总数
|
||||
func Count(ctx context.Context, filter bson.M, collection string) (count int64, err error) {
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
user, err := GetTenantInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user