Explorar o código

安装单词分析表

Bhikkhu-Kosalla %!s(int64=4) %!d(string=hai) anos
pai
achega
44f9ccc2e6

+ 47 - 1
app/Console/Commands/InstallWordStatistics.php

@@ -3,6 +3,9 @@
 namespace App\Console\Commands;
 
 use Illuminate\Console\Command;
+use App\Models\WordStatistic;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
 
 class InstallWordStatistics extends Command
 {
@@ -11,7 +14,7 @@ class InstallWordStatistics extends Command
      *
      * @var string
      */
-    protected $signature = 'command:name';
+    protected $signature = 'install:wordstatistics';
 
     /**
      * The console command description.
@@ -37,6 +40,49 @@ class InstallWordStatistics extends Command
      */
     public function handle()
     {
+		$startTime = time();
+
+		$info = "instert wordstatistics ";
+		$this->info($info.PHP_EOL);
+		Log::info($info);
+
+		#删除目标数据库中数据
+		WordStatistic::where('id', '>',-1)->delete();	
+
+		$scan = scandir(config("app.path.word_statistics"));
+		$bar = $this->output->createProgressBar(count($scan));
+		foreach($scan as $filename) {
+			$bar->advance();
+			$filename = config("app.path.word_statistics")."/".$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 = [
+								'bookid'=>$data[0],
+								'word'=>$data[1],
+								'count'=>$data[2],
+								'base'=>$data[3],
+								'end1'=>$data[4],
+								'end2'=>$data[5],
+								'type'=>$data[6],
+								'length'=>$data[7],
+							];
+							WordStatistic::create($newData);	
+							$count++;
+						}
+						Log::info("insert ".$count);
+					}
+				});				
+			}
+		}
+		$bar->finish();
+		$msg = "all done in ". time()-$startTime . "s";
+		Log::info($msg);
+		$this->info($msg);
+        return 0;
         return 0;
     }
 }

+ 2 - 0
app/Models/WordStatistic.php

@@ -8,4 +8,6 @@ use Illuminate\Database\Eloquent\Model;
 class WordStatistic extends Model
 {
     use HasFactory;
+	protected $fillable = ['bookid','word' , 'count', 'base' , 'end1' , 'end2','type', 'length'];
+
 }