UpgradeDict.php 6.0 KB

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