AgentUser.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Game;
  3. use App\Http\helper\NumConfig;
  4. use App\Services\ExtensionCopy;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Redis;
  8. class AgentUser extends Model
  9. {
  10. const TABLE = 'webgame.AgentUser';
  11. protected $connection='mysql';
  12. protected $primaryKey='GlobalUID';
  13. public $incrementing = false; // 非自增主键
  14. protected $keyType = 'string'; // 主键类型为字符串
  15. protected $table = self::TABLE;
  16. public $timestamps = false;
  17. protected $fillable = [
  18. 'GlobalUID', 'UserID', 'LinkCode','LinkRewards', 'Higher1GUID', 'Higher2GUID', 'Higher3GUID', 'Higher4GUID',
  19. 'Higher1ID', 'Higher2ID', 'Higher3ID', 'Higher4ID', 'TotalReward1', 'TotalReward2',
  20. 'TotalReward3', 'TotalReward4', 'downCount1', 'downCount2', 'downCount3', 'downCount4','RewardRate','RewardLimit'
  21. ];
  22. // 推广员奖励
  23. public static function reward($UserID, $amount, $orderSn)
  24. {
  25. $UserAgent = self::query()->where('UserID', $UserID)->first();
  26. if ($UserAgent) {
  27. // 上级ID
  28. $Higher1ID = $UserAgent->Higher1ID;
  29. // 上上级ID
  30. $Higher2ID = $UserAgent->Higher2ID;
  31. // 设置默认值
  32. $DirectRebate1 = $DirectRebate2 = 0;
  33. // 1、找出充值用户上级ID
  34. if ($Higher1ID > 0) {
  35. // 充值金额 * 百分比例
  36. $AgentRebateRatio1 = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')->where('StatusName', 'AgentRebateRatio1')->select('StatusValue')->first()->StatusValue / NumConfig::NUM_VALUE ?? 0;
  37. $DirectRebate1 = $amount * $AgentRebateRatio1;
  38. $rKey = sprintf(ExtensionCopy::RECHARGE_PEOPLE_SETS, $Higher1ID);
  39. Redis::sAdd($rKey, $UserID);
  40. Redis::expire($rKey, 86400*60);
  41. $rKey = sprintf(ExtensionCopy::RECHARGE, $Higher1ID);
  42. Redis::incr($rKey, $amount);
  43. Redis::expire($rKey, 86400*60);
  44. self::add($Higher1ID, $Higher2ID, $DirectRebate1, $UserID, $orderSn);
  45. $dailyKey = 'daily_spread_rebate_stat_'.date('Ymd');
  46. Redis::incr($dailyKey, $DirectRebate1);
  47. Redis::expire($dailyKey, 86400*3);
  48. }
  49. // 2、找出上上级ID
  50. if ($Higher2ID > 0) {
  51. // 充值金额 * 百分比例
  52. $AgentRebateRatio2 = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')->where('StatusName', 'AgentRebateRatio2')->select('StatusValue')->first()->StatusValue / NumConfig::NUM_VALUE ?? 0;
  53. $DirectRebate2 = $amount * $AgentRebateRatio2;
  54. $rKey = sprintf(ExtensionCopy::RECHARGE_PEOPLE_SETS, $Higher2ID);
  55. Redis::sAdd($rKey, $UserID);
  56. Redis::expire($rKey, 86400*60);
  57. $rKey = sprintf(ExtensionCopy::RECHARGE, $Higher2ID);
  58. Redis::incr($rKey, $amount);
  59. Redis::expire($rKey, 86400*60);
  60. self::add($Higher2ID, 0, $DirectRebate2, $UserID, $orderSn, 2);
  61. $dailyKey = 'daily_spread_rebate_stat_'.date('Ymd');
  62. Redis::incr($dailyKey, $DirectRebate2);
  63. Redis::expire($dailyKey, 86400*3);
  64. }
  65. // 修改推广奖励数据
  66. $data = [
  67. 'TotalReward1' => $DirectRebate1 + $UserAgent->TotalReward1,
  68. 'Balance1' => $DirectRebate1 + $UserAgent->Balance1,
  69. 'TotalReward2' => $DirectRebate2 + $UserAgent->TotalReward2,
  70. 'Balance2' => $DirectRebate2 + $UserAgent->Balance2,
  71. ];
  72. DB::connection('write')->table('QPAccountsDB.dbo.UserAgent')->where('UserID', $UserID)->update($data);
  73. // 增加总奖励
  74. $TotalReward = $DirectRebate1 + $DirectRebate2;
  75. DB::connection('write')->table('QPAccountsDB.dbo.SystemAgentReward')->increment('TotalReward', $TotalReward);
  76. }
  77. }
  78. public static function add($UserID, $SpreaderID, $DirectRebate, $SourceUserID, $OrderSn, $Level = 1, $orderIds = '', $Type = 3)
  79. {
  80. $data = [
  81. 'UserID' => $UserID,
  82. 'SpreaderID' => $SpreaderID,
  83. 'DirectRebate' => $DirectRebate,
  84. 'SourceUserID' => $SourceUserID,
  85. 'OrderSn' => $OrderSn,
  86. 'Level' => $Level,
  87. 'orderIds' => $orderIds,
  88. 'Type' => $Type
  89. ];
  90. self::query()->insert($data);
  91. }
  92. }