StationTaskMaterialBoxService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\Events\BroadcastToStation;
  5. use App\Exceptions\ErrorException;
  6. use App\Exceptions\Exception;
  7. use App\MaterialBox;
  8. use App\OrderCommodity;
  9. use App\StationCacheShelfGrid;
  10. use App\StationTask;
  11. use App\StationTaskBatch;
  12. use App\StationTaskMaterialBox;
  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;
  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 StationCacheShelfGridService $stationCacheShelfGridService */
  36. private $stationCacheShelfGridService;
  37. public function __construct(){
  38. $this->stationService=null;
  39. $this->stationTypeService=null;
  40. $this->stationTaskService=null;
  41. $this->materialBoxService=null;
  42. $this->stationTaskBatchService=null;
  43. $this->stationTaskCommodityService=null;
  44. }
  45. function create($kvPairs)
  46. {
  47. return StationTaskMaterialBox::query()->create($kvPairs);
  48. }
  49. function createByStationAndMaterialBox($station, $materialBox)
  50. {
  51. return StationTaskMaterialBox::query()->create([
  52. 'station_id' => $station['id'],
  53. 'material_box_id' => $materialBox['id'],
  54. 'status' => '待处理'
  55. ]);
  56. }
  57. function getOccupied_byBatches(?Collection $batches): ?Collection
  58. {
  59. return StationTaskMaterialBox::query()
  60. ->where('status','<>','完成')
  61. ->whereHas('materialBox',function ($query)use($batches){
  62. $locations=OrderCommodity::query()
  63. ->whereHas('order',function ($queryO)use($batches){
  64. $queryO->whereIn('batch_id',data_get($batches,'*.id')??[]);
  65. })->get('location');
  66. $query->whereIn('code',data_get($locations,'*.location')??[]);
  67. })
  68. ->get();
  69. }
  70. function createByBatches(Collection $batches,Collection $stationTasks_toAttach): Collection
  71. {
  72. $this->instant($this->stationTaskService,'StationTaskService');
  73. LogService::log(__METHOD__,'assignTasks','波次任务分配4.c1:'.json_encode($batches));
  74. $stationTaskMaterialBoxes_byBatch = (function () use ($batches) {
  75. $stationTaskMaterialBoxes_listByBatch = new Collection();
  76. foreach ($batches as $batch) {
  77. $stationTaskMaterialBoxes_listByBatch->push(
  78. $this->createByBatch($batch)
  79. );
  80. }
  81. return $stationTaskMaterialBoxes_listByBatch;
  82. })();
  83. LogService::log(__METHOD__,'assignTasks','波次任务分配4.c2:'.json_encode($batches));
  84. $this->stationTaskService
  85. ->registerSubTasks(
  86. $stationTasks_toAttach,
  87. $stationTaskMaterialBoxes_byBatch);
  88. return collect(data_get($stationTaskMaterialBoxes_byBatch,'*.*'));
  89. }
  90. function createByBatch(Batch $batch): ?Collection
  91. {
  92. $this->instant($this->materialBoxService,'MaterialBoxService');
  93. $this->instant($this->stationTypeService,'StationTypeService');
  94. $this->instant($this->stationService,'StationService');
  95. $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
  96. $this->instant($this->orderCommodityService,'OrderCommodityService');
  97. $stationMaterialBoxes_toCreate=new Collection();
  98. $order_ids=data_get($batch['orders'],'*.id');
  99. $orderCommodities=OrderCommodity::query()->with('orderBin')->whereIn('order_id',$order_ids)->get();
  100. $orderCommodities=$this->orderCommodityService->correctLocation_fromWMS($orderCommodities);
  101. if($orderCommodities->isEmpty())return $stationMaterialBoxes_toCreate;
  102. $stationType=$this->stationTypeService->getForMaterialBox_onBatchProcess();
  103. $stationTaskBatch=$this->stationTaskBatchService->get(['batch_id'=>$batch['id']])->first();
  104. $materialBoxIds_used=[];
  105. foreach ($orderCommodities as $orderCommodity){
  106. $station=$this->stationService->getStation_byType($stationType['name']);
  107. $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
  108. if(in_array($materialBox['id'],$materialBoxIds_used))continue;
  109. $stationMaterialBoxes_toCreate->push([
  110. 'station_id'=>$station['id'],
  111. 'material_box_id'=>$materialBox['id'],
  112. 'station_task_batch_id'=>$stationTaskBatch['id'],
  113. 'status'=>'待处理'
  114. ]);
  115. $materialBoxIds_used[]=$materialBox['id'];
  116. }
  117. return $this->insert($stationMaterialBoxes_toCreate->toArray(),true);
  118. }
  119. function get(array $kvPairs, $with=null){
  120. ksort($kvPairs);
  121. return Cache::remember('StationTaskMaterialBox'.md5(json_encode($kvPairs).json_encode([$with])), config('cache.expirations.fastChange'), function ()use($kvPairs,$with) {
  122. $query = StationTaskMaterialBox::query();
  123. if($with){
  124. $query->with($with);
  125. }
  126. foreach ($kvPairs as $column => $value){
  127. if (is_array($value))$query->whereIn($column,$value);
  128. else $query->where($column,$value);
  129. }
  130. return $query->get();
  131. });
  132. }
  133. function markHasPut(StationTaskMaterialBox $stationTaskMaterialBox){
  134. $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
  135. $this->instant($this->stationTaskCommodityService,'StationTaskCommodityService');
  136. $this->instant($this->stationTaskService,'StationTaskService');
  137. $this->instant($this->stationService,'StationService');
  138. try{
  139. LogService::log('海柔请求','markHasTaken1','');
  140. $taskType=$this->getServingTaskType($stationTaskMaterialBox);
  141. LogService::log('海柔请求','markHasTaken2',
  142. json_encode($taskType));
  143. switch ($taskType){
  144. case '分波次':
  145. $this->markProcessing($stationTaskMaterialBox);
  146. $this->stationTaskBatchService->markProcessing_byIds($stationTaskMaterialBox['station_task_batch_id']);
  147. $this->stationTaskCommodityService->markProcessing($stationTaskMaterialBox['stationTaskCommodities']);
  148. $this->stationTaskService->markProcessing_byIds(data_get($stationTaskMaterialBox['stationTaskCommodities'],'*.station_task_id'));
  149. /** @var StationTask $stationTask */
  150. $stationTask = $this->stationTaskService->getProcessing();
  151. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'], $stationTask);
  152. break;
  153. case '入立库':
  154. $this->set($stationTaskMaterialBox,[
  155. 'id' => $stationTaskMaterialBox['station_id'],
  156. 'status' => '完成',
  157. ]);
  158. break;
  159. case '入缓存架':break;
  160. default:;
  161. }
  162. }catch (\Exception $e){
  163. throw new ErrorException('放置料箱出错');
  164. }
  165. }
  166. function markHasTaken($stationTaskMaterialBox)
  167. {
  168. //TODO: 标记 料箱位置(需要其字段存在)$stationTaskMaterialBox['materialBox']['position']
  169. $this->instant($this->stationCacheShelfGridService,'StationCacheShelfGridService');
  170. // 料箱从缓存架上拿走
  171. if($stationTaskMaterialBox['station']['stationType']['name']=='缓存架'){
  172. $stationTaskMaterialBox['status'] = '完成';
  173. $json = json_encode([
  174. 'station_id'=>$stationTaskMaterialBox['station_id'],
  175. 'code' => $stationTaskMaterialBox['materialBox']['code'],
  176. 'status' => '完成'
  177. ]);
  178. $grids = StationCacheShelfGrid::query()->where(['station_id'=>$stationTaskMaterialBox['station_id'],'material_box_id'=>$stationTaskMaterialBox['material_box_id']])->get();
  179. $this->stationCacheShelfGridService->cancelTask($grids);
  180. broadcast(new BroadcastToStation($stationTaskMaterialBox['station_id'], $json));
  181. $this->markProcessed($stationTaskMaterialBox);
  182. }
  183. }
  184. function processNextQueued(StationTaskMaterialBox $stationTaskMaterialBox_lastProcessed){
  185. $station_id=$stationTaskMaterialBox_lastProcessed['station_id'];
  186. $stationTaskMaterialBox_next=StationTaskMaterialBox::query()
  187. ->where('station_id',$station_id)
  188. ->where('status','处理队列')
  189. ->orderBy('updated_at')
  190. ->first();
  191. if($stationTaskMaterialBox_next){
  192. $stationTaskMaterialBox_next->update(['status'=>'处理中']);
  193. }
  194. return $stationTaskMaterialBox_next;
  195. }
  196. function markProcessed(StationTaskMaterialBox $stationTaskMaterialBox){
  197. $stationTaskMaterialBox['status'] = '完成';
  198. $stationTaskMaterialBox->update();
  199. }
  200. function getNotProcessedSiblings($stationTaskMaterialBox){
  201. return StationTaskMaterialBox::query()
  202. ->whereNotIn('status',['完成'])
  203. ->where('station_task_id',$stationTaskMaterialBox['station_task_id'])
  204. ->get();
  205. }
  206. /**
  207. * 每波次仅将最老的作务标为“处理中”,其他置入队列;
  208. * 如某波次已经有“处理中“,则他部置入队列
  209. * @param $stationTaskMaterialBox_orBoxes ?? 单个或多个
  210. */
  211. function markProcessing($stationTaskMaterialBox_orBoxes)
  212. {
  213. $this->instant($this->stationTaskService,'StationTaskService');
  214. $stationTaskMaterialBoxes =
  215. (function()use($stationTaskMaterialBox_orBoxes){
  216. if (get_class($stationTaskMaterialBox_orBoxes)==StationTaskMaterialBox::class){
  217. return collect([$stationTaskMaterialBox_orBoxes]);
  218. }
  219. return collect($stationTaskMaterialBox_orBoxes);
  220. })();
  221. $stationTaskMaterialBoxes_grouped=
  222. ($按时间从前往后排出顺序=function ()use(&$stationTaskMaterialBoxes){
  223. return $stationTaskMaterialBoxes
  224. ->sortBy('id')
  225. ->groupBy('station_task_batch_id');
  226. })();
  227. $stationTaskMaterialBoxes_grouped->each(function(&$groupByBatch){
  228. ($将所有要标记的箱任务先放在队列里=function()use(&$groupByBatch){
  229. $groupByBatch->each(function (&$stationTaskMaterialBox){
  230. $stationTaskMaterialBox['status']='处理队列';
  231. });
  232. })();
  233. ($如果之前没有处理中则标记第一个为处理目标,准备持久化=function()use(&$groupByBatch){
  234. $stationId=$groupByBatch[0]['station_id'];
  235. $processing=$this->getProcessing_byStationId($stationId);
  236. if(!$processing){
  237. $groupByBatch[0]['status']='处理中';
  238. $groupByBatch[0]->update();
  239. }else{
  240. foreach ($groupByBatch as &$stationTaskMaterialBox){
  241. if($stationTaskMaterialBox['id']==$processing['id']){
  242. $stationTaskMaterialBox['status']='处理中';
  243. }
  244. }
  245. }
  246. })();
  247. });
  248. ($持久化处理队列的记录=function()use(&$stationTaskMaterialBoxes_grouped){
  249. $toArray = $stationTaskMaterialBoxes_grouped->collapse();
  250. $toArray=$toArray->where('status','处理队列');
  251. $ids_toUpdate = data_get($toArray, '*.id');
  252. if(count($ids_toUpdate))
  253. StationTaskMaterialBox::query()->whereIn('id',$ids_toUpdate)->update(['status'=>'处理队列']);
  254. })();
  255. // StationTaskMaterialBox::query()
  256. // ->whereIn('id', data_get($stationTaskMaterialBoxes, '*.id'))
  257. // ->update(['status'=>'处理中']);
  258. $this->stationTaskService
  259. ->markProcessing_byIds(
  260. data_get($stationTaskMaterialBoxes, '*.*.station_id')
  261. );
  262. }
  263. function getProcessing_byStationId($stationId)
  264. {
  265. //这里不能用缓存,因为更新会非常快
  266. return StationTaskMaterialBox::query()
  267. ->where('station_id',$stationId)
  268. ->where('status','处理中')
  269. ->first();
  270. }
  271. function excepted($stationTaskMaterialBoxes_orBox){
  272. if (get_class($stationTaskMaterialBoxes_orBox)==StationTaskMaterialBox::class){
  273. $stationTaskMaterialBoxes_orBox = collect([$stationTaskMaterialBoxes_orBox]);
  274. }
  275. StationTaskMaterialBox::query()->whereIn('id',data_get($stationTaskMaterialBoxes_orBox,'*.id'))
  276. ->update(['status'=>'异常']);
  277. switch (get_class($stationTaskMaterialBoxes_orBox)){
  278. case MaterialBox::class:
  279. case StationTaskMaterialBox::class:
  280. throw new ErrorException('料箱异常'.json_encode($stationTaskMaterialBoxes_orBox->toJson()));
  281. }
  282. }
  283. function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
  284. {
  285. $stationTaskMaterialBox->load('station.stationType');
  286. if($isBatching=(
  287. $stationTaskMaterialBox['station_task_batch_id'] &&
  288. $stationTaskMaterialBox['station']['stationType']['name'] == '料箱监视器')
  289. ){
  290. return '分波次';
  291. }
  292. if($isPuttingBack=(
  293. $stationTaskMaterialBox['station']['stationType']['name'] == '立库')
  294. ){
  295. return '入立库';
  296. }
  297. // if($isStoring=false){
  298. // return '入库';
  299. // }
  300. throw new ErrorException('当前类型找不到');
  301. }
  302. }