ExtensionLogic.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\Facade\TableName;
  4. use App\Models\AgentUser;
  5. use App\Models\ExtensionUserProfitLog;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Redis;
  9. class ExtensionLogic
  10. {
  11. // 绑定用户信息
  12. public function userProfit($UserID, $Type = 1)
  13. {
  14. sleep(1);
  15. return $this->invitation($UserID, $Type);
  16. }
  17. // 邀请关系绑定 -- 递归 UserID---被邀请的用户
  18. public function invitation($UserID, $Type)
  19. {
  20. // 添加日志关系表
  21. //$model = new ExtensionUserProfitLog();
  22. // 查询两者用户之间关系
  23. $UserAgent = DB::connection('write')->table('QPAccountsDB.dbo.UserAgent')->where('UserID', $UserID)->first();
  24. if (!$UserAgent) {
  25. Log::channel('invitation')->info('绑定关系表为空');
  26. return apiReturnFail('Invalid user');
  27. }
  28. // if ($Type == 2) {
  29. // $redis = Redis::connection('test');
  30. // $redis->incr($UserID);
  31. // }
  32. // 上级ID
  33. $Higher1ID = $UserAgent->Higher1ID;
  34. // 上上级ID
  35. $Higher2ID = $UserAgent->Higher2ID;
  36. // 上级ID 存在,且不为空 添加一条下级绑定记录
  37. if (!empty($Higher1ID) && $Higher1ID > 0) {
  38. Log::channel('invitation')->info('添加记录:用户ID ' . $Higher1ID . ' Type :1' . ' 被邀请用户:' . $UserID . ' 级别:1');
  39. //$model->add($Higher1ID, $Type, $UserID, 1);
  40. // $this->add($Higher1ID, 1, 0, $UserID);
  41. // 上上级ID 存在,且不为空 添加一条下级绑定记录
  42. // if (!empty($Higher2ID) && $Higher2ID > 0) {
  43. //
  44. // Log::channel('invitation')->info('添加记录:用户ID ' . $Higher2ID . ' Type :1' . ' 被邀请用户:' . $UserID . ' 级别:2');
  45. // $this->add($Higher2ID, 2, $Higher1ID, $UserID);
  46. // }
  47. sleep(1);
  48. } else {
  49. Log::channel('invitation')->info('-----end 结束!!!');
  50. return;
  51. }
  52. }
  53. // 邀请注册,添加记录
  54. public function add($UserID, $level, $SpreaderID, $SourceUserID)
  55. {
  56. $config = DB::connection('write')->table('agent.dbo.extension_config')
  57. ->where('type', 1)
  58. ->first();
  59. if ($level == 1) {
  60. // 下级
  61. $addScore = mt_rand($config->level_quota_min, $config->level_quota_max);
  62. $this->register_invite($UserID, $addScore, $level, $SpreaderID, $SourceUserID);
  63. }
  64. if ($level == 2) {
  65. // 下下级
  66. $addScore = mt_rand($config->two_level_quota_min, $config->two_level_quota_max);
  67. $this->register_invite($UserID, $addScore, $level, $SpreaderID, $SourceUserID);
  68. }
  69. return true;
  70. }
  71. public function register_invite($UserID, $Score, $level, $SpreaderID, $SourceUserID)
  72. {
  73. $Register = DB::table(TableName::agent() . 'register_invite')
  74. ->where('user_id', $UserID)
  75. ->first();
  76. $level1 = $level == 1 ? 1 : 0;
  77. $level2 = $level == 2 ? 1 : 0;
  78. if (!$Register) {
  79. DB::table(TableName::agent() . 'register_invite')
  80. ->insert([
  81. 'user_id' => $UserID,
  82. 'score' => $Score,
  83. 'level1' => $level1,
  84. 'level2' => $level2,
  85. ]);
  86. } else {
  87. DB::table(TableName::agent() . 'register_invite')
  88. ->where('user_id', $UserID)
  89. ->update([
  90. 'score' => DB::raw("score+$Score"),
  91. 'level1' => DB::raw("level1+$level1"),
  92. 'level2' => DB::raw("level2+$level2"),
  93. ]);
  94. }
  95. // 添加领取记录
  96. (new AgentUser())->add($UserID, $SpreaderID, $Score, $SourceUserID, '', 1, '', 1);
  97. }
  98. }