StationTaskBatchService.php 2.5 KB

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