This commit is contained in:
laodaming 2023-08-08 14:31:40 +08:00
parent 9e5b08b9eb
commit 3c0d1a8d7a
8 changed files with 52 additions and 43 deletions

View File

@ -1,9 +0,0 @@
package constants
type recommend_product int64
// 产品详情页推荐产品
const PRODUCT_DETAIL_RECOMMEND_CATEGORY recommend_product = 1
// 主页推荐产品
const HOME_PAGE_RECOMMEND_CATEGORY recommend_product = 2

View File

@ -9,7 +9,6 @@ type FsProductRecommend struct {
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` //
ProductId *int64 `gorm:"default:0;" json:"product_id"` // 产品ID
Status *int64 `gorm:"default:1;" json:"status"` // 状态 1正常 0不正常
Category *int64 `gorm:"default:1;" json:"category"` // 推荐类别1:详情推荐 2:列表页推荐
Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间
}
type FsProductRecommendModel struct {

View File

@ -7,10 +7,9 @@ import (
)
type GetRecommendProductListReq struct {
Ctx context.Context
Page int
Limit int
Category int64 // 0是全部
Ctx context.Context
Page int
Limit int
}
func (r *FsProductRecommendModel) GetRecommendProductList(req GetRecommendProductListReq) (resp []FsProduct, total int64, err error) {
@ -19,9 +18,6 @@ func (r *FsProductRecommendModel) GetRecommendProductList(req GetRecommendProduc
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
}
@ -30,24 +26,14 @@ func (r *FsProductRecommendModel) GetRecommendProductList(req GetRecommendProduc
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 {
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` = ? and `category` = ?", productId, category).Take(&info).Error
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` = ? and `category` = ?", productId, category).Updates(data).Error
return r.db.WithContext(ctx).Model(&FsProductRecommend{}).Where("`product_id` = ?", productId).Updates(data).Error
}

View File

@ -0,0 +1,23 @@
package gmodel
import (
"gorm.io/gorm"
)
// fs_product_tag_prop 产品标签相关属性表
type FsProductTagProp struct {
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // id
ProductId *int64 `gorm:"default:0;" json:"product_id"` // 产品id
TagId *int64 `gorm:"default:0;" json:"tag_id"` // 模板标签id
Cover *string `gorm:"default:'';" json:"cover"` //
Status *int64 `gorm:"default:1;" json:"status"` // 状态
Ctime *int64 `gorm:"default:0;" json:"ctime"` // 创建时间
}
type FsProductTagPropModel struct {
db *gorm.DB
name string
}
func NewFsProductTagPropModel(db *gorm.DB) *FsProductTagPropModel {
return &FsProductTagPropModel{db: db, name: "fs_product_tag_prop"}
}

View File

@ -0,0 +1,2 @@
package gmodel
// TODO: 使用model的属性做你想做的

View File

@ -65,6 +65,7 @@ type AllModelsGen struct {
FsProductRenderDesign *FsProductRenderDesignModel // fs_product_render_design
FsProductScene *FsProductSceneModel // fs_product_scene 产品场景表
FsProductSize *FsProductSizeModel // fs_product_size 产品尺寸表
FsProductTagProp *FsProductTagPropModel // fs_product_tag_prop 产品标签相关属性表
FsProductTemplate *FsProductTemplateModel // fs_product_template 产品模板表(已废弃)
FsProductTemplateBasemap *FsProductTemplateBasemapModel // fs_product_template_basemap 模板底图表
FsProductTemplateElement *FsProductTemplateElementModel // fs_product_template_element 云渲染配置表
@ -159,6 +160,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen {
FsProductRenderDesign: NewFsProductRenderDesignModel(gdb),
FsProductScene: NewFsProductSceneModel(gdb),
FsProductSize: NewFsProductSizeModel(gdb),
FsProductTagProp: NewFsProductTagPropModel(gdb),
FsProductTemplate: NewFsProductTemplateModel(gdb),
FsProductTemplateBasemap: NewFsProductTemplateBasemapModel(gdb),
FsProductTemplateElement: NewFsProductTemplateElementModel(gdb),

View File

@ -2,7 +2,7 @@ package logic
import (
"errors"
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/format"
@ -46,15 +46,23 @@ func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRec
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get detail product info")
}
//随机取产品列表(不包含详情产品)
recommendProductList, err := l.svcCtx.AllModels.FsProductRecommend.GetIgnoreRandomRecommendProductList(l.ctx, int(req.Num), int64(constants.PRODUCT_DETAIL_RECOMMEND_CATEGORY), []int64{productInfo.Id})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get random recommend list")
var (
recommendProductList []gmodel.FsProduct
)
if productInfo.RecommendProduct != nil && *productInfo.RecommendProduct != "" {
recommendProductIds, err := format.StrSlicToInt64Slice(strings.Split(*productInfo.RecommendProduct, ","))
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to split recommend product ids")
}
recommendProductList, err = l.svcCtx.AllModels.FsProduct.GetProductListByIds(l.ctx, recommendProductIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get recommend product list")
}
}
//需要填充时需要忽略的id
ignoreProductIds := make([]int64, 0, len(recommendProductList)+1)
ignoreProductIds = append(ignoreProductIds, productInfo.Id)
ignoreProductIds := make([]int64, 0, len(recommendProductList))
productIds := make([]int64, 0, len(recommendProductList))
//在合并之前记住推荐的产品
mapRecommend := make(map[int64]struct{})

View File

@ -3,7 +3,6 @@ package logic
import (
"errors"
"fmt"
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
@ -55,10 +54,9 @@ func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *ty
)
//获取列表推荐产品
recommendProductList, _, err = l.svcCtx.AllModels.FsProductRecommend.GetRecommendProductList(gmodel.GetRecommendProductListReq{
Ctx: l.ctx,
Page: 1,
Limit: 500, //设置最大500
Category: int64(constants.HOME_PAGE_RECOMMEND_CATEGORY),
Ctx: l.ctx,
Page: 1,
Limit: 500, //设置最大500
})
if len(recommendProductList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success", []interface{}{})