ItemChestOpenCost.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use App\Module\GameItems\Enums\CHEST_COST_TYPE;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use UCore\ModelCore;
  6. /**
  7. * 宝箱开启消耗配置
  8. *
  9. * field start
  10. * @property int $id 记录ID,主键
  11. * @property int $chest_id 宝箱ID,外键关联item_items表
  12. * @property int $cost_type 消耗类型(1:物品, 2:货币, 3:其他资源)
  13. * @property int $cost_id 消耗的物品/货币/资源ID
  14. * @property int $cost_quantity 消耗数量
  15. * @property int $is_active 是否激活(0:否, 1:是)
  16. * @property \Carbon\Carbon $created_at 创建时间
  17. * @property \Carbon\Carbon $updated_at 更新时间
  18. * field end
  19. */
  20. class ItemChestOpenCost extends ModelCore
  21. {
  22. /**
  23. * 与模型关联的表名
  24. *
  25. * @var string
  26. */
  27. protected $table = 'item_chest_open_costs';
  28. // attrlist start
  29. protected $fillable = [
  30. 'id',
  31. 'chest_id',
  32. 'cost_type',
  33. 'cost_id',
  34. 'cost_quantity',
  35. 'is_active',
  36. ];
  37. // attrlist end
  38. /**
  39. * 应该被转换为原生类型的属性
  40. *
  41. * @var array
  42. */
  43. protected $casts = [
  44. 'cost_type' => 'integer',
  45. 'cost_id' => 'integer',
  46. 'cost_quantity' => 'integer',
  47. 'is_active' => 'boolean',
  48. ];
  49. /**
  50. * 获取关联的宝箱物品
  51. *
  52. * @return BelongsTo
  53. */
  54. public function chest(): BelongsTo
  55. {
  56. return $this->belongsTo(Item::class, 'chest_id', 'id');
  57. }
  58. /**
  59. * 获取关联的消耗物品(如果消耗类型为物品)
  60. *
  61. * @return BelongsTo|null
  62. */
  63. public function costItem(): ?BelongsTo
  64. {
  65. return $this->belongsTo(Item::class, 'cost_id');
  66. }
  67. /**
  68. * 获取消耗类型的文本描述
  69. *
  70. * @return string
  71. */
  72. public function getCostTypeTextAttribute(): string
  73. {
  74. return CHEST_COST_TYPE::getAll()[$this->cost_type] ?? '未知';
  75. }
  76. }