fix
This commit is contained in:
@@ -72,7 +72,11 @@ func (p *FsProductModel) GetRandomProductList(ctx context.Context, limit int) (r
|
||||
Where("`is_del` =? and `is_shelf` = ?", 0, 1).Order("RAND()").Limit(limit).Find(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (p *FsProductModel) GetIgnoreRandomProductList(ctx context.Context, limit int, notInProductIds []int64) (resp []FsProduct, err error) {
|
||||
err = p.db.WithContext(ctx).Model(&FsProduct{}).
|
||||
Where("`is_del` =? and `is_shelf` = ? and `id` not in(?)", 0, 1, notInProductIds).Order("RAND()").Limit(limit).Find(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
func (p *FsProductModel) FindAllOnlyByIds(ctx context.Context, ids []int64) (resp []FsProduct, err error) {
|
||||
err = p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` IN (?)", ids).Find(&resp).Error
|
||||
return resp, err
|
||||
|
||||
21
model/gmodel/fs_product_recommend_gen.go
Normal file
21
model/gmodel/fs_product_recommend_gen.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package gmodel
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// fs_product_recommend 推荐商品表
|
||||
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不正常
|
||||
Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间
|
||||
}
|
||||
type FsProductRecommendModel struct {
|
||||
db *gorm.DB
|
||||
name string
|
||||
}
|
||||
|
||||
func NewFsProductRecommendModel(db *gorm.DB) *FsProductRecommendModel {
|
||||
return &FsProductRecommendModel{db: db, name: "fs_product_recommend"}
|
||||
}
|
||||
46
model/gmodel/fs_product_recommend_logic.go
Normal file
46
model/gmodel/fs_product_recommend_logic.go
Normal file
@@ -0,0 +1,46 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user