StationTaskBatchService.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\Log;
  5. use App\Station;
  6. use App\StationTask;
  7. use App\StationTaskBatch;
  8. use App\StationTaskBatchType;
  9. use Exception;
  10. use Illuminate\Support\Collection;
  11. use Illuminate\Support\Facades\Cache;
  12. class StationTaskBatchService
  13. {
  14. /** @var StationService $stationService */
  15. private $stationService;
  16. /** @var StationTaskBatchTypeService $stationTaskBatchTypeService */
  17. private $stationTaskBatchTypeService;
  18. /** @var BatchService $batchService */
  19. private $batchService;
  20. /** @var StationTypeService $stationTypeService */
  21. private $stationTypeService;
  22. /** @var StationTaskService $stationTaskService */
  23. private $stationTaskService;
  24. public function __construct(){
  25. $this->stationService=null;
  26. $this->stationTypeService=null;
  27. $this->stationTaskBatchTypeService=null;
  28. $this->batchService=null;
  29. $this->stationTaskService=null;
  30. }
  31. /**
  32. * @param $batches Batch[]
  33. * @param Collection $stationTasks_toAttach
  34. * @return Collection
  35. * @throws Exception
  36. */
  37. function createByBatches(array $batches, Collection $stationTasks_toAttach): Collection
  38. {
  39. $this->stationService=app('StationService');
  40. $this->stationTypeService=app('StationTypeService');
  41. $this->stationTaskBatchTypeService=app('StationTaskBatchTypeService');
  42. $this->batchService=app('BatchService');
  43. $stationTaskBatches_toCreate=new Collection();
  44. $id_stationTaskBatchType=$this->stationTaskBatchTypeService->firstByWhere('name','U型线分捡');
  45. $batches_handled=collect();
  46. foreach ($batches as $batch){
  47. if ($batch['status']=='未处理'){
  48. $stationType=$this->stationTypeService->getByBatch($batch);
  49. $station=$this->stationService->getStation_byType($stationType['name']);
  50. $stationTaskBatches_toCreate->push([
  51. 'batch_id'=>$batch['id'],
  52. 'station_id'=>$station['id'],
  53. 'station_task_batch_type_id'=> $id_stationTaskBatchType,
  54. 'status'=>'待处理'
  55. ]);
  56. $batches_handled->push($batch);
  57. }
  58. }
  59. $this->batchService->updateWhereIn('id',data_get($batches_handled,'*.id'),['status'=>'处理中']);
  60. $this->insert($stationTaskBatches_toCreate->toArray());
  61. $this->stationTaskService->registerSubTasks($stationTasks_toAttach,
  62. $stationTaskBatches_toCreate->map(function ($stationMissionBatch){
  63. return [$stationMissionBatch];
  64. })
  65. );
  66. return $stationTaskBatches_toCreate;
  67. }
  68. public function insert(array $stationMissionBatches): bool
  69. {
  70. $inserted = StationTaskBatch::query()->insert($stationMissionBatches);
  71. LogService::log(__METHOD__,__FUNCTION__,json_encode($stationMissionBatches).
  72. '||'.json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),0,3)));
  73. return $inserted;
  74. }
  75. }