TeamProfitRule.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Module\Team\Models;
  3. use App\Module\Team\Enums\PROFIT_SOURCE_TYPE;
  4. use UCore\ModelCore;
  5. /**
  6. * 收益分成规则
  7. *
  8. * field start
  9. * @property int $id 主键ID
  10. * @property \App\Module\Team\Enums\PROFIT_SOURCE_TYPE $source_type 来源类型
  11. * @property float $direct_profit_rate 直推分成比例
  12. * @property int $max_indirect_level 最大间推层级
  13. * @property bool $status 状态:1有效,0无效
  14. * @property array $rules 特殊规则
  15. * @property \Carbon\Carbon $created_at 创建时间
  16. * @property \Carbon\Carbon $updated_at 更新时间
  17. * field end
  18. */
  19. class TeamProfitRule extends ModelCore
  20. {
  21. /**
  22. * 与模型关联的表名
  23. *
  24. * @var string
  25. */
  26. protected $table = 'team_profit_rules';
  27. // attrlist start
  28. protected $fillable = [
  29. 'id',
  30. 'source_type',
  31. 'direct_profit_rate',
  32. 'max_indirect_level',
  33. 'status',
  34. 'rules',
  35. ];
  36. // attrlist end
  37. /**
  38. * 应该被转换为原生类型的属性
  39. *
  40. * @var array
  41. */
  42. protected $casts = [
  43. 'source_type' => PROFIT_SOURCE_TYPE::class,
  44. 'direct_profit_rate' => 'float',
  45. 'status' => 'boolean',
  46. 'rules' => 'json',
  47. ];
  48. /**
  49. * 获取规则的特定属性
  50. *
  51. * @param string $key
  52. * @param mixed $default
  53. * @return mixed
  54. */
  55. public function getRuleAttribute(string $key, $default = null)
  56. {
  57. if (empty($this->rules)) {
  58. return $default;
  59. }
  60. $rules = is_array($this->rules) ? $this->rules : json_decode($this->rules, true);
  61. return $rules[$key] ?? $default;
  62. }
  63. /**
  64. * 判断规则是否有效
  65. *
  66. * @return bool
  67. */
  68. public function isActive(): bool
  69. {
  70. return (bool)$this->status;
  71. }
  72. }