'integer', ]; /** * 逻辑类型:全部满足 */ const LOGIC_TYPE_ALL = 1; /** * 逻辑类型:任一满足 */ const LOGIC_TYPE_ANY = 2; /** * 获取逻辑类型列表 * * @return array */ public static function getLogicTypes(): array { return [ self::LOGIC_TYPE_ALL => '全部满足', self::LOGIC_TYPE_ANY => '任一满足', ]; } /** * 获取条件组中的所有条件项 * * @return HasMany */ public function conditionItems(): HasMany { return $this->hasMany(GameConditionItem::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 formatConditionDetails(): string { if ($this->conditionItems->isEmpty()) { return '暂无条件项'; } $details = []; foreach ($this->conditionItems as $item) { $detail = $this->formatSingleConditionItem($item); $details[] = $detail; } $logicType = $this->logic_type == self::LOGIC_TYPE_ALL ? '全部满足' : '任一满足'; return '
' . '' . $logicType . '
' . implode('
', $details) . '
'; } /** * 格式化标签用于显示 * * @return string */ public function formatTags(): string { if ($this->tags->isEmpty()) { return '无标签'; } $tagHtml = []; foreach ($this->tags as $tag) { $tagHtml[] = $tag->formatTag(); } return implode(' ', $tagHtml); } /** * 格式化单个条件项 * * @param GameConditionItem $item * @return string */ private function formatSingleConditionItem(GameConditionItem $item): string { $conditionTypeName = \App\Module\Game\Enums\CONDITION_TYPE::getName($item->condition_type); $targetName = $item->getTargetName(); $operator = \App\Module\Game\Enums\CONDITION_OPERATOR::getSymbol($item->operator); $value = $item->value; return sprintf( '%s %s %s %d', $conditionTypeName, $targetName, $operator, $value ); } }