fusenapi/model/gmodel/fs_product_recommend_logic.go
laodaming 0e752e31fd fix
2023-07-20 18:04:50 +08:00

53 lines
2.0 KiB
Go

package gmodel
import (
"context"
"errors"
"gorm.io/gorm"
)
type GetRecommendProductListReq struct {
Ctx context.Context
Page int
Limit int
Category int64 // 0是全部
}
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("p.is_shelf = ? and p.is_del = ? and p.status = ?", 1, 0, 1)
if req.Category != 0 {
db = db.Where("r.category = ?", req.Category)
}
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, category int64, idNotInt []int64) (resp []FsProduct, err error) {
err = r.db.WithContext(ctx).Debug().Select("p.*").
Table("fs_product_recommend as r").
Joins("inner join fs_product as p on r.product_id = p.id").
Where("r.product_id not in (?)", idNotInt).
Where("r.status = ? ", 1).
Where("p.is_shelf = ? and p.is_del = ? and p.status = ?", 1, 0, 1).
Where("r.category = ?", category).Order("RAND()").Limit(limit).Find(&resp).Error
return resp, err
}
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` = ? 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` = ? and `category` = ?", productId, category).Updates(data).Error
}