BatchService.php 5.2 KB

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