BatchService.php 6.2 KB

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