StationTaskMaterialBoxService.php 13 KB

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