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) {
|
func (c *Controller) ListSensorData(ctx *gin.Context) {
|
||||||
const actionType = "获取传感器数据列表"
|
const actionType = "获取传感器数据列表"
|
||||||
|
|
||||||
// --- 绑定并校验查询参数 ---
|
|
||||||
var req dto.ListSensorDataRequest
|
var req dto.ListSensorDataRequest
|
||||||
if err := ctx.ShouldBindQuery(&req); err != nil {
|
if err := ctx.ShouldBindQuery(&req); err != nil {
|
||||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||||
@@ -46,7 +45,6 @@ func (c *Controller) ListSensorData(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- 将请求 DTO 转换为仓库层需要的 Options 结构体 ---
|
|
||||||
opts := repository.SensorDataListOptions{
|
opts := repository.SensorDataListOptions{
|
||||||
DeviceID: req.DeviceID,
|
DeviceID: req.DeviceID,
|
||||||
OrderBy: req.OrderBy,
|
OrderBy: req.OrderBy,
|
||||||
@@ -58,24 +56,65 @@ func (c *Controller) ListSensorData(ctx *gin.Context) {
|
|||||||
opts.SensorType = &sensorType
|
opts.SensorType = &sensorType
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- 调用服务层 ---
|
|
||||||
data, total, err := c.monitorService.ListSensorData(opts, req.Page, req.PageSize)
|
data, total, err := c.monitorService.ListSensorData(opts, req.Page, req.PageSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// --- 错误处理:区分参数错误和内部错误 ---
|
|
||||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 其他未知错误按内部错误处理
|
|
||||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取传感器数据失败: "+err.Error(), actionType, "服务层查询失败", req)
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取传感器数据失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- 构建并发送成功响应 ---
|
|
||||||
resp := dto.NewListSensorDataResponse(data, total, req.Page, req.PageSize)
|
resp := dto.NewListSensorDataResponse(data, total, req.Page, req.PageSize)
|
||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
||||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取传感器数据成功", resp, actionType, "获取传感器数据成功", req)
|
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"
|
"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 定义了获取传感器数据列表的请求参数
|
// ListSensorDataRequest 定义了获取传感器数据列表的请求参数
|
||||||
// 使用 form 标签以便 Gin 可以从查询字符串中绑定数据
|
|
||||||
type ListSensorDataRequest struct {
|
type ListSensorDataRequest struct {
|
||||||
Page int `form:"page,default=1"`
|
Page int `form:"page,default=1"`
|
||||||
PageSize int `form:"pageSize,default=10"`
|
PageSize int `form:"pageSize,default=10"`
|
||||||
@@ -25,14 +35,7 @@ type SensorDataDTO struct {
|
|||||||
DeviceID uint `json:"device_id"`
|
DeviceID uint `json:"device_id"`
|
||||||
RegionalControllerID uint `json:"regional_controller_id"`
|
RegionalControllerID uint `json:"regional_controller_id"`
|
||||||
SensorType models.SensorType `json:"sensor_type"`
|
SensorType models.SensorType `json:"sensor_type"`
|
||||||
Data json.RawMessage `json:"data"` // 使用 json.RawMessage 以便直接输出 JSON 内容
|
Data json.RawMessage `json:"data"`
|
||||||
}
|
|
||||||
|
|
||||||
// PaginationDTO 定义了分页信息的标准结构
|
|
||||||
type PaginationDTO struct {
|
|
||||||
Total int64 `json:"total"`
|
|
||||||
Page int `json:"page"`
|
|
||||||
PageSize int `json:"pageSize"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListSensorDataResponse 是获取传感器数据列表的响应结构
|
// ListSensorDataResponse 是获取传感器数据列表的响应结构
|
||||||
@@ -50,7 +53,7 @@ func NewListSensorDataResponse(data []models.SensorData, total int64, page, page
|
|||||||
DeviceID: item.DeviceID,
|
DeviceID: item.DeviceID,
|
||||||
RegionalControllerID: item.RegionalControllerID,
|
RegionalControllerID: item.RegionalControllerID,
|
||||||
SensorType: item.SensorType,
|
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,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,19 +8,27 @@ import (
|
|||||||
// MonitorService 定义了监控相关的业务逻辑服务
|
// MonitorService 定义了监控相关的业务逻辑服务
|
||||||
type MonitorService struct {
|
type MonitorService struct {
|
||||||
sensorDataRepo repository.SensorDataRepository
|
sensorDataRepo repository.SensorDataRepository
|
||||||
|
deviceCommandLogRepo repository.DeviceCommandLogRepository
|
||||||
// 在这里可以添加其他超表模型的仓库依赖
|
// 在这里可以添加其他超表模型的仓库依赖
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMonitorService 创建一个新的 MonitorService 实例
|
// NewMonitorService 创建一个新的 MonitorService 实例
|
||||||
func NewMonitorService(sensorDataRepo repository.SensorDataRepository) *MonitorService {
|
func NewMonitorService(
|
||||||
|
sensorDataRepo repository.SensorDataRepository,
|
||||||
|
deviceCommandLogRepo repository.DeviceCommandLogRepository,
|
||||||
|
) *MonitorService {
|
||||||
return &MonitorService{
|
return &MonitorService{
|
||||||
sensorDataRepo: sensorDataRepo,
|
sensorDataRepo: sensorDataRepo,
|
||||||
|
deviceCommandLogRepo: deviceCommandLogRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListSensorData 负责处理查询传感器数据列表的业务逻辑
|
// ListSensorData 负责处理查询传感器数据列表的业务逻辑
|
||||||
func (s *MonitorService) ListSensorData(opts repository.SensorDataListOptions, page, pageSize int) ([]models.SensorData, int64, error) {
|
func (s *MonitorService) ListSensorData(opts repository.SensorDataListOptions, page, pageSize int) ([]models.SensorData, int64, error) {
|
||||||
// 目前业务逻辑只是简单地将请求透传给仓库层。
|
|
||||||
// 未来任何与查询传感器数据相关的业务规则(如权限校验、数据聚合等)都应在此处实现。
|
|
||||||
return s.sensorDataRepo.List(opts, page, pageSize)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,13 +7,22 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DeviceCommandLogListOptions 定义了查询设备命令日志时的可选参数
|
||||||
|
type DeviceCommandLogListOptions struct {
|
||||||
|
DeviceID *uint
|
||||||
|
ReceivedSuccess *bool
|
||||||
|
StartTime *time.Time // 基于 sent_at 字段
|
||||||
|
EndTime *time.Time // 基于 sent_at 字段
|
||||||
|
OrderBy string // 例如 "sent_at asc"
|
||||||
|
}
|
||||||
|
|
||||||
// DeviceCommandLogRepository 定义了设备下行命令历史记录的数据访问接口
|
// DeviceCommandLogRepository 定义了设备下行命令历史记录的数据访问接口
|
||||||
type DeviceCommandLogRepository interface {
|
type DeviceCommandLogRepository interface {
|
||||||
Create(record *models.DeviceCommandLog) error
|
Create(record *models.DeviceCommandLog) error
|
||||||
FindByMessageID(messageID string) (*models.DeviceCommandLog, error)
|
FindByMessageID(messageID string) (*models.DeviceCommandLog, error)
|
||||||
// UpdateAcknowledgedAt 用于更新指定 MessageID 的下行命令记录的确认时间及接收成功状态。
|
|
||||||
// AcknowledgedAt 和 ReceivedSuccess 字段会被更新。
|
|
||||||
UpdateAcknowledgedAt(messageID string, acknowledgedAt time.Time, receivedSuccess bool) error
|
UpdateAcknowledgedAt(messageID string, acknowledgedAt time.Time, receivedSuccess bool) error
|
||||||
|
// List 支持分页和过滤的列表查询
|
||||||
|
List(opts DeviceCommandLogListOptions, page, pageSize int) ([]models.DeviceCommandLog, int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// gormDeviceCommandLogRepository 是 DeviceCommandLogRepository 接口的 GORM 实现
|
// gormDeviceCommandLogRepository 是 DeviceCommandLogRepository 接口的 GORM 实现
|
||||||
@@ -42,7 +51,6 @@ func (r *gormDeviceCommandLogRepository) FindByMessageID(messageID string) (*mod
|
|||||||
|
|
||||||
// UpdateAcknowledgedAt 实现 DeviceCommandLogRepository 接口的 UpdateAcknowledgedAt 方法
|
// UpdateAcknowledgedAt 实现 DeviceCommandLogRepository 接口的 UpdateAcknowledgedAt 方法
|
||||||
func (r *gormDeviceCommandLogRepository) UpdateAcknowledgedAt(messageID string, acknowledgedAt time.Time, receivedSuccess bool) error {
|
func (r *gormDeviceCommandLogRepository) UpdateAcknowledgedAt(messageID string, acknowledgedAt time.Time, receivedSuccess bool) error {
|
||||||
// 使用 Updates 方法更新指定字段
|
|
||||||
return r.db.Model(&models.DeviceCommandLog{}).
|
return r.db.Model(&models.DeviceCommandLog{}).
|
||||||
Where("message_id = ?", messageID).
|
Where("message_id = ?", messageID).
|
||||||
Updates(map[string]interface{}{
|
Updates(map[string]interface{}{
|
||||||
@@ -50,3 +58,48 @@ func (r *gormDeviceCommandLogRepository) UpdateAcknowledgedAt(messageID string,
|
|||||||
"received_success": receivedSuccess,
|
"received_success": receivedSuccess,
|
||||||
}).Error
|
}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List 实现了分页和过滤查询设备命令日志的功能
|
||||||
|
func (r *gormDeviceCommandLogRepository) List(opts DeviceCommandLogListOptions, page, pageSize int) ([]models.DeviceCommandLog, int64, error) {
|
||||||
|
// --- 校验分页参数 ---
|
||||||
|
if page <= 0 || pageSize <= 0 {
|
||||||
|
return nil, 0, ErrInvalidPagination
|
||||||
|
}
|
||||||
|
|
||||||
|
var results []models.DeviceCommandLog
|
||||||
|
var total int64
|
||||||
|
|
||||||
|
query := r.db.Model(&models.DeviceCommandLog{})
|
||||||
|
|
||||||
|
// --- 应用过滤条件 ---
|
||||||
|
if opts.DeviceID != nil {
|
||||||
|
query = query.Where("device_id = ?", *opts.DeviceID)
|
||||||
|
}
|
||||||
|
if opts.ReceivedSuccess != nil {
|
||||||
|
query = query.Where("received_success = ?", *opts.ReceivedSuccess)
|
||||||
|
}
|
||||||
|
if opts.StartTime != nil {
|
||||||
|
query = query.Where("sent_at >= ?", *opts.StartTime)
|
||||||
|
}
|
||||||
|
if opts.EndTime != nil {
|
||||||
|
query = query.Where("sent_at <= ?", *opts.EndTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- 计算总数 ---
|
||||||
|
if err := query.Count(&total).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- 应用排序条件 ---
|
||||||
|
orderBy := "sent_at DESC" // 默认排序
|
||||||
|
if opts.OrderBy != "" {
|
||||||
|
orderBy = opts.OrderBy
|
||||||
|
}
|
||||||
|
query = query.Order(orderBy)
|
||||||
|
|
||||||
|
// --- 分页 ---
|
||||||
|
offset := (page - 1) * pageSize
|
||||||
|
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
||||||
|
|
||||||
|
return results, total, err
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user