refactor: 移除 IsDeleted 字段,统一使用 DeletedAt 进行软删除
This commit is contained in:
@@ -47,7 +47,6 @@ type SQLBaseDO struct {
|
|||||||
Updater string `orm:"updater" json:"updater"` // 更新人
|
Updater string `orm:"updater" json:"updater"` // 更新人
|
||||||
UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` // 更新时间
|
UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` // 更新时间
|
||||||
DeletedAt *gtime.Time `orm:"deleted_at" json:"deletedAt"` // 软删除时间
|
DeletedAt *gtime.Time `orm:"deleted_at" json:"deletedAt"` // 软删除时间
|
||||||
IsDeleted bool `orm:"is_deleted" json:"isDeleted"` // 是否删除
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type SQLBaseCol struct {
|
type SQLBaseCol struct {
|
||||||
@@ -58,7 +57,6 @@ type SQLBaseCol struct {
|
|||||||
Updater string
|
Updater string
|
||||||
UpdatedAt string
|
UpdatedAt string
|
||||||
DeletedAt string
|
DeletedAt string
|
||||||
IsDeleted string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var DefSQLBaseCol = SQLBaseCol{
|
var DefSQLBaseCol = SQLBaseCol{
|
||||||
@@ -69,7 +67,6 @@ var DefSQLBaseCol = SQLBaseCol{
|
|||||||
Updater: "updater",
|
Updater: "updater",
|
||||||
UpdatedAt: "updated_at",
|
UpdatedAt: "updated_at",
|
||||||
DeletedAt: "deleted_at",
|
DeletedAt: "deleted_at",
|
||||||
IsDeleted: "is_deleted",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.com/red-future/common/beans"
|
||||||
"gitea.com/red-future/common/utils"
|
"gitea.com/red-future/common/utils"
|
||||||
"github.com/gogf/gf/v2/container/gvar"
|
"github.com/gogf/gf/v2/container/gvar"
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
@@ -87,81 +88,6 @@ func indexInterface(indexName string, client ms.ServiceManager) ms.IndexManager
|
|||||||
return client.Index(indexName)
|
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 构建搜索请求
|
// buildSearchRequest 构建搜索请求
|
||||||
func (m *meilisearchDB) buildSearchRequest(ctx context.Context, searchParams *SearchParams) (*ms.SearchRequest, error) {
|
func (m *meilisearchDB) buildSearchRequest(ctx context.Context, searchParams *SearchParams) (*ms.SearchRequest, error) {
|
||||||
user, err := utils.GetUserInfo(ctx)
|
user, err := utils.GetUserInfo(ctx)
|
||||||
@@ -195,12 +121,12 @@ func (m *meilisearchDB) buildSearchRequest(ctx context.Context, searchParams *Se
|
|||||||
// 设置过滤条件(包含租户过滤和软删除过滤)
|
// 设置过滤条件(包含租户过滤和软删除过滤)
|
||||||
filter := ""
|
filter := ""
|
||||||
if !g.IsEmpty(user.TenantId) {
|
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 == "" {
|
if filter == "" {
|
||||||
filter = "isDeleted = false"
|
filter = fmt.Sprintf("%s = null", beans.DefSQLBaseCol.DeletedAt)
|
||||||
} else {
|
} 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 {
|
if err != nil {
|
||||||
return
|
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 {
|
if !m.noCache {
|
||||||
var resultStr *gvar.Var
|
var resultStr *gvar.Var
|
||||||
resultStr, err = g.Redis().Get(ctx, cacheKey)
|
resultStr, err = g.Redis().Get(ctx, cacheKey)
|
||||||
@@ -343,11 +269,6 @@ func (m *meilisearchDB) Insert(ctx context.Context, document interface{}, indexN
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 确保索引存在
|
|
||||||
if err = m.ensureIndexExists(c, indexName); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := utils.GetUserInfo(ctx)
|
user, err := utils.GetUserInfo(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -357,32 +278,32 @@ func (m *meilisearchDB) Insert(ctx context.Context, document interface{}, indexN
|
|||||||
docMap := gconv.Map(document)
|
docMap := gconv.Map(document)
|
||||||
|
|
||||||
// 设置租户ID
|
// 设置租户ID
|
||||||
if !g.IsEmpty(user.TenantId) && g.IsEmpty(docMap["tenantId"]) {
|
if !g.IsEmpty(user.TenantId) && g.IsEmpty(docMap[beans.DefSQLBaseCol.TenantId]) {
|
||||||
docMap["tenantId"] = user.TenantId
|
docMap[beans.DefSQLBaseCol.TenantId] = user.TenantId
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置创建人
|
// 设置创建人
|
||||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap["creator"]) {
|
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap[beans.DefSQLBaseCol.Creator]) {
|
||||||
docMap["creator"] = user.UserName
|
docMap[beans.DefSQLBaseCol.Creator] = user.UserName
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置更新人
|
// 设置更新人
|
||||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap["updater"]) {
|
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap[beans.DefSQLBaseCol.Updater]) {
|
||||||
docMap["updater"] = user.UserName
|
docMap[beans.DefSQLBaseCol.Updater] = user.UserName
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置时间
|
// 设置时间
|
||||||
now := gtime.Now().Time
|
now := gtime.Now().Time
|
||||||
if g.IsEmpty(docMap["createdAt"]) {
|
if g.IsEmpty(docMap[beans.DefSQLBaseCol.CreatedAt]) {
|
||||||
docMap["createdAt"] = now.Unix()
|
docMap[beans.DefSQLBaseCol.CreatedAt] = now.Unix()
|
||||||
}
|
}
|
||||||
if g.IsEmpty(docMap["updatedAt"]) {
|
if g.IsEmpty(docMap[beans.DefSQLBaseCol.UpdatedAt]) {
|
||||||
docMap["updatedAt"] = now.Unix()
|
docMap[beans.DefSQLBaseCol.UpdatedAt] = now.Unix()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置删除标记
|
// 设置删除标记
|
||||||
if g.IsEmpty(docMap["isDeleted"]) {
|
if g.IsEmpty(docMap[beans.DefSQLBaseCol.DeletedAt]) {
|
||||||
docMap["isDeleted"] = false
|
docMap[beans.DefSQLBaseCol.DeletedAt] = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行插入
|
// 执行插入
|
||||||
@@ -409,11 +330,6 @@ func (m *meilisearchDB) InsertMany(ctx context.Context, documents []interface{},
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 确保索引存在
|
|
||||||
if err = m.ensureIndexExists(c, indexName); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := utils.GetUserInfo(ctx)
|
user, err := utils.GetUserInfo(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -424,32 +340,32 @@ func (m *meilisearchDB) InsertMany(ctx context.Context, documents []interface{},
|
|||||||
docMap := gconv.Map(document)
|
docMap := gconv.Map(document)
|
||||||
|
|
||||||
// 设置租户ID
|
// 设置租户ID
|
||||||
if !g.IsEmpty(user.TenantId) && g.IsEmpty(docMap["tenantId"]) {
|
if !g.IsEmpty(user.TenantId) && g.IsEmpty(docMap[beans.DefSQLBaseCol.TenantId]) {
|
||||||
docMap["tenantId"] = user.TenantId
|
docMap[beans.DefSQLBaseCol.TenantId] = user.TenantId
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置创建人
|
// 设置创建人
|
||||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap["creator"]) {
|
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap[beans.DefSQLBaseCol.Creator]) {
|
||||||
docMap["creator"] = user.UserName
|
docMap[beans.DefSQLBaseCol.Creator] = user.UserName
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置更新人
|
// 设置更新人
|
||||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap["updater"]) {
|
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap[beans.DefSQLBaseCol.Updater]) {
|
||||||
docMap["updater"] = user.UserName
|
docMap[beans.DefSQLBaseCol.Updater] = user.UserName
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置时间
|
// 设置时间
|
||||||
now := gtime.Now().Time
|
now := gtime.Now().Time
|
||||||
if g.IsEmpty(docMap["createdAt"]) {
|
if g.IsEmpty(docMap[beans.DefSQLBaseCol.CreatedAt]) {
|
||||||
docMap["createdAt"] = now.Unix()
|
docMap[beans.DefSQLBaseCol.CreatedAt] = now.Unix()
|
||||||
}
|
}
|
||||||
if g.IsEmpty(docMap["updatedAt"]) {
|
if g.IsEmpty(docMap[beans.DefSQLBaseCol.UpdatedAt]) {
|
||||||
docMap["updatedAt"] = now.Unix()
|
docMap[beans.DefSQLBaseCol.UpdatedAt] = now.Unix()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置删除标记
|
// 设置删除标记
|
||||||
if g.IsEmpty(docMap["isDeleted"]) {
|
if g.IsEmpty(docMap[beans.DefSQLBaseCol.DeletedAt]) {
|
||||||
docMap["isDeleted"] = false
|
docMap[beans.DefSQLBaseCol.DeletedAt] = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
docs = append(docs, docMap)
|
docs = append(docs, docMap)
|
||||||
@@ -487,12 +403,12 @@ func (m *meilisearchDB) Update(ctx context.Context, document interface{}, indexN
|
|||||||
docMap := gconv.Map(document)
|
docMap := gconv.Map(document)
|
||||||
|
|
||||||
// 设置更新人
|
// 设置更新人
|
||||||
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap["updater"]) {
|
if !g.IsEmpty(user.UserName) && g.IsEmpty(docMap[beans.DefSQLBaseCol.Updater]) {
|
||||||
docMap["updater"] = user.UserName
|
docMap[beans.DefSQLBaseCol.Updater] = user.UserName
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置更新时间
|
// 设置更新时间
|
||||||
docMap["updatedAt"] = gtime.Now().Unix()
|
docMap[beans.DefSQLBaseCol.UpdatedAt] = gtime.Now().Unix()
|
||||||
|
|
||||||
// 执行更新
|
// 执行更新
|
||||||
documents := []map[string]interface{}{docMap}
|
documents := []map[string]interface{}{docMap}
|
||||||
@@ -552,10 +468,10 @@ func (m *meilisearchDB) DeleteSoft(ctx context.Context, id string, indexName str
|
|||||||
|
|
||||||
// 软删除:更新 isDeleted 字段
|
// 软删除:更新 isDeleted 字段
|
||||||
updateMap := map[string]interface{}{
|
updateMap := map[string]interface{}{
|
||||||
"id": id,
|
beans.DefSQLBaseCol.Id: id,
|
||||||
"isDeleted": true,
|
beans.DefSQLBaseCol.DeletedAt: gtime.Now().Unix(),
|
||||||
"updater": user.UserName,
|
beans.DefSQLBaseCol.Updater: user.UserName,
|
||||||
"updatedAt": gtime.Now().Unix(),
|
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 {
|
if err != nil {
|
||||||
return
|
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 {
|
if !m.noCache {
|
||||||
var resultStr *gvar.Var
|
var resultStr *gvar.Var
|
||||||
resultStr, err = g.Redis().Get(ctx, cacheKey)
|
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("文档不存在")
|
return gerror.New("文档不存在")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user