BatchService.php 5.7 KB

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