fix recording standar prod laying

This commit is contained in:
giovanni
2026-06-07 18:55:24 +07:00
parent edfd6ac95c
commit 2216f572c2
6 changed files with 445 additions and 11 deletions
@@ -205,6 +205,7 @@ func AttachProductionStandards(ctx context.Context, db *gorm.DB, warnOnly bool,
standardDetailByStd := make(map[uint]map[int]*entity.ProductionStandardDetail, len(standardIDs))
growthDetailByStd := make(map[uint]map[int]*entity.StandardGrowthDetail, len(standardIDs))
firstCommonWeekByStd := make(map[uint]int, len(standardIDs))
for standardID := range standardIDs {
details, err := standardDetailRepo.GetByProductionStandardID(ctx, standardID)
@@ -242,6 +243,10 @@ func AttachProductionStandards(ctx context.Context, db *gorm.DB, warnOnly bool,
growthMap[growth.Week] = &growth
}
growthDetailByStd[standardID] = growthMap
if firstCommonWeek, ok := firstCommonStandardWeek(detailMap, growthMap); ok {
firstCommonWeekByStd[standardID] = firstCommonWeek
}
}
// Batch-load laying transfer targets → EARLIEST source PFK chick_in_date per target.
@@ -284,6 +289,9 @@ func AttachProductionStandards(ctx context.Context, db *gorm.DB, warnOnly bool,
continue
}
week := computeTransferAwareWeek(item, sourceChickInByTarget)
if firstCommonWeek, ok := firstCommonWeekByStd[standardID]; ok {
week = effectiveProductionStandardWeek(item, week, firstCommonWeek)
}
item.StandardWeek = &week
cacheKey := standardKey{standardID: standardID, week: week}
if cached, ok := cache[cacheKey]; ok {
@@ -324,6 +332,38 @@ func applyProductionStandardValues(item *entity.Recording, values productionStan
item.StandardFcr = fcr
}
func firstCommonStandardWeek(
detailMap map[int]*entity.ProductionStandardDetail,
growthMap map[int]*entity.StandardGrowthDetail,
) (int, bool) {
firstWeek := 0
for week := range detailMap {
if week <= 0 {
continue
}
if _, ok := growthMap[week]; !ok {
continue
}
if firstWeek == 0 || week < firstWeek {
firstWeek = week
}
}
return firstWeek, firstWeek > 0
}
func effectiveProductionStandardWeek(item *entity.Recording, actualWeek int, firstCommonWeek int) int {
if item == nil || actualWeek <= 0 || firstCommonWeek <= 0 {
return actualWeek
}
if !IsLayingRecording(*item) {
return actualWeek
}
if actualWeek < firstCommonWeek {
return firstCommonWeek
}
return actualWeek
}
// collectLayingPFKIDs mengumpulkan semua project_flock_kandang_id dari recording laying
func collectLayingPFKIDs(items []*entity.Recording) []uint {
seen := make(map[uint]struct{})