任务列表返回目标名称
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m1s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m1s
This commit is contained in:
@@ -191,10 +191,14 @@ func (s *Service) List(ctx context.Context, req *dto.ListDeviceImportTaskRequest
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
targetNames, err := s.loadTargetNames(ctx, tasks)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := make([]*dto.DeviceImportTaskResponse, 0, len(tasks))
|
||||
for _, task := range tasks {
|
||||
list = append(list, s.toTaskResponse(task))
|
||||
list = append(list, s.toTaskResponse(task, targetNames))
|
||||
}
|
||||
|
||||
return &dto.ListDeviceImportTaskResponse{
|
||||
@@ -210,9 +214,13 @@ func (s *Service) GetByID(ctx context.Context, id uint) (*dto.DeviceImportTaskDe
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeNotFound, "导入任务不存在")
|
||||
}
|
||||
targetNames, err := s.loadTargetNames(ctx, []*model.DeviceImportTask{task})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &dto.DeviceImportTaskDetailResponse{
|
||||
DeviceImportTaskResponse: *s.toTaskResponse(task),
|
||||
DeviceImportTaskResponse: *s.toTaskResponse(task, targetNames),
|
||||
SkippedItems: make([]*dto.DeviceImportResultItemDTO, 0),
|
||||
FailedItems: make([]*dto.DeviceImportResultItemDTO, 0),
|
||||
WarningItems: make([]*dto.DeviceImportResultItemDTO, 0),
|
||||
@@ -239,7 +247,49 @@ func (s *Service) GetByID(ctx context.Context, id uint) (*dto.DeviceImportTaskDe
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *Service) toTaskResponse(task *model.DeviceImportTask) *dto.DeviceImportTaskResponse {
|
||||
type deviceImportTargetNames struct {
|
||||
shops map[uint]string
|
||||
series map[uint]string
|
||||
}
|
||||
|
||||
// loadTargetNames 批量解析任务目标名称,避免列表逐条查询。
|
||||
func (s *Service) loadTargetNames(ctx context.Context, tasks []*model.DeviceImportTask) (deviceImportTargetNames, error) {
|
||||
shopIDs := make([]uint, 0)
|
||||
seriesIDs := make([]uint, 0)
|
||||
for _, task := range tasks {
|
||||
if task.TargetID == nil {
|
||||
continue
|
||||
}
|
||||
switch task.OperationType {
|
||||
case constants.DeviceImportOperationAssignShop:
|
||||
shopIDs = append(shopIDs, *task.TargetID)
|
||||
case constants.DeviceImportOperationAssignSeries:
|
||||
seriesIDs = append(seriesIDs, *task.TargetID)
|
||||
}
|
||||
}
|
||||
names := deviceImportTargetNames{shops: make(map[uint]string), series: make(map[uint]string)}
|
||||
if len(shopIDs) > 0 {
|
||||
var shops []model.Shop
|
||||
if err := s.db.WithContext(ctx).Select("id, shop_name").Where("id IN ?", shopIDs).Find(&shops).Error; err != nil {
|
||||
return names, errors.Wrap(errors.CodeDatabaseError, err, "查询设备批量任务目标店铺失败")
|
||||
}
|
||||
for _, shop := range shops {
|
||||
names.shops[shop.ID] = shop.ShopName
|
||||
}
|
||||
}
|
||||
if len(seriesIDs) > 0 {
|
||||
var series []model.PackageSeries
|
||||
if err := s.db.WithContext(ctx).Select("id, series_name").Where("id IN ?", seriesIDs).Find(&series).Error; err != nil {
|
||||
return names, errors.Wrap(errors.CodeDatabaseError, err, "查询设备批量任务目标套餐系列失败")
|
||||
}
|
||||
for _, item := range series {
|
||||
names.series[item.ID] = item.SeriesName
|
||||
}
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
func (s *Service) toTaskResponse(task *model.DeviceImportTask, targetNames deviceImportTargetNames) *dto.DeviceImportTaskResponse {
|
||||
var startedAt, completedAt *time.Time
|
||||
if task.StartedAt != nil {
|
||||
startedAt = task.StartedAt
|
||||
@@ -254,6 +304,7 @@ func (s *Service) toTaskResponse(task *model.DeviceImportTask) *dto.DeviceImport
|
||||
OperationType: task.OperationType,
|
||||
OperationName: constants.GetDeviceImportOperationName(task.OperationType),
|
||||
TargetID: task.TargetID,
|
||||
TargetName: targetNames.resolve(task),
|
||||
Status: task.Status,
|
||||
StatusName: getStatusText(task.Status),
|
||||
StatusText: getStatusText(task.Status),
|
||||
@@ -273,6 +324,19 @@ func (s *Service) toTaskResponse(task *model.DeviceImportTask) *dto.DeviceImport
|
||||
}
|
||||
}
|
||||
|
||||
func (n deviceImportTargetNames) resolve(task *model.DeviceImportTask) string {
|
||||
if task.TargetID == nil {
|
||||
return ""
|
||||
}
|
||||
if task.OperationType == constants.DeviceImportOperationAssignShop {
|
||||
return n.shops[*task.TargetID]
|
||||
}
|
||||
if task.OperationType == constants.DeviceImportOperationAssignSeries {
|
||||
return n.series[*task.TargetID]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func getStatusText(status int) string {
|
||||
switch status {
|
||||
case model.ImportTaskStatusPending:
|
||||
|
||||
Reference in New Issue
Block a user