From 20e32ae078b8fdeef4a2ef977e4dde104615d42a Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 20 Jul 2023 15:43:55 +0800 Subject: [PATCH] fix --- constants/recommend_product.go | 6 +++++ model/gmodel/fs_product_recommend_gen.go | 1 + model/gmodel/fs_product_recommend_logic.go | 24 +++++++++++-------- .../logic/getrecommandproductlistlogic.go | 3 ++- 4 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 constants/recommend_product.go diff --git a/constants/recommend_product.go b/constants/recommend_product.go new file mode 100644 index 00000000..aa1be1c9 --- /dev/null +++ b/constants/recommend_product.go @@ -0,0 +1,6 @@ +package constants + +type recommend_product int64 + +// 产品详情页推荐产品 +const PRODUCT_DETAIL_RECOMMEND_CATEGORY recommend_product = 1 diff --git a/model/gmodel/fs_product_recommend_gen.go b/model/gmodel/fs_product_recommend_gen.go index b70ba056..b3dba788 100644 --- a/model/gmodel/fs_product_recommend_gen.go +++ b/model/gmodel/fs_product_recommend_gen.go @@ -9,6 +9,7 @@ type FsProductRecommend struct { Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ProductId *int64 `gorm:"unique_key;default:0;" json:"product_id"` // 产品ID Status *int64 `gorm:"default:1;" json:"status"` // 状态 1正常 0不正常 + Category *int64 `gorm:"default:1;" json:"category"` // 推荐类别1:详情推荐产品 Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间 } type FsProductRecommendModel struct { diff --git a/model/gmodel/fs_product_recommend_logic.go b/model/gmodel/fs_product_recommend_logic.go index 3de52d5c..c3a10a54 100644 --- a/model/gmodel/fs_product_recommend_logic.go +++ b/model/gmodel/fs_product_recommend_logic.go @@ -7,11 +7,12 @@ import ( ) type GetRecommendProductListReq struct { - Ctx context.Context - Page int - Limit int - OrderBy string - Status *int64 + Ctx context.Context + Page int + Limit int + OrderBy string + Category int64 + Status *int64 } func (r *FsProductRecommendModel) GetRecommendProductList(req GetRecommendProductListReq) (resp []FsProductRecommend, total int64, err error) { @@ -19,6 +20,9 @@ func (r *FsProductRecommendModel) GetRecommendProductList(req GetRecommendProduc if req.Status != nil { db = db.Where("`status` = ?", *req.Status) } + if req.Category != 0 { + db = db.Where("`category` = ?", req.Category) + } if req.OrderBy != "" { db = db.Order(req.OrderBy) } @@ -29,18 +33,18 @@ func (r *FsProductRecommendModel) GetRecommendProductList(req GetRecommendProduc err = db.Offset(offset).Limit(req.Limit).Find(&resp).Error return resp, total, err } -func (r *FsProductRecommendModel) GetIgnoreRandomRecommendProductList(ctx context.Context, limit int, idNotInt []int64) (resp []FsProductRecommend, err error) { - err = r.db.WithContext(ctx).Model(&FsProductRecommend{}).Where("`product_id` not in(?)", idNotInt).Order("RAND()").Limit(limit).Find(&resp).Error +func (r *FsProductRecommendModel) GetIgnoreRandomRecommendProductList(ctx context.Context, limit int, category int64, idNotInt []int64) (resp []FsProductRecommend, err error) { + err = r.db.WithContext(ctx).Model(&FsProductRecommend{}).Where("`product_id` not in(?) and `category` = ?", idNotInt, category).Order("RAND()").Limit(limit).Find(&resp).Error return resp, err } -func (r *FsProductRecommendModel) CreateOrUpdate(ctx context.Context, productId int64, data *FsProductRecommend) error { +func (r *FsProductRecommendModel) CreateOrUpdate(ctx context.Context, productId int64, category int64, data *FsProductRecommend) error { var info FsProductRecommend - err := r.db.WithContext(ctx).Model(&FsProductRecommend{}).Where("`product_id` = ?", productId).Take(&info).Error + err := r.db.WithContext(ctx).Model(&FsProductRecommend{}).Where("`product_id` = ? and `category` = ?", productId, category).Take(&info).Error if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { return err } if info.Id == 0 { return r.db.WithContext(ctx).Model(&FsProductRecommend{}).Create(data).Error } - return r.db.WithContext(ctx).Model(&FsProductRecommend{}).Where("`product_id` = ?", productId).Updates(data).Error + return r.db.WithContext(ctx).Model(&FsProductRecommend{}).Where("`product_id` = ? and `category` = ?", productId, category).Updates(data).Error } diff --git a/server/product/internal/logic/getrecommandproductlistlogic.go b/server/product/internal/logic/getrecommandproductlistlogic.go index 465e9dee..2c01b758 100644 --- a/server/product/internal/logic/getrecommandproductlistlogic.go +++ b/server/product/internal/logic/getrecommandproductlistlogic.go @@ -2,6 +2,7 @@ package logic import ( "errors" + "fusenapi/constants" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/format" @@ -46,7 +47,7 @@ func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRec return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get detail product info") } //随机取产品列表(不包含详情产品) - recommendList, err := l.svcCtx.AllModels.FsProductRecommend.GetIgnoreRandomRecommendProductList(l.ctx, int(req.Num), []int64{productInfo.Id}) + recommendList, err := l.svcCtx.AllModels.FsProductRecommend.GetIgnoreRandomRecommendProductList(l.ctx, int(req.Num), int64(constants.PRODUCT_DETAIL_RECOMMEND_CATEGORY), []int64{productInfo.Id}) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get random recommend list")