StationRuleBatchService.php 3.1 KB

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