findOrFail($id); if ((int)$channel->data_type !== $dataType) throw new RuntimeException('通道方向与编辑记录不一致'); $reason = $this->identityLockReason($channel); } return $options + ['identity_editable' => $reason === '', 'identity_edit_reason' => $reason]; } public function save(array $params): RechargeChannel { return DB::transaction(function () use ($params) { $id = (int)($params['id'] ?? 0); unset($params['id']); $channel = $id ? RechargeChannel::query()->lockForUpdate()->findOrFail($id) : new RechargeChannel(); if ($channel->exists && (int)$channel->data_type !== (int)$params['data_type']) { throw new RuntimeException('不能修改通道所属的充值、提现或活动方向'); } $params = PaymentProviderCatalog::normalizeChannelSelection($params); $identityChanged = !$channel->exists || (int)$channel->from !== $params['from'] || (string)$channel->type !== (string)$params['type']; $identity = PaymentProviderCatalog::channelIdentity($params); if ($identityChanged && $identity) PaymentProviderService::assertEnabled($identity['provider_code']); if ($channel->exists && ((int)$channel->from !== $params['from'] || (string)$channel->type !== (string)$params['type'])) { $reason = $this->identityLockReason($channel, true); if ($reason !== '') throw new RuntimeException($reason); } foreach (['name', 'key'] as $field) { if (array_key_exists($field, $params)) $params[$field] = trim((string)$params[$field]); if ((!$channel->exists || array_key_exists($field, $params)) && ($params[$field] ?? '') === '') { throw new RuntimeException($field === 'name' ? '请填写通道名称' : '请填写通道标识Key'); } } foreach (['status', 'sort'] as $field) { if (array_key_exists($field, $params) && $params[$field] === null) unset($params[$field]); } if (!$channel->exists) $params += ['status' => 1, 'sort' => 0]; $channel->fill($params); $channel->save(); return $channel; }, 3); } public function delete(int $id): void { DB::transaction(function () use ($id) { $channel = RechargeChannel::withTrashed()->lockForUpdate()->findOrFail($id); if (!in_array((int)$channel->data_type, [1, 2], true)) throw new RuntimeException('只支持删除充值或提现通道'); if ($channel->trashed()) return; if ($this->identityLockReason($channel, true) !== '') { throw new RuntimeException('该通道已被支付配置引用,请先解除关联再删除'); } $before = $channel->toArray(); $channel->status = 0; $channel->save(); $channel->delete(); // Groups grant access by type, so retain the grant while another row of that type remains. $remaining = RechargeChannel::query()->where('data_type', $channel->data_type) ->where('type', $channel->type)->lockForUpdate()->first(['id']); $groupChanges = []; if (!$remaining) { $field = (int)$channel->data_type === 1 ? 'recharge_type' : 'withdraw_type'; foreach (RechargeChannelGroup::query()->orderBy('id')->lockForUpdate()->get() as $group) { $types = (array)$group->{$field}; $updated = array_values(array_filter($types, fn ($type) => trim((string)$type) !== (string)$channel->type)); if ($types === $updated) continue; $groupChanges[] = ['id' => (int)$group->id, 'field' => $field, 'before' => $types, 'after' => $updated]; $group->{$field} = $updated; $group->save(); } } OperationAuditService::record('recharge_channel', $id, 'delete', $before, ['channel' => $channel->toArray(), 'group_changes' => $groupChanges]); }, 3); } private function identityLockReason(RechargeChannel $channel, bool $lock = false): string { foreach (['payment_gateways', 'payment_collection_channels', 'payment_direct_recharges'] as $table) { if (!Schema::hasTable($table) || !Schema::hasColumn($table, 'recharge_channel_id')) continue; $query = DB::table($table)->where('recharge_channel_id', $channel->id)->whereNull('deleted_at'); if ($lock) $query->lockForUpdate(); if ($query->first(['id'])) return '该通道已被支付配置引用,请先解除关联或新增通道,再更换公司或类型'; } return ''; } }