GameConsumeGroup.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 string
  48. */
  49. public function formatConsumeDetails(): string
  50. {
  51. if ($this->consumeItems->isEmpty()) {
  52. return '<span class="text-muted">暂无消耗项</span>';
  53. }
  54. $details = [];
  55. foreach ($this->consumeItems as $item) {
  56. $detail = $this->formatSingleConsumeItem($item);
  57. $details[] = $detail;
  58. }
  59. return '<div class="consume-details">' . implode('<br>', $details) . '</div>';
  60. }
  61. /**
  62. * 格式化单个消耗项
  63. *
  64. * @param GameConsumeItem $item
  65. * @return string
  66. */
  67. private function formatSingleConsumeItem(GameConsumeItem $item): string
  68. {
  69. $consumeTypeName = \App\Module\Game\Enums\CONSUME_TYPE::getName($item->consume_type);
  70. $targetName = $this->getTargetName($item);
  71. $quantity = $item->quantity;
  72. return sprintf(
  73. '<span class="badge badge-warning">%s</span> %s × %d',
  74. $consumeTypeName,
  75. $targetName,
  76. $quantity
  77. );
  78. }
  79. /**
  80. * 获取目标名称
  81. *
  82. * @param GameConsumeItem $item
  83. * @return string
  84. */
  85. private function getTargetName(GameConsumeItem $item): string
  86. {
  87. switch ($item->consume_type) {
  88. case \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value:
  89. try {
  90. $itemModel = \App\Module\GameItems\Models\Item::find($item->target_id);
  91. return $itemModel ? $itemModel->name : "物品 (ID: {$item->target_id})";
  92. } catch (\Exception $e) {
  93. return "物品 (ID: {$item->target_id})";
  94. }
  95. case \App\Module\Game\Enums\CONSUME_TYPE::CURRENCY->value:
  96. try {
  97. $currency = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id);
  98. return $currency ? $currency->name : "货币 (ID: {$item->target_id})";
  99. } catch (\Exception $e) {
  100. return "货币 (ID: {$item->target_id})";
  101. }
  102. case \App\Module\Game\Enums\CONSUME_TYPE::FUND_CONFIG->value:
  103. try {
  104. $fund = \App\Module\Fund\Models\FundConfigModel::find($item->target_id);
  105. return $fund ? $fund->name : "账户种类 (ID: {$item->target_id})";
  106. } catch (\Exception $e) {
  107. return "账户种类 (ID: {$item->target_id})";
  108. }
  109. default:
  110. return "未知消耗类型 (ID: {$item->target_id})";
  111. }
  112. }
  113. }