PointCurrencyModel.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Module\Point\Models;
  3. use App\Module\Point\Enums\POINT_CURRENCY_TYPE;
  4. use App\Module\Point\Casts\PointCurrencyDisplayAttributesCast;
  5. use UCore\ModelCore;
  6. /**
  7. * 积分类型表
  8. *
  9. * 用于存储系统支持的各种积分类型信息,如经验积分、成就积分等。
  10. * 一个积分类型可以对应多个积分账户种类(PointConfigModel)。
  11. * 专注于整数积分处理,不涉及小数运算。
  12. *
  13. * field start
  14. * @property int $id 自增
  15. * @property string $identification 积分标识
  16. * @property \App\Module\Point\Enums\POINT_CURRENCY_TYPE $type 积分类型,关联POINT_CURRENCY_TYPE枚举
  17. * @property string $icon 积分图标
  18. * @property string $name 积分名称
  19. * @property \App\Module\Point\Casts\PointCurrencyDisplayAttributesCast $display_attributes 显示属性,如图标、颜色等
  20. * @property int $create_time 创建时间
  21. * @property int $update_time 更新时间
  22. * field end
  23. */
  24. class PointCurrencyModel extends ModelCore
  25. {
  26. protected $table = 'point_currency';
  27. // attrlist start
  28. protected $fillable = [
  29. 'id',
  30. 'identification',
  31. 'type',
  32. 'icon',
  33. 'name',
  34. 'display_attributes',
  35. 'create_time',
  36. 'update_time',
  37. ];
  38. // attrlist end
  39. public $timestamps = false;
  40. protected $casts = [
  41. 'type' => POINT_CURRENCY_TYPE::class,
  42. 'display_attributes' => PointCurrencyDisplayAttributesCast::class,
  43. ];
  44. /**
  45. * 根据类型获取积分类型配置
  46. *
  47. * @param int $type 积分类型
  48. * @return PointCurrencyModel|null 积分类型模型
  49. */
  50. public static function getByType(int $type): ?PointCurrencyModel
  51. {
  52. return self::where('type', $type)->first();
  53. }
  54. /**
  55. * 根据标识获取积分类型配置
  56. *
  57. * @param string $identification 积分标识
  58. * @return PointCurrencyModel|null 积分类型模型
  59. */
  60. public static function getByIdentification(string $identification): ?PointCurrencyModel
  61. {
  62. return self::where('identification', $identification)->first();
  63. }
  64. /**
  65. * 获取所有积分类型
  66. *
  67. * @return \Illuminate\Database\Eloquent\Collection 积分类型集合
  68. */
  69. public static function getAllCurrencies()
  70. {
  71. return self::orderBy('type')->get();
  72. }
  73. /**
  74. * 检查积分类型是否存在
  75. *
  76. * @param int $type 积分类型
  77. * @return bool 是否存在
  78. */
  79. public static function typeExists(int $type): bool
  80. {
  81. return self::where('type', $type)->exists();
  82. }
  83. /**
  84. * 检查积分标识是否存在
  85. *
  86. * @param string $identification 积分标识
  87. * @return bool 是否存在
  88. */
  89. public static function identificationExists(string $identification): bool
  90. {
  91. return self::where('identification', $identification)->exists();
  92. }
  93. /**
  94. * 获取积分类型名称
  95. *
  96. * @return string 积分类型名称
  97. */
  98. public function getCurrencyName(): string
  99. {
  100. return $this->type->getCurrencyName();
  101. }
  102. /**
  103. * 获取积分类型标识
  104. *
  105. * @return string 积分类型标识
  106. */
  107. public function getIdentification(): string
  108. {
  109. return $this->type->getIdentification();
  110. }
  111. /**
  112. * 获取积分类型描述
  113. *
  114. * @return string 积分类型描述
  115. */
  116. public function getDescription(): string
  117. {
  118. return $this->type->getDescription();
  119. }
  120. /**
  121. * 获取积分类型图标
  122. *
  123. * @return string 积分类型图标
  124. */
  125. public function getTypeIcon(): string
  126. {
  127. return $this->type->getIcon();
  128. }
  129. /**
  130. * 关联积分配置表
  131. */
  132. public function configs()
  133. {
  134. return $this->hasMany(PointConfigModel::class, 'currency_id');
  135. }
  136. }