refactor(autoload): migrate theme classes to Composer PSR-4
Move 288 types from global AKSRT_* + inc/classes manual requires into
namespace AKSRT\{Domain} under src/AKSRT/, wire composer.json psr-4,
and slim bootstrap to helpers/modules/hooks only. Split multi-class
widget/gateway files, update admin contract audit paths, and document
the layout in ADR/README.
This commit is contained in:
1 parent
8f349b40fb
commit
aaf458af1c
404 files changed
+13602
-7705
No files matched your search
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
/**
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
namespace AKSRT\Payment;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class Rebate {
|
||||
|
||||
private static $instance = null;
|
||||
|
||||
const TYPE_ORDER = 'order';
|
||||
const TYPE_invite = 'invite';
|
||||
|
||||
const STATUS_PENDING = 'pending';
|
||||
const STATUS_SETTLED = 'settled';
|
||||
const STATUS_CANCELLED = 'cancelled';
|
||||
|
||||
public static function get_instance(): self {
|
||||
if (null === self::$instance) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
private function table(): string {
|
||||
return \AKSRT\Core\DBMigrator::table('rebate_log');
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建返佣记录
|
||||
*/
|
||||
public function create(array $data): int {
|
||||
global $wpdb;
|
||||
|
||||
$user_id = (int) ($data['user_id'] ?? 0);
|
||||
$order_number = sanitize_text_field((string) ($data['order_number'] ?? ''));
|
||||
$source_user_id = (int) ($data['source_user_id'] ?? 0);
|
||||
$amount = (float) ($data['amount'] ?? 0);
|
||||
$type = sanitize_key((string) ($data['type'] ?? self::TYPE_ORDER));
|
||||
$note = sanitize_textarea_field((string) ($data['note'] ?? ''));
|
||||
|
||||
if ($user_id <= 0 || $amount <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$valid_types = [self::TYPE_ORDER, self::TYPE_invite];
|
||||
if (!in_array($type, $valid_types, true)) {
|
||||
$type = self::TYPE_ORDER;
|
||||
}
|
||||
|
||||
$result = $wpdb->insert($this->table(), [
|
||||
'user_id' => $user_id,
|
||||
'order_number' => $order_number,
|
||||
'source_user_id' => $source_user_id > 0 ? $source_user_id : null,
|
||||
'amount' => $amount,
|
||||
'type' => $type,
|
||||
'status' => self::STATUS_PENDING,
|
||||
'created_at' => current_time('mysql'),
|
||||
'note' => $note,
|
||||
]);
|
||||
|
||||
$id = $result ? (int) $wpdb->insert_id : 0;
|
||||
if ($id > 0) {
|
||||
do_action('aksrt_pay_rebate_added', $user_id, $amount, [
|
||||
'rebate_id' => $id,
|
||||
'order_number' => $order_number,
|
||||
'type' => $type,
|
||||
]);
|
||||
}
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 结算返佣
|
||||
*/
|
||||
public function settle(int $id): bool {
|
||||
global $wpdb;
|
||||
|
||||
$row = $wpdb->get_row($wpdb->prepare(
|
||||
"SELECT * FROM {$this->table()} WHERE id = %d AND status = %s",
|
||||
$id, self::STATUS_PENDING
|
||||
), ARRAY_A);
|
||||
|
||||
if (!$row) return false;
|
||||
|
||||
$user_id = (int) $row['user_id'];
|
||||
$amount = (float) $row['amount'];
|
||||
|
||||
$result = $wpdb->update($this->table(), [
|
||||
'status' => self::STATUS_SETTLED,
|
||||
'settled_at' => current_time('mysql'),
|
||||
], ['id' => $id]);
|
||||
|
||||
if ($result && class_exists('\AKSRT\Credits\Credits')) {
|
||||
\AKSRT\Credits\Credits::add($user_id, (int) $amount, 'rebate', [
|
||||
'ref_type' => 'rebate',
|
||||
'ref_id' => $id,
|
||||
]);
|
||||
}
|
||||
|
||||
return (bool) $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消返佣
|
||||
*/
|
||||
public function cancel(int $id): bool {
|
||||
global $wpdb;
|
||||
return (bool) $wpdb->update($this->table(), [
|
||||
'status' => self::STATUS_CANCELLED,
|
||||
], ['id' => $id, 'status' => self::STATUS_PENDING]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取返佣列表
|
||||
*/
|
||||
public function get_list(array $args = []): array {
|
||||
global $wpdb;
|
||||
|
||||
$limit = max(1, min(100, (int) ($args['limit'] ?? 20)));
|
||||
$offset = max(0, (int) ($args['offset'] ?? 0));
|
||||
$status = sanitize_key((string) ($args['status'] ?? ''));
|
||||
$user_id = (int) ($args['user_id'] ?? 0);
|
||||
|
||||
$where = [];
|
||||
$params = [];
|
||||
|
||||
if ($status !== '') {
|
||||
$where[] = 'status = %s';
|
||||
$params[] = $status;
|
||||
}
|
||||
if ($user_id > 0) {
|
||||
$where[] = 'user_id = %d';
|
||||
$params[] = $user_id;
|
||||
}
|
||||
|
||||
$where_sql = $where ? 'WHERE ' . implode(' AND ', $where) : '';
|
||||
|
||||
$count_sql = "SELECT COUNT(*) FROM {$this->table()} {$where_sql}";
|
||||
$total = $params ? (int) $wpdb->get_var($wpdb->prepare($count_sql, $params)) : (int) $wpdb->get_var($count_sql);
|
||||
|
||||
$sql = "SELECT * FROM {$this->table()} {$where_sql} ORDER BY created_at DESC LIMIT %d OFFSET %d";
|
||||
$params[] = $limit;
|
||||
$params[] = $offset;
|
||||
|
||||
$items = $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A);
|
||||
|
||||
return [
|
||||
'items' => $items ?: [],
|
||||
'total' => $total,
|
||||
'page' => floor($offset / $limit) + 1,
|
||||
'max_pages' => ceil($total / $limit),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取统计
|
||||
*/
|
||||
public function get_stats(): array {
|
||||
global $wpdb;
|
||||
$table = $this->table();
|
||||
|
||||
$row = $wpdb->get_row(
|
||||
"SELECT
|
||||
COUNT(*) as total,
|
||||
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending,
|
||||
SUM(CASE WHEN status = 'settled' THEN 1 ELSE 0 END) as settled,
|
||||
SUM(amount) as total_amount,
|
||||
SUM(CASE WHEN status = 'settled' THEN amount ELSE 0 END) as settled_amount
|
||||
FROM {$table}",
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
return [
|
||||
'total' => (int) ($row['total'] ?? 0),
|
||||
'pending' => (int) ($row['pending'] ?? 0),
|
||||
'settled' => (int) ($row['settled'] ?? 0),
|
||||
'total_amount' => (float) ($row['total_amount'] ?? 0),
|
||||
'settled_amount' => (float) ($row['settled_amount'] ?? 0),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user