BatchService.php 5.3 KB

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