2023-07-20 06:56:28 +00:00
|
|
|
package gmodel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetRecommendProductListReq struct {
|
2023-08-08 06:31:40 +00:00
|
|
|
Ctx context.Context
|
|
|
|
Page int
|
|
|
|
Limit int
|
2023-07-20 06:56:28 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 09:35:13 +00:00
|
|
|
func (r *FsProductRecommendModel) GetRecommendProductList(req GetRecommendProductListReq) (resp []FsProduct, total int64, err error) {
|
2023-07-20 10:18:36 +00:00
|
|
|
db := r.db.WithContext(req.Ctx).
|
2023-07-20 09:35:13 +00:00
|
|
|
Table("fs_product_recommend as r").
|
|
|
|
Joins("inner join fs_product as p on r.product_id = p.id").
|
|
|
|
Where("r.status = ? ", 1).
|
2023-07-20 10:04:50 +00:00
|
|
|
Where("p.is_shelf = ? and p.is_del = ? and p.status = ?", 1, 0, 1)
|
2023-07-20 06:56:28 +00:00
|
|
|
if err = db.Limit(1).Count(&total).Error; err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2023-07-20 10:18:36 +00:00
|
|
|
db = db.Select("p.*")
|
2023-07-20 06:56:28 +00:00
|
|
|
offset := (req.Page - 1) * req.Limit
|
|
|
|
err = db.Offset(offset).Limit(req.Limit).Find(&resp).Error
|
|
|
|
return resp, total, err
|
|
|
|
}
|
2023-08-08 06:31:40 +00:00
|
|
|
func (r *FsProductRecommendModel) CreateOrUpdate(ctx context.Context, productId int64, data *FsProductRecommend) error {
|
2023-07-20 06:56:28 +00:00
|
|
|
var info FsProductRecommend
|
2023-08-08 06:31:40 +00:00
|
|
|
err := r.db.WithContext(ctx).Model(&FsProductRecommend{}).Where("`product_id` = ?", productId).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-08-08 06:31:40 +00:00
|
|
|
return r.db.WithContext(ctx).Model(&FsProductRecommend{}).Where("`product_id` = ?", productId).Updates(data).Error
|
2023-07-20 06:56:28 +00:00
|
|
|
}
|