Jelajahi Sumber

:construction:加入业务代码

visuddhinanda 1 tahun lalu
induk
melakukan
62c54590c6
1 mengubah file dengan 57 tambahan dan 5 penghapusan
  1. 57 5
      api-v8/app/Console/Commands/ExportAiPaliWordToken.php

+ 57 - 5
api-v8/app/Console/Commands/ExportAiPaliWordToken.php

@@ -4,15 +4,17 @@ namespace App\Console\Commands;
 
 use Illuminate\Console\Command;
 use App\Http\Api\DictApi;
+use Illuminate\Support\Facades\Log;
+use App\Models\UserDict;
 
 class ExportAiPaliWordToken extends Command
 {
     /**
      * The name and signature of the console command.
-     *
+     * php artisan export:ai.pali.word.token
      * @var string
      */
-    protected $signature = 'export:ai.pali.word.token';
+    protected $signature = 'export:ai.pali.word.token {--format=gz  : zip file format 7z,lzma,gz }';
 
     /**
      * The console command description.
@@ -39,11 +41,61 @@ class ExportAiPaliWordToken extends Command
     public function handle()
     {
         $this->info('export ai pali word token');
-        $dict_id = DictApi::getSysDict('robot_compound');
-        if(!$dict_id){
-            $this->error('没有找到 robot_compound 字典');
+        Log::debug('export ai pali word token');
+
+        if (\App\Tools\Tools::isStop()) {
+            return 0;
+        }
+        $exportDir = storage_path('app/tmp/export/offline');
+        if (!is_dir($exportDir)) {
+            $res = mkdir($exportDir, 0755, true);
+            if (!$res) {
+                $this->error('mkdir fail path=' . $exportDir);
+                return 1;
+            } else {
+                $this->info('make dir successful ' . $exportDir);
+            }
+        }
+
+        $dict_id = DictApi::getSysDict('system_preference');
+        if (!$dict_id) {
+            $this->error('没有找到 system_preference 字典');
+            return 1;
+        }
+
+        $filename = 'ai-pali-word-token-' . date("Y-m-d") . '.tsv';
+        $exportFile = $exportDir . '/' . $filename;
+        $fp = fopen($exportFile, 'w');
+        if ($fp === false) {
+            $this->error('无法创建文件');
             return 1;
         }
+
+        $start = time();
+        $total = UserDict::where('dict_id', $dict_id)->count();
+        $words = UserDict::where('dict_id', $dict_id)
+            ->select([
+                'word',
+                'factors',
+            ])->cursor();
+        foreach ($words as $key => $word) {
+            $output = array($word->word, $word->factors);
+            fwrite($fp, implode("\t", $output) . "\n");
+            if ($key % 100 === 0) {
+                $present = (int)($key * 100 / $total);
+                $this->info("[{$present}%]-{$key}");
+            }
+        }
+        fclose($fp);
+        $this->info((time() - $start) . ' seconds');
+
+        $this->call('export:zip', [
+            'id' => 'ai-pali-word-token',
+            'filename' => $exportFile,
+            'title' => 'ai pali word token',
+            'format' => $this->option('format'),
+        ]);
+
         return 0;
     }
 }