新增排查自己的逻辑
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m53s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m53s
This commit is contained in:
@@ -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 联级查询结果项
|
||||
|
||||
@@ -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, "查询店铺失败")
|
||||
}
|
||||
|
||||
@@ -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+"%")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user