From 4c6843afb442c38a11cce9ea731a33ad6d605f5e Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Sat, 18 Oct 2025 16:08:46 +0800 Subject: [PATCH] ListFeedUsageRecords --- .../controller/monitor/monitor_controller.go | 46 +++++++++++ internal/app/dto/monitor_dto.go | 78 +++++++++++++++++++ internal/app/service/monitor_service.go | 5 ++ .../repository/raw_material_repository.go | 54 +++++++++++++ 4 files changed, 183 insertions(+) diff --git a/internal/app/controller/monitor/monitor_controller.go b/internal/app/controller/monitor/monitor_controller.go index d810d97..f757987 100644 --- a/internal/app/controller/monitor/monitor_controller.go +++ b/internal/app/controller/monitor/monitor_controller.go @@ -407,3 +407,49 @@ func (c *Controller) ListRawMaterialStockLogs(ctx *gin.Context) { c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total) controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取原料库存日志成功", resp, actionType, "获取原料库存日志成功", req) } + +// ListFeedUsageRecords godoc +// @Summary 获取饲料使用记录列表 +// @Description 根据提供的过滤条件,分页获取饲料使用记录 +// @Tags 数据监控 +// @Security BearerAuth +// @Produce json +// @Param query query dto.ListFeedUsageRecordRequest true "查询参数" +// @Success 200 {object} controller.Response{data=dto.ListFeedUsageRecordResponse} +// @Router /api/v1/monitor/feed-usage-records [get] +func (c *Controller) ListFeedUsageRecords(ctx *gin.Context) { + const actionType = "获取饲料使用记录列表" + + var req dto.ListFeedUsageRecordRequest + 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.FeedUsageRecordListOptions{ + PenID: req.PenID, + FeedFormulaID: req.FeedFormulaID, + OperatorID: req.OperatorID, + OrderBy: req.OrderBy, + StartTime: req.StartTime, + EndTime: req.EndTime, + } + + data, total, err := c.monitorService.ListFeedUsageRecords(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.NewListFeedUsageRecordResponse(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 0d48cb6..458efc0 100644 --- a/internal/app/dto/monitor_dto.go +++ b/internal/app/dto/monitor_dto.go @@ -491,3 +491,81 @@ func NewListRawMaterialStockLogResponse(data []models.RawMaterialStockLog, total }, } } + +// --- FeedUsageRecord --- + +// ListFeedUsageRecordRequest 定义了获取饲料使用记录列表的请求参数 +type ListFeedUsageRecordRequest struct { + Page int `form:"page,default=1"` + PageSize int `form:"pageSize,default=10"` + PenID *uint `form:"pen_id"` + FeedFormulaID *uint `form:"feed_formula_id"` + 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"` +} + +// PenDTO 是用于API响应的简化版猪栏结构 +type PenDTO struct { + ID uint `json:"id"` + Name string `json:"name"` +} + +// FeedFormulaDTO 是用于API响应的简化版饲料配方结构 +type FeedFormulaDTO struct { + ID uint `json:"id"` + Name string `json:"name"` +} + +// FeedUsageRecordDTO 是用于API响应的饲料使用记录结构 +type FeedUsageRecordDTO struct { + ID uint `json:"id"` + PenID uint `json:"pen_id"` + Pen PenDTO `json:"pen"` + FeedFormulaID uint `json:"feed_formula_id"` + FeedFormula FeedFormulaDTO `json:"feed_formula"` + Amount float64 `json:"amount"` + RecordedAt time.Time `json:"recorded_at"` + OperatorID uint `json:"operator_id"` + Remarks string `json:"remarks"` +} + +// ListFeedUsageRecordResponse 是获取饲料使用记录列表的响应结构 +type ListFeedUsageRecordResponse struct { + List []FeedUsageRecordDTO `json:"list"` + 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, + }, + } +} diff --git a/internal/app/service/monitor_service.go b/internal/app/service/monitor_service.go index f1d33aa..985110c 100644 --- a/internal/app/service/monitor_service.go +++ b/internal/app/service/monitor_service.go @@ -74,3 +74,8 @@ func (s *MonitorService) ListRawMaterialPurchases(opts repository.RawMaterialPur func (s *MonitorService) ListRawMaterialStockLogs(opts repository.RawMaterialStockLogListOptions, page, pageSize int) ([]models.RawMaterialStockLog, int64, error) { return s.rawMaterialRepo.ListRawMaterialStockLogs(opts, page, pageSize) } + +// ListFeedUsageRecords 负责处理查询饲料使用记录列表的业务逻辑 +func (s *MonitorService) ListFeedUsageRecords(opts repository.FeedUsageRecordListOptions, page, pageSize int) ([]models.FeedUsageRecord, int64, error) { + return s.rawMaterialRepo.ListFeedUsageRecords(opts, page, pageSize) +} diff --git a/internal/infra/repository/raw_material_repository.go b/internal/infra/repository/raw_material_repository.go index 918ea85..1d6d230 100644 --- a/internal/infra/repository/raw_material_repository.go +++ b/internal/infra/repository/raw_material_repository.go @@ -26,10 +26,21 @@ type RawMaterialStockLogListOptions struct { OrderBy string // 例如 "happened_at asc" } +// FeedUsageRecordListOptions 定义了查询饲料使用记录时的可选参数 +type FeedUsageRecordListOptions struct { + PenID *uint + FeedFormulaID *uint + OperatorID *uint + StartTime *time.Time // 基于 recorded_at 字段 + EndTime *time.Time // 基于 recorded_at 字段 + OrderBy string // 例如 "recorded_at asc" +} + // RawMaterialRepository 定义了与原料相关的数据库操作接口 type RawMaterialRepository interface { ListRawMaterialPurchases(opts RawMaterialPurchaseListOptions, page, pageSize int) ([]models.RawMaterialPurchase, int64, error) ListRawMaterialStockLogs(opts RawMaterialStockLogListOptions, page, pageSize int) ([]models.RawMaterialStockLog, int64, error) + ListFeedUsageRecords(opts FeedUsageRecordListOptions, page, pageSize int) ([]models.FeedUsageRecord, int64, error) } // gormRawMaterialRepository 是 RawMaterialRepository 的 GORM 实现 @@ -124,3 +135,46 @@ func (r *gormRawMaterialRepository) ListRawMaterialStockLogs(opts RawMaterialSto return results, total, err } + +// ListFeedUsageRecords 实现了分页和过滤查询饲料使用记录的功能 +func (r *gormRawMaterialRepository) ListFeedUsageRecords(opts FeedUsageRecordListOptions, page, pageSize int) ([]models.FeedUsageRecord, int64, error) { + if page <= 0 || pageSize <= 0 { + return nil, 0, ErrInvalidPagination + } + + var results []models.FeedUsageRecord + var total int64 + + query := r.db.Model(&models.FeedUsageRecord{}) + + if opts.PenID != nil { + query = query.Where("pen_id = ?", *opts.PenID) + } + if opts.FeedFormulaID != nil { + query = query.Where("feed_formula_id = ?", *opts.FeedFormulaID) + } + if opts.OperatorID != nil { + query = query.Where("operator_id = ?", *opts.OperatorID) + } + if opts.StartTime != nil { + query = query.Where("recorded_at >= ?", *opts.StartTime) + } + if opts.EndTime != nil { + query = query.Where("recorded_at <= ?", *opts.EndTime) + } + + if err := query.Count(&total).Error; err != nil { + return nil, 0, err + } + + orderBy := "recorded_at DESC" + if opts.OrderBy != "" { + orderBy = opts.OrderBy + } + query = query.Order(orderBy).Preload("Pen").Preload("FeedFormula") + + offset := (page - 1) * pageSize + err := query.Limit(pageSize).Offset(offset).Find(&results).Error + + return results, total, err +}