diff --git a/internal/app/controller/monitor/monitor_controller.go b/internal/app/controller/monitor/monitor_controller.go index 3b9ddd0..e012102 100644 --- a/internal/app/controller/monitor/monitor_controller.go +++ b/internal/app/controller/monitor/monitor_controller.go @@ -552,3 +552,47 @@ func (c *Controller) ListPigBatchLogs(ctx *gin.Context) { c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total) controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪批次日志成功", resp, actionType, "获取猪批次日志成功", req) } + +// ListWeighingBatches godoc +// @Summary 获取批次称重记录列表 +// @Description 根据提供的过滤条件,分页获取批次称重记录 +// @Tags 数据监控 +// @Security BearerAuth +// @Produce json +// @Param query query dto.ListWeighingBatchRequest true "查询参数" +// @Success 200 {object} controller.Response{data=dto.ListWeighingBatchResponse} +// @Router /api/v1/monitor/weighing-batches [get] +func (c *Controller) ListWeighingBatches(ctx *gin.Context) { + const actionType = "获取批次称重记录列表" + + var req dto.ListWeighingBatchRequest + 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.WeighingBatchListOptions{ + PigBatchID: req.PigBatchID, + OrderBy: req.OrderBy, + StartTime: req.StartTime, + EndTime: req.EndTime, + } + + data, total, err := c.monitorService.ListWeighingBatches(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.NewListWeighingBatchResponse(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 new file mode 100644 index 0000000..747d31a --- /dev/null +++ b/internal/app/dto/monitor_converter.go @@ -0,0 +1,337 @@ +package dto + +import ( + "encoding/json" + + "git.huangwc.com/pig/pig-farm-controller/internal/infra/models" +) + +// 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), + } + } + + return &ListSensorDataResponse{ + List: dtos, + Pagination: PaginationDTO{ + Total: total, + Page: page, + PageSize: pageSize, + }, + } +} + +// 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, + }, + } +} + +// NewListPlanExecutionLogResponse 从模型数据创建列表响应 DTO +func NewListPlanExecutionLogResponse(data []models.PlanExecutionLog, total int64, page, pageSize int) *ListPlanExecutionLogResponse { + dtos := make([]PlanExecutionLogDTO, len(data)) + for i, item := range data { + dtos[i] = PlanExecutionLogDTO{ + ID: item.ID, + CreatedAt: item.CreatedAt, + UpdatedAt: item.UpdatedAt, + PlanID: item.PlanID, + Status: item.Status, + StartedAt: item.StartedAt, + EndedAt: item.EndedAt, + Error: item.Error, + } + } + + return &ListPlanExecutionLogResponse{ + List: dtos, + Pagination: PaginationDTO{ + Total: total, + Page: page, + PageSize: pageSize, + }, + } +} + +// NewListTaskExecutionLogResponse 从模型数据创建列表响应 DTO +func NewListTaskExecutionLogResponse(data []models.TaskExecutionLog, total int64, page, pageSize int) *ListTaskExecutionLogResponse { + dtos := make([]TaskExecutionLogDTO, len(data)) + for i, item := range data { + dtos[i] = TaskExecutionLogDTO{ + ID: item.ID, + CreatedAt: item.CreatedAt, + UpdatedAt: item.UpdatedAt, + PlanExecutionLogID: item.PlanExecutionLogID, + TaskID: item.TaskID, + Task: TaskDTO{ + ID: uint(item.Task.ID), + Name: item.Task.Name, + Description: item.Task.Description, + }, + Status: item.Status, + Output: item.Output, + StartedAt: item.StartedAt, + EndedAt: item.EndedAt, + } + } + + return &ListTaskExecutionLogResponse{ + List: dtos, + Pagination: PaginationDTO{ + Total: total, + Page: page, + PageSize: pageSize, + }, + } +} + +// NewListPendingCollectionResponse 从模型数据创建列表响应 DTO +func NewListPendingCollectionResponse(data []models.PendingCollection, total int64, page, pageSize int) *ListPendingCollectionResponse { + dtos := make([]PendingCollectionDTO, len(data)) + for i, item := range data { + dtos[i] = PendingCollectionDTO{ + CorrelationID: item.CorrelationID, + DeviceID: item.DeviceID, + CommandMetadata: item.CommandMetadata, + Status: item.Status, + FulfilledAt: item.FulfilledAt, + CreatedAt: item.CreatedAt, + } + } + + return &ListPendingCollectionResponse{ + List: dtos, + Pagination: PaginationDTO{ + Total: total, + Page: page, + PageSize: pageSize, + }, + } +} + +// NewListUserActionLogResponse 从模型数据创建列表响应 DTO +func NewListUserActionLogResponse(data []models.UserActionLog, total int64, page, pageSize int) *ListUserActionLogResponse { + dtos := make([]UserActionLogDTO, len(data)) + for i, item := range data { + dtos[i] = UserActionLogDTO{ + ID: item.ID, + Time: item.Time, + UserID: item.UserID, + Username: item.Username, + SourceIP: item.SourceIP, + ActionType: item.ActionType, + TargetResource: json.RawMessage(item.TargetResource), + Description: item.Description, + Status: item.Status, + HTTPPath: item.HTTPPath, + HTTPMethod: item.HTTPMethod, + ResultDetails: item.ResultDetails, + } + } + + return &ListUserActionLogResponse{ + List: dtos, + Pagination: PaginationDTO{ + Total: total, + Page: page, + PageSize: pageSize, + }, + } +} + +// NewListRawMaterialPurchaseResponse 从模型数据创建列表响应 DTO +func NewListRawMaterialPurchaseResponse(data []models.RawMaterialPurchase, total int64, page, pageSize int) *ListRawMaterialPurchaseResponse { + dtos := make([]RawMaterialPurchaseDTO, len(data)) + for i, item := range data { + dtos[i] = RawMaterialPurchaseDTO{ + ID: item.ID, + RawMaterialID: item.RawMaterialID, + RawMaterial: RawMaterialDTO{ + ID: item.RawMaterial.ID, + Name: item.RawMaterial.Name, + }, + Supplier: item.Supplier, + Amount: item.Amount, + UnitPrice: item.UnitPrice, + TotalPrice: item.TotalPrice, + PurchaseDate: item.PurchaseDate, + CreatedAt: item.CreatedAt, + } + } + + return &ListRawMaterialPurchaseResponse{ + List: dtos, + Pagination: PaginationDTO{ + Total: total, + Page: page, + PageSize: pageSize, + }, + } +} + +// NewListRawMaterialStockLogResponse 从模型数据创建列表响应 DTO +func NewListRawMaterialStockLogResponse(data []models.RawMaterialStockLog, total int64, page, pageSize int) *ListRawMaterialStockLogResponse { + dtos := make([]RawMaterialStockLogDTO, len(data)) + for i, item := range data { + dtos[i] = RawMaterialStockLogDTO{ + ID: item.ID, + RawMaterialID: item.RawMaterialID, + ChangeAmount: item.ChangeAmount, + SourceType: item.SourceType, + SourceID: item.SourceID, + HappenedAt: item.HappenedAt, + Remarks: item.Remarks, + } + } + + return &ListRawMaterialStockLogResponse{ + List: dtos, + Pagination: PaginationDTO{ + Total: total, + Page: page, + PageSize: pageSize, + }, + } +} + +// NewListFeedUsageRecordResponse 从模型数据创建列表响应 DTO +func NewListFeedUsageRecordResponse(data []models.FeedUsageRecord, total int64, page, pageSize int) *ListFeedUsageRecordResponse { + dtos := make([]FeedUsageRecordDTO, len(data)) + for i, item := range data { + dtos[i] = FeedUsageRecordDTO{ + ID: item.ID, + PenID: item.PenID, + Pen: PenDTO{ + ID: item.Pen.ID, + Name: item.Pen.PenNumber, + }, + FeedFormulaID: item.FeedFormulaID, + FeedFormula: FeedFormulaDTO{ + ID: item.FeedFormula.ID, + Name: item.FeedFormula.Name, + }, + Amount: item.Amount, + RecordedAt: item.RecordedAt, + OperatorID: item.OperatorID, + Remarks: item.Remarks, + } + } + + return &ListFeedUsageRecordResponse{ + List: dtos, + Pagination: PaginationDTO{ + Total: total, + Page: page, + PageSize: pageSize, + }, + } +} + +// NewListMedicationLogResponse 从模型数据创建列表响应 DTO +func NewListMedicationLogResponse(data []models.MedicationLog, total int64, page, pageSize int) *ListMedicationLogResponse { + dtos := make([]MedicationLogDTO, len(data)) + for i, item := range data { + dtos[i] = MedicationLogDTO{ + ID: item.ID, + PigBatchID: item.PigBatchID, + MedicationID: item.MedicationID, + Medication: MedicationDTO{ + ID: item.Medication.ID, + Name: item.Medication.Name, + }, + DosageUsed: item.DosageUsed, + TargetCount: item.TargetCount, + Reason: item.Reason, + Description: item.Description, + OperatorID: item.OperatorID, + HappenedAt: item.HappenedAt, + } + } + + return &ListMedicationLogResponse{ + List: dtos, + Pagination: PaginationDTO{ + Total: total, + Page: page, + PageSize: pageSize, + }, + } +} + +// NewListPigBatchLogResponse 从模型数据创建列表响应 DTO +func NewListPigBatchLogResponse(data []models.PigBatchLog, total int64, page, pageSize int) *ListPigBatchLogResponse { + dtos := make([]PigBatchLogDTO, len(data)) + for i, item := range data { + dtos[i] = PigBatchLogDTO{ + ID: item.ID, + CreatedAt: item.CreatedAt, + UpdatedAt: item.UpdatedAt, + PigBatchID: item.PigBatchID, + ChangeType: item.ChangeType, + ChangeCount: item.ChangeCount, + Reason: item.Reason, + BeforeCount: item.BeforeCount, + AfterCount: item.AfterCount, + OperatorID: item.OperatorID, + HappenedAt: item.HappenedAt, + } + } + + return &ListPigBatchLogResponse{ + List: dtos, + Pagination: PaginationDTO{ + Total: total, + Page: page, + PageSize: pageSize, + }, + } +} + +// NewListWeighingBatchResponse 从模型数据创建列表响应 DTO +func NewListWeighingBatchResponse(data []models.WeighingBatch, total int64, page, pageSize int) *ListWeighingBatchResponse { + dtos := make([]WeighingBatchDTO, len(data)) + for i, item := range data { + dtos[i] = WeighingBatchDTO{ + ID: item.ID, + CreatedAt: item.CreatedAt, + UpdatedAt: item.UpdatedAt, + WeighingTime: item.WeighingTime, + Description: item.Description, + PigBatchID: item.PigBatchID, + } + } + + return &ListWeighingBatchResponse{ + 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 3549f19..b4060f3 100644 --- a/internal/app/dto/monitor_dto.go +++ b/internal/app/dto/monitor_dto.go @@ -44,29 +44,6 @@ type ListSensorDataResponse struct { 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), - } - } - - return &ListSensorDataResponse{ - List: dtos, - Pagination: PaginationDTO{ - Total: total, - Page: page, - PageSize: pageSize, - }, - } -} - // --- DeviceCommandLog --- // ListDeviceCommandLogRequest 定义了获取设备命令日志列表的请求参数 @@ -95,29 +72,6 @@ type ListDeviceCommandLogResponse struct { 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, - }, - } -} - // --- PlanExecutionLog --- // ListPlanExecutionLogRequest 定义了获取计划执行日志列表的请求参数 @@ -149,32 +103,6 @@ type ListPlanExecutionLogResponse struct { Pagination PaginationDTO `json:"pagination"` } -// NewListPlanExecutionLogResponse 从模型数据创建列表响应 DTO -func NewListPlanExecutionLogResponse(data []models.PlanExecutionLog, total int64, page, pageSize int) *ListPlanExecutionLogResponse { - dtos := make([]PlanExecutionLogDTO, len(data)) - for i, item := range data { - dtos[i] = PlanExecutionLogDTO{ - ID: item.ID, - CreatedAt: item.CreatedAt, - UpdatedAt: item.UpdatedAt, - PlanID: item.PlanID, - Status: item.Status, - StartedAt: item.StartedAt, - EndedAt: item.EndedAt, - Error: item.Error, - } - } - - return &ListPlanExecutionLogResponse{ - List: dtos, - Pagination: PaginationDTO{ - Total: total, - Page: page, - PageSize: pageSize, - }, - } -} - // --- TaskExecutionLog --- // ListTaskExecutionLogRequest 定义了获取任务执行日志列表的请求参数 @@ -216,38 +144,6 @@ type ListTaskExecutionLogResponse struct { Pagination PaginationDTO `json:"pagination"` } -// NewListTaskExecutionLogResponse 从模型数据创建列表响应 DTO -func NewListTaskExecutionLogResponse(data []models.TaskExecutionLog, total int64, page, pageSize int) *ListTaskExecutionLogResponse { - dtos := make([]TaskExecutionLogDTO, len(data)) - for i, item := range data { - dtos[i] = TaskExecutionLogDTO{ - ID: item.ID, - CreatedAt: item.CreatedAt, - UpdatedAt: item.UpdatedAt, - PlanExecutionLogID: item.PlanExecutionLogID, - TaskID: item.TaskID, - Task: TaskDTO{ - ID: uint(item.Task.ID), - Name: item.Task.Name, - Description: item.Task.Description, - }, - Status: item.Status, - Output: item.Output, - StartedAt: item.StartedAt, - EndedAt: item.EndedAt, - } - } - - return &ListTaskExecutionLogResponse{ - List: dtos, - Pagination: PaginationDTO{ - Total: total, - Page: page, - PageSize: pageSize, - }, - } -} - // --- PendingCollection --- // ListPendingCollectionRequest 定义了获取待采集请求列表的请求参数 @@ -277,30 +173,6 @@ type ListPendingCollectionResponse struct { Pagination PaginationDTO `json:"pagination"` } -// NewListPendingCollectionResponse 从模型数据创建列表响应 DTO -func NewListPendingCollectionResponse(data []models.PendingCollection, total int64, page, pageSize int) *ListPendingCollectionResponse { - dtos := make([]PendingCollectionDTO, len(data)) - for i, item := range data { - dtos[i] = PendingCollectionDTO{ - CorrelationID: item.CorrelationID, - DeviceID: item.DeviceID, - CommandMetadata: item.CommandMetadata, - Status: item.Status, - FulfilledAt: item.FulfilledAt, - CreatedAt: item.CreatedAt, - } - } - - return &ListPendingCollectionResponse{ - List: dtos, - Pagination: PaginationDTO{ - Total: total, - Page: page, - PageSize: pageSize, - }, - } -} - // --- UserActionLog --- // ListUserActionLogRequest 定义了获取用户操作日志列表的请求参数 @@ -338,36 +210,6 @@ type ListUserActionLogResponse struct { Pagination PaginationDTO `json:"pagination"` } -// NewListUserActionLogResponse 从模型数据创建列表响应 DTO -func NewListUserActionLogResponse(data []models.UserActionLog, total int64, page, pageSize int) *ListUserActionLogResponse { - dtos := make([]UserActionLogDTO, len(data)) - for i, item := range data { - dtos[i] = UserActionLogDTO{ - ID: item.ID, - Time: item.Time, - UserID: item.UserID, - Username: item.Username, - SourceIP: item.SourceIP, - ActionType: item.ActionType, - TargetResource: json.RawMessage(item.TargetResource), - Description: item.Description, - Status: item.Status, - HTTPPath: item.HTTPPath, - HTTPMethod: item.HTTPMethod, - ResultDetails: item.ResultDetails, - } - } - - return &ListUserActionLogResponse{ - List: dtos, - Pagination: PaginationDTO{ - Total: total, - Page: page, - PageSize: pageSize, - }, - } -} - // --- RawMaterialPurchase --- // ListRawMaterialPurchaseRequest 定义了获取原料采购列表的请求参数 @@ -406,36 +248,6 @@ type ListRawMaterialPurchaseResponse struct { Pagination PaginationDTO `json:"pagination"` } -// NewListRawMaterialPurchaseResponse 从模型数据创建列表响应 DTO -func NewListRawMaterialPurchaseResponse(data []models.RawMaterialPurchase, total int64, page, pageSize int) *ListRawMaterialPurchaseResponse { - dtos := make([]RawMaterialPurchaseDTO, len(data)) - for i, item := range data { - dtos[i] = RawMaterialPurchaseDTO{ - ID: item.ID, - RawMaterialID: item.RawMaterialID, - RawMaterial: RawMaterialDTO{ - ID: item.RawMaterial.ID, - Name: item.RawMaterial.Name, - }, - Supplier: item.Supplier, - Amount: item.Amount, - UnitPrice: item.UnitPrice, - TotalPrice: item.TotalPrice, - PurchaseDate: item.PurchaseDate, - CreatedAt: item.CreatedAt, - } - } - - return &ListRawMaterialPurchaseResponse{ - List: dtos, - Pagination: PaginationDTO{ - Total: total, - Page: page, - PageSize: pageSize, - }, - } -} - // --- RawMaterialStockLog --- // ListRawMaterialStockLogRequest 定义了获取原料库存日志列表的请求参数 @@ -467,31 +279,6 @@ type ListRawMaterialStockLogResponse struct { Pagination PaginationDTO `json:"pagination"` } -// NewListRawMaterialStockLogResponse 从模型数据创建列表响应 DTO -func NewListRawMaterialStockLogResponse(data []models.RawMaterialStockLog, total int64, page, pageSize int) *ListRawMaterialStockLogResponse { - dtos := make([]RawMaterialStockLogDTO, len(data)) - for i, item := range data { - dtos[i] = RawMaterialStockLogDTO{ - ID: item.ID, - RawMaterialID: item.RawMaterialID, - ChangeAmount: item.ChangeAmount, - SourceType: item.SourceType, - SourceID: item.SourceID, - HappenedAt: item.HappenedAt, - Remarks: item.Remarks, - } - } - - return &ListRawMaterialStockLogResponse{ - List: dtos, - Pagination: PaginationDTO{ - Total: total, - Page: page, - PageSize: pageSize, - }, - } -} - // --- FeedUsageRecord --- // ListFeedUsageRecordRequest 定义了获取饲料使用记录列表的请求参数 @@ -537,39 +324,6 @@ type ListFeedUsageRecordResponse struct { Pagination PaginationDTO `json:"pagination"` } -// NewListFeedUsageRecordResponse 从模型数据创建列表响应 DTO -func NewListFeedUsageRecordResponse(data []models.FeedUsageRecord, total int64, page, pageSize int) *ListFeedUsageRecordResponse { - dtos := make([]FeedUsageRecordDTO, len(data)) - for i, item := range data { - dtos[i] = FeedUsageRecordDTO{ - ID: item.ID, - PenID: item.PenID, - Pen: PenDTO{ - ID: item.Pen.ID, - Name: item.Pen.PenNumber, - }, - FeedFormulaID: item.FeedFormulaID, - FeedFormula: FeedFormulaDTO{ - ID: item.FeedFormula.ID, - Name: item.FeedFormula.Name, - }, - Amount: item.Amount, - RecordedAt: item.RecordedAt, - OperatorID: item.OperatorID, - Remarks: item.Remarks, - } - } - - return &ListFeedUsageRecordResponse{ - List: dtos, - Pagination: PaginationDTO{ - Total: total, - Page: page, - PageSize: pageSize, - }, - } -} - // --- MedicationLog --- // ListMedicationLogRequest 定义了获取用药记录列表的请求参数 @@ -611,37 +365,6 @@ type ListMedicationLogResponse struct { Pagination PaginationDTO `json:"pagination"` } -// NewListMedicationLogResponse 从模型数据创建列表响应 DTO -func NewListMedicationLogResponse(data []models.MedicationLog, total int64, page, pageSize int) *ListMedicationLogResponse { - dtos := make([]MedicationLogDTO, len(data)) - for i, item := range data { - dtos[i] = MedicationLogDTO{ - ID: item.ID, - PigBatchID: item.PigBatchID, - MedicationID: item.MedicationID, - Medication: MedicationDTO{ - ID: item.Medication.ID, - Name: item.Medication.Name, - }, - DosageUsed: item.DosageUsed, - TargetCount: item.TargetCount, - Reason: item.Reason, - Description: item.Description, - OperatorID: item.OperatorID, - HappenedAt: item.HappenedAt, - } - } - - return &ListMedicationLogResponse{ - List: dtos, - Pagination: PaginationDTO{ - Total: total, - Page: page, - PageSize: pageSize, - }, - } -} - // --- PigBatchLog --- // ListPigBatchLogRequest 定义了获取猪批次日志列表的请求参数 @@ -677,31 +400,30 @@ type ListPigBatchLogResponse struct { Pagination PaginationDTO `json:"pagination"` } -// NewListPigBatchLogResponse 从模型数据创建列表响应 DTO -func NewListPigBatchLogResponse(data []models.PigBatchLog, total int64, page, pageSize int) *ListPigBatchLogResponse { - dtos := make([]PigBatchLogDTO, len(data)) - for i, item := range data { - dtos[i] = PigBatchLogDTO{ - ID: item.ID, - CreatedAt: item.CreatedAt, - UpdatedAt: item.UpdatedAt, - PigBatchID: item.PigBatchID, - ChangeType: item.ChangeType, - ChangeCount: item.ChangeCount, - Reason: item.Reason, - BeforeCount: item.BeforeCount, - AfterCount: item.AfterCount, - OperatorID: item.OperatorID, - HappenedAt: item.HappenedAt, - } - } +// --- WeighingBatch --- - return &ListPigBatchLogResponse{ - List: dtos, - Pagination: PaginationDTO{ - Total: total, - Page: page, - PageSize: pageSize, - }, - } +// ListWeighingBatchRequest 定义了获取批次称重记录列表的请求参数 +type ListWeighingBatchRequest struct { + Page int `form:"page,default=1"` + PageSize int `form:"pageSize,default=10"` + PigBatchID *uint `form:"pig_batch_id"` + StartTime *time.Time `form:"start_time" time_format:"rfc3339"` + EndTime *time.Time `form:"end_time" time_format:"rfc3339"` + OrderBy string `form:"order_by"` +} + +// WeighingBatchDTO 是用于API响应的批次称重记录结构 +type WeighingBatchDTO struct { + ID uint `json:"id"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + WeighingTime time.Time `json:"weighing_time"` + Description string `json:"description"` + PigBatchID uint `json:"pig_batch_id"` +} + +// ListWeighingBatchResponse 是获取批次称重记录列表的响应结构 +type ListWeighingBatchResponse struct { + List []WeighingBatchDTO `json:"list"` + Pagination PaginationDTO `json:"pagination"` } diff --git a/internal/app/service/monitor_service.go b/internal/app/service/monitor_service.go index c385a35..59dd3c8 100644 --- a/internal/app/service/monitor_service.go +++ b/internal/app/service/monitor_service.go @@ -14,6 +14,7 @@ type MonitorService struct { userActionLogRepo repository.UserActionLogRepository rawMaterialRepo repository.RawMaterialRepository medicationRepo repository.MedicationRepository + pigBatchRepo repository.PigBatchRepository pigBatchLogRepo repository.PigBatchLogRepository // 在这里可以添加其他超表模型的仓库依赖 } @@ -27,6 +28,7 @@ func NewMonitorService( userActionLogRepo repository.UserActionLogRepository, rawMaterialRepo repository.RawMaterialRepository, medicationRepo repository.MedicationRepository, + pigBatchRepo repository.PigBatchRepository, pigBatchLogRepo repository.PigBatchLogRepository, ) *MonitorService { return &MonitorService{ @@ -37,6 +39,7 @@ func NewMonitorService( userActionLogRepo: userActionLogRepo, rawMaterialRepo: rawMaterialRepo, medicationRepo: medicationRepo, + pigBatchRepo: pigBatchRepo, pigBatchLogRepo: pigBatchLogRepo, } } @@ -95,3 +98,8 @@ func (s *MonitorService) ListMedicationLogs(opts repository.MedicationLogListOpt func (s *MonitorService) ListPigBatchLogs(opts repository.PigBatchLogListOptions, page, pageSize int) ([]models.PigBatchLog, int64, error) { return s.pigBatchLogRepo.List(opts, page, pageSize) } + +// ListWeighingBatches 负责处理查询批次称重记录列表的业务逻辑 +func (s *MonitorService) ListWeighingBatches(opts repository.WeighingBatchListOptions, page, pageSize int) ([]models.WeighingBatch, int64, error) { + return s.pigBatchRepo.ListWeighingBatches(opts, page, pageSize) +} diff --git a/internal/infra/repository/pig_batch_repository.go b/internal/infra/repository/pig_batch_repository.go index 1c46c5a..8f08975 100644 --- a/internal/infra/repository/pig_batch_repository.go +++ b/internal/infra/repository/pig_batch_repository.go @@ -1,6 +1,8 @@ package repository import ( + "time" + "git.huangwc.com/pig/pig-farm-controller/internal/infra/models" "gorm.io/gorm" ) @@ -17,6 +19,17 @@ type PigBatchRepository interface { DeletePigBatch(id uint) (int64, error) DeletePigBatchTx(tx *gorm.DB, id uint) (int64, error) ListPigBatches(isActive *bool) ([]*models.PigBatch, error) + + // ListWeighingBatches 支持分页和过滤的批次称重列表查询 + ListWeighingBatches(opts WeighingBatchListOptions, page, pageSize int) ([]models.WeighingBatch, int64, error) +} + +// WeighingBatchListOptions 定义了查询批次称重记录时的可选参数 +type WeighingBatchListOptions struct { + PigBatchID *uint + StartTime *time.Time // 基于 weighing_time 字段 + EndTime *time.Time // 基于 weighing_time 字段 + OrderBy string // 例如 "weighing_time asc" } // gormPigBatchRepository 是 PigBatchRepository 的 GORM 实现 @@ -100,3 +113,40 @@ func (r *gormPigBatchRepository) GetPigBatchByIDTx(tx *gorm.DB, id uint) (*model } return &batch, nil } + +// ListWeighingBatches 实现了分页和过滤查询批次称重记录的功能 +func (r *gormPigBatchRepository) ListWeighingBatches(opts WeighingBatchListOptions, page, pageSize int) ([]models.WeighingBatch, int64, error) { + if page <= 0 || pageSize <= 0 { + return nil, 0, ErrInvalidPagination + } + + var results []models.WeighingBatch + var total int64 + + query := r.db.Model(&models.WeighingBatch{}) + + if opts.PigBatchID != nil { + query = query.Where("pig_batch_id = ?", *opts.PigBatchID) + } + if opts.StartTime != nil { + query = query.Where("weighing_time >= ?", *opts.StartTime) + } + if opts.EndTime != nil { + query = query.Where("weighing_time <= ?", *opts.EndTime) + } + + if err := query.Count(&total).Error; err != nil { + return nil, 0, err + } + + orderBy := "weighing_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 +}