StationRuleBatchService.php 1.6 KB

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