mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 07:15:43 +00:00
fix api production result
This commit is contained in:
@@ -2,6 +2,7 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
@@ -21,9 +22,9 @@ import (
|
|||||||
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
||||||
areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
||||||
customerRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/repositories"
|
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"
|
supplierDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/dto"
|
||||||
warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/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"
|
chickinRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
|
||||||
recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
||||||
purchaseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
|
purchaseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
|
||||||
@@ -47,21 +48,21 @@ type RepportService interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type repportService struct {
|
type repportService struct {
|
||||||
Log *logrus.Logger
|
Log *logrus.Logger
|
||||||
Validate *validator.Validate
|
Validate *validator.Validate
|
||||||
DB *gorm.DB
|
DB *gorm.DB
|
||||||
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
|
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
|
||||||
MarketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository
|
MarketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository
|
||||||
PurchaseRepo purchaseRepo.PurchaseRepository
|
PurchaseRepo purchaseRepo.PurchaseRepository
|
||||||
ChickinRepo chickinRepo.ProjectChickinRepository
|
ChickinRepo chickinRepo.ProjectChickinRepository
|
||||||
RecordingRepo recordingRepo.RecordingRepository
|
RecordingRepo recordingRepo.RecordingRepository
|
||||||
ApprovalSvc approvalService.ApprovalService
|
ApprovalSvc approvalService.ApprovalService
|
||||||
PurchaseSupplierRepo repportRepo.PurchaseSupplierRepository
|
PurchaseSupplierRepo repportRepo.PurchaseSupplierRepository
|
||||||
DebtSupplierRepo repportRepo.DebtSupplierRepository
|
DebtSupplierRepo repportRepo.DebtSupplierRepository
|
||||||
HppPerKandangRepo repportRepo.HppPerKandangRepository
|
HppPerKandangRepo repportRepo.HppPerKandangRepository
|
||||||
ProductionResultRepo repportRepo.ProductionResultRepository
|
ProductionResultRepo repportRepo.ProductionResultRepository
|
||||||
CustomerPaymentRepo repportRepo.CustomerPaymentRepository
|
CustomerPaymentRepo repportRepo.CustomerPaymentRepository
|
||||||
CustomerRepo customerRepo.CustomerRepository
|
CustomerRepo customerRepo.CustomerRepository
|
||||||
StandardGrowthDetailRepo productionStandardRepository.StandardGrowthDetailRepository
|
StandardGrowthDetailRepo productionStandardRepository.StandardGrowthDetailRepository
|
||||||
ProductionStandardDetailRepo productionStandardRepository.ProductionStandardDetailRepository
|
ProductionStandardDetailRepo productionStandardRepository.ProductionStandardDetailRepository
|
||||||
}
|
}
|
||||||
@@ -94,21 +95,21 @@ func NewRepportService(
|
|||||||
productionStandardDetailRepo productionStandardRepository.ProductionStandardDetailRepository,
|
productionStandardDetailRepo productionStandardRepository.ProductionStandardDetailRepository,
|
||||||
) RepportService {
|
) RepportService {
|
||||||
return &repportService{
|
return &repportService{
|
||||||
Log: utils.Log,
|
Log: utils.Log,
|
||||||
Validate: validate,
|
Validate: validate,
|
||||||
DB: db,
|
DB: db,
|
||||||
ExpenseRealizationRepo: expenseRealizationRepo,
|
ExpenseRealizationRepo: expenseRealizationRepo,
|
||||||
MarketingDeliveryRepo: marketingDeliveryRepo,
|
MarketingDeliveryRepo: marketingDeliveryRepo,
|
||||||
PurchaseRepo: purchaseRepo,
|
PurchaseRepo: purchaseRepo,
|
||||||
ChickinRepo: chickinRepo,
|
ChickinRepo: chickinRepo,
|
||||||
RecordingRepo: recordingRepo,
|
RecordingRepo: recordingRepo,
|
||||||
ApprovalSvc: approvalSvc,
|
ApprovalSvc: approvalSvc,
|
||||||
PurchaseSupplierRepo: purchaseSupplierRepo,
|
PurchaseSupplierRepo: purchaseSupplierRepo,
|
||||||
DebtSupplierRepo: debtSupplierRepo,
|
DebtSupplierRepo: debtSupplierRepo,
|
||||||
HppPerKandangRepo: hppPerKandangRepo,
|
HppPerKandangRepo: hppPerKandangRepo,
|
||||||
ProductionResultRepo: productionResultRepo,
|
ProductionResultRepo: productionResultRepo,
|
||||||
CustomerPaymentRepo: customerPaymentRepo,
|
CustomerPaymentRepo: customerPaymentRepo,
|
||||||
CustomerRepo: customerRepo,
|
CustomerRepo: customerRepo,
|
||||||
StandardGrowthDetailRepo: standardGrowthDetailRepo,
|
StandardGrowthDetailRepo: standardGrowthDetailRepo,
|
||||||
ProductionStandardDetailRepo: productionStandardDetailRepo,
|
ProductionStandardDetailRepo: productionStandardDetailRepo,
|
||||||
}
|
}
|
||||||
@@ -319,6 +320,15 @@ func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.
|
|||||||
standardDetailCache := make(map[int]*entity.ProductionStandardDetail)
|
standardDetailCache := make(map[int]*entity.ProductionStandardDetail)
|
||||||
growthDetailCache := make(map[int]*entity.StandardGrowthDetail)
|
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 cumulativeButir int64
|
||||||
var cumulativeKg float64
|
var cumulativeKg float64
|
||||||
for i := range weeklyResults {
|
for i := range weeklyResults {
|
||||||
@@ -328,6 +338,12 @@ func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.
|
|||||||
if weeklyResults[i].StdUniformity == "" {
|
if weeklyResults[i].StdUniformity == "" {
|
||||||
weeklyResults[i].StdUniformity = defaultUniformText
|
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
|
cumulativeButir += weeklyResults[i].ButiranJumlah
|
||||||
weeklyResults[i].TotalButir = cumulativeButir
|
weeklyResults[i].TotalButir = cumulativeButir
|
||||||
@@ -744,6 +760,68 @@ func getEggFlagType(egg entity.RecordingEgg) (utils.FlagType, bool) {
|
|||||||
return "", false
|
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 {
|
func summarizeProductionResults(daily []dto.ProductionResultDTO, groupSize int) []dto.ProductionResultDTO {
|
||||||
if groupSize <= 0 || len(daily) == 0 {
|
if groupSize <= 0 || len(daily) == 0 {
|
||||||
return daily
|
return daily
|
||||||
|
|||||||
Reference in New Issue
Block a user