StationTaskMaterialBoxService.php 14 KB

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