BatchService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\Events\BroadcastToStation;
  5. use App\Exceptions\ErrorException;
  6. use App\Jobs\BatchTaskJob;
  7. use App\OracleActAllocationDetails;
  8. use App\Order;
  9. use App\OrderCommodity;
  10. use App\Owner;
  11. use App\Station;
  12. use Exception;
  13. use Illuminate\Support\Collection;
  14. use Illuminate\Support\Facades\Cache;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Http;
  17. use App\Traits\ServiceAppAop;
  18. class BatchService
  19. {
  20. use ServiceAppAop;
  21. protected $modelClass=Batch::class;
  22. /** @var StationTaskBatchService $stationTaskBatchService */
  23. private $stationTaskBatchService;
  24. /** @var StationRuleBatchService $stationRuleBatchService */
  25. private $stationRuleBatchService;
  26. /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
  27. private $stationTaskMaterialBoxService;
  28. /** @var StationTaskCommodityService $stationTaskCommodityService */
  29. private $stationTaskCommodityService;
  30. /** @var StationTaskService $stationTaskService */
  31. private $stationTaskService;
  32. public function __construct(){
  33. $this->stationTaskBatchService=null;
  34. $this->stationRuleBatchService=null;
  35. $this->stationTaskMaterialBoxService=null;
  36. $this->stationTaskCommodityService=null;
  37. $this->stationTaskService=null;
  38. }
  39. public function get(array $params)
  40. {
  41. $query = Batch::query();
  42. foreach ($params as $column=>$param){
  43. if (is_array($param))$query->whereIn($column,$param);
  44. else $query->where($column,$param);
  45. }
  46. return $query->get();
  47. }
  48. public function updateWhereIn($key,$values,$updateKeyValues){
  49. Batch::query()->whereIn($key,$values)->update($updateKeyValues);
  50. }
  51. /**
  52. * 为波次附加任务,已附加的重复任务不影响
  53. * @param $batches
  54. * @throws Exception
  55. */
  56. public function assignTasks($batches)
  57. {
  58. app('LogService')->log('海柔','assignTasks1',json_encode($batches));
  59. try{
  60. $batches = collect($batches);
  61. $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
  62. $this->instant($this->stationRuleBatchService,'StationRuleBatchService');
  63. $this->instant($this->stationTaskService,'StationTaskService');
  64. $this->instant($this->stationTaskCommodityService,'StationTaskCommodityService');
  65. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  66. $stationTaskBatches=null;
  67. $stationTasks=null;
  68. app('LogService')->log('海柔','assignTasks2',json_encode($batches));
  69. $batches_shouldProcess = $this->stationRuleBatchService->getBatches_shouldProcess($batches); //按规则过滤需要的波次
  70. if($batches_shouldProcess->isEmpty()) return;
  71. // $stationTaskMaterialBoxes_occupied = $this->stationTaskMaterialBoxService->getOccupied_byBatches($batches_shouldProcess); //过滤料箱被占用的波次
  72. // app('LogService')->log('海柔','assignTasks2.5',json_encode($stationTaskMaterialBoxes_occupied));
  73. // if($stationTaskMaterialBoxes_occupied->isNotEmpty()) {
  74. // foreach ($batches_shouldProcess as $batch){
  75. // Cache::tags(['波次防重叠'.$batch['id']])->flush();
  76. // }
  77. // BatchTaskJob::dispatch($batches_shouldProcess)
  78. // ->delay(now()->addMinutes(1)); //因为料箱被占用了,所以将任务推迟1分钟后尝试
  79. // return;
  80. // }
  81. app('LogService')->log('海柔','assignTasks3',json_encode($batches_shouldProcess));
  82. DB::transaction(function ()use($batches,&$stationTaskBatches,&$batches_shouldProcess,&$stationTasks){
  83. $stationTasks = $this->stationTaskService->create($batches_shouldProcess->count()); //生成总任务
  84. $stationTaskBatches=$this->stationTaskBatchService->createByBatches($batches_shouldProcess,$stationTasks); //注册波次任务
  85. $stationTaskMaterialBoxes=$this->stationTaskMaterialBoxService->createByBatches($batches_shouldProcess,$stationTasks); //注册料箱任务
  86. $stationTaskCommodities=$this->stationTaskCommodityService->createByBatches($batches_shouldProcess,$stationTasks); //注册商品任务
  87. app('LogService')->log('海柔','assignTasks4',json_encode($batches_shouldProcess));
  88. });
  89. foreach ($stationTasks as &$stationTask){
  90. $stationTask->loadMissing([
  91. "stationTaskCommodities.commodity.barcodes",
  92. "stationTaskCommodities.stationTaskMaterialBox",
  93. "stationTaskBatches.batch.owner",
  94. "stationTaskMaterialBoxes.materialBox",
  95. ]);
  96. }
  97. app('LogService')->log('海柔','assignTasks5',json_encode($stationTasks,true).json_encode($batches_shouldProcess));
  98. $jsonStationTasks=json_encode($stationTasks,true);
  99. broadcast(new BroadcastToStation(BroadcastToStation::ALL_STATION, $jsonStationTasks));
  100. app('LogService')->log('海柔','assignTasks6',$jsonStationTasks.json_encode($batches_shouldProcess));
  101. // $ran=$this->stationTaskBatchService->runMany($stationTaskBatches);//执行波次任务
  102. }catch(Exception $e){
  103. app('LogService')->log('海柔','assignTasks7',json_encode($batches));
  104. $batchesJson='';
  105. foreach ($batches as $batch){
  106. $batchesJson.=json_encode($batch);
  107. Cache::tags(['波次防重叠'.$batch['id']])->flush();
  108. }
  109. app('LogService')->log('海柔','assignTasks8',json_encode($batches));
  110. throw new ErrorException('注册任务失败: '. $batchesJson . $e->getMessage().json_encode($e->getTrace()));
  111. }
  112. }
  113. public function getBatchByCodes($codes)
  114. {
  115. if(empty($codes))return collect();
  116. if(count($codes) == 0)return collect();
  117. return Batch::query()->whereIn('code',$codes)->get();
  118. }
  119. /**
  120. * 检查批次订单信息,存在库位异常 直接删除所有所属商品 重新拉取
  121. *
  122. * @param Collection|array $batches
  123. */
  124. public function checkBatchOrderInfo($batches)
  125. {
  126. if (!is_array($batches))$batches = $batches->toArray();
  127. $ids = array_column($batches,"id");
  128. $batches = Batch::query()->whereIn("id",$ids)->with("orders.orderCommodities")->get();
  129. foreach ($batches as $batch){
  130. if (!$batch->orders)continue;
  131. foreach ($batch->orders as $order){
  132. if (!$order->orderCommodities)app("OrderService")->notExistToRecover($order);
  133. else{
  134. $mark = false;
  135. foreach ($order->orderCommodities as $orderCommodity){
  136. if (!$orderCommodity->location){
  137. $mark = true;break;
  138. }
  139. }
  140. if ($mark){
  141. OrderCommodity::query()->where("order_id",$order->id)->delete();
  142. app("OrderService")->notExistToRecover($order);
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }