每月统计

This commit is contained in:
2026-06-08 17:18:45 +08:00
parent 007ced596d
commit dd4cfab01a
4 changed files with 382 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ use Throwable;
use app\admin\model\User;
use app\admin\model\UserMoneyLog;
use app\common\controller\Backend;
use think\facade\Db;
class MoneyLog extends Backend
{
@@ -173,4 +174,95 @@ class MoneyLog extends Backend
}
$this->success('', $history);
}
public function annualReport()
{
$year = $this->request->param('year/d', date('Y'));
$timeExpression = "create_time";
$list = Db::table('ba_user_money_log')
->whereRaw("FROM_UNIXTIME({$timeExpression}, '%Y') = ?", [$year])
->fieldRaw("
FROM_UNIXTIME({$timeExpression}, '%c') as month,
-- 1. DEPOSIT (分转元保留2位小数)
ROUND(SUM(CASE WHEN type IN (1, 3) THEN money / 100 ELSE 0 END), 2) as deposit_amount,
-- 2. WITHDRAW (从变更记录看提现通常存正数,这里直接累加,分转元)
ROUND(SUM(CASE WHEN type IN (2, 4) THEN money / 100 ELSE 0 END), 2) as withdraw_amount,
-- 3. TRANSACTION (DEPOSIT)
COUNT(CASE WHEN type IN (1, 3) THEN id END) as deposit_count,
-- 4. TRANSACTION (WITHDRAW)
COUNT(CASE WHEN type IN (2, 4) THEN id END) as withdraw_count,
-- 5. ACTIVE PLAYER (每月去重活跃用户)
COUNT(DISTINCT user_id) as active_player,
-- 6. FIRST DEPOSIT (首充去重人数)
COUNT(DISTINCT CASE WHEN type = 1 AND label = 1 THEN user_id END) as first_deposit_count,
-- 7. UNCLAIM RECEIPT (未领单数)
COUNT(CASE WHEN type = 1 AND label = 2 THEN id END) as unclaim_count,
-- 8. UNCLAIM AMOUNT (未领金额)
ROUND(SUM(CASE WHEN type = 1 AND label = 2 THEN money / 100 ELSE 0 END), 2) as unclaim_amount
")
->group("month")
->select()
->toArray();
$monthsData = [];
foreach ($list as $row) {
$monthsData[intval($row['month'])] = $row;
}
$rowsSkeleton = [
'deposit' => ['name' => 'DEPOSIT', 'is_price' => true],
'withdraw' => ['name' => 'WITHDRAW', 'is_price' => true],
'trans_deposit' => ['name' => 'TRANSACTION (DEPOSIT)', 'is_price' => false],
'trans_withdraw' => ['name' => 'TRANSACTION (WITHDRAW)', 'is_price' => false],
'active_player' => ['name' => 'ACTIVE PLAYER', 'is_price' => false],
'first_deposit' => ['name' => 'FIRST DEPOSIT', 'is_price' => false],
'unclaim_receipt' => ['name' => 'UNCLAIM RECEIPT', 'is_price' => false],
'unclaim_amount' => ['name' => 'UNCLAIM AMOUNT', 'is_price' => true],
];
$finalReport = [];
foreach ($rowsSkeleton as $key => $meta) {
$rowData = [
'title' => $meta['name']
];
$yearTotal = 0;
for ($m = 1; $m <= 12; $m++) {
$monthValue = 0;
if (isset($monthsData[$m])) {
$monthValue = match ($key) {
'deposit' => (float)$monthsData[$m]['deposit_amount'],
'withdraw' => (float)$monthsData[$m]['withdraw_amount'],
'trans_deposit' => (int)$monthsData[$m]['deposit_count'],
'trans_withdraw' => (int)$monthsData[$m]['withdraw_count'],
'active_player' => (int)$monthsData[$m]['active_player'],
'first_deposit' => (int)$monthsData[$m]['first_deposit_count'],
'unclaim_receipt' => (int)$monthsData[$m]['unclaim_count'],
'unclaim_amount' => (float)$monthsData[$m]['unclaim_amount'],
};
}
$rowData['month_' . $m] = $meta['is_price'] ? number_format($monthValue, 2, '.', '') : $monthValue;
$yearTotal += $monthValue;
}
$rowData['year_total'] = $meta['is_price'] ? number_format($yearTotal, 2, '.', '') : $yearTotal;
$finalReport[] = $rowData;
}
$this->success('', [
'year' => $year,
'report_table' => $finalReport
]);
}
}