| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- namespace App\Module\Mex\Models;
- use App\Module\Mex\Enums\MexConfigGroup;
- use App\Module\Mex\Enums\MexConfigType;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Builder;
- /**
- * Mex配置模型
- *
- * field start
- * @property int $id 配置ID,主键
- * @property string $key 配置键名,唯一标识
- * @property string $name 配置名称
- * @property string $description 配置描述
- * @property \App\Module\Mex\Enums\MexConfigGroup $group 配置分组
- * @property \App\Module\Mex\Enums\MexConfigType $type 配置类型:1布尔值,2整数,3小数,4字符串,5JSON,6数组
- * @property string $value 配置值
- * @property string $default_value 默认值
- * @property array $options 可选项配置(JSON格式)
- * @property string $validation_rules 验证规则
- * @property bool $is_enabled 是否启用:0禁用,1启用
- * @property bool $is_readonly 是否只读:0可编辑,1只读
- * @property int $sort_order 排序权重
- * @property string $remark 备注说明
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class MexConfig extends ModelCore
- {
- protected $table = 'mex_configs';
- protected $fillable = [
- 'key',
- 'name',
- 'description',
- 'group',
- 'type',
- 'value',
- 'default_value',
- 'options',
- 'validation_rules',
- 'is_enabled',
- 'is_readonly',
- 'sort_order',
- 'remark',
- ];
- protected $casts = [
- 'group' => MexConfigGroup::class,
- 'type' => MexConfigType::class,
- 'is_enabled' => 'boolean',
- 'is_readonly' => 'boolean',
- 'sort_order' => 'integer',
- 'options' => 'array',
- ];
- /**
- * 获取配置的实际值(根据类型转换)
- */
- public function getTypedValue()
- {
- if (!$this->is_enabled) {
- return $this->type->castValue($this->default_value);
- }
- return $this->type->castValue($this->value);
- }
- /**
- * 设置配置值(根据类型验证和转换)
- */
- public function setTypedValue($value): bool
- {
- if (!$this->type->validateValue($value)) {
- return false;
- }
- $this->value = $this->type->castValue($value);
- return true;
- }
- /**
- * 获取配置的显示值
- */
- public function getDisplayValue(): string
- {
- $value = $this->getTypedValue();
- return match ($this->type) {
- MexConfigType::BOOLEAN => $value ? '是' : '否',
- MexConfigType::JSON, MexConfigType::ARRAY => json_encode($value, JSON_UNESCAPED_UNICODE),
- default => (string) $value,
- };
- }
- /**
- * 检查配置是否有效
- */
- public function isValid(): bool
- {
- if (!$this->is_enabled) {
- return true;
- }
- return $this->type->validateValue($this->value);
- }
- /**
- * 重置为默认值
- */
- public function resetToDefault(): void
- {
- $this->value = $this->default_value;
- }
- /**
- * 作用域:按分组筛选
- */
- public function scopeByGroup(Builder $query, MexConfigGroup $group): Builder
- {
- return $query->where('group', $group);
- }
- /**
- * 作用域:只查询启用的配置
- */
- public function scopeEnabled(Builder $query): Builder
- {
- return $query->where('is_enabled', true);
- }
- /**
- * 作用域:只查询非只读的配置
- */
- public function scopeEditable(Builder $query): Builder
- {
- return $query->where('is_readonly', false);
- }
- /**
- * 作用域:按排序权重排序
- */
- public function scopeOrdered(Builder $query): Builder
- {
- return $query->orderBy('sort_order')->orderBy('key');
- }
- /**
- * 作用域:按键名搜索
- */
- public function scopeSearchKey(Builder $query, string $search): Builder
- {
- return $query->where(function ($q) use ($search) {
- $q->where('key', 'like', "%{$search}%")
- ->orWhere('name', 'like', "%{$search}%")
- ->orWhere('description', 'like', "%{$search}%");
- });
- }
- /**
- * 获取分组标签
- */
- public function getGroupLabelAttribute(): string
- {
- return $this->group->getLabel();
- }
- /**
- * 获取类型标签
- */
- public function getTypeLabelAttribute(): string
- {
- return $this->type->getLabel();
- }
- /**
- * 获取状态标签
- */
- public function getStatusLabelAttribute(): string
- {
- if (!$this->is_enabled) {
- return '已禁用';
- }
- if ($this->is_readonly) {
- return '只读';
- }
- return '正常';
- }
- }
|