diff --git a/internal/model/dto/shop_dto.go b/internal/model/dto/shop_dto.go index 5e21c78..1ff91b8 100644 --- a/internal/model/dto/shop_dto.go +++ b/internal/model/dto/shop_dto.go @@ -74,8 +74,9 @@ type UpdateShopParams struct { // ShopCascadeRequest 店铺联级查询请求 type ShopCascadeRequest struct { - ShopName string `json:"shop_name" query:"shop_name" validate:"omitempty,max=100" maxLength:"100" description:"店铺名称(模糊查询)"` - ParentID *uint `json:"parent_id" query:"parent_id" validate:"omitempty,min=1" minimum:"1" description:"上级店铺ID(不传则查询顶级店铺)"` + ShopName string `json:"shop_name" query:"shop_name" validate:"omitempty,max=100" maxLength:"100" description:"店铺名称(模糊查询)"` + ParentID *uint `json:"parent_id" query:"parent_id" validate:"omitempty,min=1" minimum:"1" description:"上级店铺ID(不传则查询顶级店铺)"` + ExcludeSelf bool `json:"exclude_self" query:"exclude_self" description:"是否排除当前账号自身店铺,默认false"` } // ShopCascadeItem 联级查询结果项 diff --git a/internal/service/shop/service.go b/internal/service/shop/service.go index 51acf4a..97d8a05 100644 --- a/internal/service/shop/service.go +++ b/internal/service/shop/service.go @@ -410,7 +410,7 @@ func (s *Service) List(ctx context.Context, opts *store.QueryOptions, filters ma // ListCascade 联级查询店铺,用于新增店铺或其他场景选择上级店铺 func (s *Service) ListCascade(ctx context.Context, req *dto.ShopCascadeRequest) ([]*dto.ShopCascadeItem, error) { - shops, err := s.shopStore.ListForCascade(ctx, req.ShopName, req.ParentID) + shops, err := s.shopStore.ListForCascade(ctx, req.ShopName, req.ParentID, req.ExcludeSelf) if err != nil { return nil, errors.Wrap(errors.CodeInternalError, err, "查询店铺失败") } diff --git a/internal/store/postgres/shop_store.go b/internal/store/postgres/shop_store.go index 0a0ded9..fbb2399 100644 --- a/internal/store/postgres/shop_store.go +++ b/internal/store/postgres/shop_store.go @@ -229,25 +229,32 @@ func (s *ShopStore) GetByIDs(ctx context.Context, ids []uint) ([]*model.Shop, er return shops, nil } -// ListForCascade 联级查询店铺(支持按名称模糊查询和按上级ID过滤) -func (s *ShopStore) ListForCascade(ctx context.Context, shopName string, parentID *uint) ([]*model.Shop, error) { +// ListForCascade 联级查询店铺(支持按名称模糊查询、按上级ID过滤和排除自身) +func (s *ShopStore) ListForCascade(ctx context.Context, shopName string, parentID *uint, excludeSelf bool) ([]*model.Shop, error) { query := s.db.WithContext(ctx).Model(&model.Shop{}) query = middleware.ApplyShopIDFilter(ctx, query) + currentShopID := middleware.GetShopIDFromContext(ctx) if parentID != nil { query = query.Where("parent_id = ?", *parentID) } else if middleware.GetUserTypeFromContext(ctx) == constants.UserTypeAgent { - shopID := middleware.GetShopIDFromContext(ctx) - if shopID == 0 { + if currentShopID == 0 { query = query.Where("1 = 0") + } else if excludeSelf { + // 排除自身时,代理账号的级联根节点从自身下级开始。 + query = query.Where("parent_id = ?", currentShopID) } else { // 代理账号的级联根节点是自己的店铺,避免默认根查询落到平台顶级店铺。 - query = query.Where("id = ?", shopID) + query = query.Where("id = ?", currentShopID) } } else { query = query.Where("parent_id IS NULL") } + if excludeSelf && currentShopID > 0 { + query = query.Where("id <> ?", currentShopID) + } + if shopName != "" { query = query.Where("shop_name LIKE ?", "%"+shopName+"%") }