SimulationSettings.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Services\Simulation;
  3. use App\Constants\HttpStatus;
  4. use App\Support\MemberAudience;
  5. use Exception;
  6. use Illuminate\Support\Facades\DB;
  7. /** 模拟管理的设置(config 表 simulation_* 键)以及每个写操作都要过的两道检查。 */
  8. final class SimulationSettings
  9. {
  10. /** 键 => [默认值, 最小, 最大];最小为 null 的是字符串。 */
  11. public const KEYS = [
  12. 'simulation_enabled' => ['0', 0, 1],
  13. 'simulation_max_concurrency' => ['2', 1, 10],
  14. 'simulation_rate_per_minute' => ['30', 1, 120],
  15. 'simulation_max_members_per_task' => ['20', 1, 200],
  16. 'simulation_max_members_total' => ['500', 1, 5000],
  17. 'simulation_max_amount' => ['10000', 1, 1000000],
  18. 'simulation_initial_balance' => ['0', 0, 1000000],
  19. 'simulation_wager_bypass' => ['1', 0, 1],
  20. 'simulation_default_domain' => ['sim.local', null, null],
  21. // 入股代理(股东)的独立后台是否把模拟会员的数据算进全平台统计
  22. 'simulation_show_to_equity_agents' => ['1', 0, 1],
  23. // 一键启动的默认任务:注册 → 登录 → 充值 → 下注 → 结算 → 提现 → 退出
  24. 'simulation_task_members' => ['5', 1, 200],
  25. // 金额都是区间:每次动作在 最小~最大 之间随机取整数;最大不大于最小时就是固定金额
  26. 'simulation_task_recharge' => ['500', 1, 1000000],
  27. 'simulation_task_recharge_max' => ['3000', 1, 1000000],
  28. 'simulation_task_bet_category' => ['mixed', null, null],
  29. 'simulation_task_bet_amount' => ['20', 1, 1000000],
  30. 'simulation_task_bet_amount_max' => ['500', 1, 1000000],
  31. 'simulation_task_bet_count' => ['3', 1, 50],
  32. 'simulation_task_withdraw' => ['100', 0, 1000000],
  33. 'simulation_task_withdraw_max' => ['500', 0, 1000000],
  34. 'simulation_task_interval' => ['2', 0, 3600],
  35. // 持续运行:在日期区间内一轮接一轮地自动启动上面的默认任务
  36. 'simulation_schedule_enabled' => ['0', 0, 1],
  37. 'simulation_schedule_start' => ['', null, null],
  38. 'simulation_schedule_end' => ['', null, null],
  39. 'simulation_schedule_gap_minutes' => ['10', 0, 1440],
  40. ];
  41. public static function all(): array
  42. {
  43. $stored = DB::table('config')->whereIn('field', array_keys(self::KEYS))->pluck('val', 'field');
  44. $out = [];
  45. foreach (self::KEYS as $key => [$default]) $out[$key] = (string) ($stored[$key] ?? $default);
  46. return $out;
  47. }
  48. public static function get(string $key): string
  49. {
  50. return self::all()[$key];
  51. }
  52. public static function save(array $input): array
  53. {
  54. return DB::transaction(function () use ($input) {
  55. $before = self::all();
  56. foreach (self::KEYS as $key => [, $min, $max]) {
  57. if (!array_key_exists($key, $input)) continue;
  58. $value = trim((string) $input[$key]);
  59. if ($key === 'simulation_task_bet_category') {
  60. if (!in_array($value, SimulationTaskService::BET_CATEGORIES, true)) throw new Exception('默认任务的下注分类不正确', HttpStatus::CUSTOM_ERROR);
  61. } elseif (in_array($key, ['simulation_schedule_start', 'simulation_schedule_end'], true)) {
  62. if ($value !== '' && (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) || date('Y-m-d', (int) strtotime($value)) !== $value)) {
  63. throw new Exception('持续运行的日期格式应为 YYYY-MM-DD', HttpStatus::CUSTOM_ERROR);
  64. }
  65. } elseif ($min === null) {
  66. if ($value === '' || strlen($value) > 100) throw new Exception('域名格式不正确', HttpStatus::CUSTOM_ERROR);
  67. } elseif (!is_numeric($value) || $value < $min || $value > $max) {
  68. throw new Exception("{$key} 应在 {$min} 到 {$max} 之间", HttpStatus::CUSTOM_ERROR);
  69. }
  70. $exists = DB::table('config')->where('field', $key)->exists();
  71. if ($exists) DB::table('config')->where('field', $key)->update(['val' => $value]);
  72. else DB::table('config')->insert(['field' => $key, 'val' => $value]);
  73. }
  74. $after = self::all();
  75. foreach (['simulation_task_recharge' => '充值', 'simulation_task_bet_amount' => '下注', 'simulation_task_withdraw' => '提现'] as $key => $label) {
  76. if ((float) $after[$key . '_max'] < (float) $after[$key]) throw new Exception("一键任务的{$label}金额:最大值不能小于最小值", HttpStatus::CUSTOM_ERROR);
  77. }
  78. if ($after['simulation_schedule_enabled'] === '1') {
  79. [$start, $end] = [$after['simulation_schedule_start'], $after['simulation_schedule_end']];
  80. if ($start === '' || $end === '' || $start > $end) throw new Exception('开启持续运行前,请填写正确的开始和结束日期', HttpStatus::CUSTOM_ERROR);
  81. if ((strtotime($end) - strtotime($start)) / 86400 > 92) throw new Exception('持续运行的日期区间不能超过 92 天', HttpStatus::CUSTOM_ERROR);
  82. }
  83. return ['before' => $before, 'after' => $after];
  84. });
  85. }
  86. public static function enabled(): bool
  87. {
  88. return self::get('simulation_enabled') === '1';
  89. }
  90. public static function assertEnabled(): void
  91. {
  92. if (!MemberAudience::enabled()) throw new Exception('用户类型字段尚未迁移,无法使用模拟管理', HttpStatus::CUSTOM_ERROR);
  93. if (!self::enabled()) throw new Exception('模拟功能未开启,请先在“模拟设置”中开启', HttpStatus::CUSTOM_ERROR);
  94. }
  95. /** 写操作的目标必须是后台模拟会员,返回该会员。 */
  96. public static function member($memberId): object
  97. {
  98. $user = DB::table('users')->where('member_id', (string) $memberId)->where('user_type', MemberAudience::TYPE_SIMULATED)->first();
  99. if (!$user) throw new Exception('只能操作模拟会员', HttpStatus::CUSTOM_ERROR);
  100. return $user;
  101. }
  102. public static function assertAmount($amount): string
  103. {
  104. if (!is_numeric($amount) || $amount <= 0) throw new Exception('金额格式不正确', HttpStatus::CUSTOM_ERROR);
  105. if ($amount > (float) self::get('simulation_max_amount')) throw new Exception('金额超过模拟设置的单次上限', HttpStatus::CUSTOM_ERROR);
  106. return number_format((float) $amount, 2, '.', '');
  107. }
  108. }