ListPlanExecutionLogs
This commit is contained in:
@@ -118,3 +118,51 @@ func (c *Controller) ListDeviceCommandLogs(ctx *gin.Context) {
|
||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备命令日志成功", resp, actionType, "获取设备命令日志成功", req)
|
||||
}
|
||||
|
||||
// ListPlanExecutionLogs godoc
|
||||
// @Summary 获取计划执行日志列表
|
||||
// @Description 根据提供的过滤条件,分页获取计划执行日志
|
||||
// @Tags 数据监控
|
||||
// @Security BearerAuth
|
||||
// @Produce json
|
||||
// @Param query query dto.ListPlanExecutionLogRequest true "查询参数"
|
||||
// @Success 200 {object} controller.Response{data=dto.ListPlanExecutionLogResponse}
|
||||
// @Router /api/v1/monitor/plan-execution-logs [get]
|
||||
func (c *Controller) ListPlanExecutionLogs(ctx *gin.Context) {
|
||||
const actionType = "获取计划执行日志列表"
|
||||
|
||||
var req dto.ListPlanExecutionLogRequest
|
||||
if err := ctx.ShouldBindQuery(&req); err != nil {
|
||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
opts := repository.PlanExecutionLogListOptions{
|
||||
PlanID: req.PlanID,
|
||||
OrderBy: req.OrderBy,
|
||||
StartTime: req.StartTime,
|
||||
EndTime: req.EndTime,
|
||||
}
|
||||
if req.Status != nil {
|
||||
status := models.ExecutionStatus(*req.Status)
|
||||
opts.Status = &status
|
||||
}
|
||||
|
||||
data, 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)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||
return
|
||||
}
|
||||
|
||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取计划执行日志失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
resp := dto.NewListPlanExecutionLogResponse(data, total, req.Page, req.PageSize)
|
||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划执行日志成功", resp, actionType, "获取计划执行日志成功", req)
|
||||
}
|
||||
|
||||
@@ -117,3 +117,60 @@ func NewListDeviceCommandLogResponse(data []models.DeviceCommandLog, total int64
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// --- PlanExecutionLog ---
|
||||
|
||||
// ListPlanExecutionLogRequest 定义了获取计划执行日志列表的请求参数
|
||||
type ListPlanExecutionLogRequest struct {
|
||||
Page int `form:"page,default=1"`
|
||||
PageSize int `form:"pageSize,default=10"`
|
||||
PlanID *uint `form:"plan_id"`
|
||||
Status *string `form:"status"`
|
||||
StartTime *time.Time `form:"start_time" time_format:"rfc3339"`
|
||||
EndTime *time.Time `form:"end_time" time_format:"rfc3339"`
|
||||
OrderBy string `form:"order_by"`
|
||||
}
|
||||
|
||||
// PlanExecutionLogDTO 是用于API响应的计划执行日志结构
|
||||
type PlanExecutionLogDTO struct {
|
||||
ID uint `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
PlanID uint `json:"plan_id"`
|
||||
Status models.ExecutionStatus `json:"status"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
EndedAt time.Time `json:"ended_at"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
// ListPlanExecutionLogResponse 是获取计划执行日志列表的响应结构
|
||||
type ListPlanExecutionLogResponse struct {
|
||||
List []PlanExecutionLogDTO `json:"list"`
|
||||
Pagination PaginationDTO `json:"pagination"`
|
||||
}
|
||||
|
||||
// NewListPlanExecutionLogResponse 从模型数据创建列表响应 DTO
|
||||
func NewListPlanExecutionLogResponse(data []models.PlanExecutionLog, total int64, page, pageSize int) *ListPlanExecutionLogResponse {
|
||||
dtos := make([]PlanExecutionLogDTO, len(data))
|
||||
for i, item := range data {
|
||||
dtos[i] = PlanExecutionLogDTO{
|
||||
ID: item.ID,
|
||||
CreatedAt: item.CreatedAt,
|
||||
UpdatedAt: item.UpdatedAt,
|
||||
PlanID: item.PlanID,
|
||||
Status: item.Status,
|
||||
StartedAt: item.StartedAt,
|
||||
EndedAt: item.EndedAt,
|
||||
Error: item.Error,
|
||||
}
|
||||
}
|
||||
|
||||
return &ListPlanExecutionLogResponse{
|
||||
List: dtos,
|
||||
Pagination: PaginationDTO{
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
type MonitorService struct {
|
||||
sensorDataRepo repository.SensorDataRepository
|
||||
deviceCommandLogRepo repository.DeviceCommandLogRepository
|
||||
executionLogRepo repository.ExecutionLogRepository
|
||||
// 在这里可以添加其他超表模型的仓库依赖
|
||||
}
|
||||
|
||||
@@ -16,10 +17,12 @@ type MonitorService struct {
|
||||
func NewMonitorService(
|
||||
sensorDataRepo repository.SensorDataRepository,
|
||||
deviceCommandLogRepo repository.DeviceCommandLogRepository,
|
||||
executionLogRepo repository.ExecutionLogRepository,
|
||||
) *MonitorService {
|
||||
return &MonitorService{
|
||||
sensorDataRepo: sensorDataRepo,
|
||||
deviceCommandLogRepo: deviceCommandLogRepo,
|
||||
executionLogRepo: executionLogRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,3 +35,8 @@ func (s *MonitorService) ListSensorData(opts repository.SensorDataListOptions, p
|
||||
func (s *MonitorService) ListDeviceCommandLogs(opts repository.DeviceCommandLogListOptions, page, pageSize int) ([]models.DeviceCommandLog, int64, error) {
|
||||
return s.deviceCommandLogRepo.List(opts, page, pageSize)
|
||||
}
|
||||
|
||||
// ListPlanExecutionLogs 负责处理查询计划执行日志列表的业务逻辑
|
||||
func (s *MonitorService) ListPlanExecutionLogs(opts repository.PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error) {
|
||||
return s.executionLogRepo.ListPlanExecutionLogs(opts, page, pageSize)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user