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

@@ -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,
},
}
}