167 lines
5.0 KiB
PHP
167 lines
5.0 KiB
PHP
<?php
|
|
/**
|
|
* AKSRT Withdrawal — 提现台账
|
|
*
|
|
* 管理用户提现申请、审核和处理。
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Withdrawal {
|
|
|
|
const STATUS_PENDING = 'pending';
|
|
const STATUS_APPROVED = 'approved';
|
|
const STATUS_PROCESSING = 'processing';
|
|
const STATUS_COMPLETED = 'completed';
|
|
const STATUS_REJECTED = 'rejected';
|
|
|
|
private static $instance = null;
|
|
|
|
public static function get_instance(): self {
|
|
if (null === self::$instance) self::$instance = new self();
|
|
return self::$instance;
|
|
}
|
|
|
|
private function table(): string {
|
|
return AKSRT_DB_Migrator::table('withdrawal');
|
|
}
|
|
|
|
/**
|
|
* 创建提现申请
|
|
*/
|
|
public function create(int $user_id, float $amount, string $method = '', string $account_info = ''): int {
|
|
global $wpdb;
|
|
|
|
if ($user_id <= 0 || $amount <= 0) return 0;
|
|
|
|
$wpdb->insert($this->table(), [
|
|
'user_id' => $user_id,
|
|
'amount' => $amount,
|
|
'method' => sanitize_key($method),
|
|
'account_info' => sanitize_textarea_field($account_info),
|
|
'status' => self::STATUS_PENDING,
|
|
'created_at' => current_time('mysql'),
|
|
], ['%d', '%f', '%s', '%s', '%s', '%s']);
|
|
|
|
return (int) $wpdb->insert_id;
|
|
}
|
|
|
|
/**
|
|
* 更新提现状态
|
|
*/
|
|
public function update_status(int $withdrawal_id, string $new_status, string $note = ''): bool {
|
|
global $wpdb;
|
|
|
|
$update = ['status' => sanitize_key($new_status)];
|
|
$format = ['%s'];
|
|
|
|
if ($new_status === self::STATUS_COMPLETED || $new_status === self::STATUS_REJECTED) {
|
|
$update['processed_at'] = current_time('mysql');
|
|
$format[] = '%s';
|
|
}
|
|
|
|
if ($note !== '') {
|
|
$update['note'] = sanitize_textarea_field($note);
|
|
$format[] = '%s';
|
|
}
|
|
|
|
return $wpdb->update($this->table(), $update, ['id' => $withdrawal_id], $format, ['%d']) !== false;
|
|
}
|
|
|
|
/**
|
|
* 获取用户提现列表
|
|
*/
|
|
public function get_user_withdrawals(int $user_id, array $args = []): array {
|
|
global $wpdb;
|
|
|
|
$where = ['user_id = %d'];
|
|
$params = [$user_id];
|
|
|
|
$status = sanitize_key((string) ($args['status'] ?? ''));
|
|
if ($status !== '') {
|
|
$where[] = 'status = %s';
|
|
$params[] = $status;
|
|
}
|
|
|
|
$limit = max(1, min(100, (int) ($args['limit'] ?? 20)));
|
|
$offset = max(0, (int) ($args['offset'] ?? 0));
|
|
|
|
$sql = "SELECT * FROM {$this->table()} WHERE " . implode(' AND ', $where) . " ORDER BY created_at DESC LIMIT %d OFFSET %d";
|
|
$params[] = $limit;
|
|
$params[] = $offset;
|
|
|
|
return (array) $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A);
|
|
}
|
|
|
|
/**
|
|
* 获取所有提现列表(管理后台)
|
|
*/
|
|
public function get_all(array $args = []): array {
|
|
global $wpdb;
|
|
|
|
$where = [];
|
|
$params = [];
|
|
|
|
$status = sanitize_key((string) ($args['status'] ?? ''));
|
|
if ($status !== '') {
|
|
$where[] = 'status = %s';
|
|
$params[] = $status;
|
|
}
|
|
|
|
$limit = max(1, min(100, (int) ($args['limit'] ?? 20)));
|
|
$offset = max(0, (int) ($args['offset'] ?? 0));
|
|
|
|
$where_sql = $where ? 'WHERE ' . implode(' AND ', $where) : '';
|
|
$sql = "SELECT * FROM {$this->table()} {$where_sql} ORDER BY created_at DESC LIMIT %d OFFSET %d";
|
|
$params[] = $limit;
|
|
$params[] = $offset;
|
|
|
|
if (!empty($params)) {
|
|
return (array) $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A);
|
|
}
|
|
return (array) $wpdb->get_results($sql, ARRAY_A);
|
|
}
|
|
|
|
/**
|
|
* 获取提现总数(支持状态过滤)
|
|
*/
|
|
public function get_count(array $args = []): int {
|
|
global $wpdb;
|
|
|
|
$status = sanitize_key((string) ($args['status'] ?? ''));
|
|
if ($status !== '') {
|
|
return (int) $wpdb->get_var($wpdb->prepare(
|
|
"SELECT COUNT(*) FROM {$this->table()} WHERE status = %s",
|
|
$status
|
|
));
|
|
}
|
|
return (int) $wpdb->get_var("SELECT COUNT(*) FROM {$this->table()}");
|
|
}
|
|
|
|
/**
|
|
* 获取提现统计
|
|
*/
|
|
public function get_stats(): array {
|
|
global $wpdb;
|
|
|
|
$row = $wpdb->get_row(
|
|
"SELECT
|
|
COUNT(*) as total,
|
|
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending,
|
|
SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) as completed,
|
|
COALESCE(SUM(CASE WHEN status = 'completed' THEN amount ELSE 0 END), 0) as total_paid
|
|
FROM {$this->table()}",
|
|
ARRAY_A
|
|
);
|
|
|
|
return [
|
|
'total' => (int) ($row['total'] ?? 0),
|
|
'pending' => (int) ($row['pending'] ?? 0),
|
|
'completed' => (int) ($row['completed'] ?? 0),
|
|
'total_paid' => (float) ($row['total_paid'] ?? 0),
|
|
];
|
|
}
|
|
}
|