StationTaskMaterialBoxService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 StationCacheShelfGridService $stationCacheShelfGridService */
  37. private $stationCacheShelfGridService;
  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. foreach ($orderCommodities as $orderCommodity){
  108. $station=$this->stationService->getStation_byType($stationType['name']);
  109. $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
  110. if(in_array($materialBox['id'],$materialBoxIds_used))continue;
  111. $stationMaterialBoxes_toCreate->push([
  112. 'station_id'=>$station['id'],
  113. 'material_box_id'=>$materialBox['id'],
  114. 'station_task_batch_id'=>$stationTaskBatch['id'],
  115. 'status'=>'待处理'
  116. ]);
  117. $materialBoxIds_used[]=$materialBox['id'];
  118. }
  119. return $this->insert($stationMaterialBoxes_toCreate->toArray(),true);
  120. }
  121. function get(array $kvPairs, $with=null){
  122. ksort($kvPairs);
  123. return Cache::remember('StationTaskMaterialBox'.md5(json_encode($kvPairs).json_encode([$with])), config('cache.expirations.fastChange'), function ()use($kvPairs,$with) {
  124. $query = StationTaskMaterialBox::query();
  125. if($with){
  126. $query->with($with);
  127. }
  128. foreach ($kvPairs as $column => $value){
  129. if (is_array($value))$query->whereIn($column,$value);
  130. else $query->where($column,$value);
  131. }
  132. return $query->get();
  133. });
  134. }
  135. function markHasPut(StationTaskMaterialBox $stationTaskMaterialBox){
  136. $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
  137. $this->instant($this->stationTaskCommodityService,'StationTaskCommodityService');
  138. $this->instant($this->stationTaskService,'StationTaskService');
  139. $this->instant($this->stationService,'StationService');
  140. try{
  141. LogService::log('海柔请求','markHasTaken1','');
  142. $taskType=$this->getServingTaskType($stationTaskMaterialBox);
  143. LogService::log('海柔请求','markHasTaken2',
  144. json_encode($taskType));
  145. switch ($taskType){
  146. case '分波次':
  147. $this->markProcessing($stationTaskMaterialBox);
  148. $this->stationTaskBatchService->markProcessing_byIds($stationTaskMaterialBox['station_task_batch_id']);
  149. $this->stationTaskCommodityService->markProcessing($stationTaskMaterialBox['stationTaskCommodities']);
  150. $this->stationTaskService->markProcessing_byIds(data_get($stationTaskMaterialBox['stationTaskCommodities'],'*.station_task_id'));
  151. /** @var StationTask $stationTask */
  152. $stationTask = $this->stationTaskService->getProcessing();
  153. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'], $stationTask);
  154. break;
  155. case '入立库':
  156. $this->set($stationTaskMaterialBox,[
  157. 'id' => $stationTaskMaterialBox['station_id'],
  158. 'status' => '完成',
  159. ]);
  160. break;
  161. case '入缓存架':break;
  162. default:;
  163. }
  164. }catch (\Exception $e){
  165. throw new ErrorException('放置料箱出错');
  166. }
  167. }
  168. function markHasTaken($stationTaskMaterialBox)
  169. {
  170. //TODO: 标记 料箱位置(需要其字段存在)$stationTaskMaterialBox['materialBox']['position']
  171. $this->instant($this->stationCacheShelfGridService,'StationCacheShelfGridService');
  172. // 料箱从缓存架上拿走
  173. if($stationTaskMaterialBox['station']['stationType']['name']=='缓存架'){
  174. $stationTaskMaterialBox['status'] = '完成';
  175. $json = json_encode([
  176. 'station_id'=>$stationTaskMaterialBox['station_id'],
  177. 'code' => $stationTaskMaterialBox['materialBox']['code'],
  178. 'status' => '完成'
  179. ]);
  180. $grids = StationCacheShelfGrid::query()->where(['station_id'=>$stationTaskMaterialBox['station_id'],'material_box_id'=>$stationTaskMaterialBox['material_box_id']])->get();
  181. $this->stationCacheShelfGridService->cancelTask($grids);
  182. broadcast(new BroadcastToStation($stationTaskMaterialBox['station_id'], $json));
  183. $this->markProcessed($stationTaskMaterialBox);
  184. }
  185. }
  186. function processNextQueued(StationTaskMaterialBox $stationTaskMaterialBox_lastProcessed){
  187. $station_id=$stationTaskMaterialBox_lastProcessed['station_id'];
  188. $stationTaskMaterialBox_next=StationTaskMaterialBox::query()
  189. ->where('station_id',$station_id)
  190. ->where('status','处理队列')
  191. ->orderBy('updated_at')
  192. ->first();
  193. if($stationTaskMaterialBox_next){
  194. $stationTaskMaterialBox_next->update(['status'=>'处理中']);
  195. }
  196. return $stationTaskMaterialBox_next;
  197. }
  198. function markProcessed(StationTaskMaterialBox $stationTaskMaterialBox){
  199. $stationTaskMaterialBox['status'] = '完成';
  200. $stationTaskMaterialBox->update();
  201. }
  202. function getNotProcessedSiblings($stationTaskMaterialBox){
  203. return StationTaskMaterialBox::query()
  204. ->whereNotIn('status',['完成'])
  205. ->where('station_task_id',$stationTaskMaterialBox['station_task_id'])
  206. ->get();
  207. }
  208. /**
  209. * 每波次仅将最老的作务标为“处理中”,其他置入队列;
  210. * 如某波次已经有“处理中“,则他部置入队列
  211. * @param $stationTaskMaterialBox_orBoxes ?? 单个或多个
  212. */
  213. function markProcessing($stationTaskMaterialBox_orBoxes)
  214. {
  215. $this->instant($this->stationTaskService,'StationTaskService');
  216. $stationTaskMaterialBoxes =
  217. (function()use($stationTaskMaterialBox_orBoxes){
  218. if (get_class($stationTaskMaterialBox_orBoxes)==StationTaskMaterialBox::class){
  219. return collect([$stationTaskMaterialBox_orBoxes]);
  220. }
  221. return collect($stationTaskMaterialBox_orBoxes);
  222. })();
  223. $stationTaskMaterialBoxes_grouped=
  224. ($按时间从前往后排出顺序=function ()use(&$stationTaskMaterialBoxes){
  225. return $stationTaskMaterialBoxes
  226. ->sortBy('id')
  227. ->groupBy('station_task_batch_id');
  228. })();
  229. $stationTaskMaterialBoxes_grouped->each(function(&$groupByBatch){
  230. ($将所有要标记的箱任务先放在队列里=function()use(&$groupByBatch){
  231. $groupByBatch->each(function (&$stationTaskMaterialBox){
  232. $stationTaskMaterialBox['status']='处理队列';
  233. });
  234. })();
  235. ($如果之前没有处理中则标记第一个为处理目标,准备持久化=function()use(&$groupByBatch){
  236. $stationId=$groupByBatch[0]['station_id'];
  237. $processing=$this->getProcessing_byStationId($stationId);
  238. if(!$processing){
  239. $groupByBatch[0]['status']='处理中';
  240. $groupByBatch[0]->update();
  241. }else{
  242. foreach ($groupByBatch as &$stationTaskMaterialBox){
  243. if($stationTaskMaterialBox['id']==$processing['id']){
  244. $stationTaskMaterialBox['status']='处理中';
  245. }
  246. }
  247. }
  248. })();
  249. });
  250. ($持久化处理队列的记录=function()use(&$stationTaskMaterialBoxes_grouped){
  251. $toArray = $stationTaskMaterialBoxes_grouped->collapse();
  252. $toArray=$toArray->where('status','处理队列');
  253. $ids_toUpdate = data_get($toArray, '*.id');
  254. if(count($ids_toUpdate))
  255. StationTaskMaterialBox::query()->whereIn('id',$ids_toUpdate)->update(['status'=>'处理队列']);
  256. })();
  257. // StationTaskMaterialBox::query()
  258. // ->whereIn('id', data_get($stationTaskMaterialBoxes, '*.id'))
  259. // ->update(['status'=>'处理中']);
  260. $this->stationTaskService
  261. ->markProcessing_byIds(
  262. data_get($stationTaskMaterialBoxes, '*.*.station_id')
  263. );
  264. }
  265. function getProcessing_byStationId($stationId)
  266. {
  267. //这里不能用缓存,因为更新会非常快
  268. return StationTaskMaterialBox::query()
  269. ->where('station_id',$stationId)
  270. ->where('status','处理中')
  271. ->where('created_at','>',Carbon::now()->subDay())
  272. ->first();
  273. }
  274. function excepted($stationTaskMaterialBoxes_orBox){
  275. if (get_class($stationTaskMaterialBoxes_orBox)==StationTaskMaterialBox::class){
  276. $stationTaskMaterialBoxes_orBox = collect([$stationTaskMaterialBoxes_orBox]);
  277. }
  278. StationTaskMaterialBox::query()->whereIn('id',data_get($stationTaskMaterialBoxes_orBox,'*.id'))
  279. ->update(['status'=>'异常']);
  280. switch (get_class($stationTaskMaterialBoxes_orBox)){
  281. case MaterialBox::class:
  282. case StationTaskMaterialBox::class:
  283. throw new ErrorException('料箱异常'.json_encode($stationTaskMaterialBoxes_orBox->toJson()));
  284. }
  285. }
  286. function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
  287. {
  288. $stationTaskMaterialBox->load('station.stationType');
  289. if($isBatching=(
  290. $stationTaskMaterialBox['station_task_batch_id'] &&
  291. $stationTaskMaterialBox['station']['stationType']['name'] == '料箱监视器')
  292. ){
  293. return '分波次';
  294. }
  295. if($isPuttingBack=(
  296. $stationTaskMaterialBox['station']['stationType']['name'] == '立库')
  297. ){
  298. return '入立库';
  299. }
  300. // if($isStoring=false){
  301. // return '入库';
  302. // }
  303. throw new ErrorException('当前类型找不到');
  304. }
  305. }