RechargeChannelConfigurationService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. namespace App\Services\Payment;
  3. use App\Models\RechargeChannel;
  4. use App\Models\OperationAudit;
  5. use App\Models\RechargeChannelGroup;
  6. use App\Services\OperationAuditService;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Schema;
  9. use RuntimeException;
  10. class RechargeChannelConfigurationService
  11. {
  12. public function saveGroup(array $params): array
  13. {
  14. return DB::transaction(function () use ($params) {
  15. $id = (int)($params['id'] ?? 0);
  16. unset($params['id']);
  17. $validTypes = [];
  18. foreach (['recharge_type' => 1, 'withdraw_type' => 2] as $field => $direction) {
  19. if (!array_key_exists($field, $params)) continue;
  20. $params[$field] = $this->normalizeTypes((array)($params[$field] ?? []));
  21. // Match deletion's lock order: channel rows, then the group being edited.
  22. $validTypes[$field] = $params[$field] === [] ? [] : RechargeChannel::query()
  23. ->where('data_type', $direction)->whereIn('type', $params[$field])
  24. ->orderBy('id')->lockForUpdate()->get(['type'])->pluck('type')->map(fn ($type) => (string)$type)->all();
  25. }
  26. $group = $id ? RechargeChannelGroup::query()->lockForUpdate()->findOrFail($id) : new RechargeChannelGroup();
  27. $before = $group->exists ? $group->toArray() : [];
  28. $removed = [];
  29. foreach ($validTypes as $field => $valid) {
  30. $invalid = array_values(array_diff($params[$field], $valid));
  31. $previous = $group->exists ? $this->normalizeTypes((array)$group->{$field}) : [];
  32. $newInvalid = array_values(array_diff($invalid, $previous));
  33. if ($newInvalid !== []) {
  34. throw new RuntimeException($field . ' 包含不存在或已删除的通道类型:'
  35. . implode(',', $newInvalid) . ';请提交选项的 type,不是通道 ID 或公司代码');
  36. }
  37. if ($invalid !== []) $removed[$field] = $invalid;
  38. $params[$field] = array_values(array_intersect($params[$field], $valid));
  39. }
  40. $group->fill($params);
  41. $group->save();
  42. if ($removed !== []) {
  43. OperationAuditService::record('recharge_channel_group', (int)$group->id, 'update', $before,
  44. ['group' => $group->toArray(), 'removed_types' => $removed]);
  45. }
  46. return $removed;
  47. }, 3);
  48. }
  49. public function deleteGroup(int $id): void
  50. {
  51. if ($id === 1) throw new RuntimeException('默认通道组合不能删除');
  52. DB::transaction(function () use ($id) {
  53. $group = RechargeChannelGroup::withTrashed()->lockForUpdate()->findOrFail($id);
  54. if ($group->trashed()) return;
  55. if (Schema::hasTable('users') && Schema::hasColumn('users', 'recharge_channel_group_id')
  56. && DB::table('users')->where('recharge_channel_group_id', $id)->lockForUpdate()->first(['id'])) {
  57. throw new RuntimeException('该层级仍有会员使用,请先将会员调整到其他层级');
  58. }
  59. foreach (['payment_gateways', 'payment_collection_channels', 'payment_direct_recharges'] as $table) {
  60. if (!Schema::hasTable($table) || !Schema::hasColumn($table, 'recharge_channel_group_ids')) continue;
  61. DB::table($table)->whereNull('deleted_at')->whereNotNull('recharge_channel_group_ids')
  62. ->select(['id', 'recharge_channel_group_ids'])
  63. ->orderBy('id')->lockForUpdate()->chunkById(200, function ($rows) use ($id) {
  64. foreach ($rows as $row) {
  65. // Match runtime's integer normalization, including legacy string IDs such as "02".
  66. $ids = array_map('intval', (array)json_decode($row->recharge_channel_group_ids, true));
  67. if (in_array($id, $ids, true)) throw new RuntimeException('该层级仍被支付配置引用,请先解除关联');
  68. }
  69. }, 'id', 'id');
  70. }
  71. $before = $group->toArray();
  72. $group->delete();
  73. OperationAuditService::record('recharge_channel_group', $id, 'delete', $before, $group);
  74. }, 3);
  75. }
  76. private function normalizeTypes(array $values): array
  77. {
  78. $types = [];
  79. foreach ($values as $value) {
  80. if ($value !== null && !is_string($value) && !is_int($value)) {
  81. throw new RuntimeException('通道类型应提交 type 字符串数组,不能提交选项对象');
  82. }
  83. $type = trim((string)$value);
  84. if ($type !== '') $types[] = $type;
  85. }
  86. return array_values(array_unique($types));
  87. }
  88. public function options(int $dataType, ?int $id = null): array
  89. {
  90. $options = PaymentProviderCatalog::channelFormOptions($dataType, PaymentProviderService::statuses());
  91. $reason = '';
  92. if ($id) {
  93. $channel = RechargeChannel::query()->findOrFail($id);
  94. if ((int)$channel->data_type !== $dataType) throw new RuntimeException('通道方向与编辑记录不一致');
  95. $reason = $this->identityLockReason($channel);
  96. }
  97. return $options + ['identity_editable' => $reason === '', 'identity_edit_reason' => $reason];
  98. }
  99. public function save(array $params): RechargeChannel
  100. {
  101. if (array_key_exists('logo', $params)) $params['logo'] = RechargeChannelLogoService::normalize($params['logo']);
  102. return DB::transaction(function () use ($params) {
  103. $id = (int)($params['id'] ?? 0);
  104. unset($params['id']);
  105. $channel = $id ? RechargeChannel::query()->lockForUpdate()->findOrFail($id) : new RechargeChannel();
  106. if ($channel->exists && (int)$channel->data_type !== (int)$params['data_type']) {
  107. throw new RuntimeException('不能修改通道所属的充值或提现方向');
  108. }
  109. $isNew = !$channel->exists;
  110. $before = $isNew ? [] : $channel->toArray();
  111. $params = PaymentProviderCatalog::normalizeChannelSelection($params);
  112. $params = $this->amountConfiguration($params, $channel);
  113. $identityChanged = !$channel->exists || (int)$channel->from !== $params['from'] || (string)$channel->type !== (string)$params['type'];
  114. $identity = PaymentProviderCatalog::channelIdentity($params);
  115. if ($identityChanged && $identity) PaymentProviderService::assertEnabled($identity['provider_code']);
  116. if ($channel->exists && ((int)$channel->from !== $params['from'] || (string)$channel->type !== (string)$params['type'])) {
  117. $reason = $this->identityLockReason($channel, true);
  118. if ($reason !== '') throw new RuntimeException($reason);
  119. }
  120. foreach (['name', 'key'] as $field) {
  121. if (array_key_exists($field, $params)) $params[$field] = trim((string)$params[$field]);
  122. if ((!$channel->exists || array_key_exists($field, $params)) && ($params[$field] ?? '') === '') {
  123. throw new RuntimeException($field === 'name' ? '请填写通道名称' : '请填写通道标识Key');
  124. }
  125. }
  126. foreach (['status', 'sort'] as $field) {
  127. if (array_key_exists($field, $params) && $params[$field] === null) unset($params[$field]);
  128. }
  129. if (!$channel->exists) $params += ['status' => 1, 'sort' => 0];
  130. if ($identityChanged && RechargeChannelLogoService::supportsDirection((int)$params['data_type']) && !array_key_exists('logo', $params)) {
  131. $dataType = (int)$params['data_type'];
  132. $type = (string)$params['type'];
  133. $logos = $dataType === 1 ? RechargeChannelLogoService::configured([$type]) : [];
  134. $peer = RechargeChannel::where('data_type', $dataType)->where('type', $type)->orderBy('sort')->orderBy('id')->first();
  135. $params['logo'] = array_key_exists($type, $logos) ? $logos[$type] : ($peer ? $peer->getRawOriginal('logo') : null);
  136. }
  137. $channel->fill($params);
  138. $channel->save();
  139. if (RechargeChannelLogoService::supportsDirection((int)$channel->data_type) && array_key_exists('logo', $params)) {
  140. RechargeChannelLogoService::store((string)$channel->type, $params['logo'], (int)$channel->data_type);
  141. }
  142. $after = $channel->fresh()->toArray();
  143. if ($isNew || self::diff($before, $after) !== []) {
  144. OperationAuditService::record('recharge_channel', (int)$channel->id, $isNew ? 'create' : 'update', $before, $after);
  145. }
  146. return $channel;
  147. }, 3);
  148. }
  149. private function amountConfiguration(array $params, RechargeChannel $channel): array
  150. {
  151. $fixedMode = (int)$params['data_type'] === 1 && $params['type'] === 'zfbge';
  152. if ($fixedMode || array_key_exists('fixed', $params)) {
  153. $value = array_key_exists('fixed', $params) ? $params['fixed'] : ($channel->type === 'zfbge' ? $channel->getRawOriginal('fixed') : null);
  154. if (is_string($value)) $values = explode(',', str_replace(',', ',', $value));
  155. elseif (is_array($value) && array_values($value) === $value) $values = $value;
  156. elseif ($value === null) $values = [];
  157. else throw \Illuminate\Validation\ValidationException::withMessages(['fixed' => '固定金额请提交金额数组或逗号分隔字符串']);
  158. $amounts = [];
  159. foreach ($values as $amount) {
  160. if (!is_string($amount) && !is_int($amount) && !is_float($amount)) throw \Illuminate\Validation\ValidationException::withMessages(['fixed' => '固定金额格式错误']);
  161. $amount = trim((string)$amount);
  162. if ($amount === '') continue;
  163. if (!preg_match('/^\d{1,12}(\.\d{1,2}0*)?$/D', $amount) || bccomp($amount, '0', 2) <= 0) {
  164. throw \Illuminate\Validation\ValidationException::withMessages(['fixed' => '固定金额必须大于0,最多两位小数']);
  165. }
  166. $amount = bcadd($amount, '0', 2);
  167. if (str_ends_with($amount, '.00')) $amount = substr($amount, 0, -3);
  168. if (!in_array($amount, $amounts, true)) $amounts[] = $amount;
  169. }
  170. if ($fixedMode && !$amounts) throw \Illuminate\Validation\ValidationException::withMessages(['fixed' => '请设置固定充值金额']);
  171. $params['fixed'] = $amounts ? implode(',', $amounts) : null;
  172. } elseif ($channel->exists && $channel->type !== $params['type']) {
  173. $params['fixed'] = null;
  174. }
  175. if ($fixedMode) {
  176. $params['min'] = $params['max'] = null;
  177. } else {
  178. $range = ['min' => array_key_exists('min', $params) ? $params['min'] : $channel->min,
  179. 'max' => array_key_exists('max', $params) ? $params['max'] : $channel->max];
  180. $rules = ['min' => ['nullable', 'numeric', 'min:0'], 'max' => ['nullable', 'numeric', 'min:0']];
  181. if ($range['min'] !== null && $range['max'] !== null) $rules['max'][] = 'gte:min';
  182. \Illuminate\Support\Facades\Validator::make($range, $rules)->validate();
  183. }
  184. return $params;
  185. }
  186. public function delete(int $id): void
  187. {
  188. DB::transaction(function () use ($id) {
  189. $channel = RechargeChannel::withTrashed()->lockForUpdate()->findOrFail($id);
  190. if (!in_array((int)$channel->data_type, [1, 2], true)) throw new RuntimeException('只支持删除充值或提现通道');
  191. if ($channel->trashed()) return;
  192. if ($this->identityLockReason($channel, true) !== '') {
  193. throw new RuntimeException('该通道已被支付配置引用,请先解除关联再删除');
  194. }
  195. $before = $channel->toArray();
  196. $channel->status = 0;
  197. $channel->save();
  198. $channel->delete();
  199. // Groups grant access by type, so retain the grant while another row of that type remains.
  200. $remaining = RechargeChannel::query()->where('data_type', $channel->data_type)
  201. ->where('type', $channel->type)->lockForUpdate()->first(['id']);
  202. $groupChanges = [];
  203. if (!$remaining) {
  204. $field = (int)$channel->data_type === 1 ? 'recharge_type' : 'withdraw_type';
  205. foreach (RechargeChannelGroup::query()->orderBy('id')->lockForUpdate()->get() as $group) {
  206. $types = (array)$group->{$field};
  207. $updated = array_values(array_filter($types, fn ($type) => trim((string)$type) !== (string)$channel->type));
  208. if ($types === $updated) continue;
  209. $groupChanges[] = ['id' => (int)$group->id, 'field' => $field, 'before' => $types, 'after' => $updated];
  210. $group->{$field} = $updated;
  211. $group->save();
  212. }
  213. }
  214. OperationAuditService::record('recharge_channel', $id, 'delete', $before,
  215. ['channel' => $channel->toArray(), 'group_changes' => $groupChanges]);
  216. }, 3);
  217. }
  218. /** 通道操作日志中展示的字段(顺序即展示顺序) */
  219. private const AUDIT_FIELDS = [
  220. 'name' => '通道名称', 'key' => '通道标识Key', 'from' => '来源', 'type' => '通道类型', 'rate' => '费率',
  221. 'min' => '最低金额', 'max' => '最高金额', 'fixed' => '固定金额', 'status' => '状态', 'sort' => '排序', 'logo' => 'Logo',
  222. ];
  223. private const AUDIT_ACTIONS = ['create' => '新增', 'update' => '修改', 'delete' => '删除', 'save' => '修改Logo'];
  224. /** 充值(1)/提现(2)通道操作日志:谁、什么时间、改了什么 */
  225. public function logs(array $params): array
  226. {
  227. $dataType = (int)$params['data_type'];
  228. $channels = RechargeChannel::withTrashed()->where('data_type', $dataType)->get(['id', 'name', 'type'])->keyBy('id');
  229. $query = OperationAudit::query()->where(function ($q) use ($channels, $dataType) {
  230. $q->where('resource_type', 'recharge_channel')->whereIn('resource_id', $channels->keys()->all() ?: [0]);
  231. // 充值方式Logo按充值类型统一配置,归入充值通道日志
  232. if ($dataType === 1) $q->orWhere('resource_type', 'recharge_channel_logo');
  233. });
  234. if (!empty($params['channel_id'])) {
  235. $query->where('resource_type', 'recharge_channel')->where('resource_id', (int)$params['channel_id']);
  236. }
  237. if (!empty($params['operator_name'])) $query->where('operator_name', 'like', '%' . trim($params['operator_name']) . '%');
  238. if (!empty($params['action'])) $query->where('action', $params['action']);
  239. if (!empty($params['start_time'])) $query->where('created_at', '>=', $params['start_time']);
  240. if (!empty($params['end_time'])) $query->where('created_at', '<=', $params['end_time']);
  241. $total = (clone $query)->count();
  242. $rows = $query->orderByDesc('id')->forPage((int)($params['page'] ?? 1), (int)($params['limit'] ?? 20))->get();
  243. $data = $rows->map(function (OperationAudit $log) use ($channels) {
  244. $changes = (array)$log->changes;
  245. $isLogo = $log->resource_type === 'recharge_channel_logo';
  246. if ($isLogo) {
  247. $before = ['logo' => $changes['before']['logo'] ?? ''];
  248. $after = ['logo' => $changes['after']['logo'] ?? ''];
  249. $type = (string)($changes['after']['channel_type'] ?? $changes['before']['channel_type'] ?? '');
  250. } else {
  251. $before = (array)($changes['before'] ?? []);
  252. // 删除日志的 after 为 {channel, group_changes}
  253. $after = $log->action === 'delete' ? [] : (array)($changes['after'] ?? []);
  254. $type = (string)($after['type'] ?? $before['type'] ?? '');
  255. }
  256. $channel = $isLogo ? null : $channels->get($log->resource_id);
  257. $diff = self::diff($before, $after);
  258. $created = $log->getRawOriginal('created_at');
  259. return [
  260. 'id' => (int)$log->id,
  261. 'channel_id' => $isLogo ? null : (int)$log->resource_id,
  262. 'channel_name' => (string)($after['name'] ?? $before['name'] ?? ($channel->name ?? '')),
  263. 'channel_type' => $type,
  264. 'action' => $isLogo ? 'logo' : (string)$log->action,
  265. 'action_label' => self::AUDIT_ACTIONS[$log->action] ?? (string)$log->action,
  266. 'operator_id' => $log->operator_id === null ? null : (int)$log->operator_id,
  267. 'operator_name' => (string)$log->operator_name,
  268. 'created_at' => $created ? date('Y-m-d H:i:s', strtotime((string)$created)) : null,
  269. 'changes' => $diff,
  270. 'summary' => implode(';', array_map(fn ($c) => "{$c['label']}:{$c['before_text']} → {$c['after_text']}", $diff)),
  271. ];
  272. })->all();
  273. return ['total' => $total, 'data' => $data];
  274. }
  275. private static function diff(array $before, array $after): array
  276. {
  277. $result = [];
  278. foreach (self::AUDIT_FIELDS as $field => $label) {
  279. if (!array_key_exists($field, $before) && !array_key_exists($field, $after)) continue;
  280. $old = self::auditValue($field, $before[$field] ?? null);
  281. $new = self::auditValue($field, $after[$field] ?? null);
  282. if ($old === $new) continue;
  283. $result[] = ['field' => $field, 'label' => $label, 'before' => $old, 'after' => $new,
  284. 'before_text' => self::auditText($field, $old), 'after_text' => self::auditText($field, $new)];
  285. }
  286. return $result;
  287. }
  288. private static function auditValue(string $field, $value)
  289. {
  290. if ($value === null || $value === '' || $value === []) return null;
  291. if ($field === 'fixed') return implode(',', is_array($value) ? $value : explode(',', (string)$value));
  292. if (in_array($field, ['from', 'status', 'sort'], true)) return (int)$value;
  293. if (in_array($field, ['rate', 'min', 'max'], true)) return rtrim(rtrim(bcadd((string)$value, '0', 4), '0'), '.');
  294. return (string)$value;
  295. }
  296. private static function auditText(string $field, $value): string
  297. {
  298. if ($value === null) return '空';
  299. if ($field === 'status') return $value === 1 ? '启用' : '停用';
  300. if ($field === 'from') return [1 => '第三方支付', 2 => 'USDT', 3 => '人工'][$value] ?? (string)$value;
  301. return (string)$value;
  302. }
  303. private function identityLockReason(RechargeChannel $channel, bool $lock = false): string
  304. {
  305. foreach (['payment_gateways', 'payment_collection_channels', 'payment_direct_recharges'] as $table) {
  306. if (!Schema::hasTable($table) || !Schema::hasColumn($table, 'recharge_channel_id')) continue;
  307. $query = DB::table($table)->where('recharge_channel_id', $channel->id)->whereNull('deleted_at');
  308. if ($lock) $query->lockForUpdate();
  309. if ($query->first(['id'])) return '该通道已被支付配置引用,请先解除关联或新增通道,再更换公司或类型';
  310. }
  311. return '';
  312. }
  313. }