GameConsumeGroup.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Module\Game\Models;
  3. use Illuminate\Database\Eloquent\Relations\HasMany;
  4. use UCore\ModelCore;
  5. /**
  6. * 消耗组
  7. *
  8. * field start
  9. * @property int $id 主键
  10. * @property string $name 消耗组名称
  11. * @property string $code 消耗组编码(唯一)
  12. * @property string $description 消耗组描述
  13. * @property \Carbon\Carbon $created_at 创建时间
  14. * @property \Carbon\Carbon $updated_at 更新时间
  15. * field end
  16. *
  17. * @property-read GameConsumeItem[] $consumeItems 消耗组中的所有消耗项
  18. */
  19. class GameConsumeGroup extends ModelCore
  20. {
  21. /**
  22. * 与模型关联的表名
  23. *
  24. * @var string
  25. */
  26. protected $table = 'game_consume_groups';
  27. // attrlist start
  28. protected $fillable = [
  29. 'id',
  30. 'name',
  31. 'code',
  32. 'description',
  33. ];
  34. // attrlist end
  35. /**
  36. * 获取消耗组中的所有消耗项
  37. *
  38. * @return HasMany
  39. */
  40. public function consumeItems(): HasMany
  41. {
  42. return $this->hasMany(GameConsumeItem::class, 'group_id', 'id');
  43. }
  44. /**
  45. * 获取关联的标签(多态关联)
  46. *
  47. * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
  48. */
  49. public function tags()
  50. {
  51. return $this->morphToMany(GameTag::class, 'taggable', 'game_tag_relations', 'taggable_id', 'tag_id');
  52. }
  53. /**
  54. * 格式化消耗详情用于显示
  55. *
  56. * @return string
  57. */
  58. public function formatConsumeDetails(): string
  59. {
  60. if ($this->consumeItems->isEmpty()) {
  61. return '<span class="text-muted">暂无消耗项</span>';
  62. }
  63. $details = [];
  64. foreach ($this->consumeItems as $item) {
  65. $detail = $this->formatSingleConsumeItem($item);
  66. $details[] = $detail;
  67. }
  68. return '<div class="consume-details">' . implode('<br>', $details) . '</div>';
  69. }
  70. /**
  71. * 格式化标签用于显示
  72. *
  73. * @return string
  74. */
  75. public function formatTags(): string
  76. {
  77. if ($this->tags->isEmpty()) {
  78. return '<span class="text-muted">无标签</span>';
  79. }
  80. $tagHtml = [];
  81. foreach ($this->tags as $tag) {
  82. $tagHtml[] = $tag->formatTag();
  83. }
  84. return implode(' ', $tagHtml);
  85. }
  86. /**
  87. * 格式化单个消耗项
  88. *
  89. * @param GameConsumeItem $item
  90. * @return string
  91. */
  92. private function formatSingleConsumeItem(GameConsumeItem $item): string
  93. {
  94. $consumeTypeName = \App\Module\Game\Enums\CONSUME_TYPE::getName($item->consume_type);
  95. $targetName = $this->getTargetName($item);
  96. $quantity = $item->quantity;
  97. return sprintf(
  98. '<span class="badge badge-warning">%s</span> %s × %d',
  99. $consumeTypeName,
  100. $targetName,
  101. $quantity
  102. );
  103. }
  104. /**
  105. * 获取目标名称
  106. *
  107. * @param GameConsumeItem $item
  108. * @return string
  109. */
  110. private function getTargetName(GameConsumeItem $item): string
  111. {
  112. switch ($item->consume_type) {
  113. case \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value:
  114. try {
  115. $itemModel = \App\Module\GameItems\Models\Item::find($item->target_id);
  116. return $itemModel ? $itemModel->name : "物品 (ID: {$item->target_id})";
  117. } catch (\Exception $e) {
  118. return "物品 (ID: {$item->target_id})";
  119. }
  120. case \App\Module\Game\Enums\CONSUME_TYPE::CURRENCY->value:
  121. try {
  122. $currency = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id);
  123. return $currency ? $currency->name : "货币 (ID: {$item->target_id})";
  124. } catch (\Exception $e) {
  125. return "货币 (ID: {$item->target_id})";
  126. }
  127. case \App\Module\Game\Enums\CONSUME_TYPE::FUND_CONFIG->value:
  128. try {
  129. $fund = \App\Module\Fund\Models\FundConfigModel::find($item->target_id);
  130. return $fund ? $fund->name : "账户种类 (ID: {$item->target_id})";
  131. } catch (\Exception $e) {
  132. return "账户种类 (ID: {$item->target_id})";
  133. }
  134. default:
  135. return "未知消耗类型 (ID: {$item->target_id})";
  136. }
  137. }
  138. }