StationTaskBatchService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\Log;
  5. use App\Station;
  6. use App\StationTask;
  7. use App\StationTaskBatch;
  8. use App\StationTaskBatchType;
  9. use Exception;
  10. use Illuminate\Support\Collection;
  11. use Illuminate\Support\Facades\Cache;
  12. class StationTaskBatchService
  13. {
  14. /** @var StationService $stationService */
  15. private $stationService;
  16. /** @var StationTaskBatchTypeService $stationTaskBatchTypeService */
  17. private $stationTaskBatchTypeService;
  18. /** @var BatchService $batchService */
  19. private $batchService;
  20. /** @var StationTypeService $stationTypeService */
  21. private $stationTypeService;
  22. /** @var StationTaskService $stationTaskService */
  23. private $stationTaskService;
  24. /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService */
  25. private $foreignHaiRoboticsService;
  26. public function __construct(){
  27. $this->stationService=null;
  28. $this->stationTypeService=null;
  29. $this->stationTaskBatchTypeService=null;
  30. $this->batchService=null;
  31. $this->stationTaskService=null;
  32. $this->foreignHaiRoboticsService=null;
  33. }
  34. /**
  35. * @param $batches Batch[]
  36. * @param Collection $stationTasks_toAttach
  37. * @return Collection
  38. * @throws Exception
  39. */
  40. function createByBatches(array $batches, Collection $stationTasks_toAttach): Collection
  41. {
  42. $this->stationService=app('StationService');
  43. $this->stationTypeService=app('StationTypeService');
  44. $this->stationTaskBatchTypeService=app('StationTaskBatchTypeService');
  45. $this->batchService=app('BatchService');
  46. $stationTaskBatches_toCreate=new Collection();
  47. $id_stationTaskBatchType=$this->stationTaskBatchTypeService->firstByWhere('name','U型线分捡');
  48. $batches_handled=collect();
  49. foreach ($batches as $batch){
  50. if ($batch['status']=='未处理'){
  51. $stationType=$this->stationTypeService->getByBatch($batch);
  52. $station=$this->stationService->getStation_byType($stationType['name']);
  53. $stationTaskBatches_toCreate->push([
  54. 'batch_id'=>$batch['id'],
  55. 'station_id'=>$station['id'],
  56. 'station_task_batch_type_id'=> $id_stationTaskBatchType,
  57. 'status'=>'待处理'
  58. ]);
  59. $batches_handled->push($batch);
  60. }
  61. }
  62. $this->batchService->updateWhereIn('id',data_get($batches_handled,'*.id'),['status'=>'处理中']);
  63. $this->insert($stationTaskBatches_toCreate->toArray());
  64. $this->stationTaskService->registerSubTasks($stationTasks_toAttach,
  65. $stationTaskBatches_toCreate->map(function ($stationMissionBatch){
  66. return [$stationMissionBatch];
  67. })
  68. );
  69. return $stationTaskBatches_toCreate;
  70. }
  71. function insert(array $stationMissionBatches): bool
  72. {
  73. $inserted = StationTaskBatch::query()->insert($stationMissionBatches);
  74. LogService::log(__METHOD__,__FUNCTION__,json_encode($stationMissionBatches).
  75. '||'.json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),0,3)));
  76. return $inserted;
  77. }
  78. function runMany(Collection $stationTaskBatches)
  79. {
  80. foreach ($stationTaskBatches as $stationTaskBatch) {
  81. $this->run($stationTaskBatch);
  82. }
  83. }
  84. function run(StationTaskBatch $stationTaskBatch)
  85. {
  86. $toLocation = $stationTaskBatch['station']['code'];
  87. $groupPrefix = $stationTaskBatch['id'];
  88. $taskMaterialBoxes = $stationTaskBatch['parentTask']['taskMaterialBoxes']??
  89. function()use($stationTaskBatch){throw new Exception('找不到料箱:'.json_encode($stationTaskBatch));};
  90. $this->foreignHaiRoboticsService->fetchGroupToProcessor($toLocation,$taskMaterialBoxes,$groupPrefix);
  91. }
  92. }