This commit is contained in:
@@ -53,11 +53,13 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateShopRequest) (*dto.
|
||||
}
|
||||
|
||||
level := 1
|
||||
parentShopName := ""
|
||||
if req.ParentID != nil {
|
||||
parent, err := s.shopStore.GetByID(ctx, *req.ParentID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeInvalidParentID, "上级店铺不存在或无效")
|
||||
}
|
||||
parentShopName = parent.ShopName
|
||||
level = parent.Level + 1
|
||||
if level > constants.ShopMaxLevel {
|
||||
return nil, errors.New(errors.CodeShopLevelExceeded, "店铺层级不能超过 7 级")
|
||||
@@ -178,21 +180,22 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateShopRequest) (*dto.
|
||||
}
|
||||
|
||||
return &dto.ShopResponse{
|
||||
ID: shop.ID,
|
||||
ShopName: shop.ShopName,
|
||||
ShopCode: shop.ShopCode,
|
||||
ParentID: shop.ParentID,
|
||||
Level: shop.Level,
|
||||
ContactName: shop.ContactName,
|
||||
ContactPhone: shop.ContactPhone,
|
||||
Province: shop.Province,
|
||||
City: shop.City,
|
||||
District: shop.District,
|
||||
Address: shop.Address,
|
||||
Status: shop.Status,
|
||||
StatusName: constants.GetStatusName(shop.Status),
|
||||
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: shop.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
ID: shop.ID,
|
||||
ShopName: shop.ShopName,
|
||||
ShopCode: shop.ShopCode,
|
||||
ParentID: shop.ParentID,
|
||||
ParentShopName: parentShopName,
|
||||
Level: shop.Level,
|
||||
ContactName: shop.ContactName,
|
||||
ContactPhone: shop.ContactPhone,
|
||||
Province: shop.Province,
|
||||
City: shop.City,
|
||||
District: shop.District,
|
||||
Address: shop.Address,
|
||||
Status: shop.Status,
|
||||
StatusName: constants.GetStatusName(shop.Status),
|
||||
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: shop.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -221,22 +224,31 @@ func (s *Service) Update(ctx context.Context, id uint, req *dto.UpdateShopReques
|
||||
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{
|
||||
ID: shop.ID,
|
||||
ShopName: shop.ShopName,
|
||||
ShopCode: shop.ShopCode,
|
||||
ParentID: shop.ParentID,
|
||||
Level: shop.Level,
|
||||
ContactName: shop.ContactName,
|
||||
ContactPhone: shop.ContactPhone,
|
||||
Province: shop.Province,
|
||||
City: shop.City,
|
||||
District: shop.District,
|
||||
Address: shop.Address,
|
||||
Status: shop.Status,
|
||||
StatusName: constants.GetStatusName(shop.Status),
|
||||
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: shop.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
ID: shop.ID,
|
||||
ShopName: shop.ShopName,
|
||||
ShopCode: shop.ShopCode,
|
||||
ParentID: shop.ParentID,
|
||||
ParentShopName: parentShopName,
|
||||
Level: shop.Level,
|
||||
ContactName: shop.ContactName,
|
||||
ContactPhone: shop.ContactPhone,
|
||||
Province: shop.Province,
|
||||
City: shop.City,
|
||||
District: shop.District,
|
||||
Address: shop.Address,
|
||||
Status: shop.Status,
|
||||
StatusName: constants.GetStatusName(shop.Status),
|
||||
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: shop.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -326,30 +338,72 @@ func (s *Service) ListShopResponses(ctx context.Context, req *dto.ShopListReques
|
||||
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))
|
||||
for _, shop := range shops {
|
||||
parentShopName := ""
|
||||
if shop.ParentID != nil {
|
||||
parentShopName = parentShopNameMap[*shop.ParentID]
|
||||
}
|
||||
|
||||
responses = append(responses, &dto.ShopResponse{
|
||||
ID: shop.ID,
|
||||
ShopName: shop.ShopName,
|
||||
ShopCode: shop.ShopCode,
|
||||
ParentID: shop.ParentID,
|
||||
Level: shop.Level,
|
||||
ContactName: shop.ContactName,
|
||||
ContactPhone: shop.ContactPhone,
|
||||
Province: shop.Province,
|
||||
City: shop.City,
|
||||
District: shop.District,
|
||||
Address: shop.Address,
|
||||
Status: shop.Status,
|
||||
StatusName: constants.GetStatusName(shop.Status),
|
||||
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: shop.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
ID: shop.ID,
|
||||
ShopName: shop.ShopName,
|
||||
ShopCode: shop.ShopCode,
|
||||
ParentID: shop.ParentID,
|
||||
ParentShopName: parentShopName,
|
||||
Level: shop.Level,
|
||||
ContactName: shop.ContactName,
|
||||
ContactPhone: shop.ContactPhone,
|
||||
Province: shop.Province,
|
||||
City: shop.City,
|
||||
District: shop.District,
|
||||
Address: shop.Address,
|
||||
Status: shop.Status,
|
||||
StatusName: constants.GetStatusName(shop.Status),
|
||||
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: shop.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
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) {
|
||||
return s.shopStore.List(ctx, opts, filters)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user