StationTaskBatchService.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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(
  68. $stationTasks_toAttach,
  69. $stationTaskBatches_toCreate->map(function ($stationMissionBatch) {
  70. return collect($stationMissionBatch);
  71. })
  72. );
  73. return $stationTaskBatches_toCreate;
  74. }
  75. function insert(array $stationMissionBatches): bool
  76. {
  77. $inserted = StationTaskBatch::query()->insert($stationMissionBatches);
  78. LogService::log(__METHOD__, __FUNCTION__, json_encode($stationMissionBatches) .
  79. '||' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3)));
  80. return $inserted;
  81. }
  82. function markManyExcepted(Collection $stationTaskBatches_failed)
  83. {
  84. foreach (
  85. $stationTaskBatches_failed
  86. as $stationTaskBatch) {
  87. if($stationTaskBatch['status']!='异常')
  88. $this->markExcepted($stationTaskBatch);
  89. }
  90. ($logAtFailings_andWait =
  91. function ($stationTaskBatches_failed) {
  92. if ($stationTaskBatches_failed->isEmpty()) return;
  93. LogService::log(__METHOD__, __FUNCTION__,
  94. '任务波次异常失败的:' . $stationTaskBatches_failed->toJson()
  95. . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
  96. );
  97. })($stationTaskBatches_failed);
  98. }
  99. /**
  100. * @param Collection $stationTaskBatches
  101. * @return Collection|\Tightenco\Collect\Support\Collection|null 返回执行失败的记录
  102. */
  103. function runMany(Collection $stationTaskBatches)
  104. {
  105. $stationTaskBatches_failed = null;
  106. ($execute =
  107. function (
  108. Collection $stationTaskBatches, &$stationTaskBatches_failed) {
  109. if ($stationTaskBatches->isEmpty()) return;
  110. $stationTaskBatches_failed = collect();
  111. foreach ($stationTaskBatches as $stationTaskBatch) {
  112. $failed = !$this->run($stationTaskBatch);
  113. if ($failed) $stationTaskBatches_failed->push($stationTaskBatch);
  114. }
  115. })($stationTaskBatches, $stationTaskBatches_failed);
  116. ($logAtFailings_andWait =
  117. function ($stationTaskBatches_failed) {
  118. if ($stationTaskBatches_failed->isEmpty()) return;
  119. $retry_after_sec = config('task.batchTask.retry_after_sec');
  120. LogService::log(__METHOD__, __FUNCTION__,
  121. '任务波次没有执行完的,' . $retry_after_sec . '秒后准备重试:' . $stationTaskBatches_failed->toJson()
  122. . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
  123. );
  124. sleep($retry_after_sec);
  125. })($stationTaskBatches_failed);
  126. $execute ($stationTaskBatches_failed, $stationTaskBatches_failed); //再次尝试
  127. $this->markManyExcepted ($stationTaskBatches_failed);
  128. return $stationTaskBatches_failed;
  129. }
  130. function run(StationTaskBatch $stationTaskBatch): bool
  131. {
  132. $toLocation = $stationTaskBatch['station']['code'];
  133. $groupPrefix = $stationTaskBatch['id'];
  134. $taskMaterialBoxes = $stationTaskBatch['stationTask']['taskMaterialBoxes'] ??
  135. function () use ($stationTaskBatch) {
  136. throw new Exception('找不到料箱:' . json_encode($stationTaskBatch));
  137. };
  138. $isFetchedFromRobotics = $this->foreignHaiRoboticsService->
  139. fetchGroup($toLocation, $taskMaterialBoxes, $groupPrefix);
  140. ($markNewStatus
  141. =function()use($isFetchedFromRobotics,$stationTaskBatch){
  142. $isFetchedFromRobotics?
  143. $this->markProcessing($stationTaskBatch):
  144. $this->markExcepted($stationTaskBatch);
  145. })();
  146. return $isFetchedFromRobotics;
  147. }
  148. function markProcessing($stationTaskBatches)
  149. {
  150. if (get_class($stationTaskBatches)==StationTaskBatch::class){
  151. $stationTaskBatches = collect($stationTaskBatches);
  152. }
  153. StationTaskBatch::query()
  154. ->whereIn('id', data_get($stationTaskBatches, '*.id'))
  155. ->update(['status'=>'处理中']);
  156. $this->stationTaskService
  157. ->markProcessing_byId(
  158. data_get($stationTaskBatches, '*.station_id')
  159. );
  160. }
  161. function markExcepted(StationTaskBatch $stationTaskBatch)
  162. {
  163. $stationTaskBatch['status'] = '异常';
  164. $stationTaskBatch ->update();
  165. }
  166. }