StationTaskMaterialBoxService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'],$stationTaskMaterialBox['stationTask']);
  132. break;
  133. case '入立库':
  134. $this->set($stationTaskMaterialBox,[
  135. 'id' => $stationTaskMaterialBox['station_id'],
  136. 'status' => '完成',
  137. ]);
  138. break;
  139. case '入缓存架':break;
  140. default:;
  141. }
  142. }catch (\Exception $e){
  143. throw new ErrorException('放置料箱出错');
  144. }
  145. }
  146. function markHasTaken($stationTaskMaterialBox)
  147. {
  148. //TODO: 标记 料箱位置(需要其字段存在)$stationTaskMaterialBox['materialBox']['position']
  149. $this->instant($this->stationCacheShelfGridService,'StationCacheShelfGridService');
  150. // 料箱从缓存架上拿走
  151. if($stationTaskMaterialBox['station']['stationType']['name']=='缓存架'){
  152. $stationTaskMaterialBox['status'] = '完成';
  153. $json = json_encode([
  154. 'station_id'=>$stationTaskMaterialBox['station_id'],
  155. 'code' => $stationTaskMaterialBox['materialBox']['code'],
  156. 'status' => '完成'
  157. ]);
  158. $grids = StationCacheShelfGrid::query()->where(['station_id'=>$stationTaskMaterialBox['station_id'],'material_box_id'=>$stationTaskMaterialBox['material_box_id']])->get();
  159. $this->stationCacheShelfGridService->cancelTask($grids);
  160. broadcast(new BroadcastToStation($stationTaskMaterialBox['station_id'], $json));
  161. $this->markProcessed($stationTaskMaterialBox);
  162. }
  163. }
  164. function processNextQueued(StationTaskMaterialBox $stationTaskMaterialBox_lastProcessed){
  165. $station_id=$stationTaskMaterialBox_lastProcessed['station_id'];
  166. $stationTaskMaterialBox_next=StationTaskMaterialBox::query()
  167. ->where('station_id',$station_id)
  168. ->where('status','处理队列')
  169. ->orderBy('updated_at')
  170. ->first();
  171. if($stationTaskMaterialBox_next){
  172. $stationTaskMaterialBox_next->update(['status'=>'处理中']);
  173. }
  174. return $stationTaskMaterialBox_next;
  175. }
  176. function markProcessed(StationTaskMaterialBox $stationTaskMaterialBox){
  177. $stationTaskMaterialBox['status'] = '完成';
  178. $stationTaskMaterialBox['station_id'] = 4;
  179. $stationTaskMaterialBox->update();
  180. }
  181. /**
  182. * 每波次仅将最老的作务标为“处理中”,其他置入队列;
  183. * 如某波次已经有“处理中“,则他部置入队列
  184. * @param $stationTaskMaterialBox_orBoxes ?? 单个或多个
  185. */
  186. function markProcessing($stationTaskMaterialBox_orBoxes)
  187. {
  188. $this->instant($this->stationTaskService,'StationTaskService');
  189. $stationTaskMaterialBoxes =
  190. (function()use($stationTaskMaterialBox_orBoxes){
  191. if (get_class($stationTaskMaterialBox_orBoxes)==StationTaskMaterialBox::class){
  192. return collect([$stationTaskMaterialBox_orBoxes]);
  193. }
  194. return collect($stationTaskMaterialBox_orBoxes);
  195. })();
  196. $stationTaskMaterialBoxes_grouped=
  197. ($按时间从前往后排出顺序=function ()use(&$stationTaskMaterialBoxes){
  198. return $stationTaskMaterialBoxes
  199. ->sortBy('id')
  200. ->groupBy('station_task_batch_id');
  201. })();
  202. $stationTaskMaterialBoxes_grouped->each(function(&$groupByBatch){
  203. ($将所有要标记的箱任务先放在队列里=function()use(&$groupByBatch){
  204. $groupByBatch->each(function (&$stationTaskMaterialBox){
  205. $stationTaskMaterialBox['status']='处理队列';
  206. });
  207. })();
  208. ($如果之前没有处理中则标记第一个为处理目标,并持久化=function()use(&$groupByBatch){
  209. $taskBatchId=$groupByBatch[0]['station_task_batch_id'];
  210. $processing=$this->getProcessing_byTaskBatch($taskBatchId);
  211. if(!$processing){
  212. $groupByBatch[0]['status']='处理中';
  213. $groupByBatch[0]->update();
  214. }else{
  215. foreach ($groupByBatch as &$stationTaskMaterialBox){
  216. if($stationTaskMaterialBox['id']==$processing['id']){
  217. $stationTaskMaterialBox['status']='处理中';
  218. }
  219. }
  220. }
  221. })();
  222. });
  223. ($持久化处理队列的记录=function()use(&$stationTaskMaterialBoxes_grouped){
  224. $toArray = $stationTaskMaterialBoxes_grouped->collapse();
  225. $toArray=$toArray->where('status','处理队列');
  226. $ids_toUpdate = data_get($toArray, '*.id');
  227. if(count($ids_toUpdate))
  228. StationTaskMaterialBox::query()->whereIn('id',$ids_toUpdate)->update(['status'=>'处理队列']);
  229. })();
  230. // StationTaskMaterialBox::query()
  231. // ->whereIn('id', data_get($stationTaskMaterialBoxes, '*.id'))
  232. // ->update(['status'=>'处理中']);
  233. $this->stationTaskService
  234. ->markProcessing_byIds(
  235. data_get($stationTaskMaterialBoxes, '*.*.station_id')
  236. );
  237. }
  238. function getProcessing_byTaskBatch($stationTaskBatch_id)
  239. {
  240. //这里不能用缓存,因为更新会非常快
  241. return StationTaskMaterialBox::query()
  242. ->where('station_task_batch_id',$stationTaskBatch_id)
  243. ->where('status','处理中')
  244. ->first();
  245. }
  246. function excepted($stationTaskMaterialBoxes_orBox){
  247. if (get_class($stationTaskMaterialBoxes_orBox)==StationTaskMaterialBox::class){
  248. $stationTaskMaterialBoxes_orBox = collect([$stationTaskMaterialBoxes_orBox]);
  249. }
  250. StationTaskMaterialBox::query()->whereIn('id',data_get($stationTaskMaterialBoxes_orBox,'*.id'))
  251. ->update(['status'=>'异常']);
  252. switch (get_class($stationTaskMaterialBoxes_orBox)){
  253. case MaterialBox::class:
  254. case StationTaskMaterialBox::class:
  255. throw new ErrorException('料箱异常'.json_encode($stationTaskMaterialBoxes_orBox->toJson()));
  256. }
  257. }
  258. function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
  259. {
  260. $stationTaskMaterialBox->load('station.stationType');
  261. if($isBatching=(
  262. $stationTaskMaterialBox['station_task_batch_id'] &&
  263. $stationTaskMaterialBox['station']['stationType']['name'] == '料箱监视器')
  264. ){
  265. return '分波次';
  266. }
  267. if($isPuttingBack=(
  268. $stationTaskMaterialBox['station']['stationType']['name'] == '立库')
  269. ){
  270. return '入立库';
  271. }
  272. // if($isStoring=false){
  273. // return '入库';
  274. // }
  275. throw new ErrorException('当前类型找不到');
  276. }
  277. }