功能:新增用户积分流水系统

- 新建 user_currency_logs 流水表 (Migration)
- App\Enums\CurrencySource 来源枚举(可扩展)
- App\Models\UserCurrencyLog 流水模型
- App\Services\UserCurrencyService 统一积分变更服务
- FishingController:抛竿/收竿接入流水记录
- AutoSaveExp:自动存点接入流水记录
- Admin/UserManagerController:管理员调整接入流水记录
- LeaderboardController:新增今日三榜(经验/金币/魅力)+ 个人流水日志页
- Admin/CurrencyStatsController:后台活动统计页
- views:新增个人日志页、后台统计页;排行榜新增今日榜数据传递
- routes:新增个人日志路由 /my/currency-logs、后台路由 /admin/currency-stats
This commit is contained in:
2026-02-28 12:49:26 +08:00
parent 3f5d0e9539
commit 0c5e218aa8
14 changed files with 1045 additions and 223 deletions

View File

@@ -0,0 +1,72 @@
<?php
/**
* 文件功能:用户积分流水表 Migration
* 记录所有经验、金币、魅力的变更明细,提供今日排行与活动统计数据源。
* 使用 user_id 作为真实身份锚点username 仅为写入时快照,支持改名场景。
*
* @author ChatRoom Laravel
* @version 1.0.0
*/
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* 创建 user_currency_logs 流水表。
*/
public function up(): void
{
Schema::create('user_currency_logs', function (Blueprint $table) {
$table->id();
// 真实身份锚点:使用 user_id即使用户改名历史记录仍可准确归属
$table->unsignedBigInteger('user_id')->index()->comment('用户 ID身份锚点不随改名变化');
// 写入时的用户名快照:仅用于日志可读性,排行榜展示取 users.username 当前值
$table->string('username', 50)->comment('操作时刻用户名快照');
// 货币类型(对应 CurrencySource 枚举varchar 允许未来无缝新增)
$table->string('currency', 20)->comment('货币类型: exp / gold / charm / 以后可自由扩展');
// 变更量(正数增加,负数扣除)
$table->integer('amount')->comment('变更量,正数为增加,负数为扣除');
// 变更后余额(方便对账,无需重算历史)
$table->integer('balance_after')->comment('变更后余额快照');
// 来源活动varchar 非 ENUM新增活动无需改表只在 CurrencySource 枚举加一行)
$table->string('source', 30)->comment('来源标识: auto_save / fishing_gain / send_gift / ...');
// 备注(补充说明,如"送花给流星"
$table->string('remark', 255)->default('')->comment('操作备注');
// 发生所在房间(部分操作无房间)
$table->unsignedInteger('room_id')->nullable()->comment('所在房间 ID无则为 null');
// 只记录创建时间,流水不可修改
$table->timestamp('created_at')->useCurrent()->comment('记录时间');
// ── 索引(按高频查询场景设计)────────────────────────────
// 今日排行快速聚合:按货币类型 + 日期 + 金额
$table->index(['currency', 'created_at', 'amount'], 'idx_daily_board');
// 用户个人流水查询:按 user_id + 货币类型 + 时间
$table->index(['user_id', 'currency', 'created_at'], 'idx_user_log');
// 活动统计聚合:按来源 + 货币类型 + 日期
$table->index(['source', 'currency', 'created_at'], 'idx_source_stat');
});
}
/**
* 回滚:删除流水表。
*/
public function down(): void
{
Schema::dropIfExists('user_currency_logs');
}
};