This commit is contained in:
@@ -43,6 +43,7 @@ type ShopResponse struct {
|
|||||||
ShopName string `json:"shop_name" description:"店铺名称"`
|
ShopName string `json:"shop_name" description:"店铺名称"`
|
||||||
ShopCode string `json:"shop_code" description:"店铺编号"`
|
ShopCode string `json:"shop_code" description:"店铺编号"`
|
||||||
ParentID *uint `json:"parent_id,omitempty" description:"上级店铺ID"`
|
ParentID *uint `json:"parent_id,omitempty" description:"上级店铺ID"`
|
||||||
|
ParentShopName string `json:"parent_shop_name,omitempty" description:"上级店铺名称"`
|
||||||
Level int `json:"level" description:"店铺层级 (1-7级)"`
|
Level int `json:"level" description:"店铺层级 (1-7级)"`
|
||||||
ContactName string `json:"contact_name" description:"联系人姓名"`
|
ContactName string `json:"contact_name" description:"联系人姓名"`
|
||||||
ContactPhone string `json:"contact_phone" description:"联系人电话"`
|
ContactPhone string `json:"contact_phone" description:"联系人电话"`
|
||||||
|
|||||||
@@ -53,11 +53,13 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateShopRequest) (*dto.
|
|||||||
}
|
}
|
||||||
|
|
||||||
level := 1
|
level := 1
|
||||||
|
parentShopName := ""
|
||||||
if req.ParentID != nil {
|
if req.ParentID != nil {
|
||||||
parent, err := s.shopStore.GetByID(ctx, *req.ParentID)
|
parent, err := s.shopStore.GetByID(ctx, *req.ParentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(errors.CodeInvalidParentID, "上级店铺不存在或无效")
|
return nil, errors.New(errors.CodeInvalidParentID, "上级店铺不存在或无效")
|
||||||
}
|
}
|
||||||
|
parentShopName = parent.ShopName
|
||||||
level = parent.Level + 1
|
level = parent.Level + 1
|
||||||
if level > constants.ShopMaxLevel {
|
if level > constants.ShopMaxLevel {
|
||||||
return nil, errors.New(errors.CodeShopLevelExceeded, "店铺层级不能超过 7 级")
|
return nil, errors.New(errors.CodeShopLevelExceeded, "店铺层级不能超过 7 级")
|
||||||
@@ -182,6 +184,7 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateShopRequest) (*dto.
|
|||||||
ShopName: shop.ShopName,
|
ShopName: shop.ShopName,
|
||||||
ShopCode: shop.ShopCode,
|
ShopCode: shop.ShopCode,
|
||||||
ParentID: shop.ParentID,
|
ParentID: shop.ParentID,
|
||||||
|
ParentShopName: parentShopName,
|
||||||
Level: shop.Level,
|
Level: shop.Level,
|
||||||
ContactName: shop.ContactName,
|
ContactName: shop.ContactName,
|
||||||
ContactPhone: shop.ContactPhone,
|
ContactPhone: shop.ContactPhone,
|
||||||
@@ -221,11 +224,20 @@ func (s *Service) Update(ctx context.Context, id uint, req *dto.UpdateShopReques
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parentShopName := ""
|
||||||
|
if shop.ParentID != nil {
|
||||||
|
parentShop, err := s.shopStore.GetByID(ctx, *shop.ParentID)
|
||||||
|
if err == nil {
|
||||||
|
parentShopName = parentShop.ShopName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &dto.ShopResponse{
|
return &dto.ShopResponse{
|
||||||
ID: shop.ID,
|
ID: shop.ID,
|
||||||
ShopName: shop.ShopName,
|
ShopName: shop.ShopName,
|
||||||
ShopCode: shop.ShopCode,
|
ShopCode: shop.ShopCode,
|
||||||
ParentID: shop.ParentID,
|
ParentID: shop.ParentID,
|
||||||
|
ParentShopName: parentShopName,
|
||||||
Level: shop.Level,
|
Level: shop.Level,
|
||||||
ContactName: shop.ContactName,
|
ContactName: shop.ContactName,
|
||||||
ContactPhone: shop.ContactPhone,
|
ContactPhone: shop.ContactPhone,
|
||||||
@@ -326,13 +338,24 @@ func (s *Service) ListShopResponses(ctx context.Context, req *dto.ShopListReques
|
|||||||
return nil, 0, errors.Wrap(errors.CodeInternalError, err, "查询店铺列表失败")
|
return nil, 0, errors.Wrap(errors.CodeInternalError, err, "查询店铺列表失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parentShopNameMap, err := s.buildParentShopNameMap(ctx, shops)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, errors.Wrap(errors.CodeInternalError, err, "查询上级店铺名称失败")
|
||||||
|
}
|
||||||
|
|
||||||
responses := make([]*dto.ShopResponse, 0, len(shops))
|
responses := make([]*dto.ShopResponse, 0, len(shops))
|
||||||
for _, shop := range shops {
|
for _, shop := range shops {
|
||||||
|
parentShopName := ""
|
||||||
|
if shop.ParentID != nil {
|
||||||
|
parentShopName = parentShopNameMap[*shop.ParentID]
|
||||||
|
}
|
||||||
|
|
||||||
responses = append(responses, &dto.ShopResponse{
|
responses = append(responses, &dto.ShopResponse{
|
||||||
ID: shop.ID,
|
ID: shop.ID,
|
||||||
ShopName: shop.ShopName,
|
ShopName: shop.ShopName,
|
||||||
ShopCode: shop.ShopCode,
|
ShopCode: shop.ShopCode,
|
||||||
ParentID: shop.ParentID,
|
ParentID: shop.ParentID,
|
||||||
|
ParentShopName: parentShopName,
|
||||||
Level: shop.Level,
|
Level: shop.Level,
|
||||||
ContactName: shop.ContactName,
|
ContactName: shop.ContactName,
|
||||||
ContactPhone: shop.ContactPhone,
|
ContactPhone: shop.ContactPhone,
|
||||||
@@ -350,6 +373,37 @@ func (s *Service) ListShopResponses(ctx context.Context, req *dto.ShopListReques
|
|||||||
return responses, total, nil
|
return responses, total, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// buildParentShopNameMap 批量查询店铺上级名称,避免列表接口出现 N+1 查询。
|
||||||
|
func (s *Service) buildParentShopNameMap(ctx context.Context, shops []*model.Shop) (map[uint]string, error) {
|
||||||
|
parentIDs := make([]uint, 0, len(shops))
|
||||||
|
parentIDSet := make(map[uint]struct{}, len(shops))
|
||||||
|
for _, shop := range shops {
|
||||||
|
if shop.ParentID == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, exists := parentIDSet[*shop.ParentID]; exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
parentIDSet[*shop.ParentID] = struct{}{}
|
||||||
|
parentIDs = append(parentIDs, *shop.ParentID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(parentIDs) == 0 {
|
||||||
|
return map[uint]string{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
parentShops, err := s.shopStore.GetByIDs(ctx, parentIDs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
parentShopNameMap := make(map[uint]string, len(parentShops))
|
||||||
|
for _, parentShop := range parentShops {
|
||||||
|
parentShopNameMap[parentShop.ID] = parentShop.ShopName
|
||||||
|
}
|
||||||
|
return parentShopNameMap, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Service) List(ctx context.Context, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.Shop, int64, error) {
|
func (s *Service) List(ctx context.Context, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.Shop, int64, error) {
|
||||||
return s.shopStore.List(ctx, opts, filters)
|
return s.shopStore.List(ctx, opts, filters)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user