StationTaskBatchService.php 8.4 KB

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