| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace App\Module\Game\Services;
- use App\Module\Farm\Models\FarmLandType;
- use App\Module\Fund\Models\FundConfigModel;
- use App\Module\Fund\Models\FundCurrencyModel;
- use App\Module\Game\Enums\CONDITION_OPERATOR;
- use App\Module\Game\Enums\CONDITION_TYPE;
- use App\Module\Game\Models\GameConditionGroup;
- use App\Module\GameItems\Models\Item;
- use App\Module\Pet\Models\PetConfig;
- use Illuminate\Support\Facades\Log;
- /**
- * 条件组服务类
- *
- * 提供条件组相关的服务,包括获取条件组详情、获取条件组条件等功能
- */
- class ConditionGroupService
- {
- /**
- * 获取条件组详情
- *
- * @param int|string $conditionGroupCode 条件组ID或编码
- * @return GameConditionGroup|null 条件组详情
- */
- public static function getConditionGroup($conditionGroupCode): ?GameConditionGroup
- {
- try {
- // 获取条件组
- $conditionGroup = is_numeric($conditionGroupCode)
- ? GameConditionGroup::with('conditionItems')->find($conditionGroupCode)
- : GameConditionGroup::with('conditionItems')->where('code', $conditionGroupCode)->first();
-
- return $conditionGroup;
- } catch (\Exception $e) {
- Log::error('获取条件组详情失败', [
- 'condition_group' => $conditionGroupCode,
- 'error' => $e->getMessage()
- ]);
-
- return null;
- }
- }
-
- /**
- * 获取条件组条件
- *
- * @param int|string $conditionGroupCode 条件组ID或编码
- * @return array 条件组条件
- */
- public static function getConditionItems($conditionGroupCode): array
- {
- try {
- // 获取条件组
- $conditionGroup = self::getConditionGroup($conditionGroupCode);
- if (!$conditionGroup || $conditionGroup->conditionItems->isEmpty()) {
- return [];
- }
-
- $conditions = [];
-
- // 处理每个条件项
- foreach ($conditionGroup->conditionItems as $item) {
- $conditionItem = [
- 'id' => $item->id,
- 'type' => $item->condition_type,
- 'target_id' => $item->target_id,
- 'operator' => $item->operator,
- 'value' => $item->value,
- 'group_id' => $conditionGroup->id,
- 'group_code' => $conditionGroup->code,
- 'group_name' => $conditionGroup->name,
- 'logic_type' => $conditionGroup->logic_type,
- 'operator_name' => CONDITION_OPERATOR::getName($item->operator)
- ];
-
- // 根据条件类型获取目标名称
- if ($item->condition_type == CONDITION_TYPE::LAND_LEVEL->value) {
- $landType = FarmLandType::find($item->target_id);
- $conditionItem['target_name'] = $landType ? $landType->name : "土地类型 {$item->target_id}";
- $conditionItem['condition_name'] = "土地等级";
- } elseif ($item->condition_type == CONDITION_TYPE::HOUSE_LEVEL->value) {
- $conditionItem['target_name'] = "房屋";
- $conditionItem['condition_name'] = "房屋等级";
- } elseif ($item->condition_type == CONDITION_TYPE::PET_LEVEL->value) {
- $pet = PetConfig::find($item->target_id);
- $conditionItem['target_name'] = $pet ? $pet->name : "宠物 {$item->target_id}";
- $conditionItem['condition_name'] = "宠物等级";
- } elseif ($item->condition_type == CONDITION_TYPE::ITEM_COUNT->value) {
- $itemInfo = Item::find($item->target_id);
- $conditionItem['target_name'] = $itemInfo ? $itemInfo->name : "物品 {$item->target_id}";
- $conditionItem['condition_name'] = "物品持有数";
- } elseif ($item->condition_type == CONDITION_TYPE::CURRENCY_COUNT->value) {
- $currency = FundCurrencyModel::find($item->target_id);
- $conditionItem['target_name'] = $currency ? $currency->name : "货币 {$item->target_id}";
- $conditionItem['condition_name'] = "货币持有数";
- } elseif ($item->condition_type == CONDITION_TYPE::FUND_COUNT->value) {
- $fund = FundConfigModel::find($item->target_id);
- $conditionItem['target_name'] = $fund ? $fund->name : "代币账户 {$item->target_id}";
- $conditionItem['condition_name'] = "代币持有数";
- }
-
- $conditions[] = $conditionItem;
- }
-
- return $conditions;
- } catch (\Exception $e) {
- Log::error('获取条件组条件失败', [
- 'condition_group' => $conditionGroupCode,
- 'error' => $e->getMessage()
- ]);
-
- return [];
- }
- }
- }
|