StationTaskBatchService.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. $stationTaskBatchType = $this->stationTaskBatchTypeService->firstByWhere('name', 'U型线分捡');
  50. $id_stationTaskBatchType=$stationTaskBatchType['id']??'';
  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. fetchGroup($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($stationTaskBatches)
  148. {
  149. if (get_class($stationTaskBatches)==StationTaskBatch::class){
  150. $stationTaskBatches = collect($stationTaskBatches);
  151. }
  152. StationTaskBatch::query()
  153. ->whereIn('id', data_get($stationTaskBatches, '*.id'))
  154. ->update(['status'=>'处理中']);
  155. $this->stationTaskService
  156. ->markProcessing_byId(
  157. data_get($stationTaskBatches, '*.station_id')
  158. );
  159. }
  160. function markExcepted(StationTaskBatch $stationTaskBatch)
  161. {
  162. $stationTaskBatch['status'] = '异常';
  163. $stationTaskBatch ->update();
  164. }
  165. }