From 2bb1c73e4a29c1a75a5070e572861a456101aa0f Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 14 Aug 2023 11:35:23 +0800 Subject: [PATCH] fix --- model/gmodel/fs_merchant_category_gen.go | 2 +- model/gmodel/fs_merchant_category_logic.go | 13 +++++- model/gmodel/fs_product_recommend_gen.go | 22 ---------- model/gmodel/fs_product_recommend_logic.go | 43 ------------------- model/gmodel/var_gen.go | 2 - .../homepagerecommendproductlistlogic.go | 34 ++++++++++++--- server/product/internal/types/types.go | 1 + server_api/product.api | 1 + 8 files changed, 43 insertions(+), 75 deletions(-) delete mode 100644 model/gmodel/fs_product_recommend_gen.go delete mode 100644 model/gmodel/fs_product_recommend_logic.go diff --git a/model/gmodel/fs_merchant_category_gen.go b/model/gmodel/fs_merchant_category_gen.go index 732ec88b..37944dce 100644 --- a/model/gmodel/fs_merchant_category_gen.go +++ b/model/gmodel/fs_merchant_category_gen.go @@ -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"` // 添加时间 } diff --git a/model/gmodel/fs_merchant_category_logic.go b/model/gmodel/fs_merchant_category_logic.go index e68225aa..66b709d5 100644 --- a/model/gmodel/fs_merchant_category_logic.go +++ b/model/gmodel/fs_merchant_category_logic.go @@ -1,2 +1,13 @@ package gmodel -// TODO: 使用model的属性做你想做的 \ No newline at end of file + +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 +} diff --git a/model/gmodel/fs_product_recommend_gen.go b/model/gmodel/fs_product_recommend_gen.go deleted file mode 100644 index 111494f6..00000000 --- a/model/gmodel/fs_product_recommend_gen.go +++ /dev/null @@ -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"} -} diff --git a/model/gmodel/fs_product_recommend_logic.go b/model/gmodel/fs_product_recommend_logic.go deleted file mode 100644 index 0646203d..00000000 --- a/model/gmodel/fs_product_recommend_logic.go +++ /dev/null @@ -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 -} diff --git a/model/gmodel/var_gen.go b/model/gmodel/var_gen.go index 905e8301..b62e2377 100644 --- a/model/gmodel/var_gen.go +++ b/model/gmodel/var_gen.go @@ -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), diff --git a/server/product/internal/logic/homepagerecommendproductlistlogic.go b/server/product/internal/logic/homepagerecommendproductlistlogic.go index e542aec4..61708dfa 100644 --- a/server/product/internal/logic/homepagerecommendproductlistlogic.go +++ b/server/product/internal/logic/homepagerecommendproductlistlogic.go @@ -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{}{}) } diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 5cb5c2d4..435229a7 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -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"` } diff --git a/server_api/product.api b/server_api/product.api index 1f37a789..0fd841bf 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -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"` } //获取产品配件数据