StationRuleBatchService.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\StationRuleBatch;
  5. use App\StationTaskBatch;
  6. use App\StationType;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\Cache;
  9. use App\Traits\ServiceAppAop;
  10. class StationRuleBatchService
  11. {
  12. use ServiceAppAop;
  13. protected $modelClass=StationRuleBatch::class;
  14. function getByBatch(Batch $batch): ?StationRuleBatch
  15. {
  16. $batchType = $batch['type'] ?? 'null';
  17. $ownerId = $batch['owner_id'] ?? 'null';
  18. $batch->loadMissing('stationTaskBatch');
  19. if($batch['stationTaskBatch'])return null;
  20. return Cache::remember('stationRuleBatch_batchType_'.$batchType.'_ownerId_'.$ownerId, config('cache.expirations.rarelyChange'),function()use($batch){
  21. $builder= StationRuleBatch::query()->with('stationType')
  22. ->where('owner_id',$batch['owner_id']);
  23. if($batch['type']
  24. && $batch['type']!='无'){
  25. $builder=$builder->where('batch_type',$batch['type']);
  26. }
  27. return $builder->first();
  28. });
  29. }
  30. function getStationType_toBeTask(Batch $batch): ?StationType{
  31. $stationRuleBatch=$this->getByBatch($batch);
  32. if(!$stationRuleBatch)return null;
  33. return $stationRuleBatch['stationType'];
  34. }
  35. /**
  36. * @param Collection $batches
  37. * @return Collection Batches
  38. */
  39. function getBatches_shouldProcess(Collection $batches): Collection
  40. {
  41. $batches_toProcess=collect();
  42. $batches_inTask=StationTaskBatch::query()->whereIn('batch_id',data_get($batches,'*.id'))->get();
  43. $batches=$batches->whereNotIn('id',data_get($batches_inTask,'*.id')??[]);
  44. foreach ($batches as $batch){
  45. $stationRuleBatch=$this->getByBatch($batch);
  46. if($stationRuleBatch)
  47. $batches_toProcess->push($batch);
  48. }
  49. return $batches_toProcess;
  50. }
  51. }