实现库存管理相关逻辑
This commit is contained in:
66
internal/app/dto/inventory_converter.go
Normal file
66
internal/app/dto/inventory_converter.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
// ConvertCurrentStockToDTO 将原料及其最新库存日志转换为 CurrentStockResponse DTO
|
||||
func ConvertCurrentStockToDTO(material *models.RawMaterial, latestLog *models.RawMaterialStockLog) *CurrentStockResponse {
|
||||
if material == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
stock := float32(0)
|
||||
lastUpdated := material.CreatedAt.Format(time.RFC3339) // 默认使用创建时间
|
||||
|
||||
if latestLog != nil {
|
||||
stock = latestLog.AfterQuantity
|
||||
lastUpdated = latestLog.HappenedAt.Format(time.RFC3339)
|
||||
}
|
||||
|
||||
return &CurrentStockResponse{
|
||||
RawMaterialID: material.ID,
|
||||
RawMaterialName: material.Name,
|
||||
Stock: stock,
|
||||
LastUpdated: lastUpdated,
|
||||
}
|
||||
}
|
||||
|
||||
// ConvertStockLogToDTO 将 models.RawMaterialStockLog 转换为 StockLogResponse DTO
|
||||
func ConvertStockLogToDTO(log *models.RawMaterialStockLog) *StockLogResponse {
|
||||
if log == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &StockLogResponse{
|
||||
ID: log.ID,
|
||||
RawMaterialID: log.RawMaterialID,
|
||||
RawMaterialName: log.RawMaterial.Name, // 假设 RawMaterial 已被预加载
|
||||
ChangeAmount: log.ChangeAmount,
|
||||
BeforeQuantity: log.BeforeQuantity,
|
||||
AfterQuantity: log.AfterQuantity,
|
||||
SourceType: string(log.SourceType),
|
||||
SourceID: log.SourceID,
|
||||
HappenedAt: log.HappenedAt,
|
||||
Remarks: log.Remarks,
|
||||
}
|
||||
}
|
||||
|
||||
// ConvertStockLogListToDTO 将 []models.RawMaterialStockLog 转换为 ListStockLogResponse DTO
|
||||
func ConvertStockLogListToDTO(logs []models.RawMaterialStockLog, total int64, page, pageSize int) *ListStockLogResponse {
|
||||
logDTOs := make([]StockLogResponse, len(logs))
|
||||
for i, log := range logs {
|
||||
logDTOs[i] = *ConvertStockLogToDTO(&log)
|
||||
}
|
||||
|
||||
return &ListStockLogResponse{
|
||||
List: logDTOs,
|
||||
Pagination: PaginationDTO{
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
Total: total,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user