From f9de30a838a3d9be09e4c1b84c8f47f9169d8634 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 28 Aug 2023 14:56:01 +0800 Subject: [PATCH] 1 --- model/gmodel/fs_merchant_category_gen.go | 1 + model/gmodel/fs_merchant_category_logic.go | 6 +++-- model/gmodel/var_gen.go | 8 ++++++ .../homepagerecommendproductlistlogic.go | 25 ++++++++++++------- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/model/gmodel/fs_merchant_category_gen.go b/model/gmodel/fs_merchant_category_gen.go index 732ec88b..b0c42cae 100644 --- a/model/gmodel/fs_merchant_category_gen.go +++ b/model/gmodel/fs_merchant_category_gen.go @@ -12,6 +12,7 @@ type FsMerchantCategory struct { Icon *string `gorm:"default:'';" json:"icon"` // 图标 RecommendProduct *string `gorm:"default:'';" json:"recommend_product"` // 推荐商品 Sort *int64 `gorm:"default:0;" json:"sort"` // 排序 + IsDefault *int64 `gorm:"default:0;" json:"is_default"` // 是否是默认商家类型0非1是 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 cbf8c138..e7061476 100644 --- a/model/gmodel/fs_merchant_category_logic.go +++ b/model/gmodel/fs_merchant_category_logic.go @@ -14,8 +14,10 @@ func (m *FsMerchantCategoryModel) FindOne(ctx context.Context, id int64) (resp * 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 + +// 获取默认那个 +func (m *FsMerchantCategoryModel) FindDefualtOne(ctx context.Context) (resp *FsMerchantCategory, err error) { + err = m.db.WithContext(ctx).Model(&FsMerchantCategory{}).Where("status = ? and is_default = ?", 1, 1).Take(&resp).Error return resp, err } diff --git a/model/gmodel/var_gen.go b/model/gmodel/var_gen.go index c7e2a11b..ad4f5db9 100644 --- a/model/gmodel/var_gen.go +++ b/model/gmodel/var_gen.go @@ -4,7 +4,11 @@ import "gorm.io/gorm" // AllModelsGen 所有Model集合,修改单行,只要不改字段名,不会根据新的内容修改,需要修改的话手动删除 type AllModelsGen struct { + CasbinRule *CasbinRuleModel // casbin_rule FsAddress *FsAddressModel // fs_address 用户地址表 + FsAdminAuthRole *FsAdminAuthRoleModel // fs_admin_auth_role 后台--角色表 + FsAdminDepartment *FsAdminDepartmentModel // fs_admin_department 后台--部门表 + FsAdminMenu *FsAdminMenuModel // fs_admin_menu 后台--菜单表 FsAuthAssignment *FsAuthAssignmentModel // fs_auth_assignment 用户角色和权限信息 FsAuthItem *FsAuthItemModel // fs_auth_item 用户角色和权限信息 FsAuthItemChild *FsAuthItemChildModel // fs_auth_item_child 角色和权限关系表 @@ -103,7 +107,11 @@ type AllModelsGen struct { func NewAllModels(gdb *gorm.DB) *AllModelsGen { models := &AllModelsGen{ + CasbinRule: NewCasbinRuleModel(gdb), FsAddress: NewFsAddressModel(gdb), + FsAdminAuthRole: NewFsAdminAuthRoleModel(gdb), + FsAdminDepartment: NewFsAdminDepartmentModel(gdb), + FsAdminMenu: NewFsAdminMenuModel(gdb), FsAuthAssignment: NewFsAuthAssignmentModel(gdb), FsAuthItem: NewFsAuthItemModel(gdb), FsAuthItemChild: NewFsAuthItemChildModel(gdb), diff --git a/server/product/internal/logic/homepagerecommendproductlistlogic.go b/server/product/internal/logic/homepagerecommendproductlistlogic.go index e9d4694f..389a01e0 100644 --- a/server/product/internal/logic/homepagerecommendproductlistlogic.go +++ b/server/product/internal/logic/homepagerecommendproductlistlogic.go @@ -54,16 +54,23 @@ func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *ty //选了商家类型 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") + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the merchant category is not exists") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get merchant category") + } + } else { + //获取默认的那个 + merchantInfo, err = l.svcCtx.AllModels.FsMerchantCategory.FindDefualtOne(l.ctx) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the default merchant category is not exists") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get default merchant category") } - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get merchant type info") } if *merchantInfo.RecommendProduct == "" { return resp.SetStatusWithMessage(basic.CodeOK, "success", []interface{}{})