diff --git a/internal/app/controller/monitor/monitor_controller.go b/internal/app/controller/monitor/monitor_controller.go index 60efb20..8e82632 100644 --- a/internal/app/controller/monitor/monitor_controller.go +++ b/internal/app/controller/monitor/monitor_controller.go @@ -642,3 +642,54 @@ func (c *Controller) ListWeighingRecords(ctx *gin.Context) { c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total) controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取单次称重记录成功", resp, actionType, "获取单次称重记录成功", req) } + +// ListPigTransferLogs godoc +// @Summary 获取猪只迁移日志列表 +// @Description 根据提供的过滤条件,分页获取猪只迁移日志 +// @Tags 数据监控 +// @Security BearerAuth +// @Produce json +// @Param query query dto.ListPigTransferLogRequest true "查询参数" +// @Success 200 {object} controller.Response{data=dto.ListPigTransferLogResponse} +// @Router /api/v1/monitor/pig-transfer-logs [get] +func (c *Controller) ListPigTransferLogs(ctx *gin.Context) { + const actionType = "获取猪只迁移日志列表" + + var req dto.ListPigTransferLogRequest + 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.PigTransferLogListOptions{ + PigBatchID: req.PigBatchID, + PenID: req.PenID, + OperatorID: req.OperatorID, + CorrelationID: req.CorrelationID, + OrderBy: req.OrderBy, + StartTime: req.StartTime, + EndTime: req.EndTime, + } + if req.TransferType != nil { + transferType := models.PigTransferType(*req.TransferType) + opts.TransferType = &transferType + } + + data, total, err := c.monitorService.ListPigTransferLogs(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.NewListPigTransferLogResponse(data, total, req.Page, req.PageSize) + c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total) + controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只迁移日志成功", resp, actionType, "获取猪只迁移日志成功", req) +} diff --git a/internal/app/dto/monitor_converter.go b/internal/app/dto/monitor_converter.go index 042acdc..628b8b9 100644 --- a/internal/app/dto/monitor_converter.go +++ b/internal/app/dto/monitor_converter.go @@ -362,3 +362,33 @@ func NewListWeighingRecordResponse(data []models.WeighingRecord, total int64, pa }, } } + +// NewListPigTransferLogResponse 从模型数据创建列表响应 DTO +func NewListPigTransferLogResponse(data []models.PigTransferLog, total int64, page, pageSize int) *ListPigTransferLogResponse { + dtos := make([]PigTransferLogDTO, len(data)) + for i, item := range data { + // 注意:PigTransferLog 的 ID, CreatedAt, UpdatedAt 字段是 gorm.Model 嵌入的 + dtos[i] = PigTransferLogDTO{ + ID: item.ID, + CreatedAt: item.CreatedAt, + UpdatedAt: item.UpdatedAt, + TransferTime: item.TransferTime, + PigBatchID: item.PigBatchID, + PenID: item.PenID, + Quantity: item.Quantity, + Type: item.Type, + CorrelationID: item.CorrelationID, + OperatorID: item.OperatorID, + Remarks: item.Remarks, + } + } + + return &ListPigTransferLogResponse{ + List: dtos, + Pagination: PaginationDTO{ + Total: total, + Page: page, + PageSize: pageSize, + }, + } +} diff --git a/internal/app/dto/monitor_dto.go b/internal/app/dto/monitor_dto.go index 86a8f15..3619c87 100644 --- a/internal/app/dto/monitor_dto.go +++ b/internal/app/dto/monitor_dto.go @@ -460,3 +460,40 @@ type ListWeighingRecordResponse struct { List []WeighingRecordDTO `json:"list"` Pagination PaginationDTO `json:"pagination"` } + +// --- PigTransferLog --- + +// ListPigTransferLogRequest 定义了获取猪只迁移日志列表的请求参数 +type ListPigTransferLogRequest struct { + Page int `form:"page,default=1"` + PageSize int `form:"pageSize,default=10"` + PigBatchID *uint `form:"pig_batch_id"` + PenID *uint `form:"pen_id"` + TransferType *string `form:"transfer_type"` + OperatorID *uint `form:"operator_id"` + CorrelationID *string `form:"correlation_id"` + StartTime *time.Time `form:"start_time" time_format:"rfc3339"` + EndTime *time.Time `form:"end_time" time_format:"rfc3339"` + OrderBy string `form:"order_by"` +} + +// PigTransferLogDTO 是用于API响应的猪只迁移日志结构 +type PigTransferLogDTO struct { + ID uint `json:"id"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + TransferTime time.Time `json:"transfer_time"` + PigBatchID uint `json:"pig_batch_id"` + PenID uint `json:"pen_id"` + Quantity int `json:"quantity"` + Type models.PigTransferType `json:"type"` + CorrelationID string `json:"correlation_id"` + OperatorID uint `json:"operator_id"` + Remarks string `json:"remarks"` +} + +// ListPigTransferLogResponse 是获取猪只迁移日志列表的响应结构 +type ListPigTransferLogResponse struct { + List []PigTransferLogDTO `json:"list"` + Pagination PaginationDTO `json:"pagination"` +} diff --git a/internal/app/service/monitor_service.go b/internal/app/service/monitor_service.go index 4dd21df..4a96625 100644 --- a/internal/app/service/monitor_service.go +++ b/internal/app/service/monitor_service.go @@ -16,6 +16,7 @@ type MonitorService struct { medicationRepo repository.MedicationLogRepository pigBatchRepo repository.PigBatchRepository pigBatchLogRepo repository.PigBatchLogRepository + pigTransferLogRepo repository.PigTransferLogRepository } // NewMonitorService 创建一个新的 MonitorService 实例 @@ -29,6 +30,7 @@ func NewMonitorService( medicationRepo repository.MedicationLogRepository, pigBatchRepo repository.PigBatchRepository, pigBatchLogRepo repository.PigBatchLogRepository, + pigTransferLogRepo repository.PigTransferLogRepository, ) *MonitorService { return &MonitorService{ sensorDataRepo: sensorDataRepo, @@ -40,6 +42,7 @@ func NewMonitorService( medicationRepo: medicationRepo, pigBatchRepo: pigBatchRepo, pigBatchLogRepo: pigBatchLogRepo, + pigTransferLogRepo: pigTransferLogRepo, } } @@ -107,3 +110,8 @@ func (s *MonitorService) ListWeighingBatches(opts repository.WeighingBatchListOp func (s *MonitorService) ListWeighingRecords(opts repository.WeighingRecordListOptions, page, pageSize int) ([]models.WeighingRecord, int64, error) { return s.pigBatchRepo.ListWeighingRecords(opts, page, pageSize) } + +// ListPigTransferLogs 负责处理查询猪只迁移日志列表的业务逻辑 +func (s *MonitorService) ListPigTransferLogs(opts repository.PigTransferLogListOptions, page, pageSize int) ([]models.PigTransferLog, int64, error) { + return s.pigTransferLogRepo.ListPigTransferLogs(opts, page, pageSize) +} diff --git a/internal/infra/repository/pig_transfer_log_repository.go b/internal/infra/repository/pig_transfer_log_repository.go index 4d934fe..9db57e1 100644 --- a/internal/infra/repository/pig_transfer_log_repository.go +++ b/internal/infra/repository/pig_transfer_log_repository.go @@ -7,6 +7,18 @@ import ( "gorm.io/gorm" ) +// PigTransferLogListOptions 定义了查询猪只迁移日志时的可选参数 +type PigTransferLogListOptions struct { + PigBatchID *uint + PenID *uint + TransferType *models.PigTransferType // 迁移类型 + OperatorID *uint + CorrelationID *string + StartTime *time.Time // 基于 transfer_time 字段 + EndTime *time.Time // 基于 transfer_time 字段 + OrderBy string // 例如 "transfer_time desc" +} + // PigTransferLogRepository 定义了猪只迁移日志数据持久化的接口。 type PigTransferLogRepository interface { // CreatePigTransferLog 在数据库中创建一条猪只迁移日志记录。 @@ -14,6 +26,9 @@ type PigTransferLogRepository interface { // GetLogsForPenSince 获取指定猪栏自特定时间点以来的所有迁移日志,按时间倒序排列。 GetLogsForPenSince(tx *gorm.DB, penID uint, since time.Time) ([]*models.PigTransferLog, error) + + // ListPigTransferLogs 支持分页和过滤的猪只迁移日志列表查询 + ListPigTransferLogs(opts PigTransferLogListOptions, page, pageSize int) ([]models.PigTransferLog, int64, error) } // gormPigTransferLogRepository 是 PigTransferLogRepository 接口的 GORM 实现。 @@ -37,3 +52,52 @@ func (r *gormPigTransferLogRepository) GetLogsForPenSince(tx *gorm.DB, penID uin err := tx.Where("pen_id = ? AND transfer_time >= ?", penID, since).Order("transfer_time DESC").Find(&logs).Error return logs, err } + +// ListPigTransferLogs 实现了分页和过滤查询猪只迁移日志的功能 +func (r *gormPigTransferLogRepository) ListPigTransferLogs(opts PigTransferLogListOptions, page, pageSize int) ([]models.PigTransferLog, int64, error) { + if page <= 0 || pageSize <= 0 { + return nil, 0, ErrInvalidPagination + } + + var results []models.PigTransferLog + var total int64 + + query := r.db.Model(&models.PigTransferLog{}) + + if opts.PigBatchID != nil { + query = query.Where("pig_batch_id = ?", *opts.PigBatchID) + } + if opts.PenID != nil { + query = query.Where("pen_id = ?", *opts.PenID) + } + if opts.TransferType != nil { + query = query.Where("type = ?", *opts.TransferType) + } + if opts.OperatorID != nil { + query = query.Where("operator_id = ?", *opts.OperatorID) + } + if opts.CorrelationID != nil { + query = query.Where("correlation_id = ?", *opts.CorrelationID) + } + if opts.StartTime != nil { + query = query.Where("transfer_time >= ?", *opts.StartTime) + } + if opts.EndTime != nil { + query = query.Where("transfer_time <= ?", *opts.EndTime) + } + + if err := query.Count(&total).Error; err != nil { + return nil, 0, err + } + + orderBy := "transfer_time 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 +} diff --git a/internal/infra/repository/sensor_data_repository.go b/internal/infra/repository/sensor_data_repository.go index 9212c7c..cc66db9 100644 --- a/internal/infra/repository/sensor_data_repository.go +++ b/internal/infra/repository/sensor_data_repository.go @@ -9,7 +9,7 @@ import ( ) // ErrInvalidPagination 表示分页参数无效 -var ErrInvalidPagination = errors.New("invalid pagination parameters: page and pageSize must be positive") +var ErrInvalidPagination = errors.New("无效的分页参数:page和pageSize必须为大于0") // SensorDataListOptions 定义了查询传感器数据列表时的可选参数 type SensorDataListOptions struct {