Browse Source

字典内缺字段时用字典设置内容填充

visuddhinanda 3 years ago
parent
commit
cd0622c576
1 changed files with 53 additions and 35 deletions
  1. 53 35
      app/Console/Commands/UpgradeDict.php

+ 53 - 35
app/Console/Commands/UpgradeDict.php

@@ -23,6 +23,9 @@ class UpgradeDict extends Command
      */
     protected $description = '导入csv字典';
 
+	protected $dictInfo;
+	protected $cols;
+
     /**
      * Create a new command instance.
      *
@@ -48,68 +51,70 @@ class UpgradeDict extends Command
 						//是文件,查看是否是字典信息文件
 						$infoFile = $fullPath;
 						if(pathinfo($infoFile,PATHINFO_EXTENSION) === 'ini'){
-							$dictInfo = parse_ini_file($infoFile,true);
-							if(isset($dictInfo['meta']['dictname'])){
+							$this->dictInfo = parse_ini_file($infoFile,true);
+							if(isset($this->dictInfo['meta']['dictname'])){
 								//是字典信息文件
-								$this->info($dictInfo['meta']['dictname']);
+								$this->info($this->dictInfo['meta']['dictname']);
 								if(Str::isUuid($this->argument('uuid'))){
-									if($this->argument('uuid') !== $dictInfo['meta']['uuid']){
+									if($this->argument('uuid') !== $this->dictInfo['meta']['uuid']){
 										continue;
 									}
 								}
-								if(!Str::isUuid($dictInfo['meta']['uuid'])){
+								if(!Str::isUuid($this->dictInfo['meta']['uuid'])){
 									$this->error("not uuid");
 									continue;
 								}
 								$tableDict = DictInfo::firstOrNew([
-									"id" => $dictInfo['meta']['uuid']
+									"id" => $this->dictInfo['meta']['uuid']
 								]);
-								$tableDict->id = $dictInfo['meta']['uuid'];
-								$tableDict->name = $dictInfo['meta']['dictname'];
-								$tableDict->shortname = $dictInfo['meta']['shortname'];
-								$tableDict->description = $dictInfo['meta']['description'];
-								$tableDict->src_lang = $dictInfo['meta']['src_lang'];
-								$tableDict->dest_lang = $dictInfo['meta']['dest_lang'];
-								$tableDict->rows = $dictInfo['meta']['rows'];
+								$tableDict->id = $this->dictInfo['meta']['uuid'];
+								$tableDict->name = $this->dictInfo['meta']['dictname'];
+								$tableDict->shortname = $this->dictInfo['meta']['shortname'];
+								$tableDict->description = $this->dictInfo['meta']['description'];
+								$tableDict->src_lang = $this->dictInfo['meta']['src_lang'];
+								$tableDict->dest_lang = $this->dictInfo['meta']['dest_lang'];
+								$tableDict->rows = $this->dictInfo['meta']['rows'];
 								$tableDict->owner_id = config("app.admin.root_uuid");
-								$tableDict->meta = json_encode($dictInfo['meta']);
+								$tableDict->meta = json_encode($this->dictInfo['meta']);
 								$tableDict->save();
 
-								UserDict::where("dict_id",$dictInfo['meta']['uuid'])->delete();
+								$del = UserDict::where("dict_id",$this->dictInfo['meta']['uuid'])->delete();
+								$this->info("delete {$del} rows dict id = ".$this->dictInfo['meta']['uuid']);
 								$filename = $dir.'/'.pathinfo($infoFile,PATHINFO_FILENAME);
 								$csvFile = $filename . ".csv";
 								$count = 0;
-								$bar = $this->output->createProgressBar($dictInfo['meta']['rows']);
+								$bar = $this->output->createProgressBar($this->dictInfo['meta']['rows']);
 								while (file_exists($csvFile)) {
 									# code...
 									$this->info("runing:{$csvFile}");
 									$inputRow = 0;
 									if (($fp = fopen($csvFile, "r")) !== false) {
-										$cols = array();
+										$this->cols = array();
 										while (($data = fgetcsv($fp, 0, ',')) !== false) {
 											if ($inputRow == 0) {
 												foreach ($data as $key => $colname) {
 													# 列名列表
-													$cols[$colname] = $key;
+													$this->cols[$colname] = $key;
 												}
 											}else{
-												$word["id"]=app('snowflake')->id();
-												$word["word"] = $data[$cols['word']];
-												if(isset($cols['type'])) $word["type"] = $data[$cols['type']];
-												if(isset($cols['grammar'])) $word["grammar"] = $data[$cols['grammar']];
-												if(isset($cols['parent'])) $word["parent"] = $data[$cols['parent']];
-												if(isset($cols['mean'])) $word["mean"] = $data[$cols['mean']];
-												if(isset($cols['note'])) $word["note"] = $data[$cols['note']];
-												if(isset($cols['factors'])) $word["factors"] = $data[$cols['factors']];
-												if(isset($cols['factormean'])) $word["factormean"] = $data[$cols['factormean']];
-												if(isset($cols['status'])) $word["status"] = $data[$cols['status']];
-												if(isset($cols['language'])) $word["language"] = $data[$cols['language']];
-												if(isset($cols['confidence'])) $word["confidence"] = $data[$cols['confidence']];
-												$word["source"]='_PAPER_RICH_';
-												$word["create_time"]=(int)(microtime(true)*1000);
-												$word["creator_id"]=1;
-												$word["dict_id"] = $dictInfo['meta']['uuid'];
-												$id = UserDict::insert($word);
+												$newDict = new UserDict();
+												$newDict->id = app('snowflake')->id();
+												$newDict->word = $data[$this->cols['word']];
+												$newDict->type = $this->get($data,'type');
+												$newDict->grammar = $this->get($data,'grammar');
+												$newDict->parent = $this->get($data,'parent');
+												$newDict->mean = $this->get($data,'mean');
+												$newDict->note = $this->get($data,'note');
+												$newDict->factors = $this->get($data,'factors');
+												$newDict->factormean = $this->get($data,'factormean');
+												$newDict->status = $this->get($data,'status');
+												$newDict->language = $this->get($data,'language');
+												$newDict->confidence = $this->get($data,'confidence');
+												$newDict->source = $this->get($data,'source');
+												$newDict->create_time =(int)(microtime(true)*1000);
+												$newDict->creator_id = 1;
+												$newDict->dict_id = $this->dictInfo['meta']['uuid'];
+												$newDict->save();
 												$bar->advance();
 											}
 											$inputRow++;
@@ -137,6 +142,19 @@ class UpgradeDict extends Command
 			return;
 		}
 	}
+
+	/**
+	 * 获取列的值
+	 */
+	protected function get($data,$colname,$defualt=""){
+		if(isset($this->cols[$colname])){
+			return $data[$this->cols[$colname]];
+		}else if(isset($this->dictInfo['cols'][$colname])){
+			return $this->dictInfo['cols'][$colname];
+		}else{
+			return $defualt;
+		}
+	}
     /**
      * Execute the console command.
      *