This commit is contained in:
@@ -75,7 +75,9 @@ type H5PopupConfigurationResponse struct {
|
||||
CreatedAt time.Time `json:"created_at" description:"创建时间(ISO 8601)"`
|
||||
UpdatedAt time.Time `json:"updated_at" description:"最近更新时间(ISO 8601);启停同样刷新该时间"`
|
||||
Creator uint `json:"creator" description:"创建人账号ID"`
|
||||
CreatorName string `json:"creator_name" description:"创建人账号名称"`
|
||||
Updater uint `json:"updater" description:"最近更新人账号ID"`
|
||||
UpdaterName string `json:"updater_name" description:"最近更新人账号名称"`
|
||||
}
|
||||
|
||||
// H5PopupConfigurationListRequest 是运营弹窗配置列表分页参数。
|
||||
|
||||
@@ -46,9 +46,13 @@ func (q *Query) List(ctx context.Context, request dto.H5PopupConfigurationListRe
|
||||
if err := base.Order("updated_at DESC, id DESC").Offset(offset).Limit(pageSize).Find(&records).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询运营弹窗配置列表失败")
|
||||
}
|
||||
accountNames, err := q.loadAccountNames(ctx, accountIDs(records))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]dto.H5PopupConfigurationResponse, 0, len(records))
|
||||
for index := range records {
|
||||
items = append(items, *toConfigurationResponse(&records[index]))
|
||||
items = append(items, *toConfigurationResponse(&records[index], accountNames))
|
||||
}
|
||||
return &dto.H5PopupConfigurationListResponse{Items: items, Total: total, Page: page, Size: pageSize}, nil
|
||||
}
|
||||
@@ -65,7 +69,11 @@ func (q *Query) Get(ctx context.Context, id uint) (*dto.H5PopupConfigurationResp
|
||||
}
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询运营弹窗配置失败")
|
||||
}
|
||||
return toConfigurationResponse(&record), nil
|
||||
accountNames, err := q.loadAccountNames(ctx, []uint{record.Creator, record.Updater})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toConfigurationResponse(&record, accountNames), nil
|
||||
}
|
||||
|
||||
// normalizePagination 归一化页码与每页数量并返回偏移量。
|
||||
@@ -85,8 +93,45 @@ func normalizePagination(page, pageSize int) (int, int, int, error) {
|
||||
return page, pageSize, (page - 1) * pageSize, nil
|
||||
}
|
||||
|
||||
// accountIDs 收集配置列表中的创建人与更新人账号 ID。
|
||||
func accountIDs(records []model.H5PopupConfiguration) []uint {
|
||||
ids := make([]uint, 0, len(records)*2)
|
||||
for index := range records {
|
||||
ids = append(ids, records[index].Creator, records[index].Updater)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// loadAccountNames 批量读取配置关联账号名称,缺失账号留空。
|
||||
func (q *Query) loadAccountNames(ctx context.Context, ids []uint) (map[uint]string, error) {
|
||||
uniqueIDs := make([]uint, 0, len(ids))
|
||||
seen := make(map[uint]struct{}, len(ids))
|
||||
for _, id := range ids {
|
||||
if id == 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[id]; ok {
|
||||
continue
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
uniqueIDs = append(uniqueIDs, id)
|
||||
}
|
||||
names := make(map[uint]string, len(uniqueIDs))
|
||||
if len(uniqueIDs) == 0 {
|
||||
return names, nil
|
||||
}
|
||||
var accounts []model.Account
|
||||
if err := q.db.WithContext(ctx).Unscoped().Model(&model.Account{}).Select("id", "username").Where("id IN ?", uniqueIDs).Find(&accounts).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询运营弹窗配置关联账号失败")
|
||||
}
|
||||
for index := range accounts {
|
||||
names[accounts[index].ID] = accounts[index].Username
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// toConfigurationResponse 将配置投影为对外响应,范围维度空数组表示全量。
|
||||
func toConfigurationResponse(record *model.H5PopupConfiguration) *dto.H5PopupConfigurationResponse {
|
||||
func toConfigurationResponse(record *model.H5PopupConfiguration, accountNames map[uint]string) *dto.H5PopupConfigurationResponse {
|
||||
if record == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -110,7 +155,9 @@ func toConfigurationResponse(record *model.H5PopupConfiguration) *dto.H5PopupCon
|
||||
CreatedAt: record.CreatedAt,
|
||||
UpdatedAt: record.UpdatedAt,
|
||||
Creator: record.Creator,
|
||||
CreatorName: accountNames[record.Creator],
|
||||
Updater: record.Updater,
|
||||
UpdaterName: accountNames[record.Updater],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user