mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
fix api production result
This commit is contained in:
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
@@ -21,9 +22,9 @@ import (
|
||||
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
||||
areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
||||
customerRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/repositories"
|
||||
productionStandardRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
|
||||
supplierDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/dto"
|
||||
warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
|
||||
productionStandardRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
|
||||
chickinRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
|
||||
recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
||||
purchaseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
|
||||
@@ -47,21 +48,21 @@ type RepportService interface {
|
||||
}
|
||||
|
||||
type repportService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
DB *gorm.DB
|
||||
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
|
||||
MarketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository
|
||||
PurchaseRepo purchaseRepo.PurchaseRepository
|
||||
ChickinRepo chickinRepo.ProjectChickinRepository
|
||||
RecordingRepo recordingRepo.RecordingRepository
|
||||
ApprovalSvc approvalService.ApprovalService
|
||||
PurchaseSupplierRepo repportRepo.PurchaseSupplierRepository
|
||||
DebtSupplierRepo repportRepo.DebtSupplierRepository
|
||||
HppPerKandangRepo repportRepo.HppPerKandangRepository
|
||||
ProductionResultRepo repportRepo.ProductionResultRepository
|
||||
CustomerPaymentRepo repportRepo.CustomerPaymentRepository
|
||||
CustomerRepo customerRepo.CustomerRepository
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
DB *gorm.DB
|
||||
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
|
||||
MarketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository
|
||||
PurchaseRepo purchaseRepo.PurchaseRepository
|
||||
ChickinRepo chickinRepo.ProjectChickinRepository
|
||||
RecordingRepo recordingRepo.RecordingRepository
|
||||
ApprovalSvc approvalService.ApprovalService
|
||||
PurchaseSupplierRepo repportRepo.PurchaseSupplierRepository
|
||||
DebtSupplierRepo repportRepo.DebtSupplierRepository
|
||||
HppPerKandangRepo repportRepo.HppPerKandangRepository
|
||||
ProductionResultRepo repportRepo.ProductionResultRepository
|
||||
CustomerPaymentRepo repportRepo.CustomerPaymentRepository
|
||||
CustomerRepo customerRepo.CustomerRepository
|
||||
StandardGrowthDetailRepo productionStandardRepository.StandardGrowthDetailRepository
|
||||
ProductionStandardDetailRepo productionStandardRepository.ProductionStandardDetailRepository
|
||||
}
|
||||
@@ -94,21 +95,21 @@ func NewRepportService(
|
||||
productionStandardDetailRepo productionStandardRepository.ProductionStandardDetailRepository,
|
||||
) RepportService {
|
||||
return &repportService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
DB: db,
|
||||
ExpenseRealizationRepo: expenseRealizationRepo,
|
||||
MarketingDeliveryRepo: marketingDeliveryRepo,
|
||||
PurchaseRepo: purchaseRepo,
|
||||
ChickinRepo: chickinRepo,
|
||||
RecordingRepo: recordingRepo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
PurchaseSupplierRepo: purchaseSupplierRepo,
|
||||
DebtSupplierRepo: debtSupplierRepo,
|
||||
HppPerKandangRepo: hppPerKandangRepo,
|
||||
ProductionResultRepo: productionResultRepo,
|
||||
CustomerPaymentRepo: customerPaymentRepo,
|
||||
CustomerRepo: customerRepo,
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
DB: db,
|
||||
ExpenseRealizationRepo: expenseRealizationRepo,
|
||||
MarketingDeliveryRepo: marketingDeliveryRepo,
|
||||
PurchaseRepo: purchaseRepo,
|
||||
ChickinRepo: chickinRepo,
|
||||
RecordingRepo: recordingRepo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
PurchaseSupplierRepo: purchaseSupplierRepo,
|
||||
DebtSupplierRepo: debtSupplierRepo,
|
||||
HppPerKandangRepo: hppPerKandangRepo,
|
||||
ProductionResultRepo: productionResultRepo,
|
||||
CustomerPaymentRepo: customerPaymentRepo,
|
||||
CustomerRepo: customerRepo,
|
||||
StandardGrowthDetailRepo: standardGrowthDetailRepo,
|
||||
ProductionStandardDetailRepo: productionStandardDetailRepo,
|
||||
}
|
||||
@@ -319,6 +320,15 @@ func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.
|
||||
standardDetailCache := make(map[int]*entity.ProductionStandardDetail)
|
||||
growthDetailCache := make(map[int]*entity.StandardGrowthDetail)
|
||||
|
||||
weeks := make([]int, len(weeklyResults))
|
||||
for i := range weeklyResults {
|
||||
weeks[i] = defaultStartWoa + i
|
||||
}
|
||||
uniformityMap, err := s.getUniformityByWeek(ctx.Context(), params.ProjectFlockKandangID, weeks)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var cumulativeButir int64
|
||||
var cumulativeKg float64
|
||||
for i := range weeklyResults {
|
||||
@@ -328,6 +338,12 @@ func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.
|
||||
if weeklyResults[i].StdUniformity == "" {
|
||||
weeklyResults[i].StdUniformity = defaultUniformText
|
||||
}
|
||||
if uniformity, ok := uniformityMap[defaultStartWoa+i]; ok {
|
||||
weeklyResults[i].Uniformity = uniformity.Uniformity
|
||||
if uniformity.AvgWeight != nil {
|
||||
weeklyResults[i].Bw = *uniformity.AvgWeight
|
||||
}
|
||||
}
|
||||
|
||||
cumulativeButir += weeklyResults[i].ButiranJumlah
|
||||
weeklyResults[i].TotalButir = cumulativeButir
|
||||
@@ -744,6 +760,68 @@ func getEggFlagType(egg entity.RecordingEgg) (utils.FlagType, bool) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
type uniformityWeekData struct {
|
||||
Uniformity float64
|
||||
AvgWeight *float64
|
||||
}
|
||||
|
||||
type uniformityChartPayload struct {
|
||||
Statistics *uniformityChartStats `json:"statistics"`
|
||||
}
|
||||
|
||||
type uniformityChartStats struct {
|
||||
AverageWeight *float64 `json:"average_weight"`
|
||||
}
|
||||
|
||||
func (s *repportService) getUniformityByWeek(ctx context.Context, projectFlockKandangID uint, weeks []int) (map[int]uniformityWeekData, error) {
|
||||
result := make(map[int]uniformityWeekData, len(weeks))
|
||||
if projectFlockKandangID == 0 || len(weeks) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
var rows []entity.ProjectFlockKandangUniformity
|
||||
if err := s.DB.WithContext(ctx).
|
||||
Model(&entity.ProjectFlockKandangUniformity{}).
|
||||
Select("week, uniformity, uniform_date, id").
|
||||
Where("project_flock_kandang_id = ?", projectFlockKandangID).
|
||||
Where("week IN ?", weeks).
|
||||
Order("uniform_date DESC").
|
||||
Order("id DESC").
|
||||
Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, row := range rows {
|
||||
if _, exists := result[row.Week]; exists {
|
||||
continue
|
||||
}
|
||||
result[row.Week] = uniformityWeekData{
|
||||
Uniformity: row.Uniformity,
|
||||
AvgWeight: extractAverageWeight(row.ChartData, s.Log),
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func extractAverageWeight(raw json.RawMessage, log *logrus.Logger) *float64 {
|
||||
if len(raw) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var payload uniformityChartPayload
|
||||
if err := json.Unmarshal(raw, &payload); err != nil {
|
||||
if log != nil {
|
||||
log.WithError(err).Warn("uniformity chart_data decode failed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if payload.Statistics == nil {
|
||||
return nil
|
||||
}
|
||||
return payload.Statistics.AverageWeight
|
||||
}
|
||||
|
||||
func summarizeProductionResults(daily []dto.ProductionResultDTO, groupSize int) []dto.ProductionResultDTO {
|
||||
if groupSize <= 0 || len(daily) == 0 {
|
||||
return daily
|
||||
|
||||
Reference in New Issue
Block a user