PromotionProfitRule.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Module\Promotion\Models;
  3. use App\Module\Promotion\Enums\PROFIT_SOURCE_TYPE;
  4. use UCore\ModelCore;
  5. /**
  6. * 收益分成规则
  7. *
  8. * field start
  9. * field end
  10. */
  11. class PromotionProfitRule extends ModelCore
  12. {
  13. /**
  14. * 与模型关联的表名
  15. *
  16. * @var string
  17. */
  18. protected $table = 'promotion_profit_rules';
  19. // attrlist start
  20. protected $fillable = [
  21. ];
  22. // attrlist end
  23. /**
  24. * 应该被转换为原生类型的属性
  25. *
  26. * @var array
  27. */
  28. protected $casts = [
  29. 'source_type' => PROFIT_SOURCE_TYPE::class,
  30. 'direct_profit_rate' => 'float',
  31. 'status' => 'boolean',
  32. 'rules' => 'json',
  33. ];
  34. /**
  35. * 获取规则的特定属性
  36. *
  37. * @param string $key
  38. * @param mixed $default
  39. * @return mixed
  40. */
  41. public function getRuleAttribute(string $key, $default = null)
  42. {
  43. if (empty($this->rules)) {
  44. return $default;
  45. }
  46. $rules = is_array($this->rules) ? $this->rules : json_decode($this->rules, true);
  47. return $rules[$key] ?? $default;
  48. }
  49. /**
  50. * 判断规则是否有效
  51. *
  52. * @return bool
  53. */
  54. public function isActive(): bool
  55. {
  56. return (bool)$this->status;
  57. }
  58. }