data = $data; } /** * Execute the job. * * @return void */ public function handle() { try{ DB::beginTransaction(); $this->settle(); DB::commit(); //输出信息 测试 $this->echoMessage(new ConsoleOutput()); }catch (\Exception $exception){ DB::rollBack(); file_put_contents('record_error.txt',date("Y-m-d H:i:s",time()).json_encode($this->data) . PHP_EOL.$exception->getMessage()."line" . $exception->getLine().PHP_EOL,8); } } public function settle(){ $data = $this->data; if(empty($data)){ file_put_contents('record_data.txt',date("Y-m-d H:i:s",time())."原数据:".json_encode($data).PHP_EOL,8); return; } if(empty($data['assetOas'])) { file_put_contents('record_data.txt',date("Y-m-d H:i:s",time())."数据结构错误".PHP_EOL,8); return; } //内存设置 ini_set('memory_limit', -1); //软删除 $time = time(); Asset::where('del_time',0)->update(['del_time' => $time]); $batchSize = 100; $totalAssets = count($data['assetOas']); for ($i = 0; $i < $totalAssets; $i += $batchSize) { $batch = array_slice($data['assetOas'], $i, $batchSize); // 对每个批次的数据进行处理 foreach ($batch as $value) { if(empty($value['singleCode'])) continue; // 进行您的操作 Asset::updateOrCreate( ['singleCode' => $value['singleCode']], //查询条件 [ "assetCode" => $value['assetCode'] ?? "", "assetNo" => $value['assetNo'] ?? "", "assetType" => $value['assetType'] ?? "", "brand" => $value['brand'] ?? "", "expectedLife" => $value['expectedLife'] ?? "", "gs1" => $value['gs1'] ?? "", "isKey" => $value['isKey'] ?? "", "kind" => $value['kind'] ?? "", "located" => $value['located'] ?? "", "name" => $value['name'] ?? "", "nextCalibrationTime" => $value['nextCalibrationTime'] ?? "", "originalValue" => $value['originalValue'] ?? "", "purchaseTime" => $value['purchaseTime'] ?? "", "remark" => $value['remark'] ?? "", "singleCode" => $value['singleCode'] ?? "", "startUseDate" => $value['startUseDate'] ?? "", "type" => $value['type'] ?? "", "useDept" => $value['useDept'] ?? "", "userName" => $value['userName'] ?? "", "version" => $value['version'] ?? "", "del_time" => 0 ] //添加或者修改的数据 ); } // 在处理完每个批次后,进行一些内存清理操作 unset($batch); } } protected function echoMessage(OutputInterface $output) { $output->writeln($this->data); } }