fix(poll): GetPackages 接口新增分页 POLL-03
- 新增 AssetPackagesResult DTO(含 total/page/page_size/items 字段) - GetPackages 签名新增 page/pageSize 参数,默认 page=1、pageSize=50、最大 100 - admin/asset.go:Packages 从 Query 参数读取 page/page_size 传入 Service - client_asset.go 调用处同步更新,使用默认分页参数
This commit is contained in:
@@ -343,8 +343,20 @@ func (s *Service) Refresh(ctx context.Context, assetType string, id uint) (*dto.
|
||||
}
|
||||
}
|
||||
|
||||
// GetPackages 获取资产的所有套餐列表
|
||||
func (s *Service) GetPackages(ctx context.Context, assetType string, id uint) ([]*dto.AssetPackageResponse, error) {
|
||||
// GetPackages 获取资产的所有套餐列表(支持分页)
|
||||
// page 默认 1,pageSize 默认 50,pageSize 最大 100
|
||||
func (s *Service) GetPackages(ctx context.Context, assetType string, id uint, page, pageSize int) (*dto.AssetPackagesResult, error) {
|
||||
// 分页参数边界处理
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 {
|
||||
pageSize = 50
|
||||
}
|
||||
if pageSize > 100 {
|
||||
pageSize = 100
|
||||
}
|
||||
|
||||
// assetType 对应 Store 中的 carrierType:card→iot_card, device→device
|
||||
carrierType := assetType
|
||||
if assetType == "card" {
|
||||
@@ -371,7 +383,7 @@ func (s *Service) GetPackages(ctx context.Context, assetType string, id uint) ([
|
||||
pkgMap[p.ID] = p
|
||||
}
|
||||
|
||||
result := make([]*dto.AssetPackageResponse, 0, len(usages))
|
||||
all := make([]*dto.AssetPackageResponse, 0, len(usages))
|
||||
for _, u := range usages {
|
||||
pkg := pkgMap[u.PackageID]
|
||||
ratio := 1.0
|
||||
@@ -403,15 +415,31 @@ func (s *Service) GetPackages(ctx context.Context, assetType string, id uint) ([
|
||||
Priority: u.Priority,
|
||||
CreatedAt: u.CreatedAt,
|
||||
}
|
||||
result = append(result, item)
|
||||
all = append(all, item)
|
||||
}
|
||||
|
||||
// 按 created_at DESC 排序
|
||||
sort.Slice(result, func(i, j int) bool {
|
||||
return result[i].CreatedAt.After(result[j].CreatedAt)
|
||||
sort.Slice(all, func(i, j int) bool {
|
||||
return all[i].CreatedAt.After(all[j].CreatedAt)
|
||||
})
|
||||
|
||||
return result, nil
|
||||
total := int64(len(all))
|
||||
offset := (page - 1) * pageSize
|
||||
end := offset + pageSize
|
||||
if offset >= len(all) {
|
||||
offset = len(all)
|
||||
}
|
||||
if end > len(all) {
|
||||
end = len(all)
|
||||
}
|
||||
items := all[offset:end]
|
||||
|
||||
return &dto.AssetPackagesResult{
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetCurrentPackage 获取资产当前生效的主套餐
|
||||
|
||||
Reference in New Issue
Block a user