47 lines
1.6 KiB
Go
47 lines
1.6 KiB
Go
|
package gmodel
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
type GetRecommendProductListReq struct {
|
||
|
Ctx context.Context
|
||
|
Page int
|
||
|
Limit int
|
||
|
OrderBy string
|
||
|
Status *int64
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
}
|
||
|
if req.OrderBy != "" {
|
||
|
db = db.Order(req.OrderBy)
|
||
|
}
|
||
|
if err = db.Limit(1).Count(&total).Error; err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
offset := (req.Page - 1) * req.Limit
|
||
|
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
|
||
|
return resp, err
|
||
|
}
|
||
|
func (r *FsProductRecommendModel) CreateOrUpdate(ctx context.Context, productId int64, data *FsProductRecommend) error {
|
||
|
var info FsProductRecommend
|
||
|
err := r.db.WithContext(ctx).Model(&FsProductRecommend{}).Where("`product_id` = ?", productId).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
|
||
|
}
|