StationRuleBatchService.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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('stationType_type_'.$batchType.'_ownerId_'.$ownerId, config('cache.expirations.rarelyChange'),function($batch){
  15. return StationRuleBatch::query()->with('stationType')->where('batch_type',$batch['type'])
  16. ->where('owner_id',$batch['owner_id'])
  17. ->first();
  18. });
  19. }
  20. function getStationType_toBeTask(Batch $batch): ?StationType{
  21. $stationRuleBatch=$this->getByBatch($batch);
  22. if(!$stationRuleBatch)return null;
  23. return $stationRuleBatch['stationType'];
  24. }
  25. /**
  26. * @param Batch[] $batches
  27. * @return Collection
  28. */
  29. function getBatches_canProcess(array $batches): Collection
  30. {
  31. $batches_toProcess=collect();
  32. foreach ($batches as $batch){
  33. $stationRuleBatch=$this->getByBatch($batch);
  34. if($stationRuleBatch)
  35. $batches_toProcess->push($batch);
  36. }
  37. return $batches_toProcess;
  38. }
  39. }