StationTaskMaterialBoxService.php 14 KB

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