StationRuleBatchService.php 1.7 KB

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