| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace App\Module\Game\Models;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use UCore\ModelCore;
- /**
- * 消耗组
- *
- * field start
- * @property int $id 主键
- * @property string $name 消耗组名称
- * @property string $code 消耗组编码(唯一)
- * @property string $description 消耗组描述
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- *
- * @property-read GameConsumeItem[] $consumeItems 消耗组中的所有消耗项
- */
- class GameConsumeGroup extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'game_consume_groups';
- // attrlist start
- protected $fillable = [
- 'id',
- 'name',
- 'code',
- 'description',
- ];
- // attrlist end
- /**
- * 获取消耗组中的所有消耗项
- *
- * @return HasMany
- */
- public function consumeItems(): HasMany
- {
- return $this->hasMany(GameConsumeItem::class, 'group_id', 'id');
- }
- /**
- * 获取关联的标签(多态关联)
- *
- * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
- */
- public function tags()
- {
- return $this->morphToMany(GameTag::class, 'taggable', 'game_tag_relations', 'taggable_id', 'tag_id');
- }
- /**
- * 格式化消耗详情用于显示
- *
- * @return string
- */
- public function formatConsumeDetails(): string
- {
- if ($this->consumeItems->isEmpty()) {
- return '<span class="text-muted">暂无消耗项</span>';
- }
- $details = [];
- foreach ($this->consumeItems as $item) {
- $detail = $this->formatSingleConsumeItem($item);
- $details[] = $detail;
- }
- return '<div class="consume-details">' . implode('<br>', $details) . '</div>';
- }
- /**
- * 格式化标签用于显示
- *
- * @return string
- */
- public function formatTags(): string
- {
- if ($this->tags->isEmpty()) {
- return '<span class="text-muted">无标签</span>';
- }
- $tagHtml = [];
- foreach ($this->tags as $tag) {
- $tagHtml[] = $tag->formatTag();
- }
- return implode(' ', $tagHtml);
- }
- /**
- * 格式化单个消耗项
- *
- * @param GameConsumeItem $item
- * @return string
- */
- private function formatSingleConsumeItem(GameConsumeItem $item): string
- {
- $consumeTypeName = \App\Module\Game\Enums\CONSUME_TYPE::getName($item->consume_type);
- $targetName = $this->getTargetName($item);
- $quantity = $item->quantity;
- return sprintf(
- '<span class="badge badge-warning">%s</span> %s × %d',
- $consumeTypeName,
- $targetName,
- $quantity
- );
- }
- /**
- * 获取目标名称
- *
- * @param GameConsumeItem $item
- * @return string
- */
- private function getTargetName(GameConsumeItem $item): string
- {
- switch ($item->consume_type) {
- case \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value:
- try {
- $itemModel = \App\Module\GameItems\Models\Item::find($item->target_id);
- return $itemModel ? $itemModel->name : "物品 (ID: {$item->target_id})";
- } catch (\Exception $e) {
- return "物品 (ID: {$item->target_id})";
- }
- case \App\Module\Game\Enums\CONSUME_TYPE::CURRENCY->value:
- try {
- $currency = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id);
- return $currency ? $currency->name : "货币 (ID: {$item->target_id})";
- } catch (\Exception $e) {
- return "货币 (ID: {$item->target_id})";
- }
- case \App\Module\Game\Enums\CONSUME_TYPE::FUND_CONFIG->value:
- try {
- $fund = \App\Module\Fund\Models\FundConfigModel::find($item->target_id);
- return $fund ? $fund->name : "账户种类 (ID: {$item->target_id})";
- } catch (\Exception $e) {
- return "账户种类 (ID: {$item->target_id})";
- }
- case \App\Module\Game\Enums\CONSUME_TYPE::FUND_CONFIGS->value:
- // 直接使用 GameConsumeItem 模型的 getTargetName 方法
- return $item->getTargetName();
- default:
- return "未知消耗类型 (ID: {$item->target_id})";
- }
- }
- }
|