计划监控增加计划名

This commit is contained in:
2025-10-29 17:52:07 +08:00
parent 2910c9186a
commit af8689d627
9 changed files with 271 additions and 61 deletions

View File

@@ -149,7 +149,7 @@ func (c *Controller) ListPlanExecutionLogs(ctx *gin.Context) {
opts.Status = &status
}
data, total, err := c.monitorService.ListPlanExecutionLogs(opts, req.Page, req.PageSize)
planLogs, plans, total, err := c.monitorService.ListPlanExecutionLogs(opts, req.Page, req.PageSize)
if err != nil {
if errors.Is(err, repository.ErrInvalidPagination) {
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
@@ -162,8 +162,8 @@ func (c *Controller) ListPlanExecutionLogs(ctx *gin.Context) {
return
}
resp := dto.NewListPlanExecutionLogResponse(data, total, req.Page, req.PageSize)
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
resp := dto.NewListPlanExecutionLogResponse(planLogs, plans, total, req.Page, req.PageSize)
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(planLogs), total)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划执行日志成功", resp, actionType, "获取计划执行日志成功", req)
}

View File

@@ -53,14 +53,20 @@ func NewListDeviceCommandLogResponse(data []models.DeviceCommandLog, total int64
}
// NewListPlanExecutionLogResponse 从模型数据创建列表响应 DTO
func NewListPlanExecutionLogResponse(data []models.PlanExecutionLog, total int64, page, pageSize int) *ListPlanExecutionLogResponse {
dtos := make([]PlanExecutionLogDTO, len(data))
for i, item := range data {
func NewListPlanExecutionLogResponse(planLogs []models.PlanExecutionLog, plans []models.Plan, total int64, page, pageSize int) *ListPlanExecutionLogResponse {
planId2Name := make(map[uint]string)
for _, plan := range plans {
planId2Name[plan.ID] = plan.Name
}
dtos := make([]PlanExecutionLogDTO, len(planLogs))
for i, item := range planLogs {
dtos[i] = PlanExecutionLogDTO{
ID: item.ID,
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
PlanID: item.PlanID,
PlanName: planId2Name[item.PlanID],
Status: item.Status,
StartedAt: item.StartedAt,
EndedAt: item.EndedAt,

View File

@@ -91,6 +91,7 @@ type PlanExecutionLogDTO struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
PlanID uint `json:"plan_id"`
PlanName string `json:"plan_name"`
Status models.ExecutionStatus `json:"status"`
StartedAt time.Time `json:"started_at"`
EndedAt time.Time `json:"ended_at"`

View File

@@ -9,7 +9,7 @@ import (
type MonitorService interface {
ListSensorData(opts repository.SensorDataListOptions, page, pageSize int) ([]models.SensorData, int64, error)
ListDeviceCommandLogs(opts repository.DeviceCommandLogListOptions, page, pageSize int) ([]models.DeviceCommandLog, int64, error)
ListPlanExecutionLogs(opts repository.PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error)
ListPlanExecutionLogs(opts repository.PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, []models.Plan, int64, error)
ListTaskExecutionLogs(opts repository.TaskExecutionLogListOptions, page, pageSize int) ([]models.TaskExecutionLog, int64, error)
ListPendingCollections(opts repository.PendingCollectionListOptions, page, pageSize int) ([]models.PendingCollection, int64, error)
ListUserActionLogs(opts repository.UserActionLogListOptions, page, pageSize int) ([]models.UserActionLog, int64, error)
@@ -32,6 +32,7 @@ type monitorService struct {
sensorDataRepo repository.SensorDataRepository
deviceCommandLogRepo repository.DeviceCommandLogRepository
executionLogRepo repository.ExecutionLogRepository
planRepository repository.PlanRepository
pendingCollectionRepo repository.PendingCollectionRepository
userActionLogRepo repository.UserActionLogRepository
rawMaterialRepo repository.RawMaterialRepository
@@ -49,6 +50,7 @@ func NewMonitorService(
sensorDataRepo repository.SensorDataRepository,
deviceCommandLogRepo repository.DeviceCommandLogRepository,
executionLogRepo repository.ExecutionLogRepository,
planRepository repository.PlanRepository,
pendingCollectionRepo repository.PendingCollectionRepository,
userActionLogRepo repository.UserActionLogRepository,
rawMaterialRepo repository.RawMaterialRepository,
@@ -64,6 +66,7 @@ func NewMonitorService(
sensorDataRepo: sensorDataRepo,
deviceCommandLogRepo: deviceCommandLogRepo,
executionLogRepo: executionLogRepo,
planRepository: planRepository,
pendingCollectionRepo: pendingCollectionRepo,
userActionLogRepo: userActionLogRepo,
rawMaterialRepo: rawMaterialRepo,
@@ -88,8 +91,25 @@ func (s *monitorService) ListDeviceCommandLogs(opts repository.DeviceCommandLogL
}
// ListPlanExecutionLogs 负责处理查询计划执行日志列表的业务逻辑
func (s *monitorService) ListPlanExecutionLogs(opts repository.PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error) {
return s.executionLogRepo.ListPlanExecutionLogs(opts, page, pageSize)
func (s *monitorService) ListPlanExecutionLogs(opts repository.PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, []models.Plan, int64, error) {
planLogs, total, err := s.executionLogRepo.ListPlanExecutionLogs(opts, page, pageSize)
if err != nil {
return nil, nil, 0, err
}
planIds := []uint{}
for _, datum := range planLogs {
for _, id := range planIds {
if id == datum.PlanID {
break
}
planIds = append(planIds, datum.PlanID)
}
}
plans, err := s.planRepository.GetPlansByIDs(planIds)
if err != nil {
return nil, nil, 0, err
}
return planLogs, plans, total, nil
}
// ListTaskExecutionLogs 负责处理查询任务执行日志列表的业务逻辑

View File

@@ -196,6 +196,7 @@ func initAppServices(infra *Infrastructure, domainServices *DomainServices, logg
infra.Repos.SensorDataRepo,
infra.Repos.DeviceCommandLogRepo,
infra.Repos.ExecutionLogRepo,
infra.Repos.PlanRepo,
infra.Repos.PendingCollectionRepo,
infra.Repos.UserActionLogRepo,
infra.Repos.RawMaterialRepo,

View File

@@ -44,6 +44,8 @@ type PlanRepository interface {
GetBasicPlanByID(id uint) (*models.Plan, error)
// GetPlanByID 根据ID获取计划包含子计划和任务详情
GetPlanByID(id uint) (*models.Plan, error)
// GetPlansByIDs 根据ID列表获取计划不包含子计划和任务详情
GetPlansByIDs(ids []uint) ([]models.Plan, error)
// CreatePlan 创建一个新的计划
CreatePlan(plan *models.Plan) error
// UpdatePlan 更新计划,包括子计划和任务
@@ -139,6 +141,19 @@ func (r *gormPlanRepository) GetBasicPlanByID(id uint) (*models.Plan, error) {
return &plan, nil
}
// GetPlansByIDs 根据ID列表获取计划不包含子计划和任务详情
func (r *gormPlanRepository) GetPlansByIDs(ids []uint) ([]models.Plan, error) {
var plans []models.Plan
if len(ids) == 0 {
return plans, nil
}
err := r.db.Where("id IN ?", ids).Find(&plans).Error
if err != nil {
return nil, err
}
return plans, nil
}
// GetPlanByID 根据ID获取计划包含子计划和任务详情
func (r *gormPlanRepository) GetPlanByID(id uint) (*models.Plan, error) {
var plan models.Plan