Преглед на файлове

:tada: 完成 install:wbwtemplate

visuddhinanda преди 4 години
родител
ревизия
cbd000d6e5
променени са 3 файла, в които са добавени 87 реда и са изтрити 5 реда
  1. 5 2
      app/Console/Commands/Install.php
  2. 81 3
      app/Console/Commands/InstallWbwTemplate.php
  3. 1 0
      app/Models/WbwTemplate.php

+ 5 - 2
app/Console/Commands/Install.php

@@ -11,14 +11,14 @@ class Install extends Command
      *
      * @var string
      */
-    protected $signature = 'command:name';
+    protected $signature = 'install {--queue}';
 
     /**
      * The console command description.
      *
      * @var string
      */
-    protected $description = 'Command description';
+    protected $description = 'install new host';
 
     /**
      * Create a new command instance.
@@ -37,6 +37,9 @@ class Install extends Command
      */
     public function handle()
     {
+		$this->call('install:wbwtemplate', [
+			'from' => 1, 'to' => 217
+		]);
         return 0;
     }
 }

+ 81 - 3
app/Console/Commands/InstallWbwTemplate.php

@@ -3,6 +3,9 @@
 namespace App\Console\Commands;
 
 use Illuminate\Console\Command;
+//use Symfony\Component\Console\Helper\ProgressBar;
+use App\Models\WbwTemplate;
+use Illuminate\Support\Facades\DB;
 
 class InstallWbwTemplate extends Command
 {
@@ -11,7 +14,7 @@ class InstallWbwTemplate extends Command
      *
      * @var string
      */
-    protected $signature = 'command:name';
+    protected $signature = 'install:wbwtemplate {from?} {to?}';
 
     /**
      * The console command description.
@@ -37,6 +40,81 @@ class InstallWbwTemplate extends Command
      */
     public function handle()
     {
+		$this->info("instert wbw template");
+		$GLOBALS['wbw'] = new WbwTemplate;
+
+
+		$_from = $this->argument('from');
+		$_to = $this->argument('to');
+		if(empty($_from) && empty($_to)){
+			$_from = 1;
+			$_to = 217;
+		}else if(empty($_to)){
+			$_to = $_from;
+		}
+		$fileListFileName = public_path('/palihtml/filelist.csv');
+
+		$filelist = array();
+
+		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 . "/";
+		
+		
+			#删除目标数据库中数据
+			$GLOBALS['wbw']->where('book', $from)->delete();
+
+
+			// 打开文件并读取数据
+			
+			if (($GLOBALS["fp"] = fopen($dirXmlBase . $dirXml . $outputFileNameHead . ".csv", "r")) !== false) {
+				$GLOBALS["row"]=0;
+				DB::transaction(function () {
+					$query = "INSERT INTO wbw_templates ( book , paragraph, wid , word , real , type , gramma , part , style, created_at,updated_at ) VALUES (?,?,?,?,?,?,?,?,?,now(),now())";
+
+					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]	
+						];
+						DB::table('wbw_templates')->insert($params);
+
+					}
+					
+				});
+				fclose($GLOBALS["fp"]);
+			} else {
+				$this->error("can not open csv file. filename=" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv".PHP_EOL) ;
+			}
+			
+			$bar->advance();
+			//$this->info("{$from}:updata {$GLOBALS["row"]} recorders.") ;
+		}
+		$bar->finish();
         return 0;
-    }
-}
+    
+	}
+}

+ 1 - 0
app/Models/WbwTemplate.php

@@ -8,4 +8,5 @@ use Illuminate\Database\Eloquent\Model;
 class WbwTemplate extends Model
 {
     use HasFactory;
+	protected $fillable = ['book' , 'paragraph', 'wid' , 'word' , 'real' , 'type' , 'gramma' , 'part' , 'style'];
 }