PointConfigModel.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Module\Point\Models;
  3. use App\Module\Point\Enums\POINT_TYPE;
  4. use App\Module\Point\Casts\PointDisplayAttributesCast;
  5. use UCore\ModelCore;
  6. /**
  7. * 积分配置表
  8. *
  9. * 用于存储系统支持的各种积分账户类型,如经验积分、成就积分等。
  10. * 同一个积分类型(PointCurrencyModel)可以有多个不同的账户种类。
  11. *
  12. * field start
  13. * @property int $id 自增
  14. * @property string $name 积分名称
  15. * @property int $currency_id 关联的积分类型ID,外键关联kku_point_currency表
  16. * @property \App\Module\Point\Enums\POINT_TYPE $type 积分账户类型,关联POINT_TYPE枚举
  17. * @property \App\Module\Point\Casts\PointDisplayAttributesCast $display_attributes 显示属性,如图标、颜色等
  18. * @property int $create_time
  19. * @property int $update_time 更新时间
  20. * field end
  21. */
  22. class PointConfigModel extends ModelCore
  23. {
  24. protected $table = 'point_config';
  25. // attrlist start
  26. protected $fillable = [
  27. 'id',
  28. 'name',
  29. 'currency_id',
  30. 'type',
  31. 'display_attributes',
  32. 'create_time',
  33. 'update_time',
  34. ];
  35. // attrlist end
  36. public $timestamps = false;
  37. protected $casts = [
  38. 'type' => POINT_TYPE::class,
  39. 'display_attributes' => PointDisplayAttributesCast::class,
  40. ];
  41. /**
  42. * 获取积分类型配置
  43. *
  44. * @param int $pointType 积分类型
  45. * @return PointConfigModel|null 配置模型
  46. */
  47. public static function getByType(int $pointType): ?PointConfigModel
  48. {
  49. return self::where('type', $pointType)->first();
  50. }
  51. /**
  52. * 获取所有积分配置
  53. *
  54. * @return \Illuminate\Database\Eloquent\Collection 配置集合
  55. */
  56. public static function getAllConfigs()
  57. {
  58. return self::orderBy('type')->get();
  59. }
  60. /**
  61. * 根据积分类型ID获取配置
  62. *
  63. * @param int $currencyId 积分类型ID
  64. * @return \Illuminate\Database\Eloquent\Collection 配置集合
  65. */
  66. public static function getByCurrencyId(int $currencyId)
  67. {
  68. return self::where('currency_id', $currencyId)->get();
  69. }
  70. /**
  71. * 检查积分类型是否存在
  72. *
  73. * @param int $pointType 积分类型
  74. * @return bool 是否存在
  75. */
  76. public static function typeExists(int $pointType): bool
  77. {
  78. return self::where('type', $pointType)->exists();
  79. }
  80. /**
  81. * 获取积分类型名称
  82. *
  83. * @return string 积分类型名称
  84. */
  85. public function getTypeName(): string
  86. {
  87. return $this->type->getTypeName();
  88. }
  89. /**
  90. * 获取积分类型描述
  91. *
  92. * @return string 积分类型描述
  93. */
  94. public function getTypeDescription(): string
  95. {
  96. return $this->type->getDescription();
  97. }
  98. /**
  99. * 关联积分类型表
  100. */
  101. public function currency()
  102. {
  103. return $this->belongsTo(PointCurrencyModel::class, 'currency_id');
  104. }
  105. }