70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
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) // 默认使用创建时间
|
|
var lastOperationSourceType models.StockLogSourceType
|
|
|
|
if latestLog != nil {
|
|
stock = latestLog.AfterQuantity
|
|
lastUpdated = latestLog.HappenedAt.Format(time.RFC3339)
|
|
lastOperationSourceType = latestLog.SourceType
|
|
}
|
|
|
|
return &CurrentStockResponse{
|
|
RawMaterialID: material.ID,
|
|
RawMaterialName: material.Name,
|
|
Stock: stock,
|
|
LastUpdated: lastUpdated,
|
|
LastOperationSourceType: lastOperationSourceType,
|
|
}
|
|
}
|
|
|
|
// 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: 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,
|
|
},
|
|
}
|
|
}
|