StationTaskMaterialBoxService.php 11 KB

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