ListSensorData
This commit is contained in:
65
internal/app/dto/monitor_dto.go
Normal file
65
internal/app/dto/monitor_dto.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
// ListSensorDataRequest 定义了获取传感器数据列表的请求参数
|
||||
// 使用 form 标签以便 Gin 可以从查询字符串中绑定数据
|
||||
type ListSensorDataRequest struct {
|
||||
Page int `form:"page,default=1"`
|
||||
PageSize int `form:"pageSize,default=10"`
|
||||
DeviceID *uint `form:"device_id"`
|
||||
SensorType *string `form:"sensor_type"`
|
||||
StartTime *time.Time `form:"start_time" time_format:"rfc3339"`
|
||||
EndTime *time.Time `form:"end_time" time_format:"rfc3339"`
|
||||
OrderBy string `form:"order_by"`
|
||||
}
|
||||
|
||||
// SensorDataDTO 是用于API响应的传感器数据结构
|
||||
type SensorDataDTO struct {
|
||||
Time time.Time `json:"time"`
|
||||
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"`
|
||||
}
|
||||
|
||||
// ListSensorDataResponse 是获取传感器数据列表的响应结构
|
||||
type ListSensorDataResponse struct {
|
||||
List []SensorDataDTO `json:"list"`
|
||||
Pagination PaginationDTO `json:"pagination"`
|
||||
}
|
||||
|
||||
// NewListSensorDataResponse 从模型数据创建列表响应 DTO
|
||||
func NewListSensorDataResponse(data []models.SensorData, total int64, page, pageSize int) *ListSensorDataResponse {
|
||||
dtos := make([]SensorDataDTO, len(data))
|
||||
for i, item := range data {
|
||||
dtos[i] = SensorDataDTO{
|
||||
Time: item.Time,
|
||||
DeviceID: item.DeviceID,
|
||||
RegionalControllerID: item.RegionalControllerID,
|
||||
SensorType: item.SensorType,
|
||||
Data: json.RawMessage(item.Data), // gorm.datatypes.JSON 是 []byte, 可直接转换为 json.RawMessage
|
||||
}
|
||||
}
|
||||
|
||||
return &ListSensorDataResponse{
|
||||
List: dtos,
|
||||
Pagination: PaginationDTO{
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user