StationRuleBatchService.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\StationRuleBatch;
  5. use App\StationTaskBatch;
  6. use App\StationType;
  7. use Carbon\Carbon;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Facades\Cache;
  10. use App\Traits\ServiceAppAop;
  11. use Illuminate\Support\Facades\DB;
  12. class StationRuleBatchService
  13. {
  14. use ServiceAppAop;
  15. protected $modelClass=StationRuleBatch::class;
  16. function getByBatch(?Batch $batch): ?StationRuleBatch
  17. {
  18. LogService::log(__METHOD__,'getByBatch','波次任务分配1.21:'.json_encode($batch));
  19. $batchType = $batch['type'] ?? 'null';
  20. $ownerId = $batch['owner_id'] ?? 'null';
  21. if(!$this->isLocationOfRobot($batch))return null;
  22. LogService::log(__METHOD__,'getByBatch','波次任务分配1.22:'.($this->isLocationOfRobot($batch)));
  23. $batch->loadMissing('stationTaskBatch');
  24. // if($batch['stationTaskBatch'])return null;//? 这行有啥用?
  25. return Cache::remember('stationRuleBatch_batchType_'.$batchType.'_ownerId_'.$ownerId, config('cache.expirations.rarelyChange'),function()use($batch){
  26. $builder= StationRuleBatch::query()->with('stationType')
  27. ->where('owner_id',$batch['owner_id']);
  28. if($batch['type']
  29. && $batch['type']!='无'){
  30. $builder=$builder->where('batch_type',$batch['type']);
  31. }
  32. LogService::log(__METHOD__,'getByBatch','波次任务分配1.23:');
  33. return $builder->first();
  34. });
  35. }
  36. function isLocationOfRobot(?Batch $batch): bool
  37. {
  38. if(!$batch)return false;
  39. $sql = "select count(*) as count from order_commodities where location like 'IDE%' and order_id in (select id from orders where batch_id in (select id from batches where id = ?))";
  40. $billDetails = DB::select(DB::raw($sql),[$batch['id']]);
  41. return $billDetails[0]->count>0;
  42. }
  43. function getStationType_toBeTask(Batch $batch): ?StationType{
  44. $stationRuleBatch=$this->getByBatch($batch);
  45. if(!$stationRuleBatch)return null;
  46. return $stationRuleBatch['stationType'];
  47. }
  48. /**
  49. * @param Collection $batches
  50. * @return Collection Batches
  51. */
  52. function getBatches_shouldProcess(Collection $batches): Collection
  53. {
  54. LogService::log(__METHOD__,'shouldProcess','波次任务分配1.1:'.json_encode($batches));
  55. $batches_toProcess=collect();
  56. $stationTaskBatches_inTask=StationTaskBatch::query()->whereIn('batch_id',data_get($batches,'*.id'))->get();
  57. LogService::log(__METHOD__,'shouldProcess','波次任务分配1.2:'.json_encode($stationTaskBatches_inTask));
  58. $batches=$batches->whereNotIn('id',data_get($stationTaskBatches_inTask,'*.batch_id')??[]);
  59. foreach ($batches as $batch){
  60. $stationRuleBatch=$this->getByBatch($batch);
  61. if(!$stationRuleBatch)continue;
  62. if(Cache::tags(['波次防重叠'.$batch['id']])->get($batch['id']))
  63. continue;
  64. if($batch->created_at>Carbon::now()->subDay())
  65. $batches_toProcess->push($batch);
  66. Cache::tags([ '波次防重叠'.$batch['id']])->put($batch['id'],true,config('haiRou.波次防重叠时间_秒'));
  67. }
  68. LogService::log(__METHOD__,'shouldProcess','波次任务分配1.3:'.json_encode($batches_toProcess));
  69. return $batches_toProcess;
  70. }
  71. }