mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Merge branch 'development' of https://gitlab.com/mbugroup/lti-api into dev/teguh
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ProductionResultRepository interface {
|
||||
GetRecordingsByProjectFlockKandang(ctx context.Context, projectFlockKandangID uint, offset, limit int) ([]entity.Recording, int64, error)
|
||||
}
|
||||
|
||||
type productionResultRepositoryImpl struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewProductionResultRepository(db *gorm.DB) ProductionResultRepository {
|
||||
return &productionResultRepositoryImpl{db: db}
|
||||
}
|
||||
|
||||
func (r *productionResultRepositoryImpl) GetRecordingsByProjectFlockKandang(
|
||||
ctx context.Context,
|
||||
projectFlockKandangID uint,
|
||||
offset, limit int,
|
||||
) ([]entity.Recording, int64, error) {
|
||||
if projectFlockKandangID == 0 {
|
||||
return []entity.Recording{}, 0, nil
|
||||
}
|
||||
|
||||
countQuery := r.db.WithContext(ctx).
|
||||
Model(&entity.Recording{}).
|
||||
Where("project_flock_kandangs_id = ?", projectFlockKandangID)
|
||||
|
||||
var total int64
|
||||
if err := countQuery.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if total == 0 {
|
||||
return []entity.Recording{}, 0, nil
|
||||
}
|
||||
|
||||
if limit <= 0 {
|
||||
limit = 10
|
||||
}
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
|
||||
flagNames := []string{
|
||||
string(utils.FlagTelurUtuh),
|
||||
string(utils.FlagTelurPutih),
|
||||
string(utils.FlagTelurRetak),
|
||||
string(utils.FlagTelurPecah),
|
||||
}
|
||||
|
||||
dataQuery := r.db.WithContext(ctx).
|
||||
Model(&entity.Recording{}).
|
||||
Where("project_flock_kandangs_id = ?", projectFlockKandangID).
|
||||
Preload("BodyWeights").
|
||||
Preload("Eggs", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Select("recording_eggs.*, f.name AS product_flag_name").
|
||||
Joins("LEFT JOIN product_warehouses pw ON pw.id = recording_eggs.product_warehouse_id").
|
||||
Joins("LEFT JOIN flags f ON f.flagable_id = pw.product_id AND f.flagable_type = ? AND f.name IN ?", entity.FlagableTypeProduct, flagNames)
|
||||
}).
|
||||
Preload("Eggs.ProductWarehouse").
|
||||
Order("record_datetime ASC").
|
||||
Offset(offset).
|
||||
Limit(limit)
|
||||
|
||||
var recordings []entity.Recording
|
||||
if err := dataQuery.Find(&recordings).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return recordings, total, nil
|
||||
}
|
||||
Reference in New Issue
Block a user