StationTaskMaterialBoxService.php 12 KB

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