StationTaskBatchService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace App\Services;
  3. use App\Exceptions\ErrorException;
  4. use App\StationTaskBatch;
  5. use App\StationTaskCommodity;
  6. use App\TaskTransaction;
  7. use Carbon\Carbon;
  8. use Exception;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Facades\Cache;
  11. use App\Traits\ServiceAppAop;
  12. use Illuminate\Support\Facades\DB;
  13. class StationTaskBatchService
  14. {
  15. use ServiceAppAop;
  16. protected $modelClass=StationTaskBatch::class;
  17. /** @var StationService $stationService */
  18. private $stationService;
  19. /** @var StationTaskBatchTypeService $stationTaskBatchTypeService */
  20. private $stationTaskBatchTypeService;
  21. /** @var BatchService $batchService */
  22. private $batchService;
  23. /** @var StationTypeService $stationTypeService */
  24. private $stationTypeService;
  25. /** @var StationTaskService $stationTaskService */
  26. private $stationTaskService;
  27. /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService */
  28. private $foreignHaiRoboticsService;
  29. public function __construct()
  30. {
  31. $this->stationService = null;
  32. $this->stationTypeService = null;
  33. $this->stationTaskBatchTypeService = null;
  34. $this->batchService = null;
  35. $this->stationTaskService = null;
  36. $this->foreignHaiRoboticsService = null;
  37. }
  38. /**
  39. * @param Collection $batches Batch[]
  40. * @param Collection $stationTasks_toAttach
  41. * @return Collection
  42. * @throws Exception
  43. */
  44. function createByBatches(Collection $batches, Collection $stationTasks_toAttach): Collection
  45. {
  46. $this->stationService = app('StationService');
  47. $this->stationTaskService = app('StationTaskService');
  48. $this->stationTypeService = app('StationTypeService');
  49. $this->stationTaskBatchTypeService = app('StationTaskBatchTypeService');
  50. $this->batchService = app('BatchService');
  51. $stationTaskBatches_toCreate = new Collection();
  52. $stationTaskBatchType = $this->stationTaskBatchTypeService->firstByWhere('name', 'U型线分捡');
  53. $id_stationTaskBatchType=$stationTaskBatchType['id']??'';
  54. $batches_handled = collect();
  55. foreach ($batches as $batch) {
  56. if ($batch['status'] != '已处理') {
  57. $stationType = $this->stationTypeService->getByBatch($batch);
  58. $station = $this->stationService->getStation_byType($stationType['name']);
  59. $stationTaskBatches_toCreate->push(
  60. new StationTaskBatch([
  61. 'batch_id' => $batch['id'],
  62. 'station_id' => $station['id'],
  63. 'station_task_batch_type_id' => $id_stationTaskBatchType,
  64. 'status' => '待处理'
  65. ])
  66. );
  67. $batches_handled->push($batch);
  68. }
  69. }
  70. $this->batchService->updateWhereIn('id', data_get($batches_handled, '*.id'), ['status' => '处理中']);
  71. $stationTaskBatches_toCreate=$stationTaskBatches_toCreate->reverse();//这里的波次顺序是反的,不知为什么,所以反向一次就好了。其他解耦的地方都是正序,必须保持一致
  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. $this->stationTaskService->registerStations(
  81. $stationTasks_toAttach,
  82. data_get($stationTaskBatches_toCreate,'*.station_id')
  83. );
  84. return $stationTaskBatches_toCreate;
  85. }
  86. function getAndAttachIds($stationTaskBatches): Collection
  87. {
  88. $md5=md5(is_array($stationTaskBatches)
  89. ?$md5=json_encode($stationTaskBatches):$stationTaskBatches->toJson());
  90. return Cache::remember(
  91. 'StationTaskBatch_'.$md5??md5(json_encode($stationTaskBatches->toArray()))
  92. ,config('cache.expirations.rarelyChange')
  93. ,function()use($stationTaskBatches){
  94. return StationTaskBatch::query()
  95. ->whereIn('status',data_get($stationTaskBatches,'*.status'))
  96. ->whereIn('batch_id',data_get($stationTaskBatches,'*.batch_id'))
  97. ->orderByDesc('id')
  98. ->limit(count($stationTaskBatches))
  99. ->get();
  100. });
  101. }
  102. function markManyExcepted(Collection $stationTaskBatches_failed)
  103. {
  104. foreach (
  105. $stationTaskBatches_failed
  106. as $stationTaskBatch) {
  107. if($stationTaskBatch['status']!='异常')
  108. $this->markExcepted($stationTaskBatch);
  109. }
  110. ($logAtFailings_andWait =
  111. function ($stationTaskBatches_failed) {
  112. if ($stationTaskBatches_failed->isEmpty()) return;
  113. throw new ErrorException('任务波次异常失败的');
  114. })($stationTaskBatches_failed);
  115. }
  116. /**
  117. * @param Collection|null $stationTaskBatches
  118. * @param string $locationType
  119. * @return Collection|\Tightenco\Collect\Support\Collection|null 返回执行失败的记录
  120. * @throws ErrorException
  121. */
  122. function runMany(?Collection $stationTaskBatches,string $locationType = 'OUTBIN-U-SHAPE-LINE'):?Collection
  123. {
  124. LogService::log(__METHOD__,'runMany','波次任务分配6.1:'.json_encode($stationTaskBatches));
  125. $stationTaskBatches_failed = null;
  126. ($execute = function(Collection $stationTaskBatches, &$stationTaskBatches_failed)use($locationType){
  127. if ($stationTaskBatches->isEmpty()) return; //波次任务不存在 跳出
  128. $stationTaskBatches_failed = collect();
  129. foreach ($stationTaskBatches as $stationTaskBatch){
  130. $failed = !$this->run($stationTaskBatch, $locationType); //运行波次 获取执行结果
  131. if ($failed) $stationTaskBatches_failed->push($stationTaskBatch);//执行失败 记录失败波次
  132. }
  133. })($stationTaskBatches, $stationTaskBatches_failed);
  134. (function ($stationTaskBatches_failed) {
  135. if ($stationTaskBatches_failed->isEmpty()) return;
  136. $retry_after_sec = config('task.batchTask.retry_after_sec');
  137. LogService::log(__METHOD__, __FUNCTION__,
  138. '任务波次没有执行完的,' . $retry_after_sec . '秒后准备重试:' . $stationTaskBatches_failed->toJson()
  139. . '调用堆栈r:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
  140. );
  141. sleep($retry_after_sec);
  142. })($stationTaskBatches_failed);
  143. $execute($stationTaskBatches_failed, $stationTaskBatches_failed); //再次尝试
  144. $this->markManyExcepted($stationTaskBatches_failed);
  145. return $stationTaskBatches_failed;
  146. }
  147. /**
  148. * 解析波次任务 任务下发海柔
  149. *
  150. * @param StationTaskBatch $stationTaskBatch
  151. * @param string $locationType
  152. * @return bool
  153. * @throws ErrorException
  154. */
  155. function run(StationTaskBatch $stationTaskBatch,string $locationType): bool
  156. {
  157. $this->instant($this->foreignHaiRoboticsService,'ForeignHaiRoboticsService');
  158. $this->instant($this->stationService,'StationService');
  159. $stationTaskBatch->loadMissing(['station','stationTask.stationTaskMaterialBoxes']);
  160. LogService::log(__METHOD__,'runMany','波次任务分配6.r2:'.json_encode($stationTaskBatch));
  161. $groupPrefix = $stationTaskBatch['id'];//将波次任务ID当作组前缀
  162. $taskMaterialBoxes = $stationTaskBatch['stationTask']['stationTaskMaterialBoxes'] ??
  163. (function () use ($stationTaskBatch) {
  164. LogService::log(__METHOD__,'runMany','波次任务分配6.r4:'.json_encode($stationTaskBatch));
  165. throw new Exception('找不到料箱:' . json_encode($stationTaskBatch));
  166. })();//存在任务返回任务 否则抛出无料箱异常
  167. DB::beginTransaction();
  168. try{
  169. //获取放线入口
  170. switch ($locationType){
  171. case 'OUTBIN-U-SHAPE-LINE'://U型线
  172. $toLocation = $this->stationService->getULineEntrance($stationTaskBatch['station'])['code'];
  173. $isFetchedFromRobotics = $this->foreignHaiRoboticsService->
  174. fetchGroup($toLocation, $taskMaterialBoxes, $groupPrefix);//执行料箱任务
  175. break;
  176. case 'OUTBIN-CACHE-SHELF'://缓存架
  177. list($toLocation, $taskMaterialBoxes, $map) = $this->apportionLocation($taskMaterialBoxes);
  178. if ($toLocation->count()>0){
  179. $isFetchedFromRobotics = $this->foreignHaiRoboticsService->
  180. fetchGroup_multiLocation($toLocation, $taskMaterialBoxes, $groupPrefix, '立架出至缓存架',20,false);
  181. foreach ($toLocation as $value){
  182. app("CacheShelfService")->lightUp($value,'3','0',["title"=>"机器人取箱中,禁止操作"]);
  183. Cache::forever("CACHE_SHELF_OCCUPANCY_{$map[$value]}",true);
  184. }
  185. app("StationService")->locationOccupyMulti($toLocation->toArray());
  186. }else $isFetchedFromRobotics = true;
  187. break;
  188. default:
  189. $isFetchedFromRobotics = false;
  190. }
  191. DB::commit();
  192. LogService::log(__METHOD__,'runMany','波次任务分配6.r6:'.json_encode($stationTaskBatch));
  193. }catch(Exception $e){
  194. DB::rollBack();
  195. throw new ErrorException('$stationTaskBatch运行波次机器人任务失败,获取组失败: '.$stationTaskBatch->toJson() . $e->getMessage());
  196. }
  197. (function()use($isFetchedFromRobotics,$stationTaskBatch){
  198. $isFetchedFromRobotics?
  199. $this->markProcessing($stationTaskBatch)://执行成功标记已处理
  200. $this->markExcepted($stationTaskBatch);//执行失败标记失败
  201. })();
  202. return $isFetchedFromRobotics;//返回执行结果
  203. }
  204. function markProcessing($stationTaskBatch_orCollection)
  205. {
  206. if (get_class($stationTaskBatch_orCollection)==StationTaskBatch::class){
  207. $stationTaskBatch_orCollection = collect([$stationTaskBatch_orCollection]);
  208. }
  209. $taskIds = data_get($stationTaskBatch_orCollection, '*.id');
  210. $this->markProcessing_byIds($taskIds);
  211. //app("StorageService")->handleStorage($stationTaskBatch_orCollection);
  212. }
  213. function markProcessing_byIds($ids)
  214. {
  215. if(!$ids)$ids=[];
  216. if(!is_array($ids))$ids=[$ids];
  217. $hasProcessing=StationTaskBatch::query()
  218. ->whereNotIn('id', $ids)
  219. ->where(['status'=>'处理中'])
  220. ->where('created_at','>',Carbon::now()->subDay())
  221. ->get('id')->isNotEmpty();
  222. $status = '处理中';
  223. if($hasProcessing)
  224. $status = '处理队列';
  225. StationTaskBatch::query()
  226. ->whereIn('id', $ids)
  227. ->update(['status'=>$status]);
  228. }
  229. function markProcessed($stationTaskBatch_orCollection)
  230. {
  231. if (get_class($stationTaskBatch_orCollection)==StationTaskBatch::class){
  232. $stationTaskBatch_orCollection = collect([$stationTaskBatch_orCollection]);
  233. }
  234. $this->markProcessed_byIds(data_get($stationTaskBatch_orCollection, '*.id'));
  235. }
  236. function markProcessed_byIds($ids)
  237. {
  238. if(!$ids)$ids=[];
  239. if(!is_array($ids))$ids=[$ids];
  240. StationTaskBatch::query()
  241. ->whereIn('id', $ids)
  242. ->update(['status'=>'完成']);
  243. }
  244. function markExcepted(StationTaskBatch $stationTaskBatch)
  245. {
  246. $stationTaskBatch['status'] = '异常';
  247. $stationTaskBatch ->update();
  248. }
  249. /**
  250. * 为任务分配缓存架库位
  251. *
  252. * @param \Illuminate\Database\Eloquent\Collection $taskMaterialBoxes
  253. *
  254. * @return array
  255. */
  256. public function apportionLocation(Collection $taskMaterialBoxes):array
  257. {
  258. $taskMaterialBoxes->loadMissing(["stationTaskCommodities.order","stationTaskCommodities.commodity.barcodes"]);
  259. //获取可用的库位 加行锁
  260. $stations = app("StationService")->getCacheShelf(true);
  261. $location = [];
  262. $map = [];
  263. foreach ($stations as $station){
  264. $location[] = $station->code;
  265. $map[$station->code] = $station->id;
  266. }
  267. /** @var Collection $handleTask */
  268. $handleTask = $taskMaterialBoxes->splice(0,count($location));
  269. $toLocation = collect();
  270. $updateTask = [["id","station_id"]];
  271. $insertTransaction = [];
  272. $dateTime = date("Y-m-d H:i:s");
  273. $exeInsert = function ($task,$taskCommodity,$status)use(&$insertTransaction,$dateTime){
  274. $insertTransaction[] = [
  275. "doc_code" => $taskCommodity->order->code ?? "",
  276. "bar_code" => $taskCommodity->commodity->barcodes[0]->code ?? "",
  277. "to_station_id" => $task->station_id,
  278. "material_box_id" => $task->material_box_id,
  279. "task_id" => $task->id,
  280. "commodity_id" => $taskCommodity->commodity_id,
  281. "amount" => $taskCommodity->amount,
  282. "type" => "出库",
  283. "status" => $status,
  284. "mark" => 2,
  285. "bin_number"=>$taskCommodity->bin_number,
  286. "created_at"=>$dateTime,
  287. "updated_at"=>$dateTime,
  288. ];
  289. };
  290. foreach ($handleTask as $index=>$task){
  291. $task->station_id = $map[$location[$index]];
  292. $toLocation->push($location[$index]);
  293. $handleTask->offsetSet($index,$task);
  294. $updateTask[] = ["id"=>$task->id,"station_id"=>$task->station_id];
  295. foreach ($task->stationTaskCommodities as $taskCommodity)$exeInsert($task,$taskCommodity,0);
  296. }
  297. if ($handleTask->count()>0)app("BatchUpdateService")->batchUpdate("station_task_material_boxes",$updateTask);
  298. foreach ($taskMaterialBoxes as $obj)foreach ($obj->stationTaskCommodities as $taskCommodity)$exeInsert($obj,$taskCommodity,3);
  299. TaskTransaction::query()->insert($insertTransaction);
  300. return array($toLocation, $handleTask, $map);
  301. }
  302. }