UpgradeDict.php 4.8 KB

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