ConditionGroupService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Module\Game\Services;
  3. use App\Module\Farm\Models\FarmLandType;
  4. use App\Module\Fund\Models\FundConfigModel;
  5. use App\Module\Fund\Models\FundCurrencyModel;
  6. use App\Module\Game\Enums\CONDITION_OPERATOR;
  7. use App\Module\Game\Enums\CONDITION_TYPE;
  8. use App\Module\Game\Models\GameConditionGroup;
  9. use App\Module\GameItems\Models\Item;
  10. use App\Module\Pet\Models\PetConfig;
  11. use Illuminate\Support\Facades\Log;
  12. /**
  13. * 条件组服务类
  14. *
  15. * 提供条件组相关的服务,包括获取条件组详情、获取条件组条件等功能
  16. */
  17. class ConditionGroupService
  18. {
  19. /**
  20. * 获取条件组详情
  21. *
  22. * @param int|string $conditionGroupCode 条件组ID或编码
  23. * @return GameConditionGroup|null 条件组详情
  24. */
  25. public static function getConditionGroup($conditionGroupCode): ?GameConditionGroup
  26. {
  27. try {
  28. // 获取条件组
  29. $conditionGroup = is_numeric($conditionGroupCode)
  30. ? GameConditionGroup::with('conditionItems')->find($conditionGroupCode)
  31. : GameConditionGroup::with('conditionItems')->where('code', $conditionGroupCode)->first();
  32. return $conditionGroup;
  33. } catch (\Exception $e) {
  34. Log::error('获取条件组详情失败', [
  35. 'condition_group' => $conditionGroupCode,
  36. 'error' => $e->getMessage()
  37. ]);
  38. return null;
  39. }
  40. }
  41. /**
  42. * 获取条件组条件
  43. *
  44. * @param int|string $conditionGroupCode 条件组ID或编码
  45. * @return array 条件组条件
  46. */
  47. public static function getConditionItems($conditionGroupCode): array
  48. {
  49. try {
  50. // 获取条件组
  51. $conditionGroup = self::getConditionGroup($conditionGroupCode);
  52. if (!$conditionGroup || $conditionGroup->conditionItems->isEmpty()) {
  53. return [];
  54. }
  55. $conditions = [];
  56. // 处理每个条件项
  57. foreach ($conditionGroup->conditionItems as $item) {
  58. $conditionItem = [
  59. 'id' => $item->id,
  60. 'type' => $item->condition_type,
  61. 'target_id' => $item->target_id,
  62. 'operator' => $item->operator,
  63. 'value' => $item->value,
  64. 'group_id' => $conditionGroup->id,
  65. 'group_code' => $conditionGroup->code,
  66. 'group_name' => $conditionGroup->name,
  67. 'logic_type' => $conditionGroup->logic_type,
  68. 'operator_name' => CONDITION_OPERATOR::getName($item->operator)
  69. ];
  70. // 根据条件类型获取目标名称
  71. if ($item->condition_type == CONDITION_TYPE::LAND_LEVEL->value) {
  72. $landType = FarmLandType::find($item->target_id);
  73. $conditionItem['target_name'] = $landType ? $landType->name : "土地类型 {$item->target_id}";
  74. $conditionItem['condition_name'] = "土地等级";
  75. } elseif ($item->condition_type == CONDITION_TYPE::HOUSE_LEVEL->value) {
  76. $conditionItem['target_name'] = "房屋";
  77. $conditionItem['condition_name'] = "房屋等级";
  78. } elseif ($item->condition_type == CONDITION_TYPE::PET_LEVEL->value) {
  79. $pet = PetConfig::find($item->target_id);
  80. $conditionItem['target_name'] = $pet ? $pet->name : "宠物 {$item->target_id}";
  81. $conditionItem['condition_name'] = "宠物等级";
  82. } elseif ($item->condition_type == CONDITION_TYPE::ITEM_COUNT->value) {
  83. $itemInfo = Item::find($item->target_id);
  84. $conditionItem['target_name'] = $itemInfo ? $itemInfo->name : "物品 {$item->target_id}";
  85. $conditionItem['condition_name'] = "物品持有数";
  86. } elseif ($item->condition_type == CONDITION_TYPE::CURRENCY_COUNT->value) {
  87. $currency = FundCurrencyModel::find($item->target_id);
  88. $conditionItem['target_name'] = $currency ? $currency->name : "货币 {$item->target_id}";
  89. $conditionItem['condition_name'] = "货币持有数";
  90. } elseif ($item->condition_type == CONDITION_TYPE::FUND_COUNT->value) {
  91. $fund = FundConfigModel::find($item->target_id);
  92. $conditionItem['target_name'] = $fund ? $fund->name : "代币账户 {$item->target_id}";
  93. $conditionItem['condition_name'] = "代币持有数";
  94. }
  95. $conditions[] = $conditionItem;
  96. }
  97. return $conditions;
  98. } catch (\Exception $e) {
  99. Log::error('获取条件组条件失败', [
  100. 'condition_group' => $conditionGroupCode,
  101. 'error' => $e->getMessage()
  102. ]);
  103. return [];
  104. }
  105. }
  106. }