BatchService.php 6.3 KB

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