StationTaskMaterialBoxService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. if (!$this->materialBoxService->checkBoxNorm($orderCommodity['location']))continue;
  113. $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
  114. if(in_array($materialBox['id'],$materialBoxIds_used))continue;
  115. $stationMaterialBoxes_toCreate->push([
  116. 'station_id'=>$station['id'],
  117. 'material_box_id'=>$materialBox['id'],
  118. 'station_task_batch_id'=>$stationTaskBatch['id'],
  119. 'status'=>'待处理'
  120. ]);
  121. $materialBoxIds_used[]=$materialBox['id'];
  122. }
  123. return $this->insert($stationMaterialBoxes_toCreate->toArray(),true);
  124. }
  125. function get(array $kvPairs, $with=null){
  126. ksort($kvPairs);
  127. return Cache::remember('StationTaskMaterialBox'.md5(json_encode($kvPairs).json_encode([$with])), config('cache.expirations.fastChange'), function ()use($kvPairs,$with) {
  128. $query = StationTaskMaterialBox::query();
  129. if($with){
  130. $query->with($with);
  131. }
  132. foreach ($kvPairs as $column => $value){
  133. if (is_array($value))$query->whereIn($column,$value);
  134. else $query->where($column,$value);
  135. }
  136. return $query->get();
  137. });
  138. }
  139. function markHasPut(StationTaskMaterialBox $stationTaskMaterialBox){
  140. $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
  141. $this->instant($this->stationTaskCommodityService,'StationTaskCommodityService');
  142. $this->instant($this->stationTaskService,'StationTaskService');
  143. $this->instant($this->stationService,'StationService');
  144. $this->instant($this->cacheShelfService,'CacheShelfService');
  145. $this->instant($this->storageService,'StorageService');
  146. try{
  147. $taskType=$this->getServingTaskType($stationTaskMaterialBox);
  148. LogService::log('海柔请求','markHasTaken2',
  149. json_encode($stationTaskMaterialBox));
  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. //判断取出料箱是否是在缓存架
  203. $station = Station::query()->where("material_box_id",$stationTaskMaterialBox->material_box_id)
  204. ->first();
  205. if ($station && app("StationService")->isHalfBoxLocation($station))
  206. app("CacheShelfService")->boxHasBeenTaken($station);
  207. /*//维护缓存架可用度
  208. $map = Cache::get("CACHE_SHELF_MAPPING",function (){return [];});
  209. if (isset($map[$stationTaskMaterialBox->material_box_id])){
  210. $available=Cache::get("CACHE_SHELF_AVAILABLE",function (){return [];});
  211. $available[$map[$stationTaskMaterialBox->material_box_id]] = true;
  212. Cache::forever("CACHE_SHELF_AVAILABLE",$available);
  213. CacheShelfTaskJob::dispatch("CACHE_SHELF_AVAILABLE",count($available))->delay(now()->addSeconds(config("haiRou.cacheShelf.outBinAwait")));
  214. unset($map[$stationTaskMaterialBox->material_box_id]);
  215. Cache::forever("CACHE_SHELF_MAPPING",$map);
  216. }*/
  217. }
  218. function processNextQueued(?StationTaskMaterialBox $stationTaskMaterialBox_lastProcessed){
  219. $station_id=$stationTaskMaterialBox_lastProcessed['station_id'];
  220. $stationTaskMaterialBox_next=StationTaskMaterialBox::query()
  221. ->where('station_id',$station_id)
  222. ->where('status','处理队列')
  223. ->orderBy('updated_at')
  224. ->first();
  225. if($stationTaskMaterialBox_next){
  226. $stationTaskMaterialBox_next->update(['status'=>'处理中']);
  227. }
  228. return $stationTaskMaterialBox_next;
  229. }
  230. function markProcessed(StationTaskMaterialBox $stationTaskMaterialBox){
  231. $stationTaskMaterialBox['status'] = '完成';
  232. $stationTaskMaterialBox->save();
  233. }
  234. function getNotProcessedSiblings($stationTaskMaterialBox){
  235. return StationTaskMaterialBox::query()
  236. ->whereNotIn('status',['完成'])
  237. ->where('station_task_id',$stationTaskMaterialBox['station_task_id'])
  238. ->get();
  239. }
  240. /**
  241. * 每波次仅将最老的作务标为“处理中”,其他置入队列;
  242. * 如某波次已经有“处理中“,则他部置入队列
  243. * @param $stationTaskMaterialBox_orBoxes ?? 单个或多个
  244. */
  245. function markProcessing($stationTaskMaterialBox_orBoxes)
  246. {
  247. $this->instant($this->stationTaskService,'StationTaskService');
  248. $stationTaskMaterialBoxes =
  249. (function()use($stationTaskMaterialBox_orBoxes){
  250. if (get_class($stationTaskMaterialBox_orBoxes)==StationTaskMaterialBox::class){
  251. return collect([$stationTaskMaterialBox_orBoxes]);
  252. }
  253. return collect($stationTaskMaterialBox_orBoxes);
  254. })();
  255. $stationTaskMaterialBoxes_grouped=
  256. ($按时间从前往后排出顺序=function ()use(&$stationTaskMaterialBoxes){
  257. return $stationTaskMaterialBoxes
  258. ->sortBy('id')
  259. ->groupBy('station_task_batch_id');
  260. })();
  261. $stationTaskMaterialBoxes_grouped->each(function(&$groupByBatch){
  262. ($将所有要标记的箱任务先放在队列里=function()use(&$groupByBatch){
  263. $groupByBatch->each(function (&$stationTaskMaterialBox){
  264. $stationTaskMaterialBox['status']='处理队列';
  265. });
  266. })();
  267. ($如果之前没有处理中则标记第一个为处理目标,准备持久化=function()use(&$groupByBatch){
  268. $stationId=$groupByBatch[0]['station_id'];
  269. $processing=$this->getProcessing_byStationId($stationId);
  270. if(!$processing){
  271. $groupByBatch[0]['status']='处理中';
  272. $groupByBatch[0]->update();
  273. }else{
  274. foreach ($groupByBatch as &$stationTaskMaterialBox){
  275. if($stationTaskMaterialBox['id']==$processing['id']){
  276. $stationTaskMaterialBox['status']='处理中';
  277. }
  278. }
  279. }
  280. })();
  281. });
  282. ($持久化处理队列的记录=function()use(&$stationTaskMaterialBoxes_grouped){
  283. $toArray = $stationTaskMaterialBoxes_grouped->collapse();
  284. $toArray=$toArray->where('status','处理队列');
  285. $ids_toUpdate = data_get($toArray, '*.id');
  286. if(count($ids_toUpdate))
  287. StationTaskMaterialBox::query()->whereIn('id',$ids_toUpdate)->update(['status'=>'处理队列']);
  288. })();
  289. // StationTaskMaterialBox::query()
  290. // ->whereIn('id', data_get($stationTaskMaterialBoxes, '*.id'))
  291. // ->update(['status'=>'处理中']);
  292. $this->stationTaskService
  293. ->markProcessing_byIds(
  294. data_get($stationTaskMaterialBoxes, '*.*.station_id')
  295. );
  296. }
  297. function getProcessing_byStationId($stationId)
  298. {
  299. //这里不能用缓存,因为更新会非常快
  300. return StationTaskMaterialBox::query()
  301. ->where('station_id',$stationId)
  302. ->where('status','处理中')
  303. ->where('created_at','>',Carbon::now()->subDay())
  304. ->first();
  305. }
  306. function excepted($stationTaskMaterialBoxes_orBox){
  307. if (get_class($stationTaskMaterialBoxes_orBox)==StationTaskMaterialBox::class){
  308. $stationTaskMaterialBoxes_orBox = collect([$stationTaskMaterialBoxes_orBox]);
  309. }
  310. StationTaskMaterialBox::query()->whereIn('id',data_get($stationTaskMaterialBoxes_orBox,'*.id'))
  311. ->update(['status'=>'异常']);
  312. switch (get_class($stationTaskMaterialBoxes_orBox)){
  313. case MaterialBox::class:
  314. case StationTaskMaterialBox::class:
  315. throw new ErrorException('料箱异常'.json_encode($stationTaskMaterialBoxes_orBox->toJson()));
  316. }
  317. }
  318. function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
  319. {
  320. $stationTaskMaterialBox->load('station.stationType');
  321. switch ($stationTaskMaterialBox['station']['stationType']['name']){
  322. case "立库":
  323. return '入立库';
  324. case "缓存架":
  325. return '入缓存架';
  326. case "料箱监视器":
  327. if ($stationTaskMaterialBox['station_task_batch_id'])return "分波次";
  328. default:
  329. throw new ErrorException('当前类型找不到');
  330. }
  331. }
  332. }