Intro.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Model;
  5. /**
  6. * 邀请
  7. */
  8. class User extends Model
  9. {
  10. protected $table = 'user';
  11. /**
  12. * 更新邀请码数和推荐团队人数
  13. * @param string $intro_id 用户id
  14. * @param integer $num 直推增减人数
  15. * @param integer $team 团队增减人数
  16. */
  17. public function addIntroNum($intro_id, $num, $team)
  18. {
  19. //更新直推人数
  20. if($num < 0) {
  21. $this->where(['id' => $intro_id])->setDec('intro_num', abs($num));
  22. } else {
  23. $this->where(['id' => $intro_id])->setInc('intro_num', $num);
  24. }
  25. //更新团队
  26. $rstjr = $this->where(['id' => $intro_id])->field('id,intro_ids')->find();
  27. if($rstjr) {
  28. $tjstr = $rstjr['intro_ids'] . "," . $rstjr['id'];
  29. $tjstr = trim($tjstr, ',');
  30. $arr_intro = explode(',', $tjstr);
  31. if($team < 0) {
  32. $this->where(['id' => ['in', $arr_intro]])->setDec('intro_num_all', abs($team));
  33. } else {
  34. $this->where(['id' => ['in', $arr_intro]])->setInc('intro_num_all', $team);
  35. }
  36. }
  37. }
  38. /**
  39. * 获取上几代推荐人
  40. * @param string $ids 上级id
  41. * @param int $max 代数
  42. * @return false|\PDOStatement|string|\think\Collection
  43. */
  44. public function getUp($ids = '', $max = 1)
  45. {
  46. if($ids) {
  47. $intro_arr = explode(',', $ids);
  48. $intro_arr = array_filter($intro_arr);
  49. $intro_arr = array_slice($intro_arr, -$max);
  50. $info = Db::name('user')
  51. ->where(['id' => ['in', $intro_arr]])
  52. ->field('id,username,nickname,logo,mobile,email,rank')
  53. ->order('intro_level desc') //按级别倒序,级别越大越靠近本人
  54. ->limit($max)
  55. ->select();
  56. }
  57. return $info ?? '';
  58. }
  59. /**
  60. * 获取上几代推荐人
  61. */
  62. function getUpComplex($username, $min = 1, $max = 1, $str = '', $step = 0)
  63. {
  64. if($max >= $step) {
  65. $user = Db::name('user')->where(['username' => $username])->field("username,intro")->find();
  66. if($user['username']) {
  67. if($step >= $min) {
  68. $str .= ",".$user["username"];
  69. }
  70. if($user['intro']) {
  71. return $this->getUpComplex($user['intro'], $min, $max, $str, $step + 1);
  72. }
  73. }
  74. }
  75. return $str;
  76. }
  77. }