StationRuleBatchService.php 1.5 KB

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