ListDeviceCommandLogs
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -7,8 +7,18 @@ import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
// --- General ---
|
||||
|
||||
// PaginationDTO 定义了分页信息的标准结构
|
||||
type PaginationDTO struct {
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
}
|
||||
|
||||
// --- SensorData ---
|
||||
|
||||
// ListSensorDataRequest 定义了获取传感器数据列表的请求参数
|
||||
// 使用 form 标签以便 Gin 可以从查询字符串中绑定数据
|
||||
type ListSensorDataRequest struct {
|
||||
Page int `form:"page,default=1"`
|
||||
PageSize int `form:"pageSize,default=10"`
|
||||
@@ -25,14 +35,7 @@ type SensorDataDTO struct {
|
||||
DeviceID uint `json:"device_id"`
|
||||
RegionalControllerID uint `json:"regional_controller_id"`
|
||||
SensorType models.SensorType `json:"sensor_type"`
|
||||
Data json.RawMessage `json:"data"` // 使用 json.RawMessage 以便直接输出 JSON 内容
|
||||
}
|
||||
|
||||
// PaginationDTO 定义了分页信息的标准结构
|
||||
type PaginationDTO struct {
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
|
||||
// ListSensorDataResponse 是获取传感器数据列表的响应结构
|
||||
@@ -50,7 +53,7 @@ func NewListSensorDataResponse(data []models.SensorData, total int64, page, page
|
||||
DeviceID: item.DeviceID,
|
||||
RegionalControllerID: item.RegionalControllerID,
|
||||
SensorType: item.SensorType,
|
||||
Data: json.RawMessage(item.Data), // gorm.datatypes.JSON 是 []byte, 可直接转换为 json.RawMessage
|
||||
Data: json.RawMessage(item.Data),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,3 +66,54 @@ func NewListSensorDataResponse(data []models.SensorData, total int64, page, page
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// --- DeviceCommandLog ---
|
||||
|
||||
// ListDeviceCommandLogRequest 定义了获取设备命令日志列表的请求参数
|
||||
type ListDeviceCommandLogRequest struct {
|
||||
Page int `form:"page,default=1"`
|
||||
PageSize int `form:"pageSize,default=10"`
|
||||
DeviceID *uint `form:"device_id"`
|
||||
ReceivedSuccess *bool `form:"received_success"`
|
||||
StartTime *time.Time `form:"start_time" time_format:"rfc3339"`
|
||||
EndTime *time.Time `form:"end_time" time_format:"rfc3339"`
|
||||
OrderBy string `form:"order_by"`
|
||||
}
|
||||
|
||||
// DeviceCommandLogDTO 是用于API响应的设备命令日志结构
|
||||
type DeviceCommandLogDTO struct {
|
||||
MessageID string `json:"message_id"`
|
||||
DeviceID uint `json:"device_id"`
|
||||
SentAt time.Time `json:"sent_at"`
|
||||
AcknowledgedAt *time.Time `json:"acknowledged_at"`
|
||||
ReceivedSuccess bool `json:"received_success"`
|
||||
}
|
||||
|
||||
// ListDeviceCommandLogResponse 是获取设备命令日志列表的响应结构
|
||||
type ListDeviceCommandLogResponse struct {
|
||||
List []DeviceCommandLogDTO `json:"list"`
|
||||
Pagination PaginationDTO `json:"pagination"`
|
||||
}
|
||||
|
||||
// NewListDeviceCommandLogResponse 从模型数据创建列表响应 DTO
|
||||
func NewListDeviceCommandLogResponse(data []models.DeviceCommandLog, total int64, page, pageSize int) *ListDeviceCommandLogResponse {
|
||||
dtos := make([]DeviceCommandLogDTO, len(data))
|
||||
for i, item := range data {
|
||||
dtos[i] = DeviceCommandLogDTO{
|
||||
MessageID: item.MessageID,
|
||||
DeviceID: item.DeviceID,
|
||||
SentAt: item.SentAt,
|
||||
AcknowledgedAt: item.AcknowledgedAt,
|
||||
ReceivedSuccess: item.ReceivedSuccess,
|
||||
}
|
||||
}
|
||||
|
||||
return &ListDeviceCommandLogResponse{
|
||||
List: dtos,
|
||||
Pagination: PaginationDTO{
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,20 +7,28 @@ import (
|
||||
|
||||
// MonitorService 定义了监控相关的业务逻辑服务
|
||||
type MonitorService struct {
|
||||
sensorDataRepo repository.SensorDataRepository
|
||||
sensorDataRepo repository.SensorDataRepository
|
||||
deviceCommandLogRepo repository.DeviceCommandLogRepository
|
||||
// 在这里可以添加其他超表模型的仓库依赖
|
||||
}
|
||||
|
||||
// NewMonitorService 创建一个新的 MonitorService 实例
|
||||
func NewMonitorService(sensorDataRepo repository.SensorDataRepository) *MonitorService {
|
||||
func NewMonitorService(
|
||||
sensorDataRepo repository.SensorDataRepository,
|
||||
deviceCommandLogRepo repository.DeviceCommandLogRepository,
|
||||
) *MonitorService {
|
||||
return &MonitorService{
|
||||
sensorDataRepo: sensorDataRepo,
|
||||
sensorDataRepo: sensorDataRepo,
|
||||
deviceCommandLogRepo: deviceCommandLogRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// ListSensorData 负责处理查询传感器数据列表的业务逻辑
|
||||
func (s *MonitorService) ListSensorData(opts repository.SensorDataListOptions, page, pageSize int) ([]models.SensorData, int64, error) {
|
||||
// 目前业务逻辑只是简单地将请求透传给仓库层。
|
||||
// 未来任何与查询传感器数据相关的业务规则(如权限校验、数据聚合等)都应在此处实现。
|
||||
return s.sensorDataRepo.List(opts, page, pageSize)
|
||||
}
|
||||
|
||||
// ListDeviceCommandLogs 负责处理查询设备命令日志列表的业务逻辑
|
||||
func (s *MonitorService) ListDeviceCommandLogs(opts repository.DeviceCommandLogListOptions, page, pageSize int) ([]models.DeviceCommandLog, int64, error) {
|
||||
return s.deviceCommandLogRepo.List(opts, page, pageSize)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user