MexConfig.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Module\Mex\Models;
  3. use App\Module\Mex\Enums\MexConfigGroup;
  4. use App\Module\Mex\Enums\MexConfigType;
  5. use UCore\ModelCore;
  6. use Illuminate\Database\Eloquent\Builder;
  7. /**
  8. * Mex配置模型
  9. *
  10. * field start
  11. * @property int $id 配置ID,主键
  12. * @property string $key 配置键名,唯一标识
  13. * @property string $name 配置名称
  14. * @property string $description 配置描述
  15. * @property \App\Module\Mex\Enums\MexConfigGroup $group 配置分组
  16. * @property \App\Module\Mex\Enums\MexConfigType $type 配置类型:1布尔值,2整数,3小数,4字符串,5JSON,6数组
  17. * @property string $value 配置值
  18. * @property string $default_value 默认值
  19. * @property array $options 可选项配置(JSON格式)
  20. * @property string $validation_rules 验证规则
  21. * @property bool $is_enabled 是否启用:0禁用,1启用
  22. * @property bool $is_readonly 是否只读:0可编辑,1只读
  23. * @property int $sort_order 排序权重
  24. * @property string $remark 备注说明
  25. * @property \Carbon\Carbon $created_at 创建时间
  26. * @property \Carbon\Carbon $updated_at 更新时间
  27. * field end
  28. */
  29. class MexConfig extends ModelCore
  30. {
  31. protected $table = 'mex_configs';
  32. protected $fillable = [
  33. 'key',
  34. 'name',
  35. 'description',
  36. 'group',
  37. 'type',
  38. 'value',
  39. 'default_value',
  40. 'options',
  41. 'validation_rules',
  42. 'is_enabled',
  43. 'is_readonly',
  44. 'sort_order',
  45. 'remark',
  46. ];
  47. protected $casts = [
  48. 'group' => MexConfigGroup::class,
  49. 'type' => MexConfigType::class,
  50. 'is_enabled' => 'boolean',
  51. 'is_readonly' => 'boolean',
  52. 'sort_order' => 'integer',
  53. 'options' => 'array',
  54. ];
  55. /**
  56. * 获取配置的实际值(根据类型转换)
  57. */
  58. public function getTypedValue()
  59. {
  60. if (!$this->is_enabled) {
  61. return $this->type->castValue($this->default_value);
  62. }
  63. return $this->type->castValue($this->value);
  64. }
  65. /**
  66. * 设置配置值(根据类型验证和转换)
  67. */
  68. public function setTypedValue($value): bool
  69. {
  70. if (!$this->type->validateValue($value)) {
  71. return false;
  72. }
  73. $this->value = $this->type->castValue($value);
  74. return true;
  75. }
  76. /**
  77. * 获取配置的显示值
  78. */
  79. public function getDisplayValue(): string
  80. {
  81. $value = $this->getTypedValue();
  82. return match ($this->type) {
  83. MexConfigType::BOOLEAN => $value ? '是' : '否',
  84. MexConfigType::JSON, MexConfigType::ARRAY => json_encode($value, JSON_UNESCAPED_UNICODE),
  85. default => (string) $value,
  86. };
  87. }
  88. /**
  89. * 检查配置是否有效
  90. */
  91. public function isValid(): bool
  92. {
  93. if (!$this->is_enabled) {
  94. return true;
  95. }
  96. return $this->type->validateValue($this->value);
  97. }
  98. /**
  99. * 重置为默认值
  100. */
  101. public function resetToDefault(): void
  102. {
  103. $this->value = $this->default_value;
  104. }
  105. /**
  106. * 作用域:按分组筛选
  107. */
  108. public function scopeByGroup(Builder $query, MexConfigGroup $group): Builder
  109. {
  110. return $query->where('group', $group);
  111. }
  112. /**
  113. * 作用域:只查询启用的配置
  114. */
  115. public function scopeEnabled(Builder $query): Builder
  116. {
  117. return $query->where('is_enabled', true);
  118. }
  119. /**
  120. * 作用域:只查询非只读的配置
  121. */
  122. public function scopeEditable(Builder $query): Builder
  123. {
  124. return $query->where('is_readonly', false);
  125. }
  126. /**
  127. * 作用域:按排序权重排序
  128. */
  129. public function scopeOrdered(Builder $query): Builder
  130. {
  131. return $query->orderBy('sort_order')->orderBy('key');
  132. }
  133. /**
  134. * 作用域:按键名搜索
  135. */
  136. public function scopeSearchKey(Builder $query, string $search): Builder
  137. {
  138. return $query->where(function ($q) use ($search) {
  139. $q->where('key', 'like', "%{$search}%")
  140. ->orWhere('name', 'like', "%{$search}%")
  141. ->orWhere('description', 'like', "%{$search}%");
  142. });
  143. }
  144. /**
  145. * 获取分组标签
  146. */
  147. public function getGroupLabelAttribute(): string
  148. {
  149. return $this->group->getLabel();
  150. }
  151. /**
  152. * 获取类型标签
  153. */
  154. public function getTypeLabelAttribute(): string
  155. {
  156. return $this->type->getLabel();
  157. }
  158. /**
  159. * 获取状态标签
  160. */
  161. public function getStatusLabelAttribute(): string
  162. {
  163. if (!$this->is_enabled) {
  164. return '已禁用';
  165. }
  166. if ($this->is_readonly) {
  167. return '只读';
  168. }
  169. return '正常';
  170. }
  171. }