WithdrawalAgentRatioConfig.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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小额(<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 normalizeRules(array $rules)
  54. {
  55. $result = [];
  56. foreach (array_keys(self::pixTypeOptions()) as $pixType) {
  57. foreach (($rules[$pixType] ?? []) as $agent => $ratio) {
  58. $ratio = (int)$ratio;
  59. if ($ratio <= 0) {
  60. continue;
  61. }
  62. $result[$pixType][] = [
  63. 'agent' => (int)$agent,
  64. 'ratio' => $ratio,
  65. ];
  66. }
  67. }
  68. return $result;
  69. }
  70. private static function isRatioRules(array $config)
  71. {
  72. foreach (array_keys(self::pixTypeOptions()) as $pixType) {
  73. if (isset($config[$pixType])) {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. private static function saveAll(array $config)
  80. {
  81. DB::table('QPAccountsDB.dbo.SystemStatusInfo')->updateOrInsert(
  82. ['StatusName' => self::STATUS_NAME],
  83. [
  84. 'StatusValue' => 0,
  85. 'StatusString' => json_encode($config),
  86. 'StatusTip' => '提现免审代付比例配置',
  87. 'StatusDescription' => '按提现方式配置自动免审代付比例',
  88. ]
  89. );
  90. self::clearCache();
  91. }
  92. }