StationTaskBatchService.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. {
  28. $this->stationService = null;
  29. $this->stationTypeService = null;
  30. $this->stationTaskBatchTypeService = null;
  31. $this->batchService = null;
  32. $this->stationTaskService = null;
  33. $this->foreignHaiRoboticsService = null;
  34. }
  35. /**
  36. * @param $batches Batch[]
  37. * @param Collection $stationTasks_toAttach
  38. * @return Collection
  39. * @throws Exception
  40. */
  41. function createByBatches(array $batches, Collection $stationTasks_toAttach): Collection
  42. {
  43. $this->stationService = app('StationService');
  44. $this->stationTypeService = app('StationTypeService');
  45. $this->stationTaskBatchTypeService = app('StationTaskBatchTypeService');
  46. $this->batchService = app('BatchService');
  47. $stationTaskBatches_toCreate = new Collection();
  48. $id_stationTaskBatchType = $this->stationTaskBatchTypeService->firstByWhere('name', 'U型线分捡');
  49. $batches_handled = collect();
  50. foreach ($batches as $batch) {
  51. if ($batch['status'] == '未处理') {
  52. $stationType = $this->stationTypeService->getByBatch($batch);
  53. $station = $this->stationService->getStation_byType($stationType['name']);
  54. $stationTaskBatches_toCreate->push([
  55. 'batch_id' => $batch['id'],
  56. 'station_id' => $station['id'],
  57. 'station_task_batch_type_id' => $id_stationTaskBatchType,
  58. 'status' => '待处理'
  59. ]);
  60. $batches_handled->push($batch);
  61. }
  62. }
  63. $this->batchService->updateWhereIn('id', data_get($batches_handled, '*.id'), ['status' => '处理中']);
  64. $this->insert($stationTaskBatches_toCreate->toArray());
  65. $this->stationTaskService->registerSubTasks($stationTasks_toAttach,
  66. $stationTaskBatches_toCreate->map(function ($stationMissionBatch) {
  67. return [$stationMissionBatch];
  68. })
  69. );
  70. return $stationTaskBatches_toCreate;
  71. }
  72. function insert(array $stationMissionBatches): bool
  73. {
  74. $inserted = StationTaskBatch::query()->insert($stationMissionBatches);
  75. LogService::log(__METHOD__, __FUNCTION__, json_encode($stationMissionBatches) .
  76. '||' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3)));
  77. return $inserted;
  78. }
  79. function markManyExcepted(Collection $stationTaskBatches_failed)
  80. {
  81. foreach (
  82. $stationTaskBatches_failed
  83. as $stationTaskBatch) {
  84. if($stationTaskBatch['status']!='异常')
  85. $this->markExcepted($stationTaskBatch);
  86. }
  87. ($logAtFailings_andWait =
  88. function ($stationTaskBatches_failed) {
  89. if ($stationTaskBatches_failed->isEmpty()) return;
  90. LogService::log(__METHOD__, __FUNCTION__,
  91. '任务波次异常失败的:' . $stationTaskBatches_failed->toJson()
  92. . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
  93. );
  94. })($stationTaskBatches_failed);
  95. }
  96. /**
  97. * @param Collection $stationTaskBatches
  98. * @return Collection|\Tightenco\Collect\Support\Collection|null 返回执行失败的记录
  99. */
  100. function runMany(Collection $stationTaskBatches)
  101. {
  102. $stationTaskBatches_failed = null;
  103. ($execute =
  104. function (
  105. Collection $stationTaskBatches, &$stationTaskBatches_failed) {
  106. if ($stationTaskBatches->isEmpty()) return;
  107. $stationTaskBatches_failed = collect();
  108. foreach ($stationTaskBatches as $stationTaskBatch) {
  109. $failed = !$this->run($stationTaskBatch);
  110. if ($failed) $stationTaskBatches_failed->push($stationTaskBatch);
  111. }
  112. })($stationTaskBatches, $stationTaskBatches_failed);
  113. ($logAtFailings_andWait =
  114. function ($stationTaskBatches_failed) {
  115. if ($stationTaskBatches_failed->isEmpty()) return;
  116. $retry_after_sec = config('task.batchTask.retry_after_sec');
  117. LogService::log(__METHOD__, __FUNCTION__,
  118. '任务波次没有执行完的,' . $retry_after_sec . '秒后准备重试:' . $stationTaskBatches_failed->toJson()
  119. . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
  120. );
  121. sleep($retry_after_sec);
  122. })($stationTaskBatches_failed);
  123. $execute ($stationTaskBatches_failed, $stationTaskBatches_failed); //再次尝试
  124. $this->markManyExcepted ($stationTaskBatches_failed);
  125. return $stationTaskBatches_failed;
  126. }
  127. function run(StationTaskBatch $stationTaskBatch): bool
  128. {
  129. $toLocation = $stationTaskBatch['station']['code'];
  130. $groupPrefix = $stationTaskBatch['id'];
  131. $taskMaterialBoxes = $stationTaskBatch['stationTask']['taskMaterialBoxes'] ??
  132. function () use ($stationTaskBatch) {
  133. throw new Exception('找不到料箱:' . json_encode($stationTaskBatch));
  134. };
  135. $isFetchedFromRobotics = $this->foreignHaiRoboticsService->
  136. fetchGroupToProcessor($toLocation, $taskMaterialBoxes, $groupPrefix);
  137. ($markNewStatus
  138. =function()use($isFetchedFromRobotics,$stationTaskBatch){
  139. $isFetchedFromRobotics?
  140. $this->markProcessing($stationTaskBatch):
  141. $this->markExcepted($stationTaskBatch);
  142. })();
  143. return $isFetchedFromRobotics;
  144. }
  145. function markProcessing(StationTaskBatch $stationTaskBatch)
  146. {
  147. $stationTaskBatch ['status'] = '处理中';
  148. $stationTaskBatch ->update();
  149. $stationTaskBatch ->loadMissing ('stationTask');
  150. $stationTaskBatch ['stationTask']['status'] = '异常';
  151. $stationTaskBatch ['stationTask'] ->update();
  152. }
  153. function markExcepted(StationTaskBatch $stationTaskBatch)
  154. {
  155. $stationTaskBatch['status'] = '异常';
  156. $stationTaskBatch ->update();
  157. }
  158. }