ListRawMaterialStockLogs

This commit is contained in:
2025-10-18 16:04:54 +08:00
parent 7299c8ebe6
commit eb0786ca27
4 changed files with 164 additions and 1 deletions

View File

@@ -358,3 +358,52 @@ func (c *Controller) ListRawMaterialPurchases(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)
} }
// ListRawMaterialStockLogs godoc
// @Summary 获取原料库存日志列表
// @Description 根据提供的过滤条件,分页获取原料库存日志
// @Tags 数据监控
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListRawMaterialStockLogRequest true "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListRawMaterialStockLogResponse}
// @Router /api/v1/monitor/raw-material-stock-logs [get]
func (c *Controller) ListRawMaterialStockLogs(ctx *gin.Context) {
const actionType = "获取原料库存日志列表"
var req dto.ListRawMaterialStockLogRequest
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.RawMaterialStockLogListOptions{
RawMaterialID: req.RawMaterialID,
SourceID: req.SourceID,
OrderBy: req.OrderBy,
StartTime: req.StartTime,
EndTime: req.EndTime,
}
if req.SourceType != nil {
sourceType := models.StockLogSourceType(*req.SourceType)
opts.SourceType = &sourceType
}
data, total, err := c.monitorService.ListRawMaterialStockLogs(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.NewListRawMaterialStockLogResponse(data, total, req.Page, req.PageSize)
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取原料库存日志成功", resp, actionType, "获取原料库存日志成功", req)
}

View File

@@ -435,3 +435,59 @@ func NewListRawMaterialPurchaseResponse(data []models.RawMaterialPurchase, total
}, },
} }
} }
// --- RawMaterialStockLog ---
// ListRawMaterialStockLogRequest 定义了获取原料库存日志列表的请求参数
type ListRawMaterialStockLogRequest struct {
Page int `form:"page,default=1"`
PageSize int `form:"pageSize,default=10"`
RawMaterialID *uint `form:"raw_material_id"`
SourceType *string `form:"source_type"`
SourceID *uint `form:"source_id"`
StartTime *time.Time `form:"start_time" time_format:"rfc3339"`
EndTime *time.Time `form:"end_time" time_format:"rfc3339"`
OrderBy string `form:"order_by"`
}
// RawMaterialStockLogDTO 是用于API响应的原料库存日志结构
type RawMaterialStockLogDTO struct {
ID uint `json:"id"`
RawMaterialID uint `json:"raw_material_id"`
ChangeAmount float64 `json:"change_amount"`
SourceType models.StockLogSourceType `json:"source_type"`
SourceID uint `json:"source_id"`
HappenedAt time.Time `json:"happened_at"`
Remarks string `json:"remarks"`
}
// ListRawMaterialStockLogResponse 是获取原料库存日志列表的响应结构
type ListRawMaterialStockLogResponse struct {
List []RawMaterialStockLogDTO `json:"list"`
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,
},
}
}

View File

@@ -69,3 +69,8 @@ func (s *MonitorService) ListUserActionLogs(opts repository.UserActionLogListOpt
func (s *MonitorService) ListRawMaterialPurchases(opts repository.RawMaterialPurchaseListOptions, page, pageSize int) ([]models.RawMaterialPurchase, int64, error) { func (s *MonitorService) ListRawMaterialPurchases(opts repository.RawMaterialPurchaseListOptions, page, pageSize int) ([]models.RawMaterialPurchase, int64, error) {
return s.rawMaterialRepo.ListRawMaterialPurchases(opts, page, pageSize) return s.rawMaterialRepo.ListRawMaterialPurchases(opts, page, pageSize)
} }
// ListRawMaterialStockLogs 负责处理查询原料库存日志列表的业务逻辑
func (s *MonitorService) ListRawMaterialStockLogs(opts repository.RawMaterialStockLogListOptions, page, pageSize int) ([]models.RawMaterialStockLog, int64, error) {
return s.rawMaterialRepo.ListRawMaterialStockLogs(opts, page, pageSize)
}

View File

@@ -16,10 +16,20 @@ type RawMaterialPurchaseListOptions struct {
OrderBy string // 例如 "purchase_date asc" OrderBy string // 例如 "purchase_date asc"
} }
// RawMaterialStockLogListOptions 定义了查询原料库存日志时的可选参数
type RawMaterialStockLogListOptions struct {
RawMaterialID *uint
SourceType *models.StockLogSourceType
SourceID *uint
StartTime *time.Time // 基于 happened_at 字段
EndTime *time.Time // 基于 happened_at 字段
OrderBy string // 例如 "happened_at asc"
}
// RawMaterialRepository 定义了与原料相关的数据库操作接口 // RawMaterialRepository 定义了与原料相关的数据库操作接口
type RawMaterialRepository interface { type RawMaterialRepository interface {
// ListRawMaterialPurchases 支持分页和过滤的列表查询
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)
} }
// gormRawMaterialRepository 是 RawMaterialRepository 的 GORM 实现 // gormRawMaterialRepository 是 RawMaterialRepository 的 GORM 实现
@@ -71,3 +81,46 @@ func (r *gormRawMaterialRepository) ListRawMaterialPurchases(opts RawMaterialPur
return results, total, err return results, total, err
} }
// ListRawMaterialStockLogs 实现了分页和过滤查询原料库存日志的功能
func (r *gormRawMaterialRepository) ListRawMaterialStockLogs(opts RawMaterialStockLogListOptions, page, pageSize int) ([]models.RawMaterialStockLog, int64, error) {
if page <= 0 || pageSize <= 0 {
return nil, 0, ErrInvalidPagination
}
var results []models.RawMaterialStockLog
var total int64
query := r.db.Model(&models.RawMaterialStockLog{})
if opts.RawMaterialID != nil {
query = query.Where("raw_material_id = ?", *opts.RawMaterialID)
}
if opts.SourceType != nil {
query = query.Where("source_type = ?", *opts.SourceType)
}
if opts.SourceID != nil {
query = query.Where("source_id = ?", *opts.SourceID)
}
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)
offset := (page - 1) * pageSize
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
return results, total, err
}