| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- use Illuminate\Database\Migrations\Migration;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- /** 模拟管理:自动任务的模板、运行、事件三张表,以及 config 表里的模拟设置默认值(默认关闭)。 */
- return new class extends Migration {
- private const SETTINGS = [
- 'simulation_enabled' => ['0', '模拟管理总开关:1 开启'],
- 'simulation_max_concurrency' => ['2', '模拟自动任务同时运行数上限'],
- 'simulation_rate_per_minute' => ['30', '模拟自动任务每分钟动作数上限'],
- 'simulation_max_members_per_task' => ['20', '单次创建/单个任务的模拟会员数上限'],
- 'simulation_max_members_total' => ['500', '模拟会员总数上限'],
- 'simulation_max_amount' => ['10000', '模拟充值/提现/下注单次金额上限'],
- 'simulation_initial_balance' => ['0', '新建模拟会员的初始余额'],
- 'simulation_wager_bypass' => ['1', '模拟提现是否跳过打码量:1 跳过'],
- 'simulation_default_domain' => ['sim.local', '模拟会员默认注册/登录域名'],
- ];
- public function up(): void
- {
- if (!Schema::hasTable('simulation_tasks')) {
- Schema::create('simulation_tasks', function (Blueprint $t) {
- $t->id();
- $t->string('name', 100);
- $t->json('steps');
- $t->string('target_mode', 16)->default('new')->comment('new 新建会员 / existing 使用已有模拟会员');
- $t->unsignedInteger('member_count')->default(1);
- $t->string('remark', 255)->default('');
- $t->unsignedBigInteger('created_by')->nullable();
- $t->timestamps();
- });
- }
- if (!Schema::hasTable('simulation_task_runs')) {
- Schema::create('simulation_task_runs', function (Blueprint $t) {
- $t->id();
- $t->unsignedBigInteger('task_id')->nullable();
- $t->string('name', 100);
- $t->json('steps');
- $t->json('member_ids');
- $t->unsignedInteger('member_count')->default(1);
- $t->string('status', 16)->default('queued')->comment('queued running paused stopped completed failed');
- $t->unsignedInteger('total')->default(0);
- $t->unsignedInteger('completed')->default(0);
- $t->unsignedInteger('failed')->default(0);
- $t->json('cursor')->nullable();
- $t->dateTime('next_run_at')->nullable();
- $t->dateTime('started_at')->nullable();
- $t->dateTime('finished_at')->nullable();
- $t->unsignedBigInteger('created_by')->nullable();
- $t->string('last_error', 500)->nullable();
- $t->timestamps();
- $t->index(['status', 'next_run_at']);
- });
- }
- if (!Schema::hasTable('simulation_task_events')) {
- Schema::create('simulation_task_events', function (Blueprint $t) {
- $t->id();
- $t->unsignedBigInteger('run_id');
- $t->string('member_id', 64)->default('');
- $t->unsignedInteger('step_index');
- $t->unsignedInteger('seq');
- $t->string('action', 16);
- $t->string('status', 16)->comment('ok failed');
- $t->string('ref_type', 32)->nullable();
- $t->string('ref_id', 64)->nullable();
- $t->decimal('amount', 22, 2)->nullable();
- $t->string('message', 500)->default('');
- $t->dateTime('created_at');
- $t->unique(['run_id', 'step_index', 'member_id', 'seq'], 'simulation_task_events_step_unique');
- });
- }
- if (Schema::hasTable('config')) {
- $hasRemark = Schema::hasColumn('config', 'remark');
- $hasGroup = Schema::hasColumn('config', 'group_id');
- $hasTimes = Schema::hasColumn('config', 'created_at');
- foreach (self::SETTINGS as $field => [$value, $remark]) {
- if (DB::table('config')->where('field', $field)->exists()) continue;
- $row = ['field' => $field, 'val' => $value];
- if ($hasRemark) $row['remark'] = $remark;
- if ($hasGroup) $row['group_id'] = 1;
- if ($hasTimes) $row += ['created_at' => now(), 'updated_at' => now()];
- DB::table('config')->insert($row);
- }
- }
- }
- public function down(): void
- {
- Schema::dropIfExists('simulation_task_events');
- Schema::dropIfExists('simulation_task_runs');
- Schema::dropIfExists('simulation_tasks');
- if (Schema::hasTable('config')) DB::table('config')->whereIn('field', array_keys(self::SETTINGS))->delete();
- }
- };
|