AgentUser.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Models;
  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 = 'QPAccountsDB.dbo.UserAgent';
  11. protected $table = self::TABLE;
  12. public $timestamps = false;
  13. // 推广员奖励
  14. public function reward($UserID, $amount, $orderSn)
  15. {
  16. $UserAgent = DB::connection('write')->table('QPAccountsDB.dbo.UserAgent')->where('UserID', $UserID)->first();
  17. if ($UserAgent) {
  18. // 上级ID
  19. $Higher1ID = $UserAgent->Higher1ID;
  20. // 上上级ID
  21. $Higher2ID = $UserAgent->Higher2ID;
  22. // 设置默认值
  23. $DirectRebate1 = $DirectRebate2 = 0;
  24. // 1、找出充值用户上级ID
  25. if ($Higher1ID > 0) {
  26. // 充值金额 * 百分比例
  27. $AgentRebateRatio1 = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')->where('StatusName', 'AgentRebateRatio1')->select('StatusValue')->first()->StatusValue / NumConfig::NUM_VALUE ?? 0;
  28. $DirectRebate1 = $amount * $AgentRebateRatio1;
  29. $rKey = sprintf(ExtensionCopy::RECHARGE_PEOPLE_SETS, $Higher1ID);
  30. Redis::sAdd($rKey, $UserID);
  31. Redis::expire($rKey, 86400*60);
  32. $rKey = sprintf(ExtensionCopy::RECHARGE, $Higher1ID);
  33. Redis::incr($rKey, $amount);
  34. Redis::expire($rKey, 86400*60);
  35. $this->add($Higher1ID, $Higher2ID, $DirectRebate1, $UserID, $orderSn);
  36. $dailyKey = 'daily_spread_rebate_stat_'.date('Ymd');
  37. Redis::incr($dailyKey, $DirectRebate1);
  38. Redis::expire($dailyKey, 86400*3);
  39. }
  40. // 2、找出上上级ID
  41. if ($Higher2ID > 0) {
  42. // 充值金额 * 百分比例
  43. $AgentRebateRatio2 = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')->where('StatusName', 'AgentRebateRatio2')->select('StatusValue')->first()->StatusValue / NumConfig::NUM_VALUE ?? 0;
  44. $DirectRebate2 = $amount * $AgentRebateRatio2;
  45. $rKey = sprintf(ExtensionCopy::RECHARGE_PEOPLE_SETS, $Higher2ID);
  46. Redis::sAdd($rKey, $UserID);
  47. Redis::expire($rKey, 86400*60);
  48. $rKey = sprintf(ExtensionCopy::RECHARGE, $Higher2ID);
  49. Redis::incr($rKey, $amount);
  50. Redis::expire($rKey, 86400*60);
  51. $this->add($Higher2ID, 0, $DirectRebate2, $UserID, $orderSn, 2);
  52. $dailyKey = 'daily_spread_rebate_stat_'.date('Ymd');
  53. Redis::incr($dailyKey, $DirectRebate2);
  54. Redis::expire($dailyKey, 86400*3);
  55. }
  56. // 修改推广奖励数据
  57. $data = [
  58. 'TotalReward1' => $DirectRebate1 + $UserAgent->TotalReward1,
  59. 'Balance1' => $DirectRebate1 + $UserAgent->Balance1,
  60. 'TotalReward2' => $DirectRebate2 + $UserAgent->TotalReward2,
  61. 'Balance2' => $DirectRebate2 + $UserAgent->Balance2,
  62. ];
  63. DB::connection('write')->table('QPAccountsDB.dbo.UserAgent')->where('UserID', $UserID)->update($data);
  64. // 增加总奖励
  65. $TotalReward = $DirectRebate1 + $DirectRebate2;
  66. DB::connection('write')->table('QPAccountsDB.dbo.SystemAgentReward')->increment('TotalReward', $TotalReward);
  67. }
  68. }
  69. public function add($UserID, $SpreaderID, $DirectRebate, $SourceUserID, $OrderSn, $Level = 1, $orderIds = '', $Type = 3)
  70. {
  71. $data = [
  72. 'UserID' => $UserID,
  73. 'SpreaderID' => $SpreaderID,
  74. 'DirectRebate' => $DirectRebate,
  75. 'SourceUserID' => $SourceUserID,
  76. 'OrderSn' => $OrderSn,
  77. 'Level' => $Level,
  78. 'orderIds' => $orderIds,
  79. 'Type' => $Type
  80. ];
  81. DB::connection('write')->table('QPRecordDB.dbo.RecordUserAgent')->insert($data);
  82. }
  83. }