BatchService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\Exceptions\ErrorException;
  5. use App\Jobs\BatchTaskJob;
  6. use App\OracleActAllocationDetails;
  7. use App\Order;
  8. use App\OrderCommodity;
  9. use App\Owner;
  10. use Exception;
  11. use Illuminate\Support\Collection;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Http;
  15. use App\Traits\ServiceAppAop;
  16. class BatchService
  17. {
  18. use ServiceAppAop;
  19. protected $modelClass=Batch::class;
  20. /** @var StationTaskBatchService $stationTaskBatchService */
  21. private $stationTaskBatchService;
  22. /** @var StationRuleBatchService $stationRuleBatchService */
  23. private $stationRuleBatchService;
  24. /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
  25. private $stationTaskMaterialBoxService;
  26. /** @var StationTaskCommodityService $stationTaskCommodityService */
  27. private $stationTaskCommodityService;
  28. /** @var StationTaskService $stationTaskService */
  29. private $stationTaskService;
  30. public function __construct(){
  31. $this->stationTaskBatchService=null;
  32. $this->stationRuleBatchService=null;
  33. $this->stationTaskMaterialBoxService=null;
  34. $this->stationTaskCommodityService=null;
  35. $this->stationTaskService=null;
  36. }
  37. public function get(array $params)
  38. {
  39. $query = Batch::query();
  40. foreach ($params as $column=>$param){
  41. if (is_array($param))$query->whereIn($column,$param);
  42. else $query->where($column,$param);
  43. }
  44. return $query->get();
  45. }
  46. public function updateWhereIn($key,$values,$updateKeyValues){
  47. Batch::query()->whereIn($key,$values)->update($updateKeyValues);
  48. }
  49. /**
  50. * 为波次附加任务,已附加的重复任务不影响
  51. * @param $batches
  52. * @throws Exception
  53. */
  54. public function assignTasks($batches)
  55. {
  56. try{
  57. LogService::log(__METHOD__,'assignTasks','波次任务分配1:'.json_encode($batches));
  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. LogService::log(__METHOD__,'assignTasks','波次任务分配2:'.json_encode($batches));
  68. $stationTaskMaterialBoxes_occupied = $this->stationTaskMaterialBoxService->getOccupied_byBatches($batches_shouldProcess); //按规则过滤需要的波次
  69. LogService::log(__METHOD__,'assignTasks','波次任务分配2b:'.json_encode($batches));
  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){
  79. $stationTasks = $this->stationTaskService->create($batches_shouldProcess->count()); //生成总任务
  80. LogService::log(__METHOD__,'assignTasks','波次任务分配3:'.json_encode($batches));
  81. $stationTaskBatches=$this->stationTaskBatchService->createByBatches($batches_shouldProcess,$stationTasks); //注册波次任务
  82. LogService::log(__METHOD__,'assignTasks','波次任务分配4:'.json_encode($batches));
  83. $stationTaskMaterialBoxes=$this->stationTaskMaterialBoxService->createByBatches($batches_shouldProcess,$stationTasks); //注册料箱任务
  84. LogService::log(__METHOD__,'assignTasks','波次任务分配5:'.json_encode($stationTaskMaterialBoxes).json_encode($batches));
  85. $stationTaskCommodities=$this->stationTaskCommodityService->createByBatches($batches_shouldProcess,$stationTasks); //注册商品任务
  86. LogService::log(__METHOD__,'assignTasks','波次任务分配6:'.json_encode($batches));
  87. });
  88. // $ran=$this->stationTaskBatchService->runMany($stationTaskBatches);//执行波次任务
  89. LogService::log(__METHOD__,'assignTasks','波次任务分配7:'.json_encode($batches));
  90. }catch(Exception $e){
  91. $batchesJson='';
  92. foreach ($batches as $batch){
  93. $batchesJson.=json_encode($batch);
  94. Cache::tags(['波次防重叠'.$batch['id']])->flush();
  95. }
  96. throw new ErrorException('注册任务失败: '. $batchesJson . $e->getMessage().json_encode($e->getTrace()));
  97. }
  98. }
  99. public function getBatchByCodes($codes)
  100. {
  101. if(empty($codes))return collect();
  102. if(count($codes) == 0)return collect();
  103. return Batch::query()->whereIn('code',$codes)->get();
  104. }
  105. /**
  106. * 检查批次订单信息,存在库位异常 直接删除所有所属商品 重新拉取
  107. *
  108. * @param Collection|array $batches
  109. */
  110. public function checkBatchOrderInfo($batches)
  111. {
  112. if (!is_array($batches))$batches = $batches->toArray();
  113. $ids = array_column($batches,"id");
  114. $batches = Batch::query()->whereIn("id",$ids)->with("orders.orderCommodities")->get();
  115. foreach ($batches as $batch){
  116. if (!$batch->orders)continue;
  117. foreach ($batch->orders as $order){
  118. if (!$order->orderCommodities)app("OrderService")->notExistToRecover($order);
  119. else{
  120. $mark = false;
  121. foreach ($order->orderCommodities as $orderCommodity){
  122. if (!$orderCommodity->location){
  123. $mark = true;break;
  124. }
  125. }
  126. if ($mark){
  127. OrderCommodity::query()->where("order_id",$order->id)->delete();
  128. app("OrderService")->notExistToRecover($order);
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }