This commit is contained in:
@@ -39,21 +39,22 @@ type UpdateShopRequest struct {
|
|||||||
|
|
||||||
// ShopResponse 店铺响应
|
// ShopResponse 店铺响应
|
||||||
type ShopResponse struct {
|
type ShopResponse struct {
|
||||||
ID uint `json:"id" description:"店铺ID"`
|
ID uint `json:"id" description:"店铺ID"`
|
||||||
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"`
|
||||||
Level int `json:"level" description:"店铺层级 (1-7级)"`
|
ParentShopName string `json:"parent_shop_name,omitempty" description:"上级店铺名称"`
|
||||||
ContactName string `json:"contact_name" description:"联系人姓名"`
|
Level int `json:"level" description:"店铺层级 (1-7级)"`
|
||||||
ContactPhone string `json:"contact_phone" description:"联系人电话"`
|
ContactName string `json:"contact_name" description:"联系人姓名"`
|
||||||
Province string `json:"province" description:"省份"`
|
ContactPhone string `json:"contact_phone" description:"联系人电话"`
|
||||||
City string `json:"city" description:"城市"`
|
Province string `json:"province" description:"省份"`
|
||||||
District string `json:"district" description:"区县"`
|
City string `json:"city" description:"城市"`
|
||||||
Address string `json:"address" description:"详细地址"`
|
District string `json:"district" description:"区县"`
|
||||||
Status int `json:"status" description:"状态 (0:禁用, 1:启用)"`
|
Address string `json:"address" description:"详细地址"`
|
||||||
StatusName string `json:"status_name" description:"状态名称(中文)"`
|
Status int `json:"status" description:"状态 (0:禁用, 1:启用)"`
|
||||||
CreatedAt string `json:"created_at" description:"创建时间"`
|
StatusName string `json:"status_name" description:"状态名称(中文)"`
|
||||||
UpdatedAt string `json:"updated_at" description:"更新时间"`
|
CreatedAt string `json:"created_at" description:"创建时间"`
|
||||||
|
UpdatedAt string `json:"updated_at" description:"更新时间"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShopPageResult 店铺分页响应
|
// ShopPageResult 店铺分页响应
|
||||||
|
|||||||
@@ -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 级")
|
||||||
@@ -178,21 +180,22 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateShopRequest) (*dto.
|
|||||||
}
|
}
|
||||||
|
|
||||||
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,
|
||||||
Level: shop.Level,
|
ParentShopName: parentShopName,
|
||||||
ContactName: shop.ContactName,
|
Level: shop.Level,
|
||||||
ContactPhone: shop.ContactPhone,
|
ContactName: shop.ContactName,
|
||||||
Province: shop.Province,
|
ContactPhone: shop.ContactPhone,
|
||||||
City: shop.City,
|
Province: shop.Province,
|
||||||
District: shop.District,
|
City: shop.City,
|
||||||
Address: shop.Address,
|
District: shop.District,
|
||||||
Status: shop.Status,
|
Address: shop.Address,
|
||||||
StatusName: constants.GetStatusName(shop.Status),
|
Status: shop.Status,
|
||||||
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
StatusName: constants.GetStatusName(shop.Status),
|
||||||
UpdatedAt: shop.UpdatedAt.Format("2006-01-02 15:04:05"),
|
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||||
|
UpdatedAt: shop.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,22 +224,31 @@ 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,
|
||||||
Level: shop.Level,
|
ParentShopName: parentShopName,
|
||||||
ContactName: shop.ContactName,
|
Level: shop.Level,
|
||||||
ContactPhone: shop.ContactPhone,
|
ContactName: shop.ContactName,
|
||||||
Province: shop.Province,
|
ContactPhone: shop.ContactPhone,
|
||||||
City: shop.City,
|
Province: shop.Province,
|
||||||
District: shop.District,
|
City: shop.City,
|
||||||
Address: shop.Address,
|
District: shop.District,
|
||||||
Status: shop.Status,
|
Address: shop.Address,
|
||||||
StatusName: constants.GetStatusName(shop.Status),
|
Status: shop.Status,
|
||||||
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
StatusName: constants.GetStatusName(shop.Status),
|
||||||
UpdatedAt: shop.UpdatedAt.Format("2006-01-02 15:04:05"),
|
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||||
|
UpdatedAt: shop.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -326,30 +338,72 @@ 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,
|
||||||
Level: shop.Level,
|
ParentShopName: parentShopName,
|
||||||
ContactName: shop.ContactName,
|
Level: shop.Level,
|
||||||
ContactPhone: shop.ContactPhone,
|
ContactName: shop.ContactName,
|
||||||
Province: shop.Province,
|
ContactPhone: shop.ContactPhone,
|
||||||
City: shop.City,
|
Province: shop.Province,
|
||||||
District: shop.District,
|
City: shop.City,
|
||||||
Address: shop.Address,
|
District: shop.District,
|
||||||
Status: shop.Status,
|
Address: shop.Address,
|
||||||
StatusName: constants.GetStatusName(shop.Status),
|
Status: shop.Status,
|
||||||
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
StatusName: constants.GetStatusName(shop.Status),
|
||||||
UpdatedAt: shop.UpdatedAt.Format("2006-01-02 15:04:05"),
|
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||||
|
UpdatedAt: shop.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
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