| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace App\Services;
- use App\Exceptions\ErrorException;
- use App\StationTaskBatch;
- use Exception;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use App\Traits\ServiceAppAop;
- class StationTaskBatchService
- {
- use ServiceAppAop;
- protected $modelClass=StationTaskBatch::class;
- /** @var StationService $stationService */
- private $stationService;
- /** @var StationTaskBatchTypeService $stationTaskBatchTypeService */
- private $stationTaskBatchTypeService;
- /** @var BatchService $batchService */
- private $batchService;
- /** @var StationTypeService $stationTypeService */
- private $stationTypeService;
- /** @var StationTaskService $stationTaskService */
- private $stationTaskService;
- /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService */
- private $foreignHaiRoboticsService;
- public function __construct()
- {
- $this->stationService = null;
- $this->stationTypeService = null;
- $this->stationTaskBatchTypeService = null;
- $this->batchService = null;
- $this->stationTaskService = null;
- $this->foreignHaiRoboticsService = null;
- }
- /**
- * @param Collection $batches Batch[]
- * @param Collection $stationTasks_toAttach
- * @return Collection
- * @throws Exception
- */
- function createByBatches(Collection $batches, Collection $stationTasks_toAttach): Collection
- {
- $this->stationService = app('StationService');
- $this->stationTaskService = app('StationTaskService');
- $this->stationTypeService = app('StationTypeService');
- $this->stationTaskBatchTypeService = app('StationTaskBatchTypeService');
- $this->batchService = app('BatchService');
- $stationTaskBatches_toCreate = new Collection();
- $stationTaskBatchType = $this->stationTaskBatchTypeService->firstByWhere('name', 'U型线分捡');
- $id_stationTaskBatchType=$stationTaskBatchType['id']??'';
- $batches_handled = collect();
- foreach ($batches as $batch) {
- if ($batch['status'] == '未处理') {
- $stationType = $this->stationTypeService->getByBatch($batch);
- $station = $this->stationService->getStation_byType($stationType['name']);
- $stationTaskBatches_toCreate->push(
- new StationTaskBatch([
- 'batch_id' => $batch['id'],
- 'station_id' => $station['id'],
- 'station_task_batch_type_id' => $id_stationTaskBatchType,
- 'status' => '待处理'
- ])
- );
- $batches_handled->push($batch);
- }
- }
- $this->batchService->updateWhereIn('id', data_get($batches_handled, '*.id'), ['status' => '处理中']);
- $this->insert($stationTaskBatches_toCreate->toArray());
- $stationTaskBatches_toCreate=$this->getAndAttachIds($stationTaskBatches_toCreate);
- $this->stationTaskService->registerSubTasks(
- $stationTasks_toAttach,
- $stationTaskBatches_toCreate->map(function($taskBatch){
- return [$taskBatch];
- })
- );
- return $stationTaskBatches_toCreate;
- }
- function getAndAttachIds($stationTaskBatches): Collection
- {
- $md5=md5(is_array($stationTaskBatches)
- ?$md5=json_encode($stationTaskBatches):$stationTaskBatches->toJson());
- return Cache::remember(
- 'StationTaskBatch_'.$md5??md5(json_encode($stationTaskBatches->toArray()))
- ,config('cache.expirations.rarelyChange')
- ,function()use($stationTaskBatches){
- return StationTaskBatch::query()
- ->whereIn('status',data_get($stationTaskBatches,'*.status'))
- ->whereIn('batch_id',data_get($stationTaskBatches,'*.batch_id'))
- ->get();
- });
- }
- function markManyExcepted(Collection $stationTaskBatches_failed)
- {
- foreach (
- $stationTaskBatches_failed
- as $stationTaskBatch) {
- if($stationTaskBatch['status']!='异常')
- $this->markExcepted($stationTaskBatch);
- }
- ($logAtFailings_andWait =
- function ($stationTaskBatches_failed) {
- if ($stationTaskBatches_failed->isEmpty()) return;
- throw new ErrorException('任务波次异常失败的');
- })($stationTaskBatches_failed);
- }
- /**
- * @param Collection $stationTaskBatches
- * @return Collection|\Tightenco\Collect\Support\Collection|null 返回执行失败的记录
- */
- function runMany(Collection $stationTaskBatches):?Collection
- {
- $stationTaskBatches_failed = null;
- ($execute =
- function (
- Collection $stationTaskBatches, &$stationTaskBatches_failed) {
- if ($stationTaskBatches->isEmpty()) return;
- $stationTaskBatches_failed = collect();
- foreach ($stationTaskBatches as $stationTaskBatch) {
- $failed = !$this->run($stationTaskBatch);
- if ($failed) $stationTaskBatches_failed->push($stationTaskBatch);
- }
- })($stationTaskBatches, $stationTaskBatches_failed);
- ($logAtFailings_andWait =
- function ($stationTaskBatches_failed) {
- if ($stationTaskBatches_failed->isEmpty()) return;
- $retry_after_sec = config('task.batchTask.retry_after_sec');
- LogService::log(__METHOD__, __FUNCTION__,
- '任务波次没有执行完的,' . $retry_after_sec . '秒后准备重试:' . $stationTaskBatches_failed->toJson()
- . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
- );
- sleep($retry_after_sec);
- })($stationTaskBatches_failed);
- $execute ($stationTaskBatches_failed, $stationTaskBatches_failed); //再次尝试
- $this->markManyExcepted ($stationTaskBatches_failed);
- return $stationTaskBatches_failed;
- }
- function run(StationTaskBatch $stationTaskBatch): bool
- {
- $this->instant($this->foreignHaiRoboticsService,'ForeignHaiRoboticsService');
- $this->instant($this->stationService,'StationService');
- $stationTaskBatch->loadMissing(['station','stationTask.stationTaskMaterialBoxes']);
- $toLocation = $this->stationService->getULineEntrance($stationTaskBatch['station'])['code'];
- $groupPrefix = $stationTaskBatch['id'];
- $taskMaterialBoxes = $stationTaskBatch['stationTask']['stationTaskMaterialBoxes'] ??
- (function () use ($stationTaskBatch) {
- throw new Exception('找不到料箱:' . json_encode($stationTaskBatch));
- })();
- $isFetchedFromRobotics
- = $this->foreignHaiRoboticsService->
- fetchGroup($toLocation, $taskMaterialBoxes, $groupPrefix);
- ($markNewStatus
- =function()use($isFetchedFromRobotics,$stationTaskBatch){
- $isFetchedFromRobotics?
- $this->markProcessing($stationTaskBatch):
- $this->markExcepted($stationTaskBatch);
- })();
- return $isFetchedFromRobotics;
- }
- function markProcessing($stationTaskBatches)
- {
- if (get_class($stationTaskBatches)==StationTaskBatch::class){
- $stationTaskBatches = collect($stationTaskBatches);
- }
- StationTaskBatch::query()
- ->whereIn('id', data_get($stationTaskBatches, '*.id'))
- ->update(['status'=>'处理中']);
- $this->stationTaskService
- ->markProcessing_byId(
- data_get($stationTaskBatches, '*.station_id')
- );
- }
- // function markFinished($stationTaskBatches)
- // {
- // if (get_class($stationTaskBatches)==StationTaskBatch::class){
- // $stationTaskBatches = collect($stationTaskBatches);
- // }
- // StationTaskBatch::query()
- // ->whereIn('id', data_get($stationTaskBatches, '*.id'))
- // ->update(['status'=>'完成']);
- //
- // $this->stationTaskService
- // ->markProcessing_byId(
- // data_get($stationTaskBatches, '*.station_id')
- // );
- // }
- function markExcepted(StationTaskBatch $stationTaskBatch)
- {
- $stationTaskBatch['status'] = '异常';
- $stationTaskBatch ->update();
- }
- }
|