mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
codex/fix: uniformity week calculation
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
@@ -104,6 +105,15 @@ func applyDashboardFilters(db *gorm.DB, filters *validation.DashboardFilter) *go
|
||||
return db
|
||||
}
|
||||
|
||||
func dashboardUniformityWeekExpr() string {
|
||||
return fmt.Sprintf(`CASE
|
||||
WHEN u.uniform_date IS NULL OR pc.chick_in_date IS NULL THEN 0
|
||||
WHEN u.uniform_date::date < pc.chick_in_date THEN 0
|
||||
WHEN UPPER(pf.category) = 'LAYING' THEN (((u.uniform_date::date - pc.chick_in_date)::int) / 7) + %d
|
||||
ELSE (((u.uniform_date::date - pc.chick_in_date)::int) / 7) + 1
|
||||
END`, config.LayingWeekStart())
|
||||
}
|
||||
|
||||
func (r *DashboardRepositoryImpl) GetRecordingWeeklyMetrics(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) ([]RecordingWeeklyMetric, error) {
|
||||
var rows []RecordingWeeklyMetric
|
||||
|
||||
@@ -139,20 +149,29 @@ func (r *DashboardRepositoryImpl) GetRecordingWeeklyMetrics(ctx context.Context,
|
||||
|
||||
func (r *DashboardRepositoryImpl) GetUniformityWeeklyMetrics(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) ([]UniformityWeeklyMetric, error) {
|
||||
var rows []UniformityWeeklyMetric
|
||||
weekExpr := dashboardUniformityWeekExpr()
|
||||
|
||||
db := r.DB().WithContext(ctx).
|
||||
Table("project_flock_kandang_uniformity AS u").
|
||||
Select(`u.week AS week,
|
||||
Select(fmt.Sprintf(`%s AS week,
|
||||
COALESCE(AVG(u.uniformity), 0) AS uniformity,
|
||||
COALESCE(AVG((u.chart_data->'statistics'->>'average_weight')::numeric), 0) AS average_weight`).
|
||||
COALESCE(AVG((u.chart_data->'statistics'->>'average_weight')::numeric), 0) AS average_weight`, weekExpr)).
|
||||
Joins("JOIN project_flock_kandangs AS pfk ON pfk.id = u.project_flock_kandang_id").
|
||||
Joins("JOIN project_flocks AS pf ON pf.id = pfk.project_flock_id").
|
||||
Joins("JOIN kandangs AS k ON k.id = pfk.kandang_id").
|
||||
Joins(`JOIN (
|
||||
SELECT project_flock_kandang_id, MIN(chick_in_date)::date AS chick_in_date
|
||||
FROM project_chickins
|
||||
WHERE deleted_at IS NULL
|
||||
GROUP BY project_flock_kandang_id
|
||||
) AS pc ON pc.project_flock_kandang_id = u.project_flock_kandang_id`).
|
||||
Where("u.uniform_date IS NOT NULL").
|
||||
Where("u.uniform_date >= ? AND u.uniform_date < ?", start, end)
|
||||
Where("u.uniform_date >= ? AND u.uniform_date < ?", start, end).
|
||||
Where("u.uniform_date::date >= pc.chick_in_date")
|
||||
|
||||
db = applyDashboardFilters(db, filters)
|
||||
|
||||
if err := db.Group("u.week").Order("u.week ASC").Scan(&rows).Error; err != nil {
|
||||
if err := db.Group("week").Order("week ASC").Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -518,23 +537,31 @@ func (r *DashboardRepositoryImpl) GetComparisonWeeklyUniformityMetrics(ctx conte
|
||||
}
|
||||
|
||||
var rows []ComparisonUniformityMetric
|
||||
weekExpr := dashboardUniformityWeekExpr()
|
||||
db := r.DB().WithContext(ctx).
|
||||
Table("project_flock_kandang_uniformity AS u").
|
||||
Select(fmt.Sprintf(`u.week AS week,
|
||||
Select(fmt.Sprintf(`%s AS week,
|
||||
%s AS series_id,
|
||||
COALESCE(AVG(u.uniformity), 0) AS uniformity,
|
||||
COALESCE(AVG((u.chart_data->'statistics'->>'average_weight')::numeric), 0) AS average_weight`, seriesExpr)).
|
||||
COALESCE(AVG((u.chart_data->'statistics'->>'average_weight')::numeric), 0) AS average_weight`, weekExpr, seriesExpr)).
|
||||
Joins("JOIN project_flock_kandangs AS pfk ON pfk.id = u.project_flock_kandang_id").
|
||||
Joins("JOIN kandangs AS k ON k.id = pfk.kandang_id").
|
||||
Joins("JOIN project_flocks AS pf ON pf.id = pfk.project_flock_id").
|
||||
Joins("JOIN locations AS loc ON loc.id = k.location_id").
|
||||
Joins(`JOIN (
|
||||
SELECT project_flock_kandang_id, MIN(chick_in_date)::date AS chick_in_date
|
||||
FROM project_chickins
|
||||
WHERE deleted_at IS NULL
|
||||
GROUP BY project_flock_kandang_id
|
||||
) AS pc ON pc.project_flock_kandang_id = u.project_flock_kandang_id`).
|
||||
Where("u.uniform_date IS NOT NULL").
|
||||
Where("u.uniform_date >= ? AND u.uniform_date < ?", start, end)
|
||||
Where("u.uniform_date >= ? AND u.uniform_date < ?", start, end).
|
||||
Where("u.uniform_date::date >= pc.chick_in_date")
|
||||
|
||||
db = applyDashboardFilters(db, filters)
|
||||
|
||||
groupBy := fmt.Sprintf("u.week, %s", groupExpr)
|
||||
orderBy := fmt.Sprintf("u.week ASC, %s", orderExpr)
|
||||
groupBy := fmt.Sprintf("week, %s", groupExpr)
|
||||
orderBy := fmt.Sprintf("week ASC, %s", orderExpr)
|
||||
if err := db.Group(groupBy).Order(orderBy).Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -42,7 +42,9 @@ func (r *UniformityRepositoryImpl) GetAllWithFilters(ctx context.Context, offset
|
||||
func (r *UniformityRepositoryImpl) WithDefaultRelations() func(*gorm.DB) *gorm.DB {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
return db.
|
||||
Preload("ProjectFlockKandang.ProjectFlock").
|
||||
Preload("ProjectFlockKandang.ProjectFlock.Location").
|
||||
Preload("ProjectFlockKandang.Chickins").
|
||||
Preload("ProjectFlockKandang.Kandang.Location")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,6 +106,7 @@ func (s uniformityService) GetAll(c *fiber.Ctx, params *validation.Query) ([]ent
|
||||
s.Log.Errorf("Failed to get uniformitys: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
s.normalizeUniformityWeeks(uniformitys)
|
||||
if err := s.attachLatestApprovals(c.Context(), uniformitys); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -125,6 +126,7 @@ func (s uniformityService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectFlockKa
|
||||
s.Log.Errorf("Failed get uniformity by id: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
s.normalizeUniformityWeek(uniformity)
|
||||
if err := s.attachLatestApproval(c.Context(), uniformity); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -135,6 +137,23 @@ func (s uniformityService) GetSummary(c *fiber.Ctx, id uint) (*entity.ProjectFlo
|
||||
return s.GetOne(c, id)
|
||||
}
|
||||
|
||||
func (s *uniformityService) normalizeUniformityWeeks(items []entity.ProjectFlockKandangUniformity) {
|
||||
for i := range items {
|
||||
s.normalizeUniformityWeek(&items[i])
|
||||
}
|
||||
}
|
||||
|
||||
func (s *uniformityService) normalizeUniformityWeek(item *entity.ProjectFlockKandangUniformity) {
|
||||
if item == nil || item.UniformDate == nil {
|
||||
return
|
||||
}
|
||||
computedWeek, err := s.computeUniformityWeekForPFK(&item.ProjectFlockKandang, *item.UniformDate)
|
||||
if err != nil || computedWeek <= 0 {
|
||||
return
|
||||
}
|
||||
item.Week = computedWeek
|
||||
}
|
||||
|
||||
func (s uniformityService) GetStandard(c *fiber.Ctx, uniformity *entity.ProjectFlockKandangUniformity) (*utypes.UniformityStandard, error) {
|
||||
if uniformity == nil {
|
||||
return nil, nil
|
||||
@@ -372,24 +391,18 @@ func (s *uniformityService) CreateOne(c *fiber.Ctx, req *validation.Create, file
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
category := strings.TrimSpace(pfk.ProjectFlock.Category)
|
||||
if s.ProductionStandardRepo != nil && pfk.ProjectFlock.ProductionStandardId != 0 {
|
||||
if standard, err := s.ProductionStandardRepo.GetByID(c.Context(), pfk.ProjectFlock.ProductionStandardId, nil); err == nil {
|
||||
if strings.TrimSpace(standard.ProjectCategory) != "" {
|
||||
category = standard.ProjectCategory
|
||||
}
|
||||
}
|
||||
computedWeek, err := s.computeUniformityWeekForPFK(pfk, uniformDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
weekBase := 1
|
||||
isLayingCategory := strings.EqualFold(category, string(utils.ProjectFlockCategoryLaying))
|
||||
if isLayingCategory {
|
||||
weekBase = config.LayingWeekStart()
|
||||
}
|
||||
if req.Week < weekBase {
|
||||
if isLayingCategory {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("week must start from %d for laying projects", weekBase))
|
||||
}
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "week must start from 1 for growing projects")
|
||||
isGrowingCategory := strings.EqualFold(strings.TrimSpace(pfk.ProjectFlock.Category), string(utils.ProjectFlockCategoryGrowing))
|
||||
if req.Week > 0 && req.Week != computedWeek {
|
||||
s.Log.WithFields(logrus.Fields{
|
||||
"project_flock_kandang_id": req.ProjectFlockKandangId,
|
||||
"uniform_date": uniformDate.Format("2006-01-02"),
|
||||
"requested_week": req.Week,
|
||||
"computed_week": computedWeek,
|
||||
}).Warn("Uniformity week mismatch detected; using computed week")
|
||||
}
|
||||
|
||||
var latestWeek int
|
||||
@@ -400,17 +413,11 @@ func (s *uniformityService) CreateOne(c *fiber.Ctx, req *validation.Create, file
|
||||
Scan(&latestWeek).Error; err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate uniformity week sequence")
|
||||
}
|
||||
if latestWeek == 0 && req.Week != weekBase {
|
||||
if isLayingCategory {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("week must start from %d for laying projects", weekBase))
|
||||
}
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "week must start from 1 for growing projects")
|
||||
}
|
||||
if latestWeek > 0 && req.Week > latestWeek+1 {
|
||||
if latestWeek > 0 && computedWeek > latestWeek+1 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "week must be sequential without skipping")
|
||||
}
|
||||
|
||||
if err := s.ensureUniqueUniformity(c.Context(), 0, req.ProjectFlockKandangId, req.Week); err != nil {
|
||||
if err := s.ensureUniqueUniformity(c.Context(), 0, req.ProjectFlockKandangId, computedWeek); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -438,7 +445,7 @@ func (s *uniformityService) CreateOne(c *fiber.Ctx, req *validation.Create, file
|
||||
|
||||
createBody := &entity.ProjectFlockKandangUniformity{
|
||||
Uniformity: calculation.Uniformity,
|
||||
Week: req.Week,
|
||||
Week: computedWeek,
|
||||
Cv: calculation.Cv,
|
||||
ChickQtyOfWeight: calculation.ChickQtyOfWeight,
|
||||
MeanUp: calculation.MeanUp,
|
||||
@@ -467,7 +474,7 @@ func (s *uniformityService) CreateOne(c *fiber.Ctx, req *validation.Create, file
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.EqualFold(category, string(utils.ProjectFlockCategoryGrowing)) {
|
||||
if isGrowingCategory {
|
||||
if err := s.updateGrowingFcrForWeek(tx, createBody.ProjectFlockKandangId, createBody.Week, calculation.MeanUp); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -536,9 +543,6 @@ func (s uniformityService) UpdateOne(c *fiber.Ctx, req *validation.Update, id ui
|
||||
}
|
||||
updateBody["project_flock_kandang_id"] = *req.ProjectFlockKandangId
|
||||
}
|
||||
if req.Week != nil {
|
||||
updateBody["week"] = *req.Week
|
||||
}
|
||||
|
||||
if req.Date != nil || req.ProjectFlockKandangId != nil || req.Week != nil {
|
||||
current, err := s.Repository.GetByID(c.Context(), id, nil)
|
||||
@@ -552,15 +556,11 @@ func (s uniformityService) UpdateOne(c *fiber.Ctx, req *validation.Update, id ui
|
||||
if targetDate == nil {
|
||||
targetDate = current.UniformDate
|
||||
}
|
||||
targetWeek := current.Week
|
||||
if req.Week != nil {
|
||||
targetWeek = *req.Week
|
||||
}
|
||||
targetPFKID := current.ProjectFlockKandangId
|
||||
if req.ProjectFlockKandangId != nil {
|
||||
targetPFKID = *req.ProjectFlockKandangId
|
||||
}
|
||||
if targetPFKID != 0 && targetWeek > 0 {
|
||||
if targetPFKID != 0 && targetDate != nil {
|
||||
pfk, err := s.ProjectFlockKandangRepo.GetByID(c.Context(), targetPFKID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -568,28 +568,21 @@ func (s uniformityService) UpdateOne(c *fiber.Ctx, req *validation.Update, id ui
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
category := strings.TrimSpace(pfk.ProjectFlock.Category)
|
||||
if s.ProductionStandardRepo != nil && pfk.ProjectFlock.ProductionStandardId != 0 {
|
||||
if standard, err := s.ProductionStandardRepo.GetByID(c.Context(), pfk.ProjectFlock.ProductionStandardId, nil); err == nil {
|
||||
if strings.TrimSpace(standard.ProjectCategory) != "" {
|
||||
category = standard.ProjectCategory
|
||||
}
|
||||
}
|
||||
computedWeek, err := s.computeUniformityWeekForPFK(pfk, *targetDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
weekBase := 1
|
||||
isLayingCategory := strings.EqualFold(category, string(utils.ProjectFlockCategoryLaying))
|
||||
if isLayingCategory {
|
||||
weekBase = config.LayingWeekStart()
|
||||
if req.Week != nil && *req.Week != computedWeek {
|
||||
s.Log.WithFields(logrus.Fields{
|
||||
"uniformity_id": id,
|
||||
"project_flock_kandang_id": targetPFKID,
|
||||
"uniform_date": targetDate.Format("2006-01-02"),
|
||||
"requested_week": *req.Week,
|
||||
"computed_week": computedWeek,
|
||||
}).Warn("Uniformity week mismatch detected on update; using computed week")
|
||||
}
|
||||
if targetWeek < weekBase {
|
||||
if isLayingCategory {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("week must start from %d for laying projects", weekBase))
|
||||
}
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "week must start from 1 for growing projects")
|
||||
}
|
||||
}
|
||||
if targetDate != nil {
|
||||
if err := s.ensureUniqueUniformity(c.Context(), id, targetPFKID, targetWeek); err != nil {
|
||||
updateBody["week"] = computedWeek
|
||||
if err := s.ensureUniqueUniformity(c.Context(), id, targetPFKID, computedWeek); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -734,6 +727,51 @@ func (s *uniformityService) ensureUniqueUniformity(ctx context.Context, id uint,
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *uniformityService) computeUniformityWeekForPFK(pfk *entity.ProjectFlockKandang, uniformDate time.Time) (int, error) {
|
||||
if pfk == nil || pfk.Id == 0 {
|
||||
return 0, fiber.NewError(fiber.StatusBadRequest, "Project flock kandang tidak valid")
|
||||
}
|
||||
|
||||
chickInDate, ok := earliestUniformityChickInDate(pfk.Chickins)
|
||||
if !ok {
|
||||
return 0, fiber.NewError(fiber.StatusBadRequest, "Tanggal chick in tidak ditemukan")
|
||||
}
|
||||
|
||||
chickInDay := normalizeUniformityDateOnlyUTC(chickInDate)
|
||||
uniformDay := normalizeUniformityDateOnlyUTC(uniformDate)
|
||||
if uniformDay.Before(chickInDay) {
|
||||
return 0, fiber.NewError(fiber.StatusBadRequest, "Uniformity date tidak boleh sebelum tanggal chick in")
|
||||
}
|
||||
|
||||
diff := int(uniformDay.Sub(chickInDay).Hours() / 24)
|
||||
weekBase := 1
|
||||
if strings.EqualFold(strings.TrimSpace(pfk.ProjectFlock.Category), string(utils.ProjectFlockCategoryLaying)) {
|
||||
weekBase = config.LayingWeekStart()
|
||||
}
|
||||
|
||||
return (diff / 7) + weekBase, nil
|
||||
}
|
||||
|
||||
func earliestUniformityChickInDate(chickins []entity.ProjectChickin) (time.Time, bool) {
|
||||
var earliest time.Time
|
||||
for _, chickin := range chickins {
|
||||
if chickin.ChickInDate.IsZero() {
|
||||
continue
|
||||
}
|
||||
if earliest.IsZero() || chickin.ChickInDate.Before(earliest) {
|
||||
earliest = chickin.ChickInDate
|
||||
}
|
||||
}
|
||||
if earliest.IsZero() {
|
||||
return time.Time{}, false
|
||||
}
|
||||
return earliest, true
|
||||
}
|
||||
|
||||
func normalizeUniformityDateOnlyUTC(value time.Time) time.Time {
|
||||
return time.Date(value.UTC().Year(), value.UTC().Month(), value.UTC().Day(), 0, 0, 0, 0, time.UTC)
|
||||
}
|
||||
|
||||
func (s uniformityService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
if err := m.EnsureUniformityAccess(c, s.Repository.DB(), id); err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
)
|
||||
|
||||
func TestComputeUniformityWeekForPFK(t *testing.T) {
|
||||
originalWeekStart := config.TransferToLayingGrowingMaxWeek
|
||||
config.TransferToLayingGrowingMaxWeek = 19
|
||||
t.Cleanup(func() {
|
||||
config.TransferToLayingGrowingMaxWeek = originalWeekStart
|
||||
})
|
||||
|
||||
svc := &uniformityService{}
|
||||
baseDate := time.Date(2026, time.January, 1, 9, 30, 0, 0, time.UTC)
|
||||
|
||||
t.Run("growing starts from week one", func(t *testing.T) {
|
||||
pfk := &entity.ProjectFlockKandang{
|
||||
Id: 1,
|
||||
ProjectFlock: entity.ProjectFlock{
|
||||
Category: string(utils.ProjectFlockCategoryGrowing),
|
||||
},
|
||||
Chickins: []entity.ProjectChickin{
|
||||
{ChickInDate: baseDate},
|
||||
},
|
||||
}
|
||||
|
||||
week, err := svc.computeUniformityWeekForPFK(pfk, baseDate)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if week != 1 {
|
||||
t.Fatalf("expected week 1, got %d", week)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("laying uses configured week base and earliest chick in", func(t *testing.T) {
|
||||
pfk := &entity.ProjectFlockKandang{
|
||||
Id: 2,
|
||||
ProjectFlock: entity.ProjectFlock{
|
||||
Category: string(utils.ProjectFlockCategoryLaying),
|
||||
},
|
||||
Chickins: []entity.ProjectChickin{
|
||||
{ChickInDate: baseDate.AddDate(0, 0, 4)},
|
||||
{ChickInDate: baseDate},
|
||||
},
|
||||
}
|
||||
|
||||
week, err := svc.computeUniformityWeekForPFK(pfk, baseDate.AddDate(0, 0, 7))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if week != 20 {
|
||||
t.Fatalf("expected week 20, got %d", week)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("rejects date before chick in", func(t *testing.T) {
|
||||
pfk := &entity.ProjectFlockKandang{
|
||||
Id: 3,
|
||||
ProjectFlock: entity.ProjectFlock{
|
||||
Category: string(utils.ProjectFlockCategoryLaying),
|
||||
},
|
||||
Chickins: []entity.ProjectChickin{
|
||||
{ChickInDate: baseDate},
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := svc.computeUniformityWeekForPFK(pfk, baseDate.AddDate(0, 0, -1)); err == nil {
|
||||
t.Fatal("expected error for date before chick in")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
type Create struct {
|
||||
Date string `form:"date" validate:"required"`
|
||||
ProjectFlockKandangId uint `form:"project_flock_kandang_id" validate:"required,number,min=1"`
|
||||
Week int `form:"week" validate:"required,min=1"`
|
||||
Week int `form:"week" validate:"omitempty,min=1"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
@@ -120,14 +120,14 @@ func ParseCreate(c *fiber.Ctx) (*Create, *multipart.FileHeader, error) {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "project_flock_kandang_id is required")
|
||||
}
|
||||
|
||||
week := 0
|
||||
weekStr := strings.TrimSpace(c.FormValue("week"))
|
||||
if weekStr == "" {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "week is required")
|
||||
}
|
||||
|
||||
week, err := strconv.Atoi(weekStr)
|
||||
if err != nil || week <= 0 {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "week is required")
|
||||
if weekStr != "" {
|
||||
parsedWeek, err := strconv.Atoi(weekStr)
|
||||
if err != nil || parsedWeek <= 0 {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "week is invalid")
|
||||
}
|
||||
week = parsedWeek
|
||||
}
|
||||
|
||||
file, err := c.FormFile("document")
|
||||
|
||||
@@ -939,12 +939,27 @@ func (s *repportService) getUniformityByWeek(ctx context.Context, projectFlockKa
|
||||
return result, nil
|
||||
}
|
||||
|
||||
weekExpr := fmt.Sprintf(`CASE
|
||||
WHEN u.uniform_date IS NULL OR pc.chick_in_date IS NULL THEN 0
|
||||
WHEN u.uniform_date::date < pc.chick_in_date THEN 0
|
||||
WHEN UPPER(pf.category) = 'LAYING' THEN (((u.uniform_date::date - pc.chick_in_date)::int) / 7) + %d
|
||||
ELSE (((u.uniform_date::date - pc.chick_in_date)::int) / 7) + 1
|
||||
END`, config.LayingWeekStart())
|
||||
|
||||
var rows []entity.ProjectFlockKandangUniformity
|
||||
if err := s.db.WithContext(ctx).
|
||||
Model(&entity.ProjectFlockKandangUniformity{}).
|
||||
Select("week, uniformity, uniform_date, id, chart_data").
|
||||
Where("project_flock_kandang_id = ?", projectFlockKandangID).
|
||||
Where("week IN ?", weeks).
|
||||
Table("project_flock_kandang_uniformity AS u").
|
||||
Select(fmt.Sprintf("%s AS week, u.uniformity, u.uniform_date, u.id, u.chart_data", weekExpr)).
|
||||
Joins("JOIN project_flock_kandangs AS pfk ON pfk.id = u.project_flock_kandang_id").
|
||||
Joins("JOIN project_flocks AS pf ON pf.id = pfk.project_flock_id").
|
||||
Joins(`JOIN (
|
||||
SELECT project_flock_kandang_id, MIN(chick_in_date)::date AS chick_in_date
|
||||
FROM project_chickins
|
||||
WHERE deleted_at IS NULL
|
||||
GROUP BY project_flock_kandang_id
|
||||
) AS pc ON pc.project_flock_kandang_id = u.project_flock_kandang_id`).
|
||||
Where("u.project_flock_kandang_id = ?", projectFlockKandangID).
|
||||
Where(fmt.Sprintf("%s IN ?", weekExpr), weeks).
|
||||
Order("uniform_date DESC").
|
||||
Order("id DESC").
|
||||
Find(&rows).Error; err != nil {
|
||||
|
||||
Reference in New Issue
Block a user