UpgradeDict.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Console\Command;
  5. use App\Models\UserDict;
  6. use App\Models\DictInfo;
  7. class UpgradeDict extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'upgrade:dict {uuid?}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '导入csv字典';
  21. protected $dictInfo;
  22. protected $cols;
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. private function scandict($dir){
  33. if(is_dir($dir)){
  34. $this->info("scan:".$dir);
  35. if($files = scandir($dir)){
  36. //进入目录搜索字典或子目录
  37. foreach ($files as $file) {
  38. //进入语言目录循环搜索
  39. $fullPath = $dir."/".$file;
  40. if(is_dir($fullPath) && $file !== '.' && $file !== '..'){
  41. //是目录继续搜索
  42. $this->scandict($fullPath);
  43. }else{
  44. //是文件,查看是否是字典信息文件
  45. $infoFile = $fullPath;
  46. if(pathinfo($infoFile,PATHINFO_EXTENSION) === 'ini'){
  47. $this->dictInfo = parse_ini_file($infoFile,true);
  48. if(isset($this->dictInfo['meta']['dictname'])){
  49. //是字典信息文件
  50. $this->info($this->dictInfo['meta']['dictname']);
  51. if(Str::isUuid($this->argument('uuid'))){
  52. if($this->argument('uuid') !== $this->dictInfo['meta']['uuid']){
  53. continue;
  54. }
  55. }
  56. if(!Str::isUuid($this->dictInfo['meta']['uuid'])){
  57. $this->error("not uuid");
  58. continue;
  59. }
  60. $tableDict = DictInfo::firstOrNew([
  61. "id" => $this->dictInfo['meta']['uuid']
  62. ]);
  63. $tableDict->id = $this->dictInfo['meta']['uuid'];
  64. $tableDict->name = $this->dictInfo['meta']['dictname'];
  65. $tableDict->shortname = $this->dictInfo['meta']['shortname'];
  66. $tableDict->description = $this->dictInfo['meta']['description'];
  67. $tableDict->src_lang = $this->dictInfo['meta']['src_lang'];
  68. $tableDict->dest_lang = $this->dictInfo['meta']['dest_lang'];
  69. $tableDict->rows = $this->dictInfo['meta']['rows'];
  70. $tableDict->owner_id = config("app.admin.root_uuid");
  71. $tableDict->meta = json_encode($this->dictInfo['meta']);
  72. $tableDict->save();
  73. $del = UserDict::where("dict_id",$this->dictInfo['meta']['uuid'])->delete();
  74. $this->info("delete {$del} rows dict id = ".$this->dictInfo['meta']['uuid']);
  75. $filename = $dir.'/'.pathinfo($infoFile,PATHINFO_FILENAME);
  76. $csvFile = $filename . ".csv";
  77. $count = 0;
  78. $bar = $this->output->createProgressBar($this->dictInfo['meta']['rows']);
  79. while (file_exists($csvFile)) {
  80. # code...
  81. $this->info("runing:{$csvFile}");
  82. $inputRow = 0;
  83. if (($fp = fopen($csvFile, "r")) !== false) {
  84. $this->cols = array();
  85. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  86. if ($inputRow == 0) {
  87. foreach ($data as $key => $colname) {
  88. # 列名列表
  89. $this->cols[$colname] = $key;
  90. }
  91. }else{
  92. $newDict = new UserDict();
  93. $newDict->id = app('snowflake')->id();
  94. $newDict->word = $data[$this->cols['word']];
  95. $newDict->type = $this->get($data,'type');
  96. $newDict->grammar = $this->get($data,'grammar');
  97. $newDict->parent = $this->get($data,'parent');
  98. $newDict->mean = $this->get($data,'mean');
  99. $newDict->note = $this->get($data,'note');
  100. $newDict->factors = $this->get($data,'factors');
  101. $newDict->factormean = $this->get($data,'factormean');
  102. $newDict->status = $this->get($data,'status');
  103. $newDict->language = $this->get($data,'language');
  104. $newDict->confidence = $this->get($data,'confidence');
  105. $newDict->source = $this->get($data,'source');
  106. $newDict->create_time =(int)(microtime(true)*1000);
  107. $newDict->creator_id = 1;
  108. $newDict->dict_id = $this->dictInfo['meta']['uuid'];
  109. $newDict->save();
  110. $bar->advance();
  111. }
  112. $inputRow++;
  113. }
  114. }
  115. $count++;
  116. $csvFile = $filename . "{$count}.csv";
  117. }
  118. $bar->finish();
  119. $this->info("done");
  120. }
  121. }
  122. }
  123. }
  124. //子目录搜素完毕
  125. return;
  126. }else{
  127. //获取子目录失败
  128. $this->error("scandir fail");
  129. return;
  130. }
  131. }else{
  132. $this->error("this is not dir input={$dir}");
  133. return;
  134. }
  135. }
  136. /**
  137. * 获取列的值
  138. */
  139. protected function get($data,$colname,$defualt=""){
  140. if(isset($this->cols[$colname])){
  141. return $data[$this->cols[$colname]];
  142. }else if(isset($this->dictInfo['cols'][$colname])){
  143. return $this->dictInfo['cols'][$colname];
  144. }else{
  145. return $defualt;
  146. }
  147. }
  148. /**
  149. * Execute the console command.
  150. *
  151. * @return int
  152. */
  153. public function handle()
  154. {
  155. $this->info("upgrade dict start");
  156. $this->scandict(config("app.path.dict_text"));
  157. $this->info("upgrade dict done");
  158. return 0;
  159. }
  160. }