From f6e936a76044c1750887a2dd9e691c5480ae4aba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Thu, 20 Mar 2025 18:44:01 +0800 Subject: [PATCH] feat: add product tags index --- pkg/domain/repository/product_statistics.go | 3 ++- pkg/domain/repository/tags.go | 3 ++- pkg/repository/product_statistics.go | 18 +++++++++++++----- pkg/repository/product_statistics_test.go | 9 +++++---- pkg/repository/product_tags.go | 7 ++++--- pkg/repository/product_tags_biinding.go | 19 ++++++++++++------- 6 files changed, 38 insertions(+), 21 deletions(-) diff --git a/pkg/domain/repository/product_statistics.go b/pkg/domain/repository/product_statistics.go index e7f3621..22e2c7b 100644 --- a/pkg/domain/repository/product_statistics.go +++ b/pkg/domain/repository/product_statistics.go @@ -1,8 +1,9 @@ package repository import ( - "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity" "context" + + "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity" "go.mongodb.org/mongo-driver/mongo" ) diff --git a/pkg/domain/repository/tags.go b/pkg/domain/repository/tags.go index 650add0..5e50940 100644 --- a/pkg/domain/repository/tags.go +++ b/pkg/domain/repository/tags.go @@ -1,9 +1,10 @@ package repository import ( + "context" + "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/product" - "context" "go.mongodb.org/mongo-driver/mongo" ) diff --git a/pkg/repository/product_statistics.go b/pkg/repository/product_statistics.go index 3433bd0..1793f2a 100644 --- a/pkg/repository/product_statistics.go +++ b/pkg/repository/product_statistics.go @@ -1,20 +1,22 @@ package repository import ( + "context" + "errors" + "fmt" + "math" + "time" + "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository" mgo "code.30cm.net/digimon/library-go/mongo" - "context" - "errors" - "fmt" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/mon" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" - "time" ) type ProductStatisticsRepositoryParam struct { @@ -190,8 +192,14 @@ func (repo *ProductStatisticsRepository) DecFansCount(ctx context.Context, produ // 只允許在 fans_count 大於或等於欲扣減值時進行扣減 filter := bson.M{"product_id": productID, "fans_count": bson.M{"$gte": fansCount}} now := time.Now().UnixNano() + + // 在更新之前先檢查 fansCount 是否過大 + if fansCount > uint64(math.MaxInt64) { + return fmt.Errorf("fansCount value too large: %d", fansCount) + } + delta := -int64(fansCount) update := bson.M{ - "$inc": bson.M{"fans_count": -int64(fansCount)}, + "$inc": bson.M{"fans_count": delta}, "$set": bson.M{ "fans_count_update_time": now, "updated_at": now, diff --git a/pkg/repository/product_statistics_test.go b/pkg/repository/product_statistics_test.go index fd13a90..1ef60b5 100644 --- a/pkg/repository/product_statistics_test.go +++ b/pkg/repository/product_statistics_test.go @@ -1,19 +1,20 @@ package repository import ( + "context" + "fmt" + "testing" + "time" + "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository" mgo "code.30cm.net/digimon/library-go/mongo" - "context" - "fmt" "github.com/alicebob/miniredis/v2" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/mon" "github.com/zeromicro/go-zero/core/stores/redis" - "testing" - "time" ) func SetupTestProductStatisticsRepo(db string) (repository.ProductStatisticsRepo, func(), error) { diff --git a/pkg/repository/product_tags.go b/pkg/repository/product_tags.go index 766003e..514bfac 100644 --- a/pkg/repository/product_tags.go +++ b/pkg/repository/product_tags.go @@ -1,19 +1,20 @@ package repository import ( + "context" + "errors" + "time" + "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository" mgo "code.30cm.net/digimon/library-go/mongo" - "context" - "errors" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/mon" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" - "time" ) type TagsRepositoryParam struct { diff --git a/pkg/repository/product_tags_biinding.go b/pkg/repository/product_tags_biinding.go index 86752aa..a24c5c8 100644 --- a/pkg/repository/product_tags_biinding.go +++ b/pkg/repository/product_tags_biinding.go @@ -1,14 +1,15 @@ package repository import ( + "context" + "time" + "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository" - "context" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" - "time" ) func (repo *TagsRepository) BindTag(ctx context.Context, data *entity.TagsBindingTable) error { @@ -48,12 +49,12 @@ func (repo *TagsRepository) GetBindingsByReference(ctx context.Context, referenc func (repo *TagsRepository) ListTagBinding(ctx context.Context, params repository.TagBindingQueryParams) ([]*entity.TagsBindingTable, int64, error) { // 構建查詢過濾器 filter := bson.M{} - if params.ReferenceID != nil { - filter["reference_id"] = *params.ReferenceID - } if params.TagID != nil { filter["tag_id"] = *params.TagID } + if params.ReferenceID != nil { + filter["reference_id"] = *params.ReferenceID + } // 設置排序選項 opts := options.Find().SetSkip((params.PageIndex - 1) * params.PageSize).SetLimit(params.PageSize) @@ -76,6 +77,10 @@ func (repo *TagsRepository) ListTagBinding(ctx context.Context, params repositor } func (repo *TagsRepository) IndexTagsBinding20250317001UP(ctx context.Context) (*mongo.Cursor, error) { - //TODO implement me - panic("implement me") + repo.TageBinding.PopulateMultiIndex(ctx, []string{ + "tag_id", + "reference_id", + }, []int32{1, 1}, true) + + return repo.Tags.GetClient().Indexes().List(ctx) }