ListDeviceCommandLogs

This commit is contained in:
2025-10-18 15:33:39 +08:00
parent 05820438d0
commit 51a873049e
4 changed files with 178 additions and 24 deletions

View File

@@ -38,7 +38,6 @@ func NewController(monitorService *service.MonitorService, logger *logs.Logger)
func (c *Controller) ListSensorData(ctx *gin.Context) {
const actionType = "获取传感器数据列表"
// --- 绑定并校验查询参数 ---
var req dto.ListSensorDataRequest
if err := ctx.ShouldBindQuery(&req); err != nil {
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
@@ -46,7 +45,6 @@ func (c *Controller) ListSensorData(ctx *gin.Context) {
return
}
// --- 将请求 DTO 转换为仓库层需要的 Options 结构体 ---
opts := repository.SensorDataListOptions{
DeviceID: req.DeviceID,
OrderBy: req.OrderBy,
@@ -58,24 +56,65 @@ func (c *Controller) ListSensorData(ctx *gin.Context) {
opts.SensorType = &sensorType
}
// --- 调用服务层 ---
data, total, err := c.monitorService.ListSensorData(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.NewListSensorDataResponse(data, total, req.Page, req.PageSize)
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取传感器数据成功", resp, actionType, "获取传感器数据成功", req)
}
// ListDeviceCommandLogs godoc
// @Summary 获取设备命令日志列表
// @Description 根据提供的过滤条件,分页获取设备命令日志
// @Tags 数据监控
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListDeviceCommandLogRequest true "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListDeviceCommandLogResponse}
// @Router /api/v1/monitor/device-command-logs [get]
func (c *Controller) ListDeviceCommandLogs(ctx *gin.Context) {
const actionType = "获取设备命令日志列表"
var req dto.ListDeviceCommandLogRequest
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.DeviceCommandLogListOptions{
DeviceID: req.DeviceID,
ReceivedSuccess: req.ReceivedSuccess,
OrderBy: req.OrderBy,
StartTime: req.StartTime,
EndTime: req.EndTime,
}
data, total, err := c.monitorService.ListDeviceCommandLogs(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.NewListDeviceCommandLogResponse(data, total, req.Page, req.PageSize)
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备命令日志成功", resp, actionType, "获取设备命令日志成功", req)
}