StationTaskMaterialBoxService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\Exceptions\ErrorException;
  5. use App\MaterialBox;
  6. use App\OrderCommodity;
  7. use App\StationTask;
  8. use App\StationTaskMaterialBox;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Facades\Cache;
  11. use App\Traits\ServiceAppAop;
  12. class StationTaskMaterialBoxService
  13. {
  14. use ServiceAppAop;
  15. protected $modelClass=StationTaskMaterialBox::class;
  16. /** @var StationService $stationService */
  17. private $stationService;
  18. /** @var StationTypeService $stationTypeService */
  19. private $stationTypeService;
  20. /** @var StationTaskService $stationTaskService */
  21. private $stationTaskService;
  22. /** @var StationTaskBatchService $stationTaskBatchService */
  23. private $stationTaskBatchService;
  24. /** @var StationTaskCommodityService $stationTaskCommodityService */
  25. private $stationTaskCommodityService;
  26. /** @var MaterialBoxService $materialBoxService */
  27. private $materialBoxService;
  28. public function __construct(){
  29. $this->stationService=null;
  30. $this->stationTypeService=null;
  31. $this->stationTaskService=null;
  32. $this->materialBoxService=null;
  33. $this->stationTaskBatchService=null;
  34. $this->stationTaskCommodityService=null;
  35. }
  36. function createByBatches(Collection $batches,Collection $stationTasks_toAttach): Collection
  37. {
  38. $this->instant($this->stationTaskService,'StationTaskService');
  39. $stationTaskMaterialBoxes_byBatch = (function () use ($batches) {
  40. $stationTaskMaterialBoxes_listByBatch = new Collection();
  41. foreach ($batches as $batch) {
  42. $stationTaskMaterialBoxes_listByBatch->push(
  43. $this->createByBatch($batch)
  44. );
  45. }
  46. return $stationTaskMaterialBoxes_listByBatch;
  47. })();
  48. $this->stationTaskService
  49. ->registerSubTasks(
  50. $stationTasks_toAttach,
  51. $stationTaskMaterialBoxes_byBatch);
  52. return collect(data_get($stationTaskMaterialBoxes_byBatch,'*.*'));
  53. }
  54. function createByBatch(Batch $batch): ?Collection
  55. {
  56. $this->instant($this->materialBoxService,'MaterialBoxService');
  57. $this->instant($this->stationTypeService,'StationTypeService');
  58. $this->instant($this->stationService,'StationService');
  59. $stationMaterialBoxes_toCreate=new Collection();
  60. $order_ids=data_get($batch['orders'],'*.id');
  61. $orderCommodities=OrderCommodity::query()->with('orderBin')->whereIn('order_id',$order_ids)->get();
  62. if($orderCommodities->isEmpty())return $stationMaterialBoxes_toCreate;
  63. $stationType=$this->stationTypeService->getForMaterialBox_onBatchProcess();
  64. foreach ($orderCommodities as $orderCommodity){
  65. $station=$this->stationService->getStation_byType($stationType['name']);
  66. $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
  67. $stationMaterialBoxes_toCreate->push([
  68. 'station_id'=>$station['id'],
  69. 'material_box_id'=>$materialBox['id'],
  70. 'status'=>'待处理'
  71. ]);
  72. }
  73. return $this->insert($stationMaterialBoxes_toCreate->toArray(),true);
  74. }
  75. function get(array $kvPairs, $with=null){
  76. ksort($kvPairs);
  77. return Cache::remember('StationTaskMaterialBox'.md5(json_encode($kvPairs).json_encode([$with])), config('cache.expirations.fastChange'), function ()use($kvPairs,$with) {
  78. $query = StationTaskMaterialBox::query();
  79. if($with){
  80. $query->with($query);
  81. }
  82. foreach ($kvPairs as $column => $value){
  83. if (is_array($value))$query->whereIn($column,$value);
  84. else $query->where($column,$value);
  85. }
  86. return $query->get();
  87. });
  88. }
  89. function markHasPut(StationTaskMaterialBox $stationTaskMaterialBox){
  90. try{
  91. $taskType=$this->getServingTaskType($stationTaskMaterialBox);
  92. switch ($taskType){
  93. case '分波次':
  94. $this->markProcessing($stationTaskMaterialBox);
  95. $this->stationTaskBatchService->markProcessing($stationTaskMaterialBox['stationTaskBatch']);
  96. $this->stationTaskCommodityService->markProcessing($stationTaskMaterialBox['stationTaskCommodities']);
  97. break;
  98. case '入立库':
  99. break;
  100. case '入库':break;
  101. }
  102. }catch (\Exception $e){
  103. throw new ErrorException('放置料箱出错');
  104. }
  105. }
  106. function markTaken($stationTaskMaterialBox){
  107. //TODO: 标记 料箱位置(需要其字段存在)$stationTaskMaterialBox['materialBox']['position']
  108. }
  109. function markProcessed(StationTaskMaterialBox $stationTaskMaterialBox){
  110. $stationTaskMaterialBox['status'] = '完成';
  111. $stationTaskMaterialBox->save();
  112. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'],$stationTaskMaterialBox['stationTask']);
  113. }
  114. function markProcessing($stationTaskMaterialBox)
  115. {
  116. if (get_class($stationTaskMaterialBox)==StationTaskMaterialBox::class){
  117. $stationTaskMaterialBox = collect($stationTaskMaterialBox);
  118. }
  119. StationTaskMaterialBox::query()
  120. ->whereIn('id', data_get($stationTaskMaterialBox, '*.id'))
  121. ->update(['status'=>'处理中']);
  122. $this->stationTaskService
  123. ->markProcessing_byId(
  124. data_get($stationTaskMaterialBox, '*.station_id')
  125. );
  126. }
  127. function excepted($stationTaskMaterialBox_orBox){
  128. switch (get_class($stationTaskMaterialBox_orBox)){
  129. case MaterialBox::class:
  130. case StationTaskMaterialBox::class:
  131. throw new ErrorException('料箱异常'.json_encode($stationTaskMaterialBox_orBox->toJson()));
  132. }
  133. }
  134. function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
  135. {
  136. $stationTaskMaterialBox->loadMissing('station.stationType');
  137. if($isBatching=(
  138. $stationTaskMaterialBox['station_task_batch_id'] &&
  139. $stationTaskMaterialBox['station']['stationType'] == '料箱监视器')
  140. ){
  141. return '分波次';
  142. }
  143. if($isPuttingBack=(
  144. !$stationTaskMaterialBox &&
  145. $stationTaskMaterialBox['station']['stationType'] == '立库')
  146. ){
  147. return '入立库';
  148. }
  149. if($isStoring=false){
  150. return '入库';
  151. }
  152. throw new ErrorException('当前类型找不到');
  153. }
  154. }