feat(user-center): merge promotion rewards into revenue
This commit is contained in:
1 parent
a87fd32fa3
commit
39c92017d9
14 files changed
+1048
-159
No files matched your search
+61
-28
@@ -79,31 +79,56 @@ class Rebate {
|
||||
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,
|
||||
]);
|
||||
if (!class_exists('\AKSRT\Credits\Balance')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) $result;
|
||||
}
|
||||
$wpdb->query('START TRANSACTION');
|
||||
try {
|
||||
$row = $wpdb->get_row($wpdb->prepare(
|
||||
"SELECT id, user_id, amount, order_number FROM {$this->table()} WHERE id = %d AND status = %s FOR UPDATE",
|
||||
$id,
|
||||
self::STATUS_PENDING
|
||||
), ARRAY_A);
|
||||
if (!is_array($row)) {
|
||||
$wpdb->query('ROLLBACK');
|
||||
return false;
|
||||
}
|
||||
|
||||
$credited = \AKSRT\Credits\Balance::add(
|
||||
(int) $row['user_id'],
|
||||
(float) $row['amount'],
|
||||
'rebate',
|
||||
['type' => 'rebate', 'id' => (int) $row['id']],
|
||||
sprintf(__('推广返佣结算(订单 %s)', 'aksrt'), (string) $row['order_number'])
|
||||
);
|
||||
if (is_wp_error($credited)) {
|
||||
$wpdb->query('ROLLBACK');
|
||||
return false;
|
||||
}
|
||||
|
||||
$settled = $wpdb->update(
|
||||
$this->table(),
|
||||
[
|
||||
'status' => self::STATUS_SETTLED,
|
||||
'settled_at' => current_time('mysql'),
|
||||
],
|
||||
['id' => $id, 'status' => self::STATUS_PENDING],
|
||||
['%s', '%s'],
|
||||
['%d', '%s']
|
||||
);
|
||||
if ($settled !== 1) {
|
||||
$wpdb->query('ROLLBACK');
|
||||
return false;
|
||||
}
|
||||
|
||||
$wpdb->query('COMMIT');
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
$wpdb->query('ROLLBACK');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 取消返佣
|
||||
*/
|
||||
@@ -159,24 +184,32 @@ class Rebate {
|
||||
/**
|
||||
* 获取统计
|
||||
*/
|
||||
public function get_stats(): array {
|
||||
public function get_stats(int $user_id = 0): array {
|
||||
global $wpdb;
|
||||
$table = $this->table();
|
||||
$where = '';
|
||||
$params = [];
|
||||
if ($user_id > 0) {
|
||||
$where = 'WHERE user_id = %d';
|
||||
$params[] = $user_id;
|
||||
}
|
||||
|
||||
$row = $wpdb->get_row(
|
||||
"SELECT
|
||||
$sql = "SELECT
|
||||
COUNT(*) as total,
|
||||
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending,
|
||||
SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) as pending_amount,
|
||||
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
|
||||
);
|
||||
FROM {$table} {$where}";
|
||||
$row = $params
|
||||
? $wpdb->get_row($wpdb->prepare($sql, $params), ARRAY_A)
|
||||
: $wpdb->get_row($sql, ARRAY_A);
|
||||
|
||||
return [
|
||||
'total' => (int) ($row['total'] ?? 0),
|
||||
'pending' => (int) ($row['pending'] ?? 0),
|
||||
'pending_amount' => (float) ($row['pending_amount'] ?? 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