Files
silk/server-go/internal/service/farm_stats.go
T
2026-08-17 21:43:26 +08:00

54 lines
1.8 KiB
Go

package service
// ProductionLossRow 产量损失统计原始行。
type ProductionLossRow struct {
BatchID string `gorm:"column:batch_id"`
RoomID string `gorm:"column:room_id"`
DeathCount int64 `gorm:"column:death_count"`
CulledCount int64 `gorm:"column:culled_count"`
YieldKg float64 `gorm:"column:yield_kg"`
LossKg float64 `gorm:"column:loss_kg"`
CostAmount float64 `gorm:"column:cost_amount"`
RecordCount int64 `gorm:"column:record_count"`
}
// ProductionLossStat 按房间/批次聚合后的产量损失统计。
type ProductionLossStat struct {
BatchID string `json:"batchId"`
RoomID string `json:"roomId"`
RoomName string `json:"roomName,omitempty"`
BatchName string `json:"batchName,omitempty"`
DeathCount int64 `json:"deathCount"`
CulledCount int64 `json:"culledCount"`
YieldKg float64 `json:"yieldKg"`
LossKg float64 `json:"lossKg"`
CostAmount float64 `json:"costAmount"`
RecordCount int64 `json:"recordCount"`
}
// AggregateProductionLoss 聚合产量损失记录;空输入返回空数组。
func AggregateProductionLoss(rows []ProductionLossRow) []ProductionLossStat {
byKey := make(map[string]*ProductionLossStat)
var order []string
for _, r := range rows {
key := r.RoomID + "|" + r.BatchID
stat, ok := byKey[key]
if !ok {
stat = &ProductionLossStat{RoomID: r.RoomID, BatchID: r.BatchID}
byKey[key] = stat
order = append(order, key)
}
stat.DeathCount += r.DeathCount
stat.CulledCount += r.CulledCount
stat.YieldKg += r.YieldKg
stat.LossKg += r.LossKg
stat.CostAmount += r.CostAmount
stat.RecordCount += r.RecordCount
}
result := make([]ProductionLossStat, 0, len(order))
for _, key := range order {
result = append(result, *byKey[key])
}
return result
}