StationTaskBatchService.php 7.2 KB

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