From d9afd2913e90d1c93e8806a1f7b69fa8cfdaef7a Mon Sep 17 00:00:00 2001 From: ragilap Date: Wed, 31 Dec 2025 09:13:55 +0700 Subject: [PATCH] feat(BE-278): adjustment_recording dto --- internal/entities/recording.go | 1 + .../recordings/dto/recording.dto.go | 2 ++ .../repositories/recording.repository.go | 29 ++++++++++++++++++- .../recordings/services/recording.service.go | 17 +++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/internal/entities/recording.go b/internal/entities/recording.go index e07a0a6b..03388ef2 100644 --- a/internal/entities/recording.go +++ b/internal/entities/recording.go @@ -39,4 +39,5 @@ type Recording struct { StandardFeedIntake *float64 `gorm:"-"` StandardEggMesh *float64 `gorm:"-"` StandardEggWeight *float64 `gorm:"-"` + StandardFcr *float64 `gorm:"-"` } diff --git a/internal/modules/production/recordings/dto/recording.dto.go b/internal/modules/production/recordings/dto/recording.dto.go index 12222b97..736eeaa7 100644 --- a/internal/modules/production/recordings/dto/recording.dto.go +++ b/internal/modules/production/recordings/dto/recording.dto.go @@ -35,6 +35,7 @@ type RecordingRelationDTO struct { StandardFeedIntake *float64 `json:"feed_intake_std,omitempty"` StandardEggMesh *float64 `json:"egg_mesh_std,omitempty"` StandardEggWeight *float64 `json:"egg_weight_std,omitempty"` + StandardFcr *float64 `json:"fcr_std,omitempty"` Approval approvalDTO.ApprovalRelationDTO `json:"approval"` } @@ -165,6 +166,7 @@ func ToRecordingRelationDTO(e entity.Recording) RecordingRelationDTO { StandardFeedIntake: e.StandardFeedIntake, StandardEggMesh: e.StandardEggMesh, StandardEggWeight: e.StandardEggWeight, + StandardFcr: e.StandardFcr, Approval: latestApproval, } } diff --git a/internal/modules/production/recordings/repositories/recording.repository.go b/internal/modules/production/recordings/repositories/recording.repository.go index 8642ed08..d9e0bc0b 100644 --- a/internal/modules/production/recordings/repositories/recording.repository.go +++ b/internal/modules/production/recordings/repositories/recording.repository.go @@ -42,6 +42,7 @@ type RecordingRepository interface { GetFeedUsageInGrams(tx *gorm.DB, recordingID uint) (float64, error) GetEggSummaryByRecording(tx *gorm.DB, recordingID uint) (totalQty float64, totalWeightGrams float64, err error) GetCumulativeEggQtyByProjectFlockKandang(tx *gorm.DB, projectFlockKandangId uint, recordTime time.Time) (float64, error) + GetFcrStandardNumber(tx *gorm.DB, fcrId uint, currentWeightKg float64) (float64, bool, error) GetProductionWeightAndQtyByProjectFlockID(ctx context.Context, projectFlockID uint) (totalWeight float64, totalQty float64, err error) GetTotalDepletionByProjectFlockID(ctx context.Context, projectFlockID uint) (totalDepletion float64, err error) GetLatestAvgWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (avgWeight float64, err error) @@ -344,12 +345,38 @@ func (r *RecordingRepositoryImpl) GetCumulativeEggQtyByProjectFlockKandang( return result, err } +func (r *RecordingRepositoryImpl) GetFcrStandardNumber(tx *gorm.DB, fcrId uint, currentWeightKg float64) (float64, bool, error) { + if fcrId == 0 || currentWeightKg <= 0 { + return 0, false, nil + } + + var standard entity.FcrStandard + err := tx. + Where("fcr_id = ? AND weight >= ?", fcrId, currentWeightKg). + Order("weight ASC"). + First(&standard).Error + + if errors.Is(err, gorm.ErrRecordNotFound) { + err = tx. + Where("fcr_id = ?", fcrId). + Order("weight DESC"). + First(&standard).Error + if errors.Is(err, gorm.ErrRecordNotFound) { + return 0, false, nil + } + } + if err != nil { + return 0, false, err + } + + return standard.FcrNumber, true, nil +} + func (r *RecordingRepositoryImpl) GetProductionWeightAndQtyByProjectFlockID(ctx context.Context, projectFlockID uint) (totalWeight float64, totalQty float64, err error) { // Body-weight tracking is removed; keep stub for report compatibility. return 0, 0, nil } - func (r *RecordingRepositoryImpl) GetTotalDepletionByProjectFlockID(ctx context.Context, projectFlockID uint) (float64, error) { var result float64 err := r.DB().WithContext(ctx). diff --git a/internal/modules/production/recordings/services/recording.service.go b/internal/modules/production/recordings/services/recording.service.go index a3756adf..1a63ad96 100644 --- a/internal/modules/production/recordings/services/recording.service.go +++ b/internal/modules/production/recordings/services/recording.service.go @@ -1063,6 +1063,7 @@ func (s *recordingService) attachProductionStandard(ctx context.Context, item *e growthDetailRepo := rProductionStandard.NewStandardGrowthDetailRepository(db) var standard productionStandardValues + var standardFcr *float64 if category == string(utils.ProjectFlockCategoryLaying) { detail, err := standardDetailRepo.GetByStandardIDAndWeek(ctx, standardID, week) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { @@ -1082,6 +1083,21 @@ func (s *recordingService) attachProductionStandard(ctx context.Context, item *e } if growthDetail != nil { standard.FeedIntake = growthDetail.FeedIntake + if category == string(utils.ProjectFlockCategoryLaying) && growthDetail.TargetMeanBw != nil && item.ProjectFlockKandang.ProjectFlock.FcrId > 0 { + targetWeight := *growthDetail.TargetMeanBw + if targetWeight > 10 { + targetWeight = targetWeight / 1000 + } + if targetWeight > 0 { + fcrStd, ok, err := s.Repository.GetFcrStandardNumber(db, item.ProjectFlockKandang.ProjectFlock.FcrId, targetWeight) + if err != nil { + return err + } + if ok { + standardFcr = &fcrStd + } + } + } } item.StandardHandDay = standard.HandDay @@ -1089,6 +1105,7 @@ func (s *recordingService) attachProductionStandard(ctx context.Context, item *e item.StandardFeedIntake = standard.FeedIntake item.StandardEggMesh = standard.EggMesh item.StandardEggWeight = standard.EggWeight + item.StandardFcr = standardFcr return nil }