Procházet zdrojové kódy

安装单词索引表

Bhikkhu-Kosalla před 4 roky
rodič
revize
46a78cbfba

+ 45 - 1
app/Console/Commands/InstallWordIndex.php

@@ -3,6 +3,9 @@
 namespace App\Console\Commands;
 
 use Illuminate\Console\Command;
+use App\Models\WordIndex;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
 
 class InstallWordIndex extends Command
 {
@@ -11,7 +14,7 @@ class InstallWordIndex extends Command
      *
      * @var string
      */
-    protected $signature = 'command:name';
+    protected $signature = 'install:wordindex';
 
     /**
      * The console command description.
@@ -37,6 +40,47 @@ class InstallWordIndex extends Command
      */
     public function handle()
     {
+		$startTime = time();
+
+		$info = "instert word in palibook ";
+		$this->info($info);
+		Log::info($info);
+
+		#删除目标数据库中数据
+		WordIndex::where('id', '>',-1)->delete();	
+
+		$scan = scandir(config("app.path.paliword_index"));
+		$bar = $this->output->createProgressBar(count($scan));
+		foreach($scan as $filename) {
+			$bar->advance();
+			$filename = config("app.path.paliword_index")."/".$filename;
+			if (is_file($filename)) {
+				Log::info("doing ".$filename);
+				DB::transaction(function ()use($filename) {
+				if (($fpoutput = fopen($filename, "r")) !== false) {
+						$count = 0;
+						while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
+							$newData = [
+								'id'=>$data[0],
+								'word'=>$data[1],
+								'word_en'=>$data[2],
+								'normal'=>$data[3],
+								'bold'=>$data[4],
+								'is_base'=>$data[5],
+								'len'=>$data[6],
+							];
+							WordIndex::create($newData);	
+							$count++;
+						}
+						Log::info("insert ".$count);
+					}
+				});				
+			}
+		}
+		$bar->finish();
+		$msg = "all done in ". time()-$startTime . "s";
+		Log::info($msg);
+		$this->info($msg);
         return 0;
     }
 }

+ 3 - 0
app/Models/WordIndex.php

@@ -8,4 +8,7 @@ use Illuminate\Database\Eloquent\Model;
 class WordIndex extends Model
 {
     use HasFactory;
+	protected $fillable = ['id' , 'word' , 'word_en' , 'count' , 'normal' , 'bold' , 'is_base' , 'len'  ];
+
+	
 }