WithdrawalAgentRatioConfig.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Redis;
  5. class WithdrawalAgentRatioConfig
  6. {
  7. const STATUS_NAME = 'WithdrawAgentRatioConfig';
  8. const CACHE_KEY = 'withdrawal_agent_ratio_config';
  9. const CACHE_TTL = 600;
  10. const PIX_TYPE_CASH_SMALL = 'cash_small';
  11. public static function pixTypeOptions()
  12. {
  13. return [
  14. 1 => 'CashApp',
  15. 2 => 'PayPal',
  16. self::PIX_TYPE_CASH_SMALL => 'CashApp small(<50)',
  17. ];
  18. }
  19. public static function all()
  20. {
  21. $cache = Redis::get(self::CACHE_KEY);
  22. if ($cache) {
  23. $config = json_decode($cache, true);
  24. if (is_array($config)) {
  25. return $config;
  26. }
  27. self::clearCache();
  28. }
  29. $json = DB::table('QPAccountsDB.dbo.SystemStatusInfo')
  30. ->where('StatusName', self::STATUS_NAME)
  31. ->value('StatusString');
  32. $config = json_decode($json ?: '', true);
  33. $config = is_array($config) ? $config : [];
  34. Redis::setex(self::CACHE_KEY, self::CACHE_TTL, json_encode($config));
  35. return $config;
  36. }
  37. public static function getGlobal()
  38. {
  39. $config = self::all();
  40. if (self::isRatioRules($config)) {
  41. return $config;
  42. }
  43. return $config['100'] ?? $config[100] ?? [];
  44. }
  45. public static function saveGlobal(array $rules)
  46. {
  47. self::saveAll(self::normalizeRules($rules));
  48. }
  49. public static function clearCache()
  50. {
  51. Redis::del(self::CACHE_KEY);
  52. }
  53. public static function disableAgent($agent)
  54. {
  55. $agent = (int)$agent;
  56. $config = self::getGlobal();
  57. $changed = false;
  58. foreach (array_keys(self::pixTypeOptions()) as $pixType) {
  59. $items = $config[$pixType] ?? [];
  60. if (!is_array($items)) {
  61. continue;
  62. }
  63. $filtered = [];
  64. foreach ($items as $item) {
  65. if ((int)($item['agent'] ?? 0) == $agent) {
  66. $changed = true;
  67. continue;
  68. }
  69. $filtered[] = $item;
  70. }
  71. $config[$pixType] = $filtered;
  72. }
  73. if ($changed) {
  74. self::saveAll($config);
  75. }
  76. return $changed;
  77. }
  78. public static function normalizeRules(array $rules)
  79. {
  80. $result = [];
  81. foreach (array_keys(self::pixTypeOptions()) as $pixType) {
  82. foreach (($rules[$pixType] ?? []) as $agent => $ratio) {
  83. $ratio = (int)$ratio;
  84. if ($ratio <= 0) {
  85. continue;
  86. }
  87. $result[$pixType][] = [
  88. 'agent' => (int)$agent,
  89. 'ratio' => $ratio,
  90. ];
  91. }
  92. }
  93. return $result;
  94. }
  95. private static function isRatioRules(array $config)
  96. {
  97. foreach (array_keys(self::pixTypeOptions()) as $pixType) {
  98. if (isset($config[$pixType])) {
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. private static function saveAll(array $config)
  105. {
  106. DB::table('QPAccountsDB.dbo.SystemStatusInfo')->updateOrInsert(
  107. ['StatusName' => self::STATUS_NAME],
  108. [
  109. 'StatusValue' => 0,
  110. 'StatusString' => json_encode($config),
  111. 'StatusTip' => 'withdrawal auto payout ratio config',
  112. 'StatusDescription' => 'auto payout ratio config by withdrawal method',
  113. ]
  114. );
  115. self::clearCache();
  116. }
  117. }