refactor: 移除 IsDeleted 字段,统一使用 DeletedAt 进行软删除
This commit is contained in:
@@ -47,7 +47,6 @@ type SQLBaseDO struct {
|
||||
Updater string `orm:"updater" json:"updater"` // 更新人
|
||||
UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` // 更新时间
|
||||
DeletedAt *gtime.Time `orm:"deleted_at" json:"deletedAt"` // 软删除时间
|
||||
IsDeleted bool `orm:"is_deleted" json:"isDeleted"` // 是否删除
|
||||
}
|
||||
|
||||
type SQLBaseCol struct {
|
||||
@@ -58,7 +57,6 @@ type SQLBaseCol struct {
|
||||
Updater string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
IsDeleted string
|
||||
}
|
||||
|
||||
var DefSQLBaseCol = SQLBaseCol{
|
||||
@@ -69,7 +67,6 @@ var DefSQLBaseCol = SQLBaseCol{
|
||||
Updater: "updater",
|
||||
UpdatedAt: "updated_at",
|
||||
DeletedAt: "deleted_at",
|
||||
IsDeleted: "is_deleted",
|
||||
}
|
||||
|
||||
type User struct {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/utils"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
@@ -87,81 +88,6 @@ func indexInterface(indexName string, client ms.ServiceManager) ms.IndexManager
|
||||
return client.Index(indexName)
|
||||
}
|
||||
|
||||
// ensureIndexExists 确保索引存在,不存在则自动创建
|
||||
// 同时会检查并更新 filterable attributes 设置
|
||||
func (m *meilisearchDB) ensureIndexExists(client ms.ServiceManager, indexName string) error {
|
||||
// 使用 Index 方法获取索引(不存在时不会报错)
|
||||
idx := client.Index(indexName)
|
||||
|
||||
// 先获取索引信息,检查是否存在
|
||||
_, err := idx.FetchInfo()
|
||||
if err != nil {
|
||||
// 索引不存在,创建索引并等待完成
|
||||
task, err := client.CreateIndex(&ms.IndexConfig{
|
||||
Uid: indexName,
|
||||
PrimaryKey: "id",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 等待索引创建完成(最多等待10秒)
|
||||
if _, err = client.WaitForTask(task.TaskUID, 10*time.Second); err != nil {
|
||||
return fmt.Errorf("等待索引创建失败: %w", err)
|
||||
}
|
||||
// 重新获取索引
|
||||
idx = client.Index(indexName)
|
||||
}
|
||||
|
||||
// 检查并更新 filterable attributes
|
||||
settings, err := idx.GetSettings()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
requiredFilterable := []string{"tenantId", "isDeleted", "dataset_id", "creator", "updater"}
|
||||
needUpdate := false
|
||||
|
||||
// 检查是否缺少必要的 filterable attributes
|
||||
existingFilterable := make(map[string]bool)
|
||||
for _, attr := range settings.FilterableAttributes {
|
||||
existingFilterable[attr] = true
|
||||
}
|
||||
|
||||
for _, attr := range requiredFilterable {
|
||||
if !existingFilterable[attr] {
|
||||
needUpdate = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if needUpdate {
|
||||
// 合并现有的 filterable attributes 和新增的
|
||||
allFilterable := append(settings.FilterableAttributes, requiredFilterable...)
|
||||
uniqueFilterable := make(map[string]bool)
|
||||
var finalFilterable []string
|
||||
for _, attr := range allFilterable {
|
||||
if !uniqueFilterable[attr] {
|
||||
uniqueFilterable[attr] = true
|
||||
finalFilterable = append(finalFilterable, attr)
|
||||
}
|
||||
}
|
||||
|
||||
updateSettings := &ms.Settings{
|
||||
FilterableAttributes: finalFilterable,
|
||||
}
|
||||
task, err := idx.UpdateSettings(updateSettings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 等待设置更新完成(最多等待10秒)
|
||||
if _, err = client.WaitForTask(task.TaskUID, 10*time.Second); err != nil {
|
||||
return fmt.Errorf("等待设置更新失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildSearchRequest 构建搜索请求
|
||||
func (m *meilisearchDB) buildSearchRequest(ctx context.Context, searchParams *SearchParams) (*ms.SearchRequest, error) {
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
@@ -195,12 +121,12 @@ func (m *meilisearchDB) buildSearchRequest(ctx context.Context, searchParams *Se
|
||||
// 设置过滤条件(包含租户过滤和软删除过滤)
|
||||
filter := ""
|
||||
if !g.IsEmpty(user.TenantId) {
|
||||
filter = fmt.Sprintf("tenantId = %s", gconv.String(user.TenantId))
|
||||
filter = fmt.Sprintf("%s = %s", beans.DefSQLBaseCol.TenantId, gconv.String(user.TenantId))
|
||||
}
|
||||
if filter == "" {
|
||||
filter = "isDeleted = false"
|
||||
filter = fmt.Sprintf("%s = null", beans.DefSQLBaseCol.DeletedAt)
|
||||
} else {
|
||||
filter += " AND isDeleted = false"
|
||||
filter += fmt.Sprintf("AND %s = null", beans.DefSQLBaseCol.DeletedAt)
|
||||
}
|
||||
|
||||
// 添加用户自定义过滤条件
|
||||
@@ -254,7 +180,7 @@ func (m *meilisearchDB) Search(ctx context.Context, searchParams *SearchParams,
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cacheKey := fmt.Sprintf("meilisearch:search:%s:%s:%+v", user.TenantId, indexName, searchParams)
|
||||
cacheKey := fmt.Sprintf("meilisearch:search:%v:%s:%+v", user.TenantId, indexName, searchParams)
|
||||
if !m.noCache {
|
||||
var resultStr *gvar.Var
|
||||
resultStr, err = g.Redis().Get(ctx, cacheKey)
|
||||
@@ -343,11 +269,6 @@ func (m *meilisearchDB) Insert(ctx context.Context, document interface{}, indexN
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 确保索引存在
|
||||
if err = m.ensureIndexExists(c, indexName); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -357,32 +278,32 @@ func (m *meilisearchDB) Insert(ctx context.Context, document interface{}, indexN
|
||||
docMap := gconv.Map(document)
|
||||
|
||||
// 设置租户ID
|
||||
if !g.IsEmpty(user.TenantId) && g.IsEmpty(docMap["tenantId"]) {
|
||||
docMap["tenantId"] = user.TenantId
|
||||
if !g.IsEmpty(user.TenantId) && g.IsEmpty(docMap[beans.DefSQLBaseCol.TenantId]) {
|
||||
docMap[beans.DefSQLBaseCol.TenantId] = user.TenantId
|
||||
}
|
||||
|
||||
// 设置创建人
|
||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap["creator"]) {
|
||||
docMap["creator"] = user.UserName
|
||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap[beans.DefSQLBaseCol.Creator]) {
|
||||
docMap[beans.DefSQLBaseCol.Creator] = user.UserName
|
||||
}
|
||||
|
||||
// 设置更新人
|
||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap["updater"]) {
|
||||
docMap["updater"] = user.UserName
|
||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap[beans.DefSQLBaseCol.Updater]) {
|
||||
docMap[beans.DefSQLBaseCol.Updater] = user.UserName
|
||||
}
|
||||
|
||||
// 设置时间
|
||||
now := gtime.Now().Time
|
||||
if g.IsEmpty(docMap["createdAt"]) {
|
||||
docMap["createdAt"] = now.Unix()
|
||||
if g.IsEmpty(docMap[beans.DefSQLBaseCol.CreatedAt]) {
|
||||
docMap[beans.DefSQLBaseCol.CreatedAt] = now.Unix()
|
||||
}
|
||||
if g.IsEmpty(docMap["updatedAt"]) {
|
||||
docMap["updatedAt"] = now.Unix()
|
||||
if g.IsEmpty(docMap[beans.DefSQLBaseCol.UpdatedAt]) {
|
||||
docMap[beans.DefSQLBaseCol.UpdatedAt] = now.Unix()
|
||||
}
|
||||
|
||||
// 设置删除标记
|
||||
if g.IsEmpty(docMap["isDeleted"]) {
|
||||
docMap["isDeleted"] = false
|
||||
if g.IsEmpty(docMap[beans.DefSQLBaseCol.DeletedAt]) {
|
||||
docMap[beans.DefSQLBaseCol.DeletedAt] = nil
|
||||
}
|
||||
|
||||
// 执行插入
|
||||
@@ -409,11 +330,6 @@ func (m *meilisearchDB) InsertMany(ctx context.Context, documents []interface{},
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 确保索引存在
|
||||
if err = m.ensureIndexExists(c, indexName); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -424,32 +340,32 @@ func (m *meilisearchDB) InsertMany(ctx context.Context, documents []interface{},
|
||||
docMap := gconv.Map(document)
|
||||
|
||||
// 设置租户ID
|
||||
if !g.IsEmpty(user.TenantId) && g.IsEmpty(docMap["tenantId"]) {
|
||||
docMap["tenantId"] = user.TenantId
|
||||
if !g.IsEmpty(user.TenantId) && g.IsEmpty(docMap[beans.DefSQLBaseCol.TenantId]) {
|
||||
docMap[beans.DefSQLBaseCol.TenantId] = user.TenantId
|
||||
}
|
||||
|
||||
// 设置创建人
|
||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap["creator"]) {
|
||||
docMap["creator"] = user.UserName
|
||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap[beans.DefSQLBaseCol.Creator]) {
|
||||
docMap[beans.DefSQLBaseCol.Creator] = user.UserName
|
||||
}
|
||||
|
||||
// 设置更新人
|
||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap["updater"]) {
|
||||
docMap["updater"] = user.UserName
|
||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap[beans.DefSQLBaseCol.Updater]) {
|
||||
docMap[beans.DefSQLBaseCol.Updater] = user.UserName
|
||||
}
|
||||
|
||||
// 设置时间
|
||||
now := gtime.Now().Time
|
||||
if g.IsEmpty(docMap["createdAt"]) {
|
||||
docMap["createdAt"] = now.Unix()
|
||||
if g.IsEmpty(docMap[beans.DefSQLBaseCol.CreatedAt]) {
|
||||
docMap[beans.DefSQLBaseCol.CreatedAt] = now.Unix()
|
||||
}
|
||||
if g.IsEmpty(docMap["updatedAt"]) {
|
||||
docMap["updatedAt"] = now.Unix()
|
||||
if g.IsEmpty(docMap[beans.DefSQLBaseCol.UpdatedAt]) {
|
||||
docMap[beans.DefSQLBaseCol.UpdatedAt] = now.Unix()
|
||||
}
|
||||
|
||||
// 设置删除标记
|
||||
if g.IsEmpty(docMap["isDeleted"]) {
|
||||
docMap["isDeleted"] = false
|
||||
if g.IsEmpty(docMap[beans.DefSQLBaseCol.DeletedAt]) {
|
||||
docMap[beans.DefSQLBaseCol.DeletedAt] = nil
|
||||
}
|
||||
|
||||
docs = append(docs, docMap)
|
||||
@@ -487,12 +403,12 @@ func (m *meilisearchDB) Update(ctx context.Context, document interface{}, indexN
|
||||
docMap := gconv.Map(document)
|
||||
|
||||
// 设置更新人
|
||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap["updater"]) {
|
||||
docMap["updater"] = user.UserName
|
||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap[beans.DefSQLBaseCol.Updater]) {
|
||||
docMap[beans.DefSQLBaseCol.Updater] = user.UserName
|
||||
}
|
||||
|
||||
// 设置更新时间
|
||||
docMap["updatedAt"] = gtime.Now().Unix()
|
||||
docMap[beans.DefSQLBaseCol.UpdatedAt] = gtime.Now().Unix()
|
||||
|
||||
// 执行更新
|
||||
documents := []map[string]interface{}{docMap}
|
||||
@@ -552,10 +468,10 @@ func (m *meilisearchDB) DeleteSoft(ctx context.Context, id string, indexName str
|
||||
|
||||
// 软删除:更新 isDeleted 字段
|
||||
updateMap := map[string]interface{}{
|
||||
"id": id,
|
||||
"isDeleted": true,
|
||||
"updater": user.UserName,
|
||||
"updatedAt": gtime.Now().Unix(),
|
||||
beans.DefSQLBaseCol.Id: id,
|
||||
beans.DefSQLBaseCol.DeletedAt: gtime.Now().Unix(),
|
||||
beans.DefSQLBaseCol.Updater: user.UserName,
|
||||
beans.DefSQLBaseCol.UpdatedAt: gtime.Now().Unix(),
|
||||
}
|
||||
|
||||
// 执行更新
|
||||
@@ -587,7 +503,7 @@ func (m *meilisearchDB) Get(ctx context.Context, id string, indexName string, re
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cacheKey := fmt.Sprintf("meilisearch:doc:%s:%s:%s", user.TenantId, indexName, id)
|
||||
cacheKey := fmt.Sprintf("meilisearch:doc:%v:%s:%s", user.TenantId, indexName, id)
|
||||
if !m.noCache {
|
||||
var resultStr *gvar.Var
|
||||
resultStr, err = g.Redis().Get(ctx, cacheKey)
|
||||
@@ -608,7 +524,7 @@ func (m *meilisearchDB) Get(ctx context.Context, id string, indexName string, re
|
||||
}
|
||||
|
||||
// 过滤已删除的文档
|
||||
if gconv.Bool(doc["isDeleted"]) {
|
||||
if !g.IsEmpty(doc[beans.DefSQLBaseCol.DeletedAt]) {
|
||||
return gerror.New("文档不存在")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user