StationTaskMaterialBoxService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\Components\ErrorPush;
  5. use App\Exceptions\ErrorException;
  6. use App\Jobs\CacheShelfTaskJob;
  7. use App\MaterialBox;
  8. use App\OrderCommodity;
  9. use App\Station;
  10. use App\StationTask;
  11. use App\StationTaskMaterialBox;
  12. use Carbon\Carbon;
  13. use Illuminate\Support\Collection;
  14. use Illuminate\Support\Facades\Cache;
  15. use App\Traits\ServiceAppAop;
  16. use Illuminate\Support\Facades\DB;
  17. class StationTaskMaterialBoxService
  18. {
  19. use ServiceAppAop,ErrorPush;
  20. protected $modelClass=StationTaskMaterialBox::class;
  21. /** @var StationService $stationService */
  22. private $stationService;
  23. /** @var StationTypeService $stationTypeService */
  24. private $stationTypeService;
  25. /** @var StationTaskService $stationTaskService */
  26. private $stationTaskService;
  27. /** @var StationTaskBatchService $stationTaskBatchService */
  28. private $stationTaskBatchService;
  29. /** @var StationTaskCommodityService $stationTaskCommodityService */
  30. private $stationTaskCommodityService;
  31. /** @var OrderCommodityService $orderCommodityService */
  32. private $orderCommodityService;
  33. /** @var MaterialBoxService $materialBoxService */
  34. private $materialBoxService;
  35. /** @var CacheShelfService $cacheShelfService */
  36. private $cacheShelfService;
  37. /** @var StorageService $storageService */
  38. private $storageService;
  39. public function __construct(){
  40. $this->stationService=null;
  41. $this->stationTypeService=null;
  42. $this->stationTaskService=null;
  43. $this->materialBoxService=null;
  44. $this->stationTaskBatchService=null;
  45. $this->stationTaskCommodityService=null;
  46. }
  47. function create($kvPairs)
  48. {
  49. return StationTaskMaterialBox::query()->create($kvPairs);
  50. }
  51. function createByStationAndMaterialBox($station, $materialBox)
  52. {
  53. return StationTaskMaterialBox::query()->create([
  54. 'station_id' => $station['id'],
  55. 'material_box_id' => $materialBox['id'],
  56. 'status' => '待处理'
  57. ]);
  58. }
  59. function getOccupied_byBatches(?Collection $batches): ?Collection
  60. {
  61. return StationTaskMaterialBox::query()
  62. ->where('status','<>','完成')
  63. ->where('created_at','>',Carbon::now()->subHours(2))
  64. ->whereHas('materialBox',function ($query)use($batches){
  65. $locations=OrderCommodity::query()
  66. ->whereHas('order',function ($queryO)use($batches){
  67. $queryO->whereIn('batch_id',data_get($batches,'*.id')??[]);
  68. })->get('location');
  69. $query->whereIn('code',data_get($locations,'*.location')??[]);
  70. })
  71. ->get();
  72. }
  73. function createByBatches(Collection $batches,Collection $stationTasks_toAttach): Collection
  74. {
  75. $this->instant($this->stationTaskService,'StationTaskService');
  76. LogService::log(__METHOD__,'assignTasks','波次任务分配4.c1:'.json_encode($batches));
  77. $stationTaskMaterialBoxes_byBatch = (function () use ($batches) {
  78. $stationTaskMaterialBoxes_listByBatch = new Collection();
  79. foreach ($batches as $batch) {
  80. $stationTaskMaterialBoxes_listByBatch->push(
  81. $this->createByBatch($batch)
  82. );
  83. }
  84. return $stationTaskMaterialBoxes_listByBatch;
  85. })();
  86. LogService::log(__METHOD__,'assignTasks','波次任务分配4.c2:'.json_encode($batches));
  87. $this->stationTaskService
  88. ->registerSubTasks(
  89. $stationTasks_toAttach,
  90. $stationTaskMaterialBoxes_byBatch);
  91. return collect(data_get($stationTaskMaterialBoxes_byBatch,'*.*'));
  92. }
  93. function createByBatch(Batch $batch): ?Collection
  94. {
  95. $this->instant($this->materialBoxService,'MaterialBoxService');
  96. $this->instant($this->stationTypeService,'StationTypeService');
  97. $this->instant($this->stationService,'StationService');
  98. $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
  99. $this->instant($this->orderCommodityService,'OrderCommodityService');
  100. $stationMaterialBoxes_toCreate=new Collection();
  101. $order_ids=data_get($batch['orders'],'*.id');
  102. $orderCommodities=OrderCommodity::query()->orderByRaw("commodity_id,amount")//同商品多条 数量最小优先
  103. ->with('orderBin')->whereIn('order_id',$order_ids)->get();
  104. //$orderCommodities=$this->orderCommodityService->correctLocation_fromWMS($orderCommodities);
  105. if($orderCommodities->isEmpty())return $stationMaterialBoxes_toCreate;
  106. $stationType=$this->stationTypeService->getForMaterialBox_onBatchProcess();
  107. $stationTaskBatch=$this->stationTaskBatchService->get(['batch_id'=>$batch['id']])->first();
  108. $materialBoxIds_used=[];
  109. //$orderCommodities=$orderCommodities->sortBy('commodity_id');//按商品排序后,出货可以同商品挨在一起
  110. foreach ($orderCommodities as $orderCommodity){
  111. $station=$this->stationService->getStation_byType($stationType['name']);
  112. $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
  113. if(in_array($materialBox['id'],$materialBoxIds_used))continue;
  114. $stationMaterialBoxes_toCreate->push([
  115. 'station_id'=>$station['id'],
  116. 'material_box_id'=>$materialBox['id'],
  117. 'station_task_batch_id'=>$stationTaskBatch['id'],
  118. 'status'=>'待处理'
  119. ]);
  120. $materialBoxIds_used[]=$materialBox['id'];
  121. }
  122. return $this->insert($stationMaterialBoxes_toCreate->toArray(),true);
  123. }
  124. function get(array $kvPairs, $with=null){
  125. ksort($kvPairs);
  126. return Cache::remember('StationTaskMaterialBox'.md5(json_encode($kvPairs).json_encode([$with])), config('cache.expirations.fastChange'), function ()use($kvPairs,$with) {
  127. $query = StationTaskMaterialBox::query();
  128. if($with){
  129. $query->with($with);
  130. }
  131. foreach ($kvPairs as $column => $value){
  132. if (is_array($value))$query->whereIn($column,$value);
  133. else $query->where($column,$value);
  134. }
  135. return $query->get();
  136. });
  137. }
  138. function markHasPut(StationTaskMaterialBox $stationTaskMaterialBox){
  139. $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
  140. $this->instant($this->stationTaskCommodityService,'StationTaskCommodityService');
  141. $this->instant($this->stationTaskService,'StationTaskService');
  142. $this->instant($this->stationService,'StationService');
  143. $this->instant($this->cacheShelfService,'CacheShelfService');
  144. $this->instant($this->storageService,'StorageService');
  145. try{
  146. $taskType=$this->getServingTaskType($stationTaskMaterialBox);
  147. LogService::log('海柔请求','markHasTaken2',
  148. json_encode($stationTaskMaterialBox));
  149. switch ($taskType){
  150. case '分波次':
  151. $this->markProcessing($stationTaskMaterialBox);
  152. $this->stationTaskBatchService->markProcessing_byIds($stationTaskMaterialBox['station_task_batch_id']);
  153. $this->stationTaskCommodityService->markProcessing($stationTaskMaterialBox['stationTaskCommodities']);
  154. $this->stationTaskService->markProcessing_byIds(data_get($stationTaskMaterialBox['stationTaskCommodities'],'*.station_task_id'));
  155. /** @var StationTask $stationTask */
  156. $stationTask = $this->stationTaskService->getProcessing();
  157. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'], $stationTask);
  158. $stationTaskMaterialBox->materialBox['status']='在U型线';
  159. $stationTaskMaterialBox->materialBox->update();
  160. break;
  161. case '入立库':
  162. $stationTaskMaterialBox->materialBox['status']='在立库';
  163. $stationTaskMaterialBox->materialBox->update();
  164. break;
  165. case '入缓存架':
  166. $stationTaskMaterialBox->materialBox['status']='在缓存架';
  167. $stationTaskMaterialBox->materialBox->update();
  168. $stationTaskMaterialBox->loadMissing("station"); //提前加载站,后续都需要站信息来处理
  169. $this->storageService->putCacheShelf($stationTaskMaterialBox);
  170. break;
  171. default:;
  172. }
  173. $this->taskCompleted($stationTaskMaterialBox);
  174. }catch (\Exception $e){
  175. throw new ErrorException('放置料箱出错');
  176. }
  177. }
  178. private function taskCompleted($stationTaskMaterialBox)
  179. {
  180. $this->set($stationTaskMaterialBox,[
  181. 'id' => $stationTaskMaterialBox['station_id'],
  182. 'status' => '完成',
  183. ]);
  184. if (!$stationTaskMaterialBox->station_task_id)return;
  185. $task = StationTaskMaterialBox::query()->select(DB::raw(1))
  186. ->where("station_task_id",$stationTaskMaterialBox->station_task_id)
  187. ->where("status","!=","完成")->first();
  188. if (!$task)StationTask::query()->where("id",$stationTaskMaterialBox->station_task_id)
  189. ->update(["status"=>"完成"]);
  190. }
  191. /**
  192. * 取出料箱通知
  193. *
  194. * @param StationTaskMaterialBox|\stdClass $stationTaskMaterialBox
  195. * @throws \Exception
  196. */
  197. function markHasTaken($stationTaskMaterialBox)
  198. {
  199. $this->instant($this->cacheShelfService,'CacheShelfService');
  200. //$stationTaskMaterialBox->loadMissing("station");
  201. //判断取出料箱是否是在缓存架
  202. $station = Station::query()->where("material_box_id",$stationTaskMaterialBox->material_box_id)
  203. ->first();
  204. if ($station && app("StationService")->isHalfBoxLocation($station))
  205. app("CacheShelfService")->boxHasBeenTaken($station);
  206. /*//维护缓存架可用度
  207. $map = Cache::get("CACHE_SHELF_MAPPING",function (){return [];});
  208. if (isset($map[$stationTaskMaterialBox->material_box_id])){
  209. $available=Cache::get("CACHE_SHELF_AVAILABLE",function (){return [];});
  210. $available[$map[$stationTaskMaterialBox->material_box_id]] = true;
  211. Cache::forever("CACHE_SHELF_AVAILABLE",$available);
  212. CacheShelfTaskJob::dispatch("CACHE_SHELF_AVAILABLE",count($available))->delay(now()->addSeconds(config("haiRou.cacheShelf.outBinAwait")));
  213. unset($map[$stationTaskMaterialBox->material_box_id]);
  214. Cache::forever("CACHE_SHELF_MAPPING",$map);
  215. }*/
  216. }
  217. function processNextQueued(?StationTaskMaterialBox $stationTaskMaterialBox_lastProcessed){
  218. $station_id=$stationTaskMaterialBox_lastProcessed['station_id'];
  219. $stationTaskMaterialBox_next=StationTaskMaterialBox::query()
  220. ->where('station_id',$station_id)
  221. ->where('status','处理队列')
  222. ->orderBy('updated_at')
  223. ->first();
  224. if($stationTaskMaterialBox_next){
  225. $stationTaskMaterialBox_next->update(['status'=>'处理中']);
  226. }
  227. return $stationTaskMaterialBox_next;
  228. }
  229. function markProcessed(StationTaskMaterialBox $stationTaskMaterialBox){
  230. $stationTaskMaterialBox['status'] = '完成';
  231. $stationTaskMaterialBox->save();
  232. }
  233. function getNotProcessedSiblings($stationTaskMaterialBox){
  234. return StationTaskMaterialBox::query()
  235. ->whereNotIn('status',['完成'])
  236. ->where('station_task_id',$stationTaskMaterialBox['station_task_id'])
  237. ->get();
  238. }
  239. /**
  240. * 每波次仅将最老的作务标为“处理中”,其他置入队列;
  241. * 如某波次已经有“处理中“,则他部置入队列
  242. * @param $stationTaskMaterialBox_orBoxes ?? 单个或多个
  243. */
  244. function markProcessing($stationTaskMaterialBox_orBoxes)
  245. {
  246. $this->instant($this->stationTaskService,'StationTaskService');
  247. $stationTaskMaterialBoxes =
  248. (function()use($stationTaskMaterialBox_orBoxes){
  249. if (get_class($stationTaskMaterialBox_orBoxes)==StationTaskMaterialBox::class){
  250. return collect([$stationTaskMaterialBox_orBoxes]);
  251. }
  252. return collect($stationTaskMaterialBox_orBoxes);
  253. })();
  254. $stationTaskMaterialBoxes_grouped=
  255. ($按时间从前往后排出顺序=function ()use(&$stationTaskMaterialBoxes){
  256. return $stationTaskMaterialBoxes
  257. ->sortBy('id')
  258. ->groupBy('station_task_batch_id');
  259. })();
  260. $stationTaskMaterialBoxes_grouped->each(function(&$groupByBatch){
  261. ($将所有要标记的箱任务先放在队列里=function()use(&$groupByBatch){
  262. $groupByBatch->each(function (&$stationTaskMaterialBox){
  263. $stationTaskMaterialBox['status']='处理队列';
  264. });
  265. })();
  266. ($如果之前没有处理中则标记第一个为处理目标,准备持久化=function()use(&$groupByBatch){
  267. $stationId=$groupByBatch[0]['station_id'];
  268. $processing=$this->getProcessing_byStationId($stationId);
  269. if(!$processing){
  270. $groupByBatch[0]['status']='处理中';
  271. $groupByBatch[0]->update();
  272. }else{
  273. foreach ($groupByBatch as &$stationTaskMaterialBox){
  274. if($stationTaskMaterialBox['id']==$processing['id']){
  275. $stationTaskMaterialBox['status']='处理中';
  276. }
  277. }
  278. }
  279. })();
  280. });
  281. ($持久化处理队列的记录=function()use(&$stationTaskMaterialBoxes_grouped){
  282. $toArray = $stationTaskMaterialBoxes_grouped->collapse();
  283. $toArray=$toArray->where('status','处理队列');
  284. $ids_toUpdate = data_get($toArray, '*.id');
  285. if(count($ids_toUpdate))
  286. StationTaskMaterialBox::query()->whereIn('id',$ids_toUpdate)->update(['status'=>'处理队列']);
  287. })();
  288. // StationTaskMaterialBox::query()
  289. // ->whereIn('id', data_get($stationTaskMaterialBoxes, '*.id'))
  290. // ->update(['status'=>'处理中']);
  291. $this->stationTaskService
  292. ->markProcessing_byIds(
  293. data_get($stationTaskMaterialBoxes, '*.*.station_id')
  294. );
  295. }
  296. function getProcessing_byStationId($stationId)
  297. {
  298. //这里不能用缓存,因为更新会非常快
  299. return StationTaskMaterialBox::query()
  300. ->where('station_id',$stationId)
  301. ->where('status','处理中')
  302. ->where('created_at','>',Carbon::now()->subDay())
  303. ->first();
  304. }
  305. function excepted($stationTaskMaterialBoxes_orBox){
  306. if (get_class($stationTaskMaterialBoxes_orBox)==StationTaskMaterialBox::class){
  307. $stationTaskMaterialBoxes_orBox = collect([$stationTaskMaterialBoxes_orBox]);
  308. }
  309. StationTaskMaterialBox::query()->whereIn('id',data_get($stationTaskMaterialBoxes_orBox,'*.id'))
  310. ->update(['status'=>'异常']);
  311. switch (get_class($stationTaskMaterialBoxes_orBox)){
  312. case MaterialBox::class:
  313. case StationTaskMaterialBox::class:
  314. throw new ErrorException('料箱异常'.json_encode($stationTaskMaterialBoxes_orBox->toJson()));
  315. }
  316. }
  317. function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
  318. {
  319. $stationTaskMaterialBox->load('station.stationType');
  320. switch ($stationTaskMaterialBox['station']['stationType']['name']){
  321. case "立库":
  322. return '入立库';
  323. case "缓存架":
  324. return '入缓存架';
  325. case "料箱监视器":
  326. if ($stationTaskMaterialBox['station_task_batch_id'])return "分波次";
  327. default:
  328. throw new ErrorException('当前类型找不到');
  329. }
  330. }
  331. }