BatchService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 ErrorException;
  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 Collection $batches
  49. * @throws Exception
  50. */
  51. public function assignTasks(Collection $batches)
  52. {
  53. try{
  54. // $this->directTemp($batches);
  55. LogService::log(__CLASS__,__FUNCTION__,'1');
  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. LogService::log(__CLASS__,__FUNCTION__,'2'.$batches->toJson());
  62. $batches_shouldProcess = $this->stationRuleBatchService->getBatches_shouldProcess($batches); //按规则过滤需要的波次
  63. LogService::log(__CLASS__,__FUNCTION__,'3 $batches_shouldProcess:'.$batches_shouldProcess->toJson());
  64. if($batches_shouldProcess->isEmpty()) return;
  65. LogService::log(__CLASS__,__FUNCTION__,'4');
  66. $stationTasks = $this->stationTaskService->create($batches_shouldProcess->count()); //生成总任务
  67. LogService::log(__CLASS__,__FUNCTION__,'5'.$stationTasks->toJson());
  68. $stationTaskBatches=$this->stationTaskBatchService->createByBatches($batches_shouldProcess,$stationTasks); //注册波次任务
  69. LogService::log(__CLASS__,__FUNCTION__,'6'.$stationTaskBatches->toJson());
  70. $stationTaskCommodities=$this->stationTaskCommodityService->createByBatches($batches_shouldProcess,$stationTasks); //注册商品任务
  71. LogService::log(__CLASS__,__FUNCTION__,'7'.$stationTaskCommodities->toJson());
  72. $stationTaskMaterialBoxes=$this->stationTaskMaterialBoxService->createByBatches($batches_shouldProcess,$stationTasks); //注册料箱任务
  73. LogService::log(__CLASS__,__FUNCTION__,'8'.$stationTaskMaterialBoxes->toJson());
  74. $ran=$this->stationTaskBatchService->runMany($stationTaskBatches);//执行波次任务
  75. LogService::log(__CLASS__,__FUNCTION__,'10'.json_encode([$ran]));
  76. }catch(Exception $e){
  77. throw new ErrorException('注册任务失败: '.json_encode($batches). $e->getMessage());
  78. }
  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. }