Prechádzať zdrojové kódy

:sparkles: command Install:wordbook

visuddhinanda 4 rokov pred
rodič
commit
b31b83610c

+ 64 - 2
app/Console/Commands/InstallWordBook.php

@@ -3,6 +3,9 @@
 namespace App\Console\Commands;
 
 use Illuminate\Console\Command;
+use App\Models\BookWord;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
 
 class InstallWordBook extends Command
 {
@@ -11,14 +14,14 @@ class InstallWordBook extends Command
      *
      * @var string
      */
-    protected $signature = 'command:name';
+    protected $signature = 'install:wordbook {from?} {to?}';
 
     /**
      * The console command description.
      *
      * @var string
      */
-    protected $description = 'Command description';
+    protected $description = 'install palibook word list in each book';
 
     /**
      * Create a new command instance.
@@ -37,6 +40,65 @@ class InstallWordBook extends Command
      */
     public function handle()
     {
+		$startTime = time();
+
+		$this->info("instert word in palibook ");
+		Log::info("instert word in palibook ");
+
+		$_from = $this->argument('from');
+		$_to = $this->argument('to');
+		if(empty($_from) && empty($_to)){
+			$_from = 1;
+			$_to = 217;
+		}else if(empty($_to)){
+			$_to = $_from;
+		}
+
+		$bar = $this->output->createProgressBar($_to-$_from+1);
+
+		for ($book=$_from; $book <= $_to; $book++) { 
+			Log::info("doing ".($book));
+
+			#删除目标数据库中数据
+			BookWord::where('book', $book)->delete();
+
+			//分类汇总得到单词表
+			$bookword = array();
+			$fileId = $book-1;
+			if (($fpoutput = fopen(config("app.path.paliword_book") . "/{$fileId}_words.csv", "r")) !== false) {
+				$count = 0;
+				while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
+					$book = $data[1];
+					if (isset($bookword[$data[3]])) {
+						$bookword[$data[3]]++;
+					} else {
+						$bookword[$data[3]] = 1;
+					}
+		
+					$count++;
+				}
+			}else{
+				Log::error("open csv fail");
+				continue;
+			}
+			DB::transaction(function ()use($book,$bookword) {
+				foreach ($bookword as $key => $value) {
+					$newData = [
+						'book'=>$book,
+						'wordindex'=>$key,
+						'count'=>$value,
+					];
+					BookWord::create($newData);				
+				}
+			});
+			$bar->advance();
+		}
+		$bar->finish();
+
+		$msg = "all done in ". time()-$startTime . "s";
+		$this->info($msg.PHP_EOL);
+		Log::info($msg);
+		
         return 0;
     }
 }

+ 2 - 0
app/Models/BookWord.php

@@ -8,4 +8,6 @@ use Illuminate\Database\Eloquent\Model;
 class BookWord extends Model
 {
     use HasFactory;
+	protected $fillable = ['book' , 'wordindex', 'count'];
+
 }

+ 2 - 1
config/app.php

@@ -245,6 +245,7 @@ return [
 		'palitext_filelist' => public_path('palihtml').'/filelist.csv',
 		'palicsv' => public_path('tmp').'/palicsv',
 		'pali_title' => public_path('pali_title'),
-
+		'paliword' => public_path('paliword'),
+		'paliword_book' => public_path('paliword')."/book",
 	],
 ];