BatchService.php 5.8 KB

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