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

@@ -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 {

View File

@@ -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
}