info('instert wbw template'); $_from = $this->argument('from'); $_to = $this->argument('to'); if (empty($_from) && empty($_to)) { $_from = 1; $_to = 217; } elseif (empty($_to)) { $_to = $_from; } $fileListFileName = public_path('/palihtml/filelist.csv'); $filelist = []; if (($handle = fopen($fileListFileName, 'r')) !== false) { while (($filelist[] = fgetcsv($handle, 0, ',')) !== false) { } } $bar = $this->output->createProgressBar($_to - $_from + 1); for ($from = $_from; $from <= $_to; $from++) { // code... $fileSn = $from - 1; $outputFileNameHead = $filelist[$fileSn][1]; $dirXmlBase = public_path('/tmp/palicsv').'/'; $dirXml = $outputFileNameHead.'/'; // 删除目标数据库中数据 WbwTemplate::where('book', $from)->delete(); // 打开文件并读取数据 if (($GLOBALS['fp'] = fopen($dirXmlBase.$dirXml.$outputFileNameHead.'.csv', 'r')) !== false) { $GLOBALS['row'] = 0; DB::transaction(function () { while (($data = fgetcsv($GLOBALS['fp'], 0, ',')) !== false) { $GLOBALS['row']++; if ($GLOBALS['row'] == 1) { continue; } // 或略第一行 标题行 $params = [ 'book' => mb_substr($data[2], 1), 'paragraph' => $data[3], 'wid' => $data[16], 'word' => $data[4], 'real' => $data[5], 'type' => $data[6], 'gramma' => $data[7], 'part' => $data[10], 'style' => $data[15], ]; WbwTemplate::insert($params); } }); fclose($GLOBALS['fp']); } else { $this->error('can not open csv file. filename='.$dirXmlBase.$dirXml.$outputFileNameHead.'.csv'.PHP_EOL); Log::error('can not open csv file. filename='.$dirXmlBase.$dirXml.$outputFileNameHead.'.csv'.PHP_EOL); } $bar->advance(); } $bar->finish(); return 0; } }