BatchService.php 5.7 KB

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