GameConditionGroup.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 string
  76. */
  77. public function formatConditionDetails(): string
  78. {
  79. if ($this->conditionItems->isEmpty()) {
  80. return '<span class="text-muted">暂无条件项</span>';
  81. }
  82. $details = [];
  83. foreach ($this->conditionItems as $item) {
  84. $detail = $this->formatSingleConditionItem($item);
  85. $details[] = $detail;
  86. }
  87. $logicType = $this->logic_type == self::LOGIC_TYPE_ALL ? '全部满足' : '任一满足';
  88. return '<div class="condition-details">' .
  89. '<span class="badge badge-secondary">' . $logicType . '</span><br>' .
  90. implode('<br>', $details) .
  91. '</div>';
  92. }
  93. /**
  94. * 格式化单个条件项
  95. *
  96. * @param GameConditionItem $item
  97. * @return string
  98. */
  99. private function formatSingleConditionItem(GameConditionItem $item): string
  100. {
  101. $conditionTypeName = \App\Module\Game\Enums\CONDITION_TYPE::getName($item->condition_type);
  102. $targetName = $item->getTargetName();
  103. $operator = \App\Module\Game\Enums\CONDITION_OPERATOR::getSymbol($item->operator);
  104. $value = $item->value;
  105. return sprintf(
  106. '<span class="badge badge-info">%s</span> %s %s %d',
  107. $conditionTypeName,
  108. $targetName,
  109. $operator,
  110. $value
  111. );
  112. }
  113. }