| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace Longyi\RewardPoints\Models;
- use Illuminate\Database\Eloquent\Model;
- class RewardPointSetting extends Model
- {
- protected $table = 'mw_reward_points_settings';
-
- protected $fillable = [
- 'code',
- 'name',
- 'value',
- 'type',
- 'group',
- 'sort_order',
- 'options',
- 'description',
- 'is_enabled'
- ];
-
- protected $casts = [
- 'sort_order' => 'integer',
- 'is_enabled' => 'boolean',
- 'options' => 'array'
- ];
-
- /**
- * 获取配置值(自动转换类型)
- */
- public function getTypedValueAttribute()
- {
- switch ($this->type) {
- case 'boolean':
- return (bool) $this->value;
- case 'number':
- return (float) $this->value;
- case 'select':
- $options = $this->options ?? [];
- return $options[$this->value] ?? $this->value;
- default:
- return $this->value;
- }
- }
-
- /**
- * 按分组获取所有启用的配置
- */
- public static function getGroupSettings($group)
- {
- return self::where('group', $group)
- ->where('is_enabled', true)
- ->orderBy('sort_order')
- ->get()
- ->pluck('value', 'code')
- ->toArray();
- }
-
- /**
- * 获取单个配置值
- */
- public static function getValue($code, $default = null)
- {
- $setting = self::where('code', $code)->first();
-
- if (!$setting) {
- return $default;
- }
-
- return $setting->typed_value;
- }
-
- /**
- * 设置配置值
- */
- public static function setValue($code, $value)
- {
- return self::updateOrCreate(
- ['code' => $code],
- ['value' => $value]
- );
- }
- }
|