BatchService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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->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. $batches_shouldProcess = $this->stationRuleBatchService->getBatches_shouldProcess($batches); //按规则过滤需要的波次
  60. if($batches_shouldProcess->isEmpty()) return;
  61. $stationTasks = $this->stationTaskService->create($batches_shouldProcess->count()); //生成总任务
  62. $stationTaskBatches=$this->stationTaskBatchService->createByBatches($batches_shouldProcess,$stationTasks); //注册波次任务
  63. $stationTaskMaterialBoxes=$this->stationTaskMaterialBoxService->createByBatches($batches_shouldProcess,$stationTasks); //注册料箱任务
  64. $stationTaskCommodities=$this->stationTaskCommodityService->createByBatches($batches_shouldProcess,$stationTasks); //注册商品任务
  65. $ran=$this->stationTaskBatchService->runMany($stationTaskBatches);//执行波次任务
  66. LogService::log('batchservice',__FUNCTION__,'8'.$ran->toJson());
  67. }catch(Exception $e){
  68. throw new ErrorException('注册任务失败: '.json_encode($batches). $e->getMessage());
  69. }
  70. }
  71. // public function directTemp($batches){
  72. // $ownerName='';
  73. // $batches=$batches->map(function(Batch $batch)use($ownerName){
  74. // $owner=Owner::query()->where('name',$ownerName)->get();
  75. // if(!$owner) return false;
  76. // if($batch['owner_id']==$owner['id'])return $batch;
  77. // return false;
  78. // });
  79. // $bins=$batches->map(function (Batch $batch){
  80. // $batch->loadMissing('orders.orderCommodities');
  81. //
  82. // foreach ($batch['orders'] as $order){
  83. // foreach ($order['orderCommodities'] as $orderCommodity){
  84. // return [
  85. // "taskCode" =>'testTask'.microtime(),
  86. // "binCode" => $orderCommodity['location'],
  87. // "toLocCode" => 'BIN-OUT1',
  88. // ];
  89. // }
  90. // }
  91. // return null;
  92. // });
  93. //
  94. // $json= [
  95. // "taskMode" =>2,
  96. // "bins"=>$bins,
  97. // "groupCode"=>'testGroup'.microtime(),
  98. // "priority"=>10,
  99. // "sequenceFlag"=>1,
  100. // ];
  101. // $response = Http::post(config('api.haiq.storage.moveBin'),$json);
  102. // LogService::log(__CLASS__,__METHOD__,$response->json());
  103. // }
  104. public function getBatchByCodes($codes)
  105. {
  106. if(empty($codes))return collect();
  107. if(count($codes) == 0)return collect();
  108. return Batch::query()->whereIn('code',$codes)->get();
  109. }
  110. }