feat: add product tags index

This commit is contained in:
王性驊 2025-03-20 18:44:01 +08:00
parent 2451bc4257
commit f6e936a760
6 changed files with 38 additions and 21 deletions

View File

@ -1,8 +1,9 @@
package repository package repository
import ( import (
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity"
"context" "context"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
) )

View File

@ -1,9 +1,10 @@
package repository package repository
import ( import (
"context"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/product" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/product"
"context"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
) )

View File

@ -1,20 +1,22 @@
package repository package repository
import ( 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"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository"
mgo "code.30cm.net/digimon/library-go/mongo" 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/cache"
"github.com/zeromicro/go-zero/core/stores/mon" "github.com/zeromicro/go-zero/core/stores/mon"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"time"
) )
type ProductStatisticsRepositoryParam struct { type ProductStatisticsRepositoryParam struct {
@ -190,8 +192,14 @@ func (repo *ProductStatisticsRepository) DecFansCount(ctx context.Context, produ
// 只允許在 fans_count 大於或等於欲扣減值時進行扣減 // 只允許在 fans_count 大於或等於欲扣減值時進行扣減
filter := bson.M{"product_id": productID, "fans_count": bson.M{"$gte": fansCount}} filter := bson.M{"product_id": productID, "fans_count": bson.M{"$gte": fansCount}}
now := time.Now().UnixNano() 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{ update := bson.M{
"$inc": bson.M{"fans_count": -int64(fansCount)}, "$inc": bson.M{"fans_count": delta},
"$set": bson.M{ "$set": bson.M{
"fans_count_update_time": now, "fans_count_update_time": now,
"updated_at": now, "updated_at": now,

View File

@ -1,19 +1,20 @@
package repository package repository
import ( 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/entity"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository"
mgo "code.30cm.net/digimon/library-go/mongo" mgo "code.30cm.net/digimon/library-go/mongo"
"context"
"fmt"
"github.com/alicebob/miniredis/v2" "github.com/alicebob/miniredis/v2"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/mon" "github.com/zeromicro/go-zero/core/stores/mon"
"github.com/zeromicro/go-zero/core/stores/redis" "github.com/zeromicro/go-zero/core/stores/redis"
"testing"
"time"
) )
func SetupTestProductStatisticsRepo(db string) (repository.ProductStatisticsRepo, func(), error) { func SetupTestProductStatisticsRepo(db string) (repository.ProductStatisticsRepo, func(), error) {

View File

@ -1,19 +1,20 @@
package repository package repository
import ( import (
"context"
"errors"
"time"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain" "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/entity"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository"
mgo "code.30cm.net/digimon/library-go/mongo" 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/cache"
"github.com/zeromicro/go-zero/core/stores/mon" "github.com/zeromicro/go-zero/core/stores/mon"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"time"
) )
type TagsRepositoryParam struct { type TagsRepositoryParam struct {

View File

@ -1,14 +1,15 @@
package repository package repository
import ( import (
"context"
"time"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository"
"context"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"time"
) )
func (repo *TagsRepository) BindTag(ctx context.Context, data *entity.TagsBindingTable) error { 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) { func (repo *TagsRepository) ListTagBinding(ctx context.Context, params repository.TagBindingQueryParams) ([]*entity.TagsBindingTable, int64, error) {
// 構建查詢過濾器 // 構建查詢過濾器
filter := bson.M{} filter := bson.M{}
if params.ReferenceID != nil {
filter["reference_id"] = *params.ReferenceID
}
if params.TagID != nil { if params.TagID != nil {
filter["tag_id"] = *params.TagID 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) 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) { func (repo *TagsRepository) IndexTagsBinding20250317001UP(ctx context.Context) (*mongo.Cursor, error) {
//TODO implement me repo.TageBinding.PopulateMultiIndex(ctx, []string{
panic("implement me") "tag_id",
"reference_id",
}, []int32{1, 1}, true)
return repo.Tags.GetClient().Indexes().List(ctx)
} }