BatchService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. if($stationTaskMaterialBoxes_occupied->isNotEmpty()) {
  73. foreach ($batches_shouldProcess as $batch){
  74. Cache::tags(['波次防重叠'.$batch['id']])->flush();
  75. }
  76. BatchTaskJob::dispatch($batches_shouldProcess)
  77. ->delay(now()->addMinutes(1)); //因为料箱被占用了,所以将任务推迟1分钟后尝试
  78. return;
  79. }
  80. app('LogService')->log('海柔','assignTasks3',json_encode($batches_shouldProcess));
  81. DB::transaction(function ()use($batches,&$stationTaskBatches,&$batches_shouldProcess,&$stationTasks){
  82. $stationTasks = $this->stationTaskService->create($batches_shouldProcess->count()); //生成总任务
  83. $stationTaskBatches=$this->stationTaskBatchService->createByBatches($batches_shouldProcess,$stationTasks); //注册波次任务
  84. $stationTaskMaterialBoxes=$this->stationTaskMaterialBoxService->createByBatches($batches_shouldProcess,$stationTasks); //注册料箱任务
  85. $stationTaskCommodities=$this->stationTaskCommodityService->createByBatches($batches_shouldProcess,$stationTasks); //注册商品任务
  86. app('LogService')->log('海柔','assignTasks4',json_encode($batches_shouldProcess));
  87. });
  88. foreach ($stationTasks as &$stationTask){
  89. $stationTask->loadMissing([
  90. "stationTaskCommodities.commodity.barcodes",
  91. "stationTaskCommodities.materialBox",
  92. "stationTaskBatches.batch.owner",
  93. "stationTaskMaterialBoxes.materialBox",
  94. ]);
  95. }
  96. app('LogService')->log('海柔','assignTasks5',json_encode($stationTask).json_encode($batches_shouldProcess));
  97. $jsonStationTasks=json_encode($stationTasks);
  98. broadcast(new BroadcastToStation(BroadcastToStation::ALL_STATION, $jsonStationTasks));
  99. app('LogService')->log('海柔','assignTasks6',$jsonStationTasks.json_encode($batches_shouldProcess));
  100. // $ran=$this->stationTaskBatchService->runMany($stationTaskBatches);//执行波次任务
  101. }catch(Exception $e){
  102. app('LogService')->log('海柔','assignTasks7',json_encode($batches));
  103. $batchesJson='';
  104. foreach ($batches as $batch){
  105. $batchesJson.=json_encode($batch);
  106. Cache::tags(['波次防重叠'.$batch['id']])->flush();
  107. }
  108. app('LogService')->log('海柔','assignTasks8',json_encode($batches));
  109. throw new ErrorException('注册任务失败: '. $batchesJson . $e->getMessage().json_encode($e->getTrace()));
  110. }
  111. }
  112. public function getBatchByCodes($codes)
  113. {
  114. if(empty($codes))return collect();
  115. if(count($codes) == 0)return collect();
  116. return Batch::query()->whereIn('code',$codes)->get();
  117. }
  118. /**
  119. * 检查批次订单信息,存在库位异常 直接删除所有所属商品 重新拉取
  120. *
  121. * @param Collection|array $batches
  122. */
  123. public function checkBatchOrderInfo($batches)
  124. {
  125. if (!is_array($batches))$batches = $batches->toArray();
  126. $ids = array_column($batches,"id");
  127. $batches = Batch::query()->whereIn("id",$ids)->with("orders.orderCommodities")->get();
  128. foreach ($batches as $batch){
  129. if (!$batch->orders)continue;
  130. foreach ($batch->orders as $order){
  131. if (!$order->orderCommodities)app("OrderService")->notExistToRecover($order);
  132. else{
  133. $mark = false;
  134. foreach ($order->orderCommodities as $orderCommodity){
  135. if (!$orderCommodity->location){
  136. $mark = true;break;
  137. }
  138. }
  139. if ($mark){
  140. OrderCommodity::query()->where("order_id",$order->id)->delete();
  141. app("OrderService")->notExistToRecover($order);
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }