StationRuleBatchService.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\StationRuleBatch;
  5. use App\StationType;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. class StationRuleBatchService
  9. {
  10. function getByBatch(Batch $batch): ?StationRuleBatch
  11. {
  12. $batchType = $batch['type'] ?? 'null';
  13. $ownerId = $batch['owner_id'] ?? 'null';
  14. return Cache::remember('stationRuleBatch_batchType_'.$batchType.'_ownerId_'.$ownerId, config('cache.expirations.rarelyChange'),function()use($batch){
  15. return StationRuleBatch::query()->with('stationType')
  16. ->where('batch_type',$batch['type'])
  17. ->where('owner_id',$batch['owner_id'])
  18. ->first();
  19. });
  20. }
  21. function getStationType_toBeTask(Batch $batch): ?StationType{
  22. $stationRuleBatch=$this->getByBatch($batch);
  23. if(!$stationRuleBatch)return null;
  24. return $stationRuleBatch['stationType'];
  25. }
  26. /**
  27. * @param Collection $batches
  28. * @return Collection Batches
  29. */
  30. function getBatches_shouldProcess(Collection $batches): Collection
  31. {
  32. $batches_toProcess=collect();
  33. foreach ($batches as $batch){
  34. $stationRuleBatch=$this->getByBatch($batch);
  35. if($stationRuleBatch)
  36. $batches_toProcess->push($batch);
  37. }
  38. return $batches_toProcess;
  39. }
  40. }