ListFeedUsageRecords
This commit is contained in:
@@ -407,3 +407,49 @@ func (c *Controller) ListRawMaterialStockLogs(ctx *gin.Context) {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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) {
|
func (s *MonitorService) ListRawMaterialStockLogs(opts repository.RawMaterialStockLogListOptions, page, pageSize int) ([]models.RawMaterialStockLog, int64, error) {
|
||||||
return s.rawMaterialRepo.ListRawMaterialStockLogs(opts, page, pageSize)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -26,10 +26,21 @@ type RawMaterialStockLogListOptions struct {
|
|||||||
OrderBy string // 例如 "happened_at asc"
|
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 定义了与原料相关的数据库操作接口
|
// RawMaterialRepository 定义了与原料相关的数据库操作接口
|
||||||
type RawMaterialRepository interface {
|
type RawMaterialRepository interface {
|
||||||
ListRawMaterialPurchases(opts RawMaterialPurchaseListOptions, page, pageSize int) ([]models.RawMaterialPurchase, int64, error)
|
ListRawMaterialPurchases(opts RawMaterialPurchaseListOptions, page, pageSize int) ([]models.RawMaterialPurchase, int64, error)
|
||||||
ListRawMaterialStockLogs(opts RawMaterialStockLogListOptions, page, pageSize int) ([]models.RawMaterialStockLog, 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 实现
|
// gormRawMaterialRepository 是 RawMaterialRepository 的 GORM 实现
|
||||||
@@ -124,3 +135,46 @@ func (r *gormRawMaterialRepository) ListRawMaterialStockLogs(opts RawMaterialSto
|
|||||||
|
|
||||||
return results, total, err
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user