112 lines
3.3 KiB
PHP
112 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Model;
|
|
|
|
use Hyperf\DbConnection\Db;
|
|
|
|
/**
|
|
* @property int $translation_id
|
|
* @property string $group
|
|
* @property string $translation_key
|
|
* @property string $remark
|
|
* @property string $create_time
|
|
* @property string $update_time
|
|
* @property string $deleted_at
|
|
*/
|
|
class Translation extends Model
|
|
{
|
|
/**
|
|
* The table associated with the model.
|
|
*/
|
|
protected ?string $table = 'translation';
|
|
|
|
protected string $primaryKey = "translation_id";
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*/
|
|
protected array $fillable = [];
|
|
|
|
/**
|
|
* The attributes that should be cast to native types.
|
|
*/
|
|
protected array $casts = ['translation_id' => 'integer'];
|
|
|
|
public static function list(array $param)
|
|
{
|
|
$model = self::query()->with("values");
|
|
if (isset($param['group']) && $param['group'] != '') {
|
|
$model = $model->where('group', $param['group']);
|
|
}
|
|
if (isset($param['translation_key']) && $param['translation_key'] != '') {
|
|
$model = $model->where('translation_key', $param['translation_key']);
|
|
}
|
|
return $model->orderByDesc("translation_id")
|
|
->select(["translation_id", "group", "translation_key", "remark", "create_time", "update_time"])
|
|
->paginate((int)$param['limit']);
|
|
}
|
|
|
|
public static function getLangValue(string $locale, string $key): string|null
|
|
{
|
|
list($group, $key) = explode('.', $key);
|
|
return (new self())->setTable("t")
|
|
->from("translation as t")
|
|
->join("translation_value as tv", "tv.translation_id", "=", "t.translation_id")
|
|
->where("t.group", $group)
|
|
->where("t.translation_key", $key)
|
|
->where("tv.lang_code", strtolower(str_replace("_", "-", $locale)))
|
|
->value("tv.lang_value");
|
|
}
|
|
|
|
public static function getLang(string $locale)
|
|
{
|
|
$data = self::with(["values" => function ($query) use ($locale) {
|
|
var_dump(strtolower(str_replace("_", "-", $locale)));
|
|
$query->where('lang_code', strtolower(str_replace("_", "-", $locale)))->select(['lang_value','translation_id']);
|
|
}])->select(['group', 'translation_key', 'translation_id'])
|
|
->get()
|
|
->toArray();
|
|
// 组合数据
|
|
$json = [];
|
|
foreach ($data as $item) {
|
|
$json[$item['group']][$item['translation_key']] = count($item['values']) > 0 ? $item['values'][0]['lang_value'] : "";
|
|
}
|
|
return $json;
|
|
}
|
|
|
|
public function values()
|
|
{
|
|
return $this->hasMany(TranslationValue::class, 'translation_id', 'translation_id');
|
|
}
|
|
|
|
public static function add($data): bool
|
|
{
|
|
$values = $data['values'];
|
|
unset($data['values']);
|
|
// 更新主表
|
|
$id = parent::add($data);
|
|
if (!$id) {
|
|
return false;
|
|
}
|
|
// 更新子表
|
|
TranslationValue::setValue($id, $values);
|
|
return true;
|
|
}
|
|
|
|
public static function edit($data): bool
|
|
{
|
|
$values = $data['values'];
|
|
unset($data['values']);
|
|
// 更新主表
|
|
$res = parent::edit($data);
|
|
if (!$res) {
|
|
return false;
|
|
}
|
|
// 更新子表
|
|
TranslationValue::setValue($data['translation_id'], $values);
|
|
return true;
|
|
}
|
|
}
|