diff --git a/internal/app/controller/monitor/monitor_controller.go b/internal/app/controller/monitor/monitor_controller.go index f757987..02d57ca 100644 --- a/internal/app/controller/monitor/monitor_controller.go +++ b/internal/app/controller/monitor/monitor_controller.go @@ -453,3 +453,53 @@ func (c *Controller) ListFeedUsageRecords(ctx *gin.Context) { c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total) controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取饲料使用记录成功", resp, actionType, "获取饲料使用记录成功", req) } + +// ListMedicationLogs godoc +// @Summary 获取用药记录列表 +// @Description 根据提供的过滤条件,分页获取用药记录 +// @Tags 数据监控 +// @Security BearerAuth +// @Produce json +// @Param query query dto.ListMedicationLogRequest true "查询参数" +// @Success 200 {object} controller.Response{data=dto.ListMedicationLogResponse} +// @Router /api/v1/monitor/medication-logs [get] +func (c *Controller) ListMedicationLogs(ctx *gin.Context) { + const actionType = "获取用药记录列表" + + var req dto.ListMedicationLogRequest + 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.MedicationLogListOptions{ + PigBatchID: req.PigBatchID, + MedicationID: req.MedicationID, + OperatorID: req.OperatorID, + OrderBy: req.OrderBy, + StartTime: req.StartTime, + EndTime: req.EndTime, + } + if req.Reason != nil { + reason := models.MedicationReasonType(*req.Reason) + opts.Reason = &reason + } + + data, total, err := c.monitorService.ListMedicationLogs(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.NewListMedicationLogResponse(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_dto.go b/internal/app/dto/monitor_dto.go index 458efc0..704ff8b 100644 --- a/internal/app/dto/monitor_dto.go +++ b/internal/app/dto/monitor_dto.go @@ -191,7 +191,7 @@ type ListTaskExecutionLogRequest struct { // TaskDTO 是用于API响应的简化版任务结构 type TaskDTO struct { - ID uint `json:"id"` + ID int `json:"id"` Name string `json:"name"` Description string `json:"description"` } @@ -227,7 +227,7 @@ func NewListTaskExecutionLogResponse(data []models.TaskExecutionLog, total int64 PlanExecutionLogID: item.PlanExecutionLogID, TaskID: item.TaskID, Task: TaskDTO{ - ID: uint(item.Task.ID), + ID: item.Task.ID, Name: item.Task.Name, Description: item.Task.Description, }, @@ -569,3 +569,75 @@ func NewListFeedUsageRecordResponse(data []models.FeedUsageRecord, total int64, }, } } + +// --- MedicationLog --- + +// ListMedicationLogRequest 定义了获取用药记录列表的请求参数 +type ListMedicationLogRequest struct { + Page int `form:"page,default=1"` + PageSize int `form:"pageSize,default=10"` + PigBatchID *uint `form:"pig_batch_id"` + MedicationID *uint `form:"medication_id"` + Reason *string `form:"reason"` + OperatorID *uint `form:"operator_id"` + StartTime *time.Time `form:"start_time" time_format:"rfc3339"` + EndTime *time.Time `form:"end_time" time_format:"rfc3339"` + OrderBy string `form:"order_by"` +} + +// MedicationDTO 是用于API响应的简化版药品结构 +type MedicationDTO struct { + ID uint `json:"id"` + Name string `json:"name"` +} + +// MedicationLogDTO 是用于API响应的用药记录结构 +type MedicationLogDTO struct { + ID uint `json:"id"` + PigBatchID uint `json:"pig_batch_id"` + MedicationID uint `json:"medication_id"` + Medication MedicationDTO `json:"medication"` + DosageUsed float64 `json:"dosage_used"` + TargetCount int `json:"target_count"` + Reason models.MedicationReasonType `json:"reason"` + Description string `json:"description"` + OperatorID uint `json:"operator_id"` + HappenedAt time.Time `json:"happened_at"` +} + +// ListMedicationLogResponse 是获取用药记录列表的响应结构 +type ListMedicationLogResponse struct { + List []MedicationLogDTO `json:"list"` + 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, + }, + } +} diff --git a/internal/app/service/monitor_service.go b/internal/app/service/monitor_service.go index 985110c..b279321 100644 --- a/internal/app/service/monitor_service.go +++ b/internal/app/service/monitor_service.go @@ -13,6 +13,7 @@ type MonitorService struct { pendingCollectionRepo repository.PendingCollectionRepository userActionLogRepo repository.UserActionLogRepository rawMaterialRepo repository.RawMaterialRepository + medicationRepo repository.MedicationRepository // 在这里可以添加其他超表模型的仓库依赖 } @@ -24,6 +25,7 @@ func NewMonitorService( pendingCollectionRepo repository.PendingCollectionRepository, userActionLogRepo repository.UserActionLogRepository, rawMaterialRepo repository.RawMaterialRepository, + medicationRepo repository.MedicationRepository, ) *MonitorService { return &MonitorService{ sensorDataRepo: sensorDataRepo, @@ -32,6 +34,7 @@ func NewMonitorService( pendingCollectionRepo: pendingCollectionRepo, userActionLogRepo: userActionLogRepo, rawMaterialRepo: rawMaterialRepo, + medicationRepo: medicationRepo, } } @@ -79,3 +82,8 @@ func (s *MonitorService) ListRawMaterialStockLogs(opts repository.RawMaterialSto func (s *MonitorService) ListFeedUsageRecords(opts repository.FeedUsageRecordListOptions, page, pageSize int) ([]models.FeedUsageRecord, int64, error) { return s.rawMaterialRepo.ListFeedUsageRecords(opts, page, pageSize) } + +// ListMedicationLogs 负责处理查询用药记录列表的业务逻辑 +func (s *MonitorService) ListMedicationLogs(opts repository.MedicationLogListOptions, page, pageSize int) ([]models.MedicationLog, int64, error) { + return s.medicationRepo.ListMedicationLogs(opts, page, pageSize) +} diff --git a/internal/infra/repository/medication_repository.go b/internal/infra/repository/medication_repository.go new file mode 100644 index 0000000..cbc4b73 --- /dev/null +++ b/internal/infra/repository/medication_repository.go @@ -0,0 +1,80 @@ +package repository + +import ( + "time" + + "git.huangwc.com/pig/pig-farm-controller/internal/infra/models" + "gorm.io/gorm" +) + +// MedicationLogListOptions 定义了查询用药记录时的可选参数 +type MedicationLogListOptions struct { + PigBatchID *uint + MedicationID *uint + Reason *models.MedicationReasonType + OperatorID *uint + StartTime *time.Time // 基于 happened_at 字段 + EndTime *time.Time // 基于 happened_at 字段 + OrderBy string // 例如 "happened_at asc" +} + +// MedicationRepository 定义了与药品相关的数据库操作接口 +type MedicationRepository interface { + ListMedicationLogs(opts MedicationLogListOptions, page, pageSize int) ([]models.MedicationLog, int64, error) +} + +// gormMedicationRepository 是 MedicationRepository 的 GORM 实现 +type gormMedicationRepository struct { + db *gorm.DB +} + +// NewGormMedicationRepository 创建一个新的 MedicationRepository GORM 实现 +func NewGormMedicationRepository(db *gorm.DB) MedicationRepository { + return &gormMedicationRepository{db: db} +} + +// ListMedicationLogs 实现了分页和过滤查询用药记录的功能 +func (r *gormMedicationRepository) ListMedicationLogs(opts MedicationLogListOptions, page, pageSize int) ([]models.MedicationLog, int64, error) { + if page <= 0 || pageSize <= 0 { + return nil, 0, ErrInvalidPagination + } + + var results []models.MedicationLog + var total int64 + + query := r.db.Model(&models.MedicationLog{}) + + if opts.PigBatchID != nil { + query = query.Where("pig_batch_id = ?", *opts.PigBatchID) + } + if opts.MedicationID != nil { + query = query.Where("medication_id = ?", *opts.MedicationID) + } + if opts.Reason != nil { + query = query.Where("reason = ?", *opts.Reason) + } + if opts.OperatorID != nil { + query = query.Where("operator_id = ?", *opts.OperatorID) + } + if opts.StartTime != nil { + query = query.Where("happened_at >= ?", *opts.StartTime) + } + if opts.EndTime != nil { + query = query.Where("happened_at <= ?", *opts.EndTime) + } + + if err := query.Count(&total).Error; err != nil { + return nil, 0, err + } + + orderBy := "happened_at DESC" + if opts.OrderBy != "" { + orderBy = opts.OrderBy + } + query = query.Order(orderBy).Preload("Medication") + + offset := (page - 1) * pageSize + err := query.Limit(pageSize).Offset(offset).Find(&results).Error + + return results, total, err +}