StationTaskMaterialBoxService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. LogService::log('海柔请求','markHasTaken1','');
  147. $taskType=$this->getServingTaskType($stationTaskMaterialBox);
  148. LogService::log('海柔请求','markHasTaken2',
  149. json_encode($taskType));
  150. switch ($taskType){
  151. case '分波次':
  152. $this->markProcessing($stationTaskMaterialBox);
  153. $this->stationTaskBatchService->markProcessing_byIds($stationTaskMaterialBox['station_task_batch_id']);
  154. $this->stationTaskCommodityService->markProcessing($stationTaskMaterialBox['stationTaskCommodities']);
  155. $this->stationTaskService->markProcessing_byIds(data_get($stationTaskMaterialBox['stationTaskCommodities'],'*.station_task_id'));
  156. /** @var StationTask $stationTask */
  157. $stationTask = $this->stationTaskService->getProcessing();
  158. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'], $stationTask);
  159. $stationTaskMaterialBox->materialBox['status']='在U型线';
  160. $stationTaskMaterialBox->materialBox->update();
  161. break;
  162. case '入立库':
  163. $stationTaskMaterialBox->materialBox['status']='在立库';
  164. $stationTaskMaterialBox->materialBox->update();
  165. break;
  166. case '入缓存架':
  167. $stationTaskMaterialBox->materialBox['status']='在缓存架';
  168. $stationTaskMaterialBox->materialBox->update();
  169. $stationTaskMaterialBox->loadMissing("station"); //提前加载站,后续都需要站信息来处理
  170. $this->storageService->putCacheShelf($stationTaskMaterialBox);
  171. break;
  172. default:;
  173. }
  174. $this->taskCompleted($stationTaskMaterialBox);
  175. }catch (\Exception $e){
  176. throw new ErrorException('放置料箱出错');
  177. }
  178. }
  179. private function taskCompleted($stationTaskMaterialBox)
  180. {
  181. $this->set($stationTaskMaterialBox,[
  182. 'id' => $stationTaskMaterialBox['station_id'],
  183. 'status' => '完成',
  184. ]);
  185. if (!$stationTaskMaterialBox->station_task_id)return;
  186. $task = StationTaskMaterialBox::query()->select(DB::raw(1))
  187. ->where("station_task_id",$stationTaskMaterialBox->station_task_id)
  188. ->where("status","!=","完成")->first();
  189. if (!$task)StationTask::query()->where("id",$stationTaskMaterialBox->station_task_id)
  190. ->update(["status"=>"完成"]);
  191. }
  192. /**
  193. * 取出料箱通知
  194. *
  195. * @param StationTaskMaterialBox|\stdClass $stationTaskMaterialBox
  196. * @throws \Exception
  197. */
  198. function markHasTaken($stationTaskMaterialBox)
  199. {
  200. $this->instant($this->cacheShelfService,'CacheShelfService');
  201. //$stationTaskMaterialBox->loadMissing("station");
  202. $this->push("1","取出料箱通知","任务信息:".$stationTaskMaterialBox->toJson());
  203. //判断取出料箱是否是在缓存架
  204. $station = Station::query()->where("material_box_id",$stationTaskMaterialBox->material_box_id)
  205. ->first();
  206. if ($station && app("StationService")->isHalfBoxLocation($station))
  207. app("CacheShelfService")->boxHasBeenTaken($station);
  208. /*//维护缓存架可用度
  209. $map = Cache::get("CACHE_SHELF_MAPPING",function (){return [];});
  210. if (isset($map[$stationTaskMaterialBox->material_box_id])){
  211. $available=Cache::get("CACHE_SHELF_AVAILABLE",function (){return [];});
  212. $available[$map[$stationTaskMaterialBox->material_box_id]] = true;
  213. Cache::forever("CACHE_SHELF_AVAILABLE",$available);
  214. CacheShelfTaskJob::dispatch("CACHE_SHELF_AVAILABLE",count($available))->delay(now()->addSeconds(config("haiRou.cacheShelf.outBinAwait")));
  215. unset($map[$stationTaskMaterialBox->material_box_id]);
  216. Cache::forever("CACHE_SHELF_MAPPING",$map);
  217. }*/
  218. }
  219. function processNextQueued(?StationTaskMaterialBox $stationTaskMaterialBox_lastProcessed){
  220. $station_id=$stationTaskMaterialBox_lastProcessed['station_id'];
  221. $stationTaskMaterialBox_next=StationTaskMaterialBox::query()
  222. ->where('station_id',$station_id)
  223. ->where('status','处理队列')
  224. ->orderBy('updated_at')
  225. ->first();
  226. if($stationTaskMaterialBox_next){
  227. $stationTaskMaterialBox_next->update(['status'=>'处理中']);
  228. }
  229. return $stationTaskMaterialBox_next;
  230. }
  231. function markProcessed(StationTaskMaterialBox $stationTaskMaterialBox){
  232. $stationTaskMaterialBox['status'] = '完成';
  233. $stationTaskMaterialBox->save();
  234. }
  235. function getNotProcessedSiblings($stationTaskMaterialBox){
  236. return StationTaskMaterialBox::query()
  237. ->whereNotIn('status',['完成'])
  238. ->where('station_task_id',$stationTaskMaterialBox['station_task_id'])
  239. ->get();
  240. }
  241. /**
  242. * 每波次仅将最老的作务标为“处理中”,其他置入队列;
  243. * 如某波次已经有“处理中“,则他部置入队列
  244. * @param $stationTaskMaterialBox_orBoxes ?? 单个或多个
  245. */
  246. function markProcessing($stationTaskMaterialBox_orBoxes)
  247. {
  248. $this->instant($this->stationTaskService,'StationTaskService');
  249. $stationTaskMaterialBoxes =
  250. (function()use($stationTaskMaterialBox_orBoxes){
  251. if (get_class($stationTaskMaterialBox_orBoxes)==StationTaskMaterialBox::class){
  252. return collect([$stationTaskMaterialBox_orBoxes]);
  253. }
  254. return collect($stationTaskMaterialBox_orBoxes);
  255. })();
  256. $stationTaskMaterialBoxes_grouped=
  257. ($按时间从前往后排出顺序=function ()use(&$stationTaskMaterialBoxes){
  258. return $stationTaskMaterialBoxes
  259. ->sortBy('id')
  260. ->groupBy('station_task_batch_id');
  261. })();
  262. $stationTaskMaterialBoxes_grouped->each(function(&$groupByBatch){
  263. ($将所有要标记的箱任务先放在队列里=function()use(&$groupByBatch){
  264. $groupByBatch->each(function (&$stationTaskMaterialBox){
  265. $stationTaskMaterialBox['status']='处理队列';
  266. });
  267. })();
  268. ($如果之前没有处理中则标记第一个为处理目标,准备持久化=function()use(&$groupByBatch){
  269. $stationId=$groupByBatch[0]['station_id'];
  270. $processing=$this->getProcessing_byStationId($stationId);
  271. if(!$processing){
  272. $groupByBatch[0]['status']='处理中';
  273. $groupByBatch[0]->update();
  274. }else{
  275. foreach ($groupByBatch as &$stationTaskMaterialBox){
  276. if($stationTaskMaterialBox['id']==$processing['id']){
  277. $stationTaskMaterialBox['status']='处理中';
  278. }
  279. }
  280. }
  281. })();
  282. });
  283. ($持久化处理队列的记录=function()use(&$stationTaskMaterialBoxes_grouped){
  284. $toArray = $stationTaskMaterialBoxes_grouped->collapse();
  285. $toArray=$toArray->where('status','处理队列');
  286. $ids_toUpdate = data_get($toArray, '*.id');
  287. if(count($ids_toUpdate))
  288. StationTaskMaterialBox::query()->whereIn('id',$ids_toUpdate)->update(['status'=>'处理队列']);
  289. })();
  290. // StationTaskMaterialBox::query()
  291. // ->whereIn('id', data_get($stationTaskMaterialBoxes, '*.id'))
  292. // ->update(['status'=>'处理中']);
  293. $this->stationTaskService
  294. ->markProcessing_byIds(
  295. data_get($stationTaskMaterialBoxes, '*.*.station_id')
  296. );
  297. }
  298. function getProcessing_byStationId($stationId)
  299. {
  300. //这里不能用缓存,因为更新会非常快
  301. return StationTaskMaterialBox::query()
  302. ->where('station_id',$stationId)
  303. ->where('status','处理中')
  304. ->where('created_at','>',Carbon::now()->subDay())
  305. ->first();
  306. }
  307. function excepted($stationTaskMaterialBoxes_orBox){
  308. if (get_class($stationTaskMaterialBoxes_orBox)==StationTaskMaterialBox::class){
  309. $stationTaskMaterialBoxes_orBox = collect([$stationTaskMaterialBoxes_orBox]);
  310. }
  311. StationTaskMaterialBox::query()->whereIn('id',data_get($stationTaskMaterialBoxes_orBox,'*.id'))
  312. ->update(['status'=>'异常']);
  313. switch (get_class($stationTaskMaterialBoxes_orBox)){
  314. case MaterialBox::class:
  315. case StationTaskMaterialBox::class:
  316. throw new ErrorException('料箱异常'.json_encode($stationTaskMaterialBoxes_orBox->toJson()));
  317. }
  318. }
  319. function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
  320. {
  321. $stationTaskMaterialBox->load('station.stationType');
  322. switch ($stationTaskMaterialBox['station']['stationType']['name']){
  323. case "立库":
  324. return '入立库';
  325. case "缓存架":
  326. return '入缓存架';
  327. case "料箱监视器":
  328. if ($stationTaskMaterialBox['station_task_batch_id'])return "分波次";
  329. default:
  330. throw new ErrorException('当前类型找不到');
  331. }
  332. }
  333. }