StationTaskBatchService.php 7.1 KB

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