mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 15:25:43 +00:00
init depresiasi
This commit is contained in:
@@ -517,7 +517,14 @@ func (s *recordingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
||||
return nil, transactionErr
|
||||
}
|
||||
|
||||
return s.GetOne(c, createdRecording.Id)
|
||||
created, err := s.GetOne(c, createdRecording.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if created != nil {
|
||||
s.invalidateDepreciationSnapshots(c.Context(), nil, created.ProjectFlockKandangId, created.RecordDatetime)
|
||||
}
|
||||
return created, nil
|
||||
}
|
||||
|
||||
func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Recording, error) {
|
||||
@@ -848,6 +855,13 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
if err := recordingutil.AttachProductionStandards(ctx, s.Repository.DB(), false, s.Log, updatedRecording); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
invalidateFromDate := commonSvc.MinNonZeroDateOnlyUTC(recordingEntity.RecordDatetime, updatedRecording.RecordDatetime)
|
||||
s.invalidateDepreciationSnapshots(
|
||||
c.Context(),
|
||||
nil,
|
||||
updatedRecording.ProjectFlockKandangId,
|
||||
invalidateFromDate,
|
||||
)
|
||||
return updatedRecording, nil
|
||||
}
|
||||
|
||||
@@ -965,6 +979,12 @@ func (s recordingService) Approval(c *fiber.Ctx, req *validation.Approve) ([]ent
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(
|
||||
c.Context(),
|
||||
nil,
|
||||
recording.ProjectFlockKandangId,
|
||||
recording.RecordDatetime,
|
||||
)
|
||||
updated = append(updated, *recording)
|
||||
}
|
||||
|
||||
@@ -985,7 +1005,7 @@ func (s recordingService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
}
|
||||
note := recordingutil.RecordingNote("Delete", id)
|
||||
|
||||
return s.Repository.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
err = s.Repository.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
recording, err := s.Repository.WithTx(tx).GetByID(ctx, id, nil)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -1029,9 +1049,60 @@ func (s recordingService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
s.Log.Errorf("Failed to recalculate recordings after delete: %+v", err)
|
||||
return err
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(ctx, tx, recording.ProjectFlockKandangId, recording.RecordDatetime)
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s recordingService) invalidateDepreciationSnapshots(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
projectFlockKandangID uint,
|
||||
fromDate time.Time,
|
||||
) {
|
||||
if projectFlockKandangID == 0 || fromDate.IsZero() {
|
||||
return
|
||||
}
|
||||
|
||||
targetDB := s.Repository.DB()
|
||||
if tx != nil {
|
||||
targetDB = tx
|
||||
}
|
||||
|
||||
farmIDs, err := commonSvc.ResolveProjectFlockIDsByProjectFlockKandangIDs(ctx, targetDB, []uint{projectFlockKandangID})
|
||||
if err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to resolve farm for recording depreciation invalidation (pfk=%d): %+v",
|
||||
projectFlockKandangID,
|
||||
err,
|
||||
)
|
||||
farmIDs = nil
|
||||
}
|
||||
|
||||
if len(farmIDs) == 0 {
|
||||
if err := commonSvc.InvalidateFarmDepreciationSnapshotsFromDate(ctx, targetDB, nil, fromDate); err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to invalidate depreciation snapshots globally (from=%s): %+v",
|
||||
fromDate.Format("2006-01-02"),
|
||||
err,
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err := commonSvc.InvalidateFarmDepreciationSnapshotsFromDate(ctx, targetDB, farmIDs, fromDate); err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to invalidate depreciation snapshots (farm_ids=%v, from=%s): %+v",
|
||||
farmIDs,
|
||||
fromDate.Format("2006-01-02"),
|
||||
err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *recordingService) resolveRecordingCategory(ctx context.Context, recording *entity.Recording) (string, error) {
|
||||
|
||||
Reference in New Issue
Block a user