StationTaskBatchService.php 12 KB

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