BatchService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\OracleActAllocationDetails;
  5. use App\Order;
  6. use App\OrderCommodity;
  7. use App\Owner;
  8. use Exception;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Facades\Http;
  11. use App\Traits\ServiceAppAop;
  12. Class BatchService
  13. {
  14. use ServiceAppAop;
  15. /** @var StationTaskBatchService $stationTaskBatchService */
  16. private $stationTaskBatchService;
  17. /** @var StationRuleBatchService $stationRuleBatchService */
  18. private $stationRuleBatchService;
  19. /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
  20. private $stationTaskMaterialBoxService;
  21. /** @var StationTaskCommodityService $stationTaskCommodityService */
  22. private $stationTaskCommodityService;
  23. /** @var StationTaskService $stationTaskService */
  24. private $stationTaskService;
  25. public function __construct(){
  26. $this->stationTaskBatchService=null;
  27. $this->stationRuleBatchService=null;
  28. $this->stationTaskMaterialBoxService=null;
  29. $this->stationTaskCommodityService=null;
  30. $this->stationTaskService=null;
  31. }
  32. public function get(array $params)
  33. {
  34. $query = Batch::query();
  35. foreach ($params as $column=>$param){
  36. if (is_array($param))$query->whereIn($column,$param);
  37. else $query->where($column,$param);
  38. }
  39. return $query->get();
  40. }
  41. public function insert(array $insert): bool
  42. {
  43. $result = Batch::query()->insert($insert);
  44. // if($result)$this->assignTasks($insert);
  45. return $result;
  46. }
  47. public function updateWhereIn($key,$values,$updateKeyValues){
  48. Batch::query()->whereIn($key,$values)->update($updateKeyValues);
  49. }
  50. /**
  51. * 为波次附加任务,已附加的重复任务不影响
  52. * @param Collection $batches
  53. * @throws Exception
  54. */
  55. public function assignTasks(Collection $batches)
  56. {
  57. // $this->directTemp($batches);
  58. // LogService::log(__CLASS__,__FUNCTION__,'1');
  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. // LogService::log(__CLASS__,__FUNCTION__,'2'.$batches->toJson());
  65. $batches_shouldProcess = $this->stationRuleBatchService->getBatches_shouldProcess($batches); //按规则过滤需要的波次
  66. // LogService::log(__CLASS__,__FUNCTION__,'3 $batches_shouldProcess:'.$batches_shouldProcess->toJson());
  67. if($batches_shouldProcess->isEmpty()) return;
  68. // LogService::log(__CLASS__,__FUNCTION__,'4');
  69. $stationTasks = $this->stationTaskService->create($batches_shouldProcess->count()); //生成总任务
  70. // LogService::log(__CLASS__,__FUNCTION__,'5'.$stationTasks->toJson());
  71. $stationTaskBatches=$this->stationTaskBatchService->createByBatches($batches_shouldProcess,$stationTasks); //注册波次任务
  72. // LogService::log(__CLASS__,__FUNCTION__,'6'.$stationTaskBatches->toJson());
  73. $stationTaskCommodities=$this->stationTaskCommodityService->createByBatches($batches_shouldProcess,$stationTasks); //注册商品任务
  74. // LogService::log(__CLASS__,__FUNCTION__,'7'.$stationTaskCommodities->toJson());
  75. $stationTaskMaterialBoxes=$this->stationTaskMaterialBoxService->createByBatches($batches_shouldProcess,$stationTasks); //注册料箱任务
  76. // LogService::log(__CLASS__,__FUNCTION__,'8'.$stationTaskMaterialBoxes->toJson());
  77. $ran=$this->stationTaskBatchService->runMany($stationTaskBatches);//执行波次任务
  78. // LogService::log(__CLASS__,__FUNCTION__,'10'.json_encode([$ran]));
  79. }
  80. // public function directTemp($batches){
  81. // $ownerName='';
  82. // $batches=$batches->map(function(Batch $batch)use($ownerName){
  83. // $owner=Owner::query()->where('name',$ownerName)->get();
  84. // if(!$owner) return false;
  85. // if($batch['owner_id']==$owner['id'])return $batch;
  86. // return false;
  87. // });
  88. // $bins=$batches->map(function (Batch $batch){
  89. // $batch->loadMissing('orders.orderCommodities');
  90. //
  91. // foreach ($batch['orders'] as $order){
  92. // foreach ($order['orderCommodities'] as $orderCommodity){
  93. // return [
  94. // "taskCode" =>'testTask'.microtime(),
  95. // "binCode" => $orderCommodity['location'],
  96. // "toLocCode" => 'BIN-OUT1',
  97. // ];
  98. // }
  99. // }
  100. // return null;
  101. // });
  102. //
  103. // $json= [
  104. // "taskMode" =>2,
  105. // "bins"=>$bins,
  106. // "groupCode"=>'testGroup'.microtime(),
  107. // "priority"=>10,
  108. // "sequenceFlag"=>1,
  109. // ];
  110. // $response = Http::post(config('api.haiq.storage.moveBin'),$json);
  111. // LogService::log(__CLASS__,__METHOD__,$response->json());
  112. // }
  113. public function getBatchByCodes($codes)
  114. {
  115. if(empty($codes))return collect();
  116. if(count($codes) == 0)return collect();
  117. return Batch::query()->whereIn('code',$codes)->get();
  118. }
  119. }