BatchService.php 5.8 KB

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