package srv_homeconfig import ( "context" "hkdj-web-server/pub/common/srvdef" "hkdj-web-server/pub/common/utils" "hkdj-web-server/pub/models/homeconfigmod" "hkdj-web-server/web/api/proto" "strings" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo/options" ) func ensureHomeConfigSlug(ctx context.Context, ac *homeconfigmod.HomeConfig, excludeId int64) *srvdef.SrvErr { ac.Slug = utils.SanitizeSlug(ac.Slug) if ac.Slug != "" { if utils.IsSlugAllDigits(ac.Slug) { return srvdef.New(srvdef.ErrParamError, "Slug不能全是数字", "Slug不能全是数字") } filter := bson.M{homeconfigmod.FN.HomeConfig.Slug: ac.Slug} if excludeId > 0 { filter[homeconfigmod.FN.HomeConfig.Id] = bson.M{"$ne": excludeId} } exist, _ := homeconfigmod.FindOneHomeConfig(ctx, filter) if exist != nil { return srvdef.New(srvdef.ErrDbInputExist, "Slug已存在", "Slug已存在") } return nil } source := utils.SlugSourceFromKeywordsOrTitle(ac.SeoInfo.Keyworld, ac.Name) ac.Slug = utils.MakeUniqueSlug(source, func(slug string) bool { filter := bson.M{homeconfigmod.FN.HomeConfig.Slug: slug} if excludeId > 0 { filter[homeconfigmod.FN.HomeConfig.Id] = bson.M{"$ne": excludeId} } exist, _ := homeconfigmod.FindOneHomeConfig(ctx, filter) return exist != nil }) return nil } func prepareHomeConfigSlugOnUpdate(ctx context.Context, id int64, info *homeconfigmod.HomeConfig, req *proto.HomeConfigUpdateReq, setDoc bson.M) *srvdef.SrvErr { slug := info.Slug if req.Slug != nil { slug = utils.SanitizeSlug(*req.Slug) } if slug != "" { if req.Slug != nil { if utils.IsSlugAllDigits(slug) { return srvdef.New(srvdef.ErrParamError, "Slug不能全是数字", "Slug不能全是数字") } exist, _ := homeconfigmod.FindOneHomeConfig(ctx, bson.M{ homeconfigmod.FN.HomeConfig.Slug: slug, homeconfigmod.FN.HomeConfig.Id: bson.M{"$ne": id}, }) if exist != nil { return srvdef.New(srvdef.ErrDbInputExist, "Slug已存在", "Slug已存在") } setDoc[homeconfigmod.FN.HomeConfig.Slug] = slug } return nil } name := info.Name if req.Name != nil { name = strings.TrimSpace(*req.Name) } keywords := info.SeoInfo.Keyworld if req.SeoInfo != nil { keywords = normalizeHomeConfigSeoValue(*req.SeoInfo).Keyworld } source := utils.SlugSourceFromKeywordsOrTitle(keywords, name) setDoc[homeconfigmod.FN.HomeConfig.Slug] = utils.MakeUniqueSlug(source, func(s string) bool { exist, _ := homeconfigmod.FindOneHomeConfig(ctx, bson.M{ homeconfigmod.FN.HomeConfig.Slug: s, homeconfigmod.FN.HomeConfig.Id: bson.M{"$ne": id}, }) return exist != nil }) return nil } func emptyHomeConfigSlugFilter() bson.M { return bson.M{ "$or": []bson.M{ {homeconfigmod.FN.HomeConfig.Slug: ""}, {homeconfigmod.FN.HomeConfig.Slug: bson.M{"$exists": false}}, }, } } // SyncEmptySlugs 为首页配置中空 slug 自动生成。 func SyncEmptySlugs(ctx context.Context, maxSize int64) (int64, bool, *srvdef.SrvErr) { if maxSize <= 0 { return 0, false, nil } list, err := homeconfigmod.FindManyHomeConfig(ctx, emptyHomeConfigSlugFilter(), options.Find(). SetSort(bson.D{{Key: homeconfigmod.FN.HomeConfig.Id, Value: 1}}). SetLimit(maxSize+1)) if err != nil { return 0, false, err } hasMore := int64(len(list)) > maxSize if hasMore { list = list[:maxSize] } var updated int64 for _, item := range list { if item == nil { continue } if serr := ensureHomeConfigSlug(ctx, item, item.Id); serr != nil { return updated, hasMore, serr } if strings.TrimSpace(item.Slug) == "" { continue } if serr := homeconfigmod.UpdateOneHomeConfig(ctx, bson.M{homeconfigmod.FN.HomeConfig.Id: item.Id}, bson.M{"$set": bson.M{homeconfigmod.FN.HomeConfig.Slug: item.Slug}}, ); serr != nil { return updated, hasMore, serr } updated++ } return updated, hasMore, nil } func HasEmptySlug(ctx context.Context) bool { count, _ := homeconfigmod.CountHomeConfig(ctx, emptyHomeConfigSlugFilter()) return count > 0 }