StationTaskMaterialBoxService.php 11 KB

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