StationRuleBatchService.php 1.4 KB

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