fusenapi/model/gmodel/fs_product_recommend_logic.go

51 lines
1.8 KiB
Go
Raw Normal View History

2023-07-20 06:56:28 +00:00
package gmodel
import (
"context"
"errors"
"gorm.io/gorm"
)
type GetRecommendProductListReq struct {
2023-07-20 07:43:55 +00:00
Ctx context.Context
Page int
Limit int
OrderBy string
Category int64
Status *int64
2023-07-20 06:56:28 +00:00
}
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)
}
2023-07-20 07:43:55 +00:00
if req.Category != 0 {
db = db.Where("`category` = ?", req.Category)
}
2023-07-20 06:56:28 +00:00
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
}
2023-07-20 07:43:55 +00:00
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
2023-07-20 06:56:28 +00:00
return resp, err
}
2023-07-20 07:43:55 +00:00
func (r *FsProductRecommendModel) CreateOrUpdate(ctx context.Context, productId int64, category int64, data *FsProductRecommend) error {
2023-07-20 06:56:28 +00:00
var info FsProductRecommend
2023-07-20 07:43:55 +00:00
err := r.db.WithContext(ctx).Model(&FsProductRecommend{}).Where("`product_id` = ? and `category` = ?", productId, category).Take(&info).Error
2023-07-20 06:56:28 +00:00
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
if info.Id == 0 {
return r.db.WithContext(ctx).Model(&FsProductRecommend{}).Create(data).Error
}
2023-07-20 07:43:55 +00:00
return r.db.WithContext(ctx).Model(&FsProductRecommend{}).Where("`product_id` = ? and `category` = ?", productId, category).Updates(data).Error
2023-07-20 06:56:28 +00:00
}