| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?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 int $logic_type 逻辑类型(1:全部满足, 2:任一满足)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class GameConditionGroup extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'game_condition_groups';
- // attrlist start
- protected $fillable = [
- 'id',
- 'name',
- 'code',
- 'description',
- 'logic_type',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'logic_type' => '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 string
- */
- public function formatConditionDetails(): string
- {
- if ($this->conditionItems->isEmpty()) {
- return '<span class="text-muted">暂无条件项</span>';
- }
- $details = [];
- foreach ($this->conditionItems as $item) {
- $detail = $this->formatSingleConditionItem($item);
- $details[] = $detail;
- }
- $logicType = $this->logic_type == self::LOGIC_TYPE_ALL ? '全部满足' : '任一满足';
- return '<div class="condition-details">' .
- '<span class="badge badge-secondary">' . $logicType . '</span><br>' .
- implode('<br>', $details) .
- '</div>';
- }
- /**
- * 格式化单个条件项
- *
- * @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(
- '<span class="badge badge-info">%s</span> %s %s %d',
- $conditionTypeName,
- $targetName,
- $operator,
- $value
- );
- }
- }
|