BatchService.php 5.3 KB

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