UserJobs.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * 会员相关队列
  4. */
  5. namespace jobs;
  6. use app\common\model\UserFansModel;
  7. use app\common\model\UserModel;
  8. use think\Exception;
  9. use think\facade\Db;
  10. class UserJobs extends BaseJob
  11. {
  12. protected $model = null;
  13. public function __construct (UserModel $model)
  14. {
  15. $this->model = $model;
  16. }
  17. /**
  18. * 绑定用户关系和添加用户静态数据
  19. * @param string $invite_code
  20. * @param int $uid
  21. * @return bool
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public function userRelation (string $invite_code, int $uid)
  27. {
  28. $data['px_pid'] = $data['pid'] = $data['is_px'] = 0;
  29. if (($up_user = $this->model->where('code', $invite_code)->field('id')->lock(true)->find()) !== false && !empty($up_user)) { // 直推码
  30. $data['pid'] = $up_user['id']; // pid
  31. $relation = $this->model->where('id', $up_user['id'])->value('path');
  32. $data['path'] = $relation . (empty($relation)?'':',') . $up_user['id'];
  33. } elseif (($up_user = $this->model->where('px_code', $invite_code)->field('id')->lock(true)->find()) !== false && !empty($up_user)) { // 排线码
  34. $last_user = $this->model->where('id', $up_user['id'])->field('uid,px_pid,path')->order('id desc')->find(); // 排线最后一位
  35. if (!empty($last_user)) { // 如果有则在该用户下继续排线
  36. $data['pid'] = $last_user['id']; // 上级id
  37. $data['path'] = $last_user['path'] . ',' . $last_user['id'];
  38. } else { // 没有就开启新的排线
  39. $relation = $this->model->where('id', $up_user['id'])->value('path');
  40. $data['pid'] = $up_user['id']; // pid
  41. $data['path'] = $relation . ',' . $up_user['id'];
  42. }
  43. $data['px_pid'] = $up_user['id']; // 排线pid
  44. $data['is_px'] = 1; // 标记排线
  45. }
  46. $data['path'] = trim_string($data['path']);
  47. $data['nickname'] = '会员_' . $uid;
  48. if (!isset($data['path']) || empty($data['path']))
  49. throw new Exception('注册失败');
  50. $this->model->where('id', $uid)->save($data);
  51. $data['path'] .= ',' . $uid;
  52. $data['path'] = trim_string($data['path']);
  53. $this->model->where('id', 'in', $data['path'])->save(['total_number' => ['inc', 1]]);
  54. return !0;
  55. }
  56. // invite_code:注册的邀请码 uid:注册用户的 uid
  57. public function userRelationRegister (string $invite_code, int $uid)
  58. {
  59. $model = new UserModel();
  60. $modelfans = new UserFansModel();
  61. Db::startTrans();
  62. try {
  63. $curid = $uid;
  64. while (1){
  65. $cur_user = $model->where(['id' => $curid])->find();
  66. if ($cur_user['pid'] == 0){
  67. break;
  68. }
  69. $curid = $cur_user['pid'];
  70. if (!$modelfans->where(['uid'=>$cur_user['pid'], 'fans_uid'=>$uid])->find()){
  71. $model->where(['id'=>$cur_user['pid']])->save(['total_number' => ['inc', 1]]);
  72. $modelfans->insert(['uid'=>$cur_user['pid'], 'fans_uid'=>$uid, 'create_time'=>sr_getcurtime(time())]);
  73. }
  74. }
  75. Db::commit();
  76. }catch (\Exception $e){
  77. Db::rollback();
  78. sr_testDb($e->getMessage());
  79. Db::name('failed_jobs')->save(['connection'=>'redis', 'queue'=>'aa', 'payload'=>'aa', 'failed_at'=>sr_getcurtime(time()), 'attempts'=>2, 'exception'=>$e->getMessage()]);
  80. }
  81. return true;
  82. }
  83. // invite_code:注册的邀请码 uid:注册用户的 uid
  84. public function userRelationAuth (string $invite_code, int $uid)
  85. {
  86. $model = new UserModel();
  87. Db::startTrans();
  88. try {
  89. $curid = $uid;
  90. while (1){
  91. $cur_user = $model->where(['id' => $curid])->find();
  92. if ($cur_user['pid'] == 0){
  93. break;
  94. }
  95. $model->where(['id'=>$cur_user['pid']])->save(['total_number_real' => ['inc', 1]]);
  96. $curid = $cur_user['pid'];
  97. }
  98. Db::commit();
  99. }catch (\Exception $e){
  100. Db::rollback();
  101. Db::name('failed_jobs')->save(['connection'=>'redis', 'queue'=>'aa', 'payload'=>'aa', 'failed_at'=>sr_getcurtime(time()), 'attempts'=>2, 'exception'=>$e->getMessage()]);
  102. }
  103. return !0;
  104. }
  105. // $uid 当前产生
  106. /**
  107. * 邀新奖励(分30天发放)
  108. * @param int $uid
  109. * @param int $from_uid
  110. * @return bool
  111. */
  112. public function inviteRewards (int $uid, int $from_uid)
  113. {
  114. $user = $this->model->where('id', $uid)->field('level,vip,vip_type')->findOrEmpty();
  115. if (empty($user))
  116. return !0;
  117. if (!$user['level'] && !$user['vip'])
  118. return !0;
  119. if ($user['vip']) {
  120. $invite = get_vip_config($user['vip_type'])['invite'] ?? 0;
  121. } else {
  122. $invite = get_star_config($user['level'])['invite'] ?? 0;
  123. }
  124. if (!$invite)
  125. return !0;
  126. $insert['uid'] = $uid;
  127. $insert['type'] = 1; // 邀新奖励
  128. $insert['total_amount'] = $invite;
  129. $insert['total_days'] = 30;
  130. $insert['surplus_days'] = $insert['total_days'];
  131. $insert['daily_amount'] = $invite / $insert['total_days'];
  132. $insert['from_uid'] = $from_uid;
  133. Db::name('daily_reward')->insert();
  134. return !0;
  135. }
  136. }