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), ]; } }