StationTaskBatchService.php 7.8 KB

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