BatchService.php 4.3 KB

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