This commit is contained in:
laodaming 2023-08-14 11:35:23 +08:00
parent cfc7b22090
commit 2bb1c73e4a
8 changed files with 43 additions and 75 deletions

View File

@ -11,7 +11,7 @@ type FsMerchantCategory struct {
EnName *string `gorm:"default:'';" json:"en_name"` // 英文名
Icon *string `gorm:"default:'';" json:"icon"` // 图标
RecommendProduct *string `gorm:"default:'';" json:"recommend_product"` // 推荐商品
Sort *int64 `gorm:"default:0;" json:"sort"` // 排序
Sort *int64 `gorm:"default:128;" json:"sort"` // 排序
Status *int64 `gorm:"default:0;" json:"status"` // 状态
Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间
}

View File

@ -1,2 +1,13 @@
package gmodel
import "context"
// TODO: 使用model的属性做你想做的
func (m *FsMerchantCategoryModel) FindOne(ctx context.Context, id int64) (resp *FsMerchantCategory, err error) {
err = m.db.WithContext(ctx).Model(&FsMerchantCategory{}).Where("id = ? and status = ?", id, 1).Take(&resp).Error
return resp, err
}
func (m *FsMerchantCategoryModel) FindRandOne(ctx context.Context) (resp *FsMerchantCategory, err error) {
err = m.db.WithContext(ctx).Model(&FsMerchantCategory{}).Where("status = ?", 1).Order("RAND()").Take(&resp).Error
return resp, err
}

View File

@ -1,22 +0,0 @@
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:"default:0;" json:"product_id"` // 产品ID
MerchantType *int64 `gorm:"default:0;" json:"merchant_type"` // 商家类型
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"}
}

View File

@ -1,43 +0,0 @@
package gmodel
import (
"context"
"errors"
"gorm.io/gorm"
)
type GetRecommendProductListReq struct {
Ctx context.Context
MerchantType int64
Page int
Limit int
}
func (r *FsProductRecommendModel) GetRecommendProductList(req GetRecommendProductListReq) (resp []FsProduct, total int64, err error) {
db := r.db.WithContext(req.Ctx).
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.MerchantType > 0 {
db = db.Where("merchant_type = ?", req.MerchantType)
}
if err = db.Limit(1).Count(&total).Error; err != nil {
return nil, 0, err
}
db = db.Select("p.*")
offset := (req.Page - 1) * req.Limit
err = db.Offset(offset).Limit(req.Limit).Find(&resp).Error
return resp, total, 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
}

View File

@ -62,7 +62,6 @@ type AllModelsGen struct {
FsProductModel3dLight *FsProductModel3dLightModel // fs_product_model3d_light 模型-灯光组表
FsProductOption *FsProductOptionModel // fs_product_option 产品选项表(已废弃)
FsProductPrice *FsProductPriceModel // fs_product_price 阶梯价格表
FsProductRecommend *FsProductRecommendModel // fs_product_recommend 推荐商品表
FsProductRenderDesign *FsProductRenderDesignModel // fs_product_render_design
FsProductScene *FsProductSceneModel // fs_product_scene 产品场景表
FsProductSize *FsProductSizeModel // fs_product_size 产品尺寸表
@ -158,7 +157,6 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen {
FsProductModel3dLight: NewFsProductModel3dLightModel(gdb),
FsProductOption: NewFsProductOptionModel(gdb),
FsProductPrice: NewFsProductPriceModel(gdb),
FsProductRecommend: NewFsProductRecommendModel(gdb),
FsProductRenderDesign: NewFsProductRenderDesignModel(gdb),
FsProductScene: NewFsProductSceneModel(gdb),
FsProductSize: NewFsProductSizeModel(gdb),

View File

@ -42,6 +42,7 @@ func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *ty
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err")
}
var (
merchantInfo *gmodel.FsMerchantCategory
recommendProductList []gmodel.FsProduct //产品列表select 字段需要看查询的地方)
productOptionalPartList []gmodel.GetGroupPartListByProductIdsRsp //产品配件列表
mapProductHaveOptionFitting = make(map[int64]struct{})
@ -52,13 +53,34 @@ func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *ty
mapProductSizeCount = make(map[int64]int64) //产品尺寸数量map
mapProductTemplate = make(map[int64]struct{}) //产品模板map
)
//选了商家类型
if req.MerchantType > 0 {
merchantInfo, err = l.svcCtx.AllModels.FsMerchantCategory.FindOne(l.ctx, req.MerchantType)
} else {
//随机获取一个商家类型
merchantInfo, err = l.svcCtx.AllModels.FsMerchantCategory.FindRandOne(l.ctx)
}
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "none of merchant type found")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get merchant type info")
}
if *merchantInfo.RecommendProduct == "" {
return resp.SetStatusWithMessage(basic.CodeOK, "success", []interface{}{})
}
recommendProductIds, err := format.StrSlicToInt64Slice(strings.Split(*merchantInfo.RecommendProduct, ","))
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse recommend product")
}
//获取列表推荐产品
recommendProductList, _, err = l.svcCtx.AllModels.FsProductRecommend.GetRecommendProductList(gmodel.GetRecommendProductListReq{
Ctx: l.ctx,
MerchantType: req.MerchantType,
Page: 1,
Limit: 500, //设置最大500
})
recommendProductList, err = l.svcCtx.AllModels.FsProduct.GetProductListByIds(l.ctx, recommendProductIds, "sort-desc")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product list")
}
if len(recommendProductList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success", []interface{}{})
}

View File

@ -341,6 +341,7 @@ type GetSizeByPidRsp struct {
type GetTemplateByPidReq struct {
Pid string `form:"pid"`
ProductSizeId int64 `form:"product_size_id,optional"`
ProductTemplateTagId int64 `form:"product_template_tag_id"`
}

View File

@ -386,6 +386,7 @@ type GetSizeByPidRsp {
//获取产品模板
type GetTemplateByPidReq {
Pid string `form:"pid"`
ProductSizeId int64 `form:"product_size_id,optional"`
ProductTemplateTagId int64 `form:"product_template_tag_id"`
}
//获取产品配件数据