StationTaskBatchService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. $this->stationTaskService->registerStations(
  76. $stationTasks_toAttach,
  77. data_get($stationTaskBatches_toCreate,'*.station_id')
  78. );
  79. return $stationTaskBatches_toCreate;
  80. }
  81. function getAndAttachIds($stationTaskBatches): Collection
  82. {
  83. $md5=md5(is_array($stationTaskBatches)
  84. ?$md5=json_encode($stationTaskBatches):$stationTaskBatches->toJson());
  85. return Cache::remember(
  86. 'StationTaskBatch_'.$md5??md5(json_encode($stationTaskBatches->toArray()))
  87. ,config('cache.expirations.rarelyChange')
  88. ,function()use($stationTaskBatches){
  89. return StationTaskBatch::query()
  90. ->whereIn('status',data_get($stationTaskBatches,'*.status'))
  91. ->whereIn('batch_id',data_get($stationTaskBatches,'*.batch_id'))
  92. ->get();
  93. });
  94. }
  95. function markManyExcepted(Collection $stationTaskBatches_failed)
  96. {
  97. foreach (
  98. $stationTaskBatches_failed
  99. as $stationTaskBatch) {
  100. if($stationTaskBatch['status']!='异常')
  101. $this->markExcepted($stationTaskBatch);
  102. }
  103. ($logAtFailings_andWait =
  104. function ($stationTaskBatches_failed) {
  105. if ($stationTaskBatches_failed->isEmpty()) return;
  106. throw new ErrorException('任务波次异常失败的');
  107. })($stationTaskBatches_failed);
  108. }
  109. /**
  110. * @param Collection $stationTaskBatches
  111. * @return Collection|\Tightenco\Collect\Support\Collection|null 返回执行失败的记录
  112. */
  113. function runMany(Collection $stationTaskBatches):?Collection
  114. {
  115. LogService::log(__METHOD__,'runMany','波次任务分配6.1:'.json_encode($stationTaskBatches));
  116. $stationTaskBatches_failed = null;
  117. ($execute =
  118. function (
  119. Collection $stationTaskBatches, &$stationTaskBatches_failed) {
  120. LogService::log(__METHOD__,'runMany','波次任务分配6.2:'.json_encode($stationTaskBatches));
  121. if ($stationTaskBatches->isEmpty()) return;
  122. LogService::log(__METHOD__,'runMany','波次任务分配6.3:'.json_encode($stationTaskBatches));
  123. $stationTaskBatches_failed = collect();
  124. LogService::log(__METHOD__,'runMany','波次任务分配6.4:'.json_encode($stationTaskBatches));
  125. foreach ($stationTaskBatches as $stationTaskBatch) {
  126. LogService::log(__METHOD__,'runMany','波次任务分配6.5:'.json_encode($stationTaskBatches));
  127. $failed = !$this->run($stationTaskBatch);
  128. LogService::log(__METHOD__,'runMany','波次任务分配6.6:'.json_encode($stationTaskBatches));
  129. if ($failed) $stationTaskBatches_failed->push($stationTaskBatch);
  130. }
  131. })($stationTaskBatches, $stationTaskBatches_failed);
  132. ($logAtFailings_andWait =
  133. function ($stationTaskBatches_failed) {
  134. LogService::log(__METHOD__,'runMany','波次任务分配6.7:'.json_encode($stationTaskBatches_failed));
  135. if ($stationTaskBatches_failed->isEmpty()) return;
  136. LogService::log(__METHOD__,'runMany','波次任务分配6.8:'.json_encode($stationTaskBatches_failed));
  137. $retry_after_sec = config('task.batchTask.retry_after_sec');
  138. LogService::log(__METHOD__, __FUNCTION__,
  139. '任务波次没有执行完的,' . $retry_after_sec . '秒后准备重试:' . $stationTaskBatches_failed->toJson()
  140. . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
  141. );
  142. LogService::log(__METHOD__,'runMany','波次任务分配6.9:'.json_encode($stationTaskBatches_failed));
  143. sleep($retry_after_sec);
  144. LogService::log(__METHOD__,'runMany','波次任务分配6.91:'.json_encode($stationTaskBatches_failed));
  145. })($stationTaskBatches_failed);
  146. LogService::log(__METHOD__,'runMany','波次任务分配6.92:'.json_encode($stationTaskBatches));
  147. $execute ($stationTaskBatches_failed, $stationTaskBatches_failed); //再次尝试
  148. LogService::log(__METHOD__,'runMany','波次任务分配6.93:'.json_encode($stationTaskBatches));
  149. $this->markManyExcepted ($stationTaskBatches_failed);
  150. LogService::log(__METHOD__,'runMany','波次任务分配6.94:'.json_encode($stationTaskBatches));
  151. return $stationTaskBatches_failed;
  152. }
  153. function run(StationTaskBatch $stationTaskBatch): bool
  154. {
  155. LogService::log(__METHOD__,'runMany','波次任务分配6.r1:'.json_encode($stationTaskBatch));
  156. $this->instant($this->foreignHaiRoboticsService,'ForeignHaiRoboticsService');
  157. $this->instant($this->stationService,'StationService');
  158. $stationTaskBatch->loadMissing(['station','stationTask.stationTaskMaterialBoxes']);
  159. LogService::log(__METHOD__,'runMany','波次任务分配6.r2:'.json_encode($stationTaskBatch));
  160. $toLocation = $this->stationService->getULineEntrance($stationTaskBatch['station'])['code'];
  161. LogService::log(__METHOD__,'runMany','波次任务分配6.r3:'.json_encode($stationTaskBatch));
  162. $groupPrefix = $stationTaskBatch['id'];
  163. $taskMaterialBoxes = $stationTaskBatch['stationTask']['stationTaskMaterialBoxes'] ??
  164. (function () use ($stationTaskBatch) {
  165. LogService::log(__METHOD__,'runMany','波次任务分配6.r4:'.json_encode($stationTaskBatch));
  166. throw new Exception('找不到料箱:' . json_encode($stationTaskBatch));
  167. })();
  168. try{
  169. LogService::log(__METHOD__,'runMany','波次任务分配6.r5:'.json_encode($stationTaskBatch));
  170. $isFetchedFromRobotics
  171. = $this->foreignHaiRoboticsService->
  172. fetchGroup($toLocation, $taskMaterialBoxes, $groupPrefix);
  173. LogService::log(__METHOD__,'runMany','波次任务分配6.r6:'.json_encode($stationTaskBatch));
  174. }catch(Exception $e){
  175. throw new ErrorException('$stationTaskBatch运行波次机器人任务失败,获取组失败: '.$stationTaskBatch->toJson() . $e->getMessage());
  176. }
  177. LogService::log(__METHOD__,'runMany','波次任务分配6.r7:'.json_encode($stationTaskBatch));
  178. ($markNewStatus
  179. =function()use($isFetchedFromRobotics,$stationTaskBatch){
  180. $isFetchedFromRobotics?
  181. $this->markProcessing($stationTaskBatch):
  182. $this->markExcepted($stationTaskBatch);
  183. LogService::log(__METHOD__,'runMany','波次任务分配6.r8:'.json_encode($stationTaskBatch));
  184. })();
  185. LogService::log(__METHOD__,'runMany','波次任务分配6.r9:'.json_encode($stationTaskBatch));
  186. return $isFetchedFromRobotics;
  187. }
  188. function markProcessing($stationTaskBatch_orCollection)
  189. {
  190. if (get_class($stationTaskBatch_orCollection)==StationTaskBatch::class){
  191. $stationTaskBatch_orCollection = collect([$stationTaskBatch_orCollection]);
  192. }
  193. $this->markProcessing_byIds(data_get($stationTaskBatch_orCollection, '*.id'));
  194. }
  195. function markProcessing_byIds($ids)
  196. {
  197. if(!$ids)$ids=[];
  198. if(!is_array($ids))$ids=[$ids];
  199. StationTaskBatch::query()
  200. ->whereIn('id', $ids)
  201. ->update(['status'=>'处理中']);
  202. }
  203. // function markFinished($stationTaskBatches)
  204. // {
  205. // if (get_class($stationTaskBatches)==StationTaskBatch::class){
  206. // $stationTaskBatches = collect($stationTaskBatches);
  207. // }
  208. // StationTaskBatch::query()
  209. // ->whereIn('id', data_get($stationTaskBatches, '*.id'))
  210. // ->update(['status'=>'完成']);
  211. //
  212. // $this->stationTaskService
  213. // ->markProcessing_byId(
  214. // data_get($stationTaskBatches, '*.station_id')
  215. // );
  216. // }
  217. function markExcepted(StationTaskBatch $stationTaskBatch)
  218. {
  219. $stationTaskBatch['status'] = '异常';
  220. $stationTaskBatch ->update();
  221. }
  222. }