This commit is contained in:
laodaming 2023-07-20 15:43:55 +08:00
parent 4a1655d6fa
commit 20e32ae078
4 changed files with 23 additions and 11 deletions

View File

@ -0,0 +1,6 @@
package constants
type recommend_product int64
// 产品详情页推荐产品
const PRODUCT_DETAIL_RECOMMEND_CATEGORY recommend_product = 1

View File

@ -9,6 +9,7 @@ type FsProductRecommend struct {
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` //
ProductId *int64 `gorm:"unique_key;default:0;" json:"product_id"` // 产品ID ProductId *int64 `gorm:"unique_key;default:0;" json:"product_id"` // 产品ID
Status *int64 `gorm:"default:1;" json:"status"` // 状态 1正常 0不正常 Status *int64 `gorm:"default:1;" json:"status"` // 状态 1正常 0不正常
Category *int64 `gorm:"default:1;" json:"category"` // 推荐类别1:详情推荐产品
Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间 Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间
} }
type FsProductRecommendModel struct { type FsProductRecommendModel struct {

View File

@ -7,11 +7,12 @@ import (
) )
type GetRecommendProductListReq struct { type GetRecommendProductListReq struct {
Ctx context.Context Ctx context.Context
Page int Page int
Limit int Limit int
OrderBy string OrderBy string
Status *int64 Category int64
Status *int64
} }
func (r *FsProductRecommendModel) GetRecommendProductList(req GetRecommendProductListReq) (resp []FsProductRecommend, total int64, err error) { 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 { if req.Status != nil {
db = db.Where("`status` = ?", *req.Status) db = db.Where("`status` = ?", *req.Status)
} }
if req.Category != 0 {
db = db.Where("`category` = ?", req.Category)
}
if req.OrderBy != "" { if req.OrderBy != "" {
db = db.Order(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 err = db.Offset(offset).Limit(req.Limit).Find(&resp).Error
return resp, total, err return resp, total, err
} }
func (r *FsProductRecommendModel) GetIgnoreRandomRecommendProductList(ctx context.Context, limit int, idNotInt []int64) (resp []FsProductRecommend, err 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(?)", idNotInt).Order("RAND()").Limit(limit).Find(&resp).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 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 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) { if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err return err
} }
if info.Id == 0 { if info.Id == 0 {
return r.db.WithContext(ctx).Model(&FsProductRecommend{}).Create(data).Error 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
} }

View File

@ -2,6 +2,7 @@ package logic
import ( import (
"errors" "errors"
"fusenapi/constants"
"fusenapi/utils/auth" "fusenapi/utils/auth"
"fusenapi/utils/basic" "fusenapi/utils/basic"
"fusenapi/utils/format" "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") 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 { if err != nil {
logx.Error(err) logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get random recommend list") return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get random recommend list")