This commit is contained in:
laodaming
2023-07-20 17:35:13 +08:00
parent fd1a56c11a
commit 0e72027ff3
8 changed files with 319 additions and 19 deletions

View File

@@ -9,7 +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:详情推荐产品
Category *int64 `gorm:"default:1;" json:"category"` // 推荐类别1:详情推荐 2:列表页推荐
Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间
}
type FsProductRecommendModel struct {

View File

@@ -10,21 +10,17 @@ type GetRecommendProductListReq struct {
Ctx context.Context
Page int
Limit int
OrderBy string
Category int64
Status *int64
Category int64 // 0是全部
}
func (r *FsProductRecommendModel) GetRecommendProductList(req GetRecommendProductListReq) (resp []FsProductRecommend, total int64, err error) {
db := r.db.WithContext(req.Ctx).Model(&FsProductRecommend{})
if req.Status != nil {
db = db.Where("`status` = ?", *req.Status)
}
func (r *FsProductRecommendModel) GetRecommendProductList(req GetRecommendProductListReq) (resp []FsProduct, total int64, err error) {
db := r.db.WithContext(req.Ctx).Select("p.*").
Table("fs_product_recommend as r").
Joins("inner join fs_product as p on r.product_id = p.id").
Where("r.status = ? ", 1).
Where("and p.is_shelf = ? and p.is_del = ? and p.status = ?", 1, 0, 1)
if req.Category != 0 {
db = db.Where("`category` = ?", req.Category)
}
if req.OrderBy != "" {
db = db.Order(req.OrderBy)
db = db.Where("r.category = ?", req.Category)
}
if err = db.Limit(1).Count(&total).Error; err != nil {
return nil, 0, err
@@ -34,7 +30,9 @@ func (r *FsProductRecommendModel) GetRecommendProductList(req GetRecommendProduc
return resp, total, err
}
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
err = r.db.WithContext(ctx).Model(&FsProductRecommend{}).
Where("`product_id` not in(?) and `category` = ? and `status` = ?", idNotInt, category, 1).
Order("RAND()").Limit(limit).Find(&resp).Error
return resp, err
}
func (r *FsProductRecommendModel) CreateOrUpdate(ctx context.Context, productId int64, category int64, data *FsProductRecommend) error {