PaymentChannelLinkService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Services;
  3. use App\Models\RechargeChannel;
  4. use App\Models\RechargeChannelGroup;
  5. use Illuminate\Database\Eloquent\Collection as EloquentCollection;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Support\Collection;
  8. use RuntimeException;
  9. class PaymentChannelLinkService
  10. {
  11. public function validate(int $channelId, array $groupIds, int $expectedDataType): array
  12. {
  13. $channel = RechargeChannel::query()->find($channelId);
  14. if (!$channel) throw new RuntimeException('充值通道不存在');
  15. if ((int)$channel->data_type !== $expectedDataType) {
  16. throw new RuntimeException($expectedDataType === 2 ? '请选择提现通道' : '请选择充值通道');
  17. }
  18. $groupIds = array_values(array_unique(array_filter(array_map('intval', $groupIds))));
  19. if (!$groupIds) throw new RuntimeException('至少选择一个通道组合');
  20. $groups = RechargeChannelGroup::query()->whereIn('id', $groupIds)->get();
  21. if ($groups->count() !== count($groupIds)) throw new RuntimeException('部分通道组合不存在');
  22. $field = $this->groupField($expectedDataType);
  23. foreach ($groups as $group) {
  24. if (!in_array((string)$channel->type, (array)$group->{$field}, true)) {
  25. throw new RuntimeException("通道组合“{$group->name}”没有包含通道“{$channel->name}”");
  26. }
  27. }
  28. return [$channel, $groupIds];
  29. }
  30. public function formatMany($models): Collection
  31. {
  32. $models = $models instanceof Collection ? $models : collect($models);
  33. if ($models instanceof EloquentCollection) $models->loadMissing('rechargeChannel');
  34. $groupIds = $models->flatMap(fn ($model) => (array)($model->recharge_channel_group_ids ?? []))
  35. ->map(fn ($id) => (int)$id)->filter()->unique()->values();
  36. $groups = RechargeChannelGroup::query()->whereIn('id', $groupIds)->get()->keyBy('id');
  37. return $models->map(fn (Model $model) => $this->formatOne($model, $groups));
  38. }
  39. public function formatOne(Model $model, ?Collection $groups = null): array
  40. {
  41. $model->loadMissing('rechargeChannel');
  42. $groups ??= RechargeChannelGroup::query()
  43. ->whereIn('id', (array)($model->recharge_channel_group_ids ?? []))->get()->keyBy('id');
  44. $channel = $model->rechargeChannel;
  45. $assignedIds = array_values(array_unique(array_filter(array_map(
  46. 'intval',
  47. (array)($model->recharge_channel_group_ids ?? [])
  48. ))));
  49. $effectiveIds = [];
  50. $unavailableIds = [];
  51. $groupRows = [];
  52. foreach ($assignedIds as $groupId) {
  53. $group = $groups->get($groupId);
  54. $effective = false;
  55. if ($group && $channel) {
  56. $effective = in_array(
  57. (string)$channel->type,
  58. (array)$group->{$this->groupField((int)$channel->data_type)},
  59. true
  60. );
  61. }
  62. if ($effective) {
  63. $effectiveIds[] = $groupId;
  64. } else {
  65. $unavailableIds[] = $groupId;
  66. }
  67. $groupRows[] = [
  68. 'id' => $groupId,
  69. 'name' => (string)($group->name ?? ''),
  70. 'effective' => $effective ? 1 : 0,
  71. ];
  72. }
  73. $data = $model->toArray();
  74. $data['recharge_channel_group_ids'] = $assignedIds;
  75. $data['effective_group_ids'] = $effectiveIds;
  76. $data['unavailable_group_ids'] = $unavailableIds;
  77. $data['groups'] = $groupRows;
  78. $data['recharge_channel'] = $channel ? [
  79. 'id' => (int)$channel->id,
  80. 'data_type' => (int)$channel->data_type,
  81. 'name' => (string)$channel->name,
  82. 'key' => (string)$channel->key,
  83. 'type' => (string)$channel->type,
  84. 'rate' => number_format((float)$channel->rate, 4, '.', ''),
  85. 'fee_rate' => bcmul((string)$channel->rate, '100', 4),
  86. 'min_amount' => $channel->min === null ? null : number_format((float)$channel->min, 2, '.', ''),
  87. 'max_amount' => $channel->max === null ? null : number_format((float)$channel->max, 2, '.', ''),
  88. 'fixed_amounts' => $channel->fixed ?: [],
  89. 'sort' => (int)$channel->sort,
  90. 'status' => (int)$channel->status,
  91. ] : null;
  92. $data['fee_rate'] = $channel ? bcmul((string)$channel->rate, '100', 4) : null;
  93. $data['min_amount'] = $channel?->min;
  94. $data['max_amount'] = $channel?->max;
  95. $data['sort'] = $channel ? (int)$channel->sort : null;
  96. $data['config_status'] = (int)$model->status;
  97. $data['channel_status'] = $channel ? (int)$channel->status : 0;
  98. $data['runtime_channel_available'] = $channel
  99. && (int)$channel->status === 1 && count($effectiveIds) > 0 ? 1 : 0;
  100. $data['effective_status'] = (int)$model->status === 1
  101. && $data['runtime_channel_available'] === 1 ? 1 : 0;
  102. if (array_key_exists('amount', $data)) {
  103. $data['actual_amount'] = $channel
  104. ? bcsub((string)$model->amount, bcmul((string)$model->amount, (string)$channel->rate, 6), 2)
  105. : null;
  106. }
  107. $data['channel_fields_readonly'] = [
  108. 'recharge_channel.name', 'recharge_channel.key', 'recharge_channel.type',
  109. 'fee_rate', 'min_amount', 'max_amount', 'recharge_channel.fixed_amounts',
  110. 'sort', 'channel_status',
  111. ];
  112. unset($data['level_scope']);
  113. return $data;
  114. }
  115. private function groupField(int $dataType): string
  116. {
  117. return match ($dataType) {
  118. 2 => 'withdraw_type',
  119. 3 => 'activity_type',
  120. default => 'recharge_type',
  121. };
  122. }
  123. }