StationTaskMaterialBoxService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 Carbon\Carbon;
  14. use Illuminate\Support\Collection;
  15. use Illuminate\Support\Facades\Cache;
  16. use App\Traits\ServiceAppAop;
  17. use Illuminate\Support\Facades\DB;
  18. class StationTaskMaterialBoxService
  19. {
  20. use ServiceAppAop;
  21. protected $modelClass=StationTaskMaterialBox::class;
  22. /** @var StationService $stationService */
  23. private $stationService;
  24. /** @var StationTypeService $stationTypeService */
  25. private $stationTypeService;
  26. /** @var StationTaskService $stationTaskService */
  27. private $stationTaskService;
  28. /** @var StationTaskBatchService $stationTaskBatchService */
  29. private $stationTaskBatchService;
  30. /** @var StationTaskCommodityService $stationTaskCommodityService */
  31. private $stationTaskCommodityService;
  32. /** @var OrderCommodityService $orderCommodityService */
  33. private $orderCommodityService;
  34. /** @var MaterialBoxService $materialBoxService */
  35. private $materialBoxService;
  36. /** @var CacheShelfService $cacheShelfService */
  37. private $cacheShelfService;
  38. public function __construct(){
  39. $this->stationService=null;
  40. $this->stationTypeService=null;
  41. $this->stationTaskService=null;
  42. $this->materialBoxService=null;
  43. $this->stationTaskBatchService=null;
  44. $this->stationTaskCommodityService=null;
  45. }
  46. function create($kvPairs)
  47. {
  48. return StationTaskMaterialBox::query()->create($kvPairs);
  49. }
  50. function createByStationAndMaterialBox($station, $materialBox)
  51. {
  52. return StationTaskMaterialBox::query()->create([
  53. 'station_id' => $station['id'],
  54. 'material_box_id' => $materialBox['id'],
  55. 'status' => '待处理'
  56. ]);
  57. }
  58. function getOccupied_byBatches(?Collection $batches): ?Collection
  59. {
  60. return StationTaskMaterialBox::query()
  61. ->where('status','<>','完成')
  62. ->where('created_at','>',Carbon::now()->subHours(2))
  63. ->whereHas('materialBox',function ($query)use($batches){
  64. $locations=OrderCommodity::query()
  65. ->whereHas('order',function ($queryO)use($batches){
  66. $queryO->whereIn('batch_id',data_get($batches,'*.id')??[]);
  67. })->get('location');
  68. $query->whereIn('code',data_get($locations,'*.location')??[]);
  69. })
  70. ->get();
  71. }
  72. function createByBatches(Collection $batches,Collection $stationTasks_toAttach): Collection
  73. {
  74. $this->instant($this->stationTaskService,'StationTaskService');
  75. LogService::log(__METHOD__,'assignTasks','波次任务分配4.c1:'.json_encode($batches));
  76. $stationTaskMaterialBoxes_byBatch = (function () use ($batches) {
  77. $stationTaskMaterialBoxes_listByBatch = new Collection();
  78. foreach ($batches as $batch) {
  79. $stationTaskMaterialBoxes_listByBatch->push(
  80. $this->createByBatch($batch)
  81. );
  82. }
  83. return $stationTaskMaterialBoxes_listByBatch;
  84. })();
  85. LogService::log(__METHOD__,'assignTasks','波次任务分配4.c2:'.json_encode($batches));
  86. $this->stationTaskService
  87. ->registerSubTasks(
  88. $stationTasks_toAttach,
  89. $stationTaskMaterialBoxes_byBatch);
  90. return collect(data_get($stationTaskMaterialBoxes_byBatch,'*.*'));
  91. }
  92. function createByBatch(Batch $batch): ?Collection
  93. {
  94. $this->instant($this->materialBoxService,'MaterialBoxService');
  95. $this->instant($this->stationTypeService,'StationTypeService');
  96. $this->instant($this->stationService,'StationService');
  97. $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
  98. $this->instant($this->orderCommodityService,'OrderCommodityService');
  99. $stationMaterialBoxes_toCreate=new Collection();
  100. $order_ids=data_get($batch['orders'],'*.id');
  101. $orderCommodities=OrderCommodity::query()->with('orderBin')->whereIn('order_id',$order_ids)->get();
  102. $orderCommodities=$this->orderCommodityService->correctLocation_fromWMS($orderCommodities);
  103. if($orderCommodities->isEmpty())return $stationMaterialBoxes_toCreate;
  104. $stationType=$this->stationTypeService->getForMaterialBox_onBatchProcess();
  105. $stationTaskBatch=$this->stationTaskBatchService->get(['batch_id'=>$batch['id']])->first();
  106. $materialBoxIds_used=[];
  107. $orderCommodities=$orderCommodities->sortBy('commodity_id');//按商品排序后,出货可以同商品挨在一起
  108. foreach ($orderCommodities as $orderCommodity){
  109. $station=$this->stationService->getStation_byType($stationType['name']);
  110. $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
  111. if(in_array($materialBox['id'],$materialBoxIds_used))continue;
  112. $stationMaterialBoxes_toCreate->push([
  113. 'station_id'=>$station['id'],
  114. 'material_box_id'=>$materialBox['id'],
  115. 'station_task_batch_id'=>$stationTaskBatch['id'],
  116. 'status'=>'待处理'
  117. ]);
  118. $materialBoxIds_used[]=$materialBox['id'];
  119. }
  120. return $this->insert($stationMaterialBoxes_toCreate->toArray(),true);
  121. }
  122. function get(array $kvPairs, $with=null){
  123. ksort($kvPairs);
  124. return Cache::remember('StationTaskMaterialBox'.md5(json_encode($kvPairs).json_encode([$with])), config('cache.expirations.fastChange'), function ()use($kvPairs,$with) {
  125. $query = StationTaskMaterialBox::query();
  126. if($with){
  127. $query->with($with);
  128. }
  129. foreach ($kvPairs as $column => $value){
  130. if (is_array($value))$query->whereIn($column,$value);
  131. else $query->where($column,$value);
  132. }
  133. return $query->get();
  134. });
  135. }
  136. function markHasPut(StationTaskMaterialBox $stationTaskMaterialBox){
  137. $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
  138. $this->instant($this->stationTaskCommodityService,'StationTaskCommodityService');
  139. $this->instant($this->stationTaskService,'StationTaskService');
  140. $this->instant($this->stationService,'StationService');
  141. $this->instant($this->cacheShelfService,'CacheShelfService');
  142. try{
  143. LogService::log('海柔请求','markHasTaken1','');
  144. $taskType=$this->getServingTaskType($stationTaskMaterialBox);
  145. LogService::log('海柔请求','markHasTaken2',
  146. json_encode($taskType));
  147. switch ($taskType){
  148. case '分波次':
  149. $this->markProcessing($stationTaskMaterialBox);
  150. $this->stationTaskBatchService->markProcessing_byIds($stationTaskMaterialBox['station_task_batch_id']);
  151. $this->stationTaskCommodityService->markProcessing($stationTaskMaterialBox['stationTaskCommodities']);
  152. $this->stationTaskService->markProcessing_byIds(data_get($stationTaskMaterialBox['stationTaskCommodities'],'*.station_task_id'));
  153. /** @var StationTask $stationTask */
  154. $stationTask = $this->stationTaskService->getProcessing();
  155. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'], $stationTask);
  156. break;
  157. case '入立库':
  158. $this->set($stationTaskMaterialBox,[
  159. 'id' => $stationTaskMaterialBox['station_id'],
  160. 'status' => '完成',
  161. ]);
  162. $this->cacheShelfService->putStationTaskMaterialBoxProcess($stationTaskMaterialBox);
  163. break;
  164. case '入缓存架':break;
  165. default:;
  166. }
  167. }catch (\Exception $e){
  168. throw new ErrorException('放置料箱出错');
  169. }
  170. }
  171. // TODO 料箱处理
  172. function markHasTaken($stationTaskMaterialBox)
  173. {
  174. //TODO: 标记 料箱位置(需要其字段存在)$stationTaskMaterialBox['materialBox']['position']
  175. $this->instant($this->cacheShelfService,'CacheShelfService');
  176. // 料箱从缓存架上拿走
  177. if($stationTaskMaterialBox['station']['stationType']['name']=='缓存架'){
  178. $stationTaskMaterialBox['status'] = '完成';
  179. $result = $this->cacheShelfService->lightOffTask($stationTaskMaterialBox['station']['code'],0);
  180. if($result['success'])
  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->save();
  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. ->where('created_at','>',Carbon::now()->subDay())
  270. ->first();
  271. }
  272. function excepted($stationTaskMaterialBoxes_orBox){
  273. if (get_class($stationTaskMaterialBoxes_orBox)==StationTaskMaterialBox::class){
  274. $stationTaskMaterialBoxes_orBox = collect([$stationTaskMaterialBoxes_orBox]);
  275. }
  276. StationTaskMaterialBox::query()->whereIn('id',data_get($stationTaskMaterialBoxes_orBox,'*.id'))
  277. ->update(['status'=>'异常']);
  278. switch (get_class($stationTaskMaterialBoxes_orBox)){
  279. case MaterialBox::class:
  280. case StationTaskMaterialBox::class:
  281. throw new ErrorException('料箱异常'.json_encode($stationTaskMaterialBoxes_orBox->toJson()));
  282. }
  283. }
  284. function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
  285. {
  286. $stationTaskMaterialBox->load('station.stationType');
  287. if($isBatching=(
  288. $stationTaskMaterialBox['station_task_batch_id'] &&
  289. $stationTaskMaterialBox['station']['stationType']['name'] == '料箱监视器')
  290. ){
  291. return '分波次';
  292. }
  293. if($isPuttingBack=(
  294. $stationTaskMaterialBox['station']['stationType']['name'] == '立库')
  295. ){
  296. return '入立库';
  297. }
  298. // if($isStoring=false){
  299. // return '入库';
  300. // }
  301. throw new ErrorException('当前类型找不到');
  302. }
  303. }