visuddhinanda 1 ay önce
ebeveyn
işleme
f1bdba95ad

+ 1 - 1
api-v12/app/Console/Commands/ExportAiTrainingData.php

@@ -45,7 +45,7 @@ class ExportAiTrainingData extends Command
      */
      */
     public function handle()
     public function handle()
     {
     {
-        Log::debug('task export offline sentence-table start');
+        Log::info('task export offline sentence-table start');
         //创建文件夹
         //创建文件夹
         $base = 'app/tmp/export/offline';
         $base = 'app/tmp/export/offline';
         $exportDir = storage_path($base);
         $exportDir = storage_path($base);

+ 102 - 0
api-v12/app/Console/Commands/ExportGlossary.php

@@ -0,0 +1,102 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\File;
+use App\Services\TermService;
+
+
+class ExportGlossary extends Command
+{
+    /**
+     * The name and signature of the console command.
+     * php artisan export:export-glossary zh-Hans
+     * @var string
+     */
+    protected $signature = 'export:export-glossary {lang}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '导出术语表';
+
+    protected TermService $termService;
+
+    public function __construct(TermService $termService)
+    {
+        $this->termService = $termService;
+        parent::__construct();
+    }
+    /**
+     * Execute the console command.
+     */
+    public function handle()
+    {
+        Log::info('task export offline sentence-table start');
+        $lang = $this->argument('lang');
+        //创建文件夹
+        $base = 'app/tmp/export/offline';
+        $exportDir = storage_path($base);
+        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);
+            }
+        }
+
+        //创建临时文件夹\
+        $dirname = $exportDir . '/' . 'wikipali_glossary_' . date("YmdHis");
+
+        $tmp = mkdir($dirname, 0755, true);
+        if (!$tmp) {
+            $this->error('mkdir fail path=' . $dirname);
+            return 1;
+        } else {
+            $this->info('make dir successful ' . $dirname);
+        }
+
+        $fpIndex = fopen($dirname . '/index.md', 'w');
+        if ($fpIndex === false) {
+            die('无法创建索引文件');
+        }
+
+        // 创建json文件
+        $this->info('export start' . $lang);
+        $filename = 'glossary_' . $lang . '.jsonl';
+        $exportFile = $dirname . '/' . $filename;
+        $fp = fopen($exportFile, 'w');
+        if ($fp === false) {
+            die('无法创建文件');
+        }
+        $start = time();
+
+        //**业务逻辑 */
+
+        $data = $this->termService->getCommunityGlossary($lang);
+        foreach ($data['items'] as $key => $value) {
+            fwrite($fp, json_encode($value, JSON_UNESCAPED_UNICODE) . "\n");
+        }
+
+        fclose($fpIndex);
+
+        $this->info((time() - $start) . ' seconds');
+        $this->call('export:zip2', [
+            'id' => 'wikipali_glossary',
+            'filename' => $dirname,
+            'title' => 'wikipali glossary of community',
+            'format' => 'jsonl',
+        ]);
+
+        sleep(5);
+        File::deleteDirectory($dirname);
+
+        return 0;
+    }
+}

+ 28 - 31
api-v12/app/Http/Controllers/TermVocabularyController.php

@@ -5,10 +5,16 @@ namespace App\Http\Controllers;
 use App\Models\DhammaTerm;
 use App\Models\DhammaTerm;
 use Illuminate\Http\Request;
 use Illuminate\Http\Request;
 use App\Http\Resources\TermVocabularyResource;
 use App\Http\Resources\TermVocabularyResource;
-use App\Http\Api\ChannelApi;
+use App\Services\TermService;
 
 
 class TermVocabularyController extends Controller
 class TermVocabularyController extends Controller
 {
 {
+    protected TermService $termService;
+
+    public function __construct(TermService $termService)
+    {
+        $this->termService = $termService;
+    }
     /**
     /**
      * Display a listing of the resource.
      * Display a listing of the resource.
      *
      *
@@ -16,36 +22,27 @@ class TermVocabularyController extends Controller
      */
      */
     public function index(Request $request)
     public function index(Request $request)
     {
     {
-        //
-        $table = DhammaTerm::select(['guid','word','tag','meaning','other_meaning']);
-        switch ($request->get('view')) {
-            case "grammar":
-                $localTerm = ChannelApi::getSysChannel(
-                    "_System_Grammar_Term_".strtolower($request->get('lang'))."_",
-                    "_System_Grammar_Term_en_"
-                );
-                if(!$localTerm){
-                    return $this->error('no term channel');
-                }
-                $table = $table->where('channal',$localTerm);
-                break;
-            case "studio":
-                break;
-            case "user":
-                break;
-            case "community":
-                $localTerm = ChannelApi::getSysChannel(
-                    "_community_term_".strtolower($request->get('lang'))."_",
-                    "_community_term_en_"
-                );
-                if(!$localTerm){
-                    return $this->error('no term channel');
-                }
-                $table = $table->where('channal',$localTerm);
-                break;
-        }
-        $result = $table->get();
-        return $this->ok(["rows"=>TermVocabularyResource::collection($result),'count'=>count($result)]);
+        // ✅ 数据验证
+        $validated = $request->validate([
+            'view' => ['required', 'string'],
+            'lang' => ['nullable', 'string'],
+        ]);
+
+        $view = $validated['view'];
+        $lang = $validated['lang'] ?? null;
+
+        // ✅ 使用 match 替代 switch
+        $data = match ($view) {
+            'grammar'   => $this->termService->getGrammarGlossary($lang),
+            'community' => $this->termService->getCommunityGlossary($lang),
+            'studio', 'user' => throw new \Exception('not implemented'),
+            default     => throw new \InvalidArgumentException('invalid view'),
+        };
+
+        return $this->ok([
+            'rows'  => TermVocabularyResource::collection($data['items']),
+            'count' => $data['total'],
+        ]);
     }
     }
 
 
     /**
     /**

+ 25 - 2
api-v12/app/Services/TermService.php

@@ -2,9 +2,32 @@
 
 
 namespace App\Services;
 namespace App\Services;
 
 
-use App\Models\TagMap;
+use App\Models\DhammaTerm;
+use App\Http\Api\ChannelApi;
+
 
 
 class TermService
 class TermService
 {
 {
-    public function getCommunityGlossary($lang) {}
+    public function getCommunityGlossary($lang)
+    {
+        $localTermChannel = ChannelApi::getSysChannel(
+            "_community_term_" . strtolower($lang) . "_",
+            "_community_term_en_"
+        );
+        $result = DhammaTerm::select(['word', 'tag', 'meaning', 'other_meaning'])
+            ->where('channal', $localTermChannel)
+            ->get();
+        return ['items' => $result, 'total' => count($result)];
+    }
+    public function getGrammarGlossary($lang)
+    {
+        $localTermChannel = ChannelApi::getSysChannel(
+            "_System_Grammar_Term_" . strtolower($lang) . "_",
+            "_System_Grammar_Term_en_"
+        );
+        $result = DhammaTerm::select(['word', 'tag', 'meaning', 'other_meaning'])
+            ->where('channal', $localTermChannel)
+            ->get();
+        return ['items' => $result, 'total' => count($result)];
+    }
 }
 }