GameConditionGroup.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 int $logic_type 逻辑类型(1:全部满足, 2:任一满足)
  14. * @property \Carbon\Carbon $created_at 创建时间
  15. * @property \Carbon\Carbon $updated_at 更新时间
  16. * field end
  17. */
  18. class GameConditionGroup extends ModelCore
  19. {
  20. /**
  21. * 与模型关联的表名
  22. *
  23. * @var string
  24. */
  25. protected $table = 'game_condition_groups';
  26. // attrlist start
  27. protected $fillable = [
  28. 'id',
  29. 'name',
  30. 'code',
  31. 'description',
  32. 'logic_type',
  33. ];
  34. // attrlist end
  35. /**
  36. * 应该被转换为原生类型的属性
  37. *
  38. * @var array
  39. */
  40. protected $casts = [
  41. 'logic_type' => 'integer',
  42. ];
  43. /**
  44. * 逻辑类型:全部满足
  45. */
  46. const LOGIC_TYPE_ALL = 1;
  47. /**
  48. * 逻辑类型:任一满足
  49. */
  50. const LOGIC_TYPE_ANY = 2;
  51. /**
  52. * 获取逻辑类型列表
  53. *
  54. * @return array
  55. */
  56. public static function getLogicTypes(): array
  57. {
  58. return [
  59. self::LOGIC_TYPE_ALL => '全部满足',
  60. self::LOGIC_TYPE_ANY => '任一满足',
  61. ];
  62. }
  63. /**
  64. * 获取条件组中的所有条件项
  65. *
  66. * @return HasMany
  67. */
  68. public function conditionItems(): HasMany
  69. {
  70. return $this->hasMany(GameConditionItem::class, 'group_id', 'id');
  71. }
  72. /**
  73. * 获取关联的标签(多态关联)
  74. *
  75. * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
  76. */
  77. public function tags()
  78. {
  79. return $this->morphToMany(GameTag::class, 'taggable', 'game_tag_relations', 'taggable_id', 'tag_id');
  80. }
  81. /**
  82. * 格式化条件详情用于显示
  83. *
  84. * @return string
  85. */
  86. public function formatConditionDetails(): string
  87. {
  88. if ($this->conditionItems->isEmpty()) {
  89. return '<span class="text-muted">暂无条件项</span>';
  90. }
  91. $details = [];
  92. foreach ($this->conditionItems as $item) {
  93. $detail = $this->formatSingleConditionItem($item);
  94. $details[] = $detail;
  95. }
  96. $logicType = $this->logic_type == self::LOGIC_TYPE_ALL ? '全部满足' : '任一满足';
  97. return '<div class="condition-details">' .
  98. '<span class="badge badge-secondary">' . $logicType . '</span><br>' .
  99. implode('<br>', $details) .
  100. '</div>';
  101. }
  102. /**
  103. * 格式化标签用于显示
  104. *
  105. * @return string
  106. */
  107. public function formatTags(): string
  108. {
  109. if ($this->tags->isEmpty()) {
  110. return '<span class="text-muted">无标签</span>';
  111. }
  112. $tagHtml = [];
  113. foreach ($this->tags as $tag) {
  114. $tagHtml[] = $tag->formatTag();
  115. }
  116. return implode(' ', $tagHtml);
  117. }
  118. /**
  119. * 格式化单个条件项
  120. *
  121. * @param GameConditionItem $item
  122. * @return string
  123. */
  124. private function formatSingleConditionItem(GameConditionItem $item): string
  125. {
  126. $conditionTypeName = \App\Module\Game\Enums\CONDITION_TYPE::getName($item->condition_type);
  127. $targetName = $item->getTargetName();
  128. $operator = \App\Module\Game\Enums\CONDITION_OPERATOR::getSymbol($item->operator);
  129. $value = $item->value;
  130. return sprintf(
  131. '<span class="badge badge-info">%s</span> %s %s %d',
  132. $conditionTypeName,
  133. $targetName,
  134. $operator,
  135. $value
  136. );
  137. }
  138. }