StationTaskBatchService.php 8.4 KB

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