FarmConfig.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Module\Farm\Models;
  3. use UCore\ModelCore;
  4. /**
  5. * 农场配置模型
  6. * field start
  7. * @property int $id 主键ID
  8. * @property string $config_key 配置键
  9. * @property string $config_name 配置名称
  10. * @property string $config_value 配置值
  11. * @property string $config_type 配置类型:string,integer,float,boolean,json
  12. * @property string $description 配置描述
  13. * @property string $default_value 默认值
  14. * @property bool $is_active 是否启用:0否,1是
  15. * @property \Carbon\Carbon $created_at 创建时间
  16. * @property \Carbon\Carbon $updated_at 更新时间
  17. * field end
  18. */
  19. class FarmConfig extends ModelCore
  20. {
  21. /**
  22. * 与模型关联的表名
  23. *
  24. * @var string
  25. */
  26. protected $table = 'farm_configs';
  27. // attrlist start
  28. protected $fillable = [
  29. 'id',
  30. 'config_key',
  31. 'config_name',
  32. 'config_value',
  33. 'config_type',
  34. 'description',
  35. 'default_value',
  36. 'is_active',
  37. ];
  38. // attrlist end
  39. /**
  40. * 应该被转换为原生类型的属性
  41. *
  42. * @var array
  43. */
  44. protected $casts = [
  45. 'is_active' => 'boolean',
  46. ];
  47. /**
  48. * 获取配置值(根据类型转换)
  49. *
  50. * @return mixed
  51. */
  52. public function getTypedValue()
  53. {
  54. $value = $this->config_value ?? $this->default_value;
  55. if ($value === null) {
  56. return null;
  57. }
  58. switch ($this->config_type) {
  59. case 'integer':
  60. return (int) $value;
  61. case 'float':
  62. return (float) $value;
  63. case 'boolean':
  64. return in_array(strtolower($value), ['1', 'true', 'yes', 'on']);
  65. case 'json':
  66. return json_decode($value, true);
  67. case 'string':
  68. default:
  69. return (string) $value;
  70. }
  71. }
  72. /**
  73. * 设置配置值(根据类型转换)
  74. *
  75. * @param mixed $value
  76. * @return void
  77. */
  78. public function setTypedValue($value): void
  79. {
  80. switch ($this->config_type) {
  81. case 'json':
  82. $this->config_value = json_encode($value);
  83. break;
  84. case 'boolean':
  85. $this->config_value = $value ? '1' : '0';
  86. break;
  87. default:
  88. $this->config_value = (string) $value;
  89. break;
  90. }
  91. }
  92. /**
  93. * 获取配置类型的中文名称
  94. *
  95. * @return string
  96. */
  97. public function getConfigTypeNameAttribute(): string
  98. {
  99. $types = [
  100. 'string' => '字符串',
  101. 'integer' => '整数',
  102. 'float' => '浮点数',
  103. 'boolean' => '布尔值',
  104. 'json' => 'JSON对象',
  105. ];
  106. return $types[$this->config_type] ?? '未知';
  107. }
  108. /**
  109. * 获取启用状态的中文名称
  110. *
  111. * @return string
  112. */
  113. public function getIsActiveNameAttribute(): string
  114. {
  115. return $this->is_active ? '启用' : '禁用';
  116. }
  117. }