mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 05:21:57 +00:00
99 lines
3.8 KiB
Go
99 lines
3.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type DailyChecklistEmptyKandangRepository interface {
|
|
repository.BaseRepository[entity.DailyChecklistEmptyKandang]
|
|
FindByDailyChecklistID(ctx context.Context, dailyChecklistID uint) (*entity.DailyChecklistEmptyKandang, error)
|
|
FindOverlapping(ctx context.Context, kandangID uint, startDate, endDate time.Time, excludeDailyChecklistID uint) (*entity.DailyChecklistEmptyKandang, error)
|
|
FindActiveCoveringDate(ctx context.Context, kandangID uint, date time.Time) (*entity.DailyChecklistEmptyKandang, error)
|
|
FindOverlappingInRange(ctx context.Context, kandangIDs []uint, rangeStart, rangeEnd time.Time) ([]entity.DailyChecklistEmptyKandang, error)
|
|
SoftDeleteByDailyChecklistID(ctx context.Context, dailyChecklistID uint, actorID *uint) error
|
|
}
|
|
|
|
type DailyChecklistEmptyKandangRepositoryImpl struct {
|
|
*repository.BaseRepositoryImpl[entity.DailyChecklistEmptyKandang]
|
|
}
|
|
|
|
func NewDailyChecklistEmptyKandangRepository(db *gorm.DB) DailyChecklistEmptyKandangRepository {
|
|
return &DailyChecklistEmptyKandangRepositoryImpl{
|
|
BaseRepositoryImpl: repository.NewBaseRepository[entity.DailyChecklistEmptyKandang](db),
|
|
}
|
|
}
|
|
|
|
func (r *DailyChecklistEmptyKandangRepositoryImpl) FindByDailyChecklistID(ctx context.Context, dailyChecklistID uint) (*entity.DailyChecklistEmptyKandang, error) {
|
|
var rec entity.DailyChecklistEmptyKandang
|
|
if err := r.DB().WithContext(ctx).
|
|
Where("daily_checklist_id = ?", dailyChecklistID).
|
|
First(&rec).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return &rec, nil
|
|
}
|
|
|
|
func (r *DailyChecklistEmptyKandangRepositoryImpl) FindOverlapping(ctx context.Context, kandangID uint, startDate, endDate time.Time, excludeDailyChecklistID uint) (*entity.DailyChecklistEmptyKandang, error) {
|
|
var rec entity.DailyChecklistEmptyKandang
|
|
query := r.DB().WithContext(ctx).
|
|
Where("kandang_id = ? AND start_date <= ? AND end_date >= ?", kandangID, endDate, startDate)
|
|
if excludeDailyChecklistID > 0 {
|
|
query = query.Where("daily_checklist_id <> ?", excludeDailyChecklistID)
|
|
}
|
|
if err := query.First(&rec).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return &rec, nil
|
|
}
|
|
|
|
func (r *DailyChecklistEmptyKandangRepositoryImpl) FindActiveCoveringDate(ctx context.Context, kandangID uint, date time.Time) (*entity.DailyChecklistEmptyKandang, error) {
|
|
var rec entity.DailyChecklistEmptyKandang
|
|
if err := r.DB().WithContext(ctx).
|
|
Where("kandang_id = ? AND start_date <= ? AND end_date >= ?", kandangID, date, date).
|
|
First(&rec).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return &rec, nil
|
|
}
|
|
|
|
func (r *DailyChecklistEmptyKandangRepositoryImpl) FindOverlappingInRange(ctx context.Context, kandangIDs []uint, rangeStart, rangeEnd time.Time) ([]entity.DailyChecklistEmptyKandang, error) {
|
|
if len(kandangIDs) == 0 {
|
|
return []entity.DailyChecklistEmptyKandang{}, nil
|
|
}
|
|
var recs []entity.DailyChecklistEmptyKandang
|
|
if err := r.DB().WithContext(ctx).
|
|
Where("kandang_id IN ? AND start_date <= ? AND end_date >= ?", kandangIDs, rangeEnd, rangeStart).
|
|
Find(&recs).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return recs, nil
|
|
}
|
|
|
|
func (r *DailyChecklistEmptyKandangRepositoryImpl) SoftDeleteByDailyChecklistID(ctx context.Context, dailyChecklistID uint, actorID *uint) error {
|
|
updates := map[string]any{
|
|
"deleted_at": time.Now(),
|
|
}
|
|
if actorID != nil {
|
|
updates["deleted_by"] = *actorID
|
|
}
|
|
return r.DB().WithContext(ctx).
|
|
Model(&entity.DailyChecklistEmptyKandang{}).
|
|
Where("daily_checklist_id = ? AND deleted_at IS NULL", dailyChecklistID).
|
|
Updates(updates).Error
|
|
}
|