| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace App\Module\Farm\Models;
- use UCore\ModelCore;
- /**
- * 农场配置模型
- * field start
- * @property int $id 主键ID
- * @property string $config_key 配置键
- * @property string $config_name 配置名称
- * @property string $config_value 配置值
- * @property string $config_type 配置类型:string,integer,float,boolean,json
- * @property string $description 配置描述
- * @property string $default_value 默认值
- * @property bool $is_active 是否启用:0否,1是
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class FarmConfig extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'farm_configs';
- // attrlist start
- protected $fillable = [
- 'id',
- 'config_key',
- 'config_name',
- 'config_value',
- 'config_type',
- 'description',
- 'default_value',
- 'is_active',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'is_active' => 'boolean',
- ];
- /**
- * 获取配置值(根据类型转换)
- *
- * @return mixed
- */
- public function getTypedValue()
- {
- $value = $this->config_value ?? $this->default_value;
- if ($value === null) {
- return null;
- }
- switch ($this->config_type) {
- case 'integer':
- return (int) $value;
- case 'float':
- return (float) $value;
- case 'boolean':
- return in_array(strtolower($value), ['1', 'true', 'yes', 'on']);
- case 'json':
- return json_decode($value, true);
- case 'string':
- default:
- return (string) $value;
- }
- }
- /**
- * 设置配置值(根据类型转换)
- *
- * @param mixed $value
- * @return void
- */
- public function setTypedValue($value): void
- {
- switch ($this->config_type) {
- case 'json':
- $this->config_value = json_encode($value);
- break;
- case 'boolean':
- $this->config_value = $value ? '1' : '0';
- break;
- default:
- $this->config_value = (string) $value;
- break;
- }
- }
- /**
- * 获取配置类型的中文名称
- *
- * @return string
- */
- public function getConfigTypeNameAttribute(): string
- {
- $types = [
- 'string' => '字符串',
- 'integer' => '整数',
- 'float' => '浮点数',
- 'boolean' => '布尔值',
- 'json' => 'JSON对象',
- ];
- return $types[$this->config_type] ?? '未知';
- }
- /**
- * 获取启用状态的中文名称
- *
- * @return string
- */
- public function getIsActiveNameAttribute(): string
- {
- return $this->is_active ? '启用' : '禁用';
- }
- }
|