StationTaskBatchService.php 7.7 KB

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