mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-23 06:45:43 +00:00
feat(BE-281):add dto standart mean bw and uniformity
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
rProductionStandard "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
|
||||
rProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/validations"
|
||||
@@ -30,6 +31,8 @@ type UniformityService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandangUniformity, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectFlockKandangUniformity, error)
|
||||
GetSummary(ctx *fiber.Ctx, id uint) (*entity.ProjectFlockKandangUniformity, error)
|
||||
GetStandard(ctx *fiber.Ctx, uniformity *entity.ProjectFlockKandangUniformity) (*UniformityStandard, error)
|
||||
MapStandards(ctx *fiber.Ctx, items []entity.ProjectFlockKandangUniformity) (map[uint]UniformityStandard, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create, file *multipart.FileHeader, rows []BodyWeightExcelRow) (*entity.ProjectFlockKandangUniformity, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint, file *multipart.FileHeader, rows []BodyWeightExcelRow) (*entity.ProjectFlockKandangUniformity, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
@@ -40,13 +43,15 @@ type UniformityService interface {
|
||||
}
|
||||
|
||||
type uniformityService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.UniformityRepository
|
||||
DocumentSvc commonSvc.DocumentService
|
||||
ApprovalRepo commonRepo.ApprovalRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
ProjectFlockKandangRepo rProjectFlock.ProjectFlockKandangRepository
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.UniformityRepository
|
||||
DocumentSvc commonSvc.DocumentService
|
||||
ApprovalRepo commonRepo.ApprovalRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
ProjectFlockKandangRepo rProjectFlock.ProjectFlockKandangRepository
|
||||
ProductionStandardRepo rProductionStandard.ProductionStandardRepository
|
||||
StandardGrowthDetailRepo rProductionStandard.StandardGrowthDetailRepository
|
||||
}
|
||||
|
||||
func NewUniformityService(
|
||||
@@ -55,16 +60,20 @@ func NewUniformityService(
|
||||
approvalRepo commonRepo.ApprovalRepository,
|
||||
approvalSvc commonSvc.ApprovalService,
|
||||
projectFlockKandangRepo rProjectFlock.ProjectFlockKandangRepository,
|
||||
productionStandardRepo rProductionStandard.ProductionStandardRepository,
|
||||
standardGrowthDetailRepo rProductionStandard.StandardGrowthDetailRepository,
|
||||
validate *validator.Validate,
|
||||
) UniformityService {
|
||||
return &uniformityService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
DocumentSvc: documentSvc,
|
||||
ApprovalRepo: approvalRepo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
ProjectFlockKandangRepo: projectFlockKandangRepo,
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
DocumentSvc: documentSvc,
|
||||
ApprovalRepo: approvalRepo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
ProjectFlockKandangRepo: projectFlockKandangRepo,
|
||||
ProductionStandardRepo: productionStandardRepo,
|
||||
StandardGrowthDetailRepo: standardGrowthDetailRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,6 +130,64 @@ func (s uniformityService) GetSummary(c *fiber.Ctx, id uint) (*entity.ProjectFlo
|
||||
return s.GetOne(c, id)
|
||||
}
|
||||
|
||||
func (s uniformityService) GetStandard(c *fiber.Ctx, uniformity *entity.ProjectFlockKandangUniformity) (*UniformityStandard, error) {
|
||||
if uniformity == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.resolveUniformityStandard(c.Context(), *uniformity)
|
||||
}
|
||||
|
||||
func (s uniformityService) MapStandards(c *fiber.Ctx, items []entity.ProjectFlockKandangUniformity) (map[uint]UniformityStandard, error) {
|
||||
if len(items) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
if s.ProductionStandardRepo == nil || s.StandardGrowthDetailRepo == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
categoryStandard := make(map[string]*entity.ProductionStandard)
|
||||
detailCache := make(map[uint]map[int]entity.StandardGrowthDetail)
|
||||
result := make(map[uint]UniformityStandard, len(items))
|
||||
|
||||
for _, item := range items {
|
||||
if item.Id == 0 {
|
||||
continue
|
||||
}
|
||||
standard, err := s.resolveCategoryStandard(c.Context(), item.ProjectFlockKandang.ProjectFlock.Category, categoryStandard)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if standard == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
weekMap, ok := detailCache[standard.Id]
|
||||
if !ok {
|
||||
details, err := s.StandardGrowthDetailRepo.GetByProductionStandardID(c.Context(), standard.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
weekMap = make(map[int]entity.StandardGrowthDetail, len(details))
|
||||
for _, detail := range details {
|
||||
weekMap[detail.Week] = detail
|
||||
}
|
||||
detailCache[standard.Id] = weekMap
|
||||
}
|
||||
|
||||
detail, ok := weekMap[item.Week]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
standardDTO := UniformityStandard{
|
||||
MeanWeight: cloneFloat64(detail.TargetMeanBw),
|
||||
Uniformity: float64Ptr(detail.MinUniformity),
|
||||
}
|
||||
result[item.Id] = standardDTO
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *uniformityService) CreateOne(c *fiber.Ctx, req *validation.Create, file *multipart.FileHeader, rows []BodyWeightExcelRow) (*entity.ProjectFlockKandangUniformity, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
@@ -516,6 +583,11 @@ type UniformityCalculation struct {
|
||||
Details []UniformityDetailItem
|
||||
}
|
||||
|
||||
type UniformityStandard struct {
|
||||
MeanWeight *float64
|
||||
Uniformity *float64
|
||||
}
|
||||
|
||||
func (s uniformityService) ComputeUniformity(rows []BodyWeightExcelRow) (UniformityCalculation, error) {
|
||||
return computeUniformity(rows)
|
||||
}
|
||||
@@ -664,6 +736,81 @@ func (s *uniformityService) attachLatestApproval(ctx context.Context, item *enti
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *uniformityService) resolveUniformityStandard(ctx context.Context, item entity.ProjectFlockKandangUniformity) (*UniformityStandard, error) {
|
||||
if s.ProductionStandardRepo == nil || s.StandardGrowthDetailRepo == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
standard, err := s.resolveCategoryStandard(ctx, item.ProjectFlockKandang.ProjectFlock.Category, nil)
|
||||
if err != nil || standard == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
detail, err := s.StandardGrowthDetailRepo.GetByStandardIDAndWeek(ctx, standard.Id, item.Week)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &UniformityStandard{
|
||||
MeanWeight: cloneFloat64(detail.TargetMeanBw),
|
||||
Uniformity: float64Ptr(detail.MinUniformity),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *uniformityService) resolveCategoryStandard(
|
||||
ctx context.Context,
|
||||
category string,
|
||||
cache map[string]*entity.ProductionStandard,
|
||||
) (*entity.ProductionStandard, error) {
|
||||
category = strings.TrimSpace(category)
|
||||
if category == "" {
|
||||
return nil, nil
|
||||
}
|
||||
if cache != nil {
|
||||
if cached, ok := cache[category]; ok {
|
||||
return cached, nil
|
||||
}
|
||||
}
|
||||
|
||||
var standard entity.ProductionStandard
|
||||
err := s.ProductionStandardRepo.DB().WithContext(ctx).
|
||||
Where("project_category = ?", category).
|
||||
Where("deleted_at IS NULL").
|
||||
Order("created_at DESC").
|
||||
First(&standard).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
if cache != nil {
|
||||
cache[category] = nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
standardCopy := standard
|
||||
if cache != nil {
|
||||
cache[category] = &standardCopy
|
||||
}
|
||||
return &standardCopy, nil
|
||||
}
|
||||
|
||||
func cloneFloat64(value *float64) *float64 {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
copy := *value
|
||||
return ©
|
||||
}
|
||||
|
||||
func float64Ptr(value float64) *float64 {
|
||||
copy := value
|
||||
return ©
|
||||
}
|
||||
|
||||
func (s *uniformityService) rollbackUniformityCreate(ctx context.Context, uniformityID uint) {
|
||||
if uniformityID == 0 {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user