Relation.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. # 关系类
  3. namespace Invite\Lib;
  4. use Dever;
  5. class Relation
  6. {
  7. private $table = 'invite/relation';
  8. # 只记录几级关系
  9. private $total = 10;
  10. # 通用的邀请方法:
  11. # uid 当前用户的上级,需要通过code邀请码来得到
  12. # to_uid 被邀请人,当前登录用户,注册后得到
  13. public function set($uid, $to_uid)
  14. {
  15. $this->setParent($uid, $to_uid);
  16. $this->add($uid, $to_uid, 1);
  17. return true;
  18. }
  19. public function setParent($uid, $to_uid, $level = 1)
  20. {
  21. $parent = $this->getParent($uid);
  22. if ($parent) {
  23. $level = $level + 1;
  24. if ($level > $this->total) {
  25. return;
  26. }
  27. $this->add($parent['uid'], $to_uid, $level);
  28. $this->setParent($parent['uid'], $to_uid, $level);
  29. }
  30. }
  31. # 更换上级
  32. public function replaceParent($uid, $old_parent, $new_parent, $call = true)
  33. {
  34. $info = Dever::db($this->table)->find(['to_uid' => $uid, 'uid' => $old_parent]);
  35. if ($info) {
  36. $state = Dever::db($this->table)->update($info['id'], ['uid' => $new_parent]);
  37. if ($state && $call) {
  38. $child = $this->getChild($uid, false, false);
  39. if ($child) {
  40. foreach ($child as $k => $v) {
  41. $this->replaceParent($v['to_uid'], $old_parent, $new_parent, false);
  42. }
  43. }
  44. }
  45. }
  46. return true;
  47. }
  48. # 重置上级
  49. public function resetParent($uid, $parent)
  50. {
  51. Dever::db($this->table)->delete(['to_uid' => $uid]);
  52. $this->set($parent, $uid);
  53. $child = $this->getChild($uid, 1, false);
  54. if ($child) {
  55. foreach ($child as $k => $v) {
  56. $this->resetParent($v['to_uid'], $uid);
  57. }
  58. }
  59. return true;
  60. }
  61. # 清理邀请关系
  62. public function dropParent($uid, $parent)
  63. {
  64. return Dever::db($this->table)->delete(['uid' => $parent, 'to_uid' => $uid]);
  65. }
  66. # 获取某个用户的上级数据
  67. public function getParent($uid, $level = 1)
  68. {
  69. return Dever::db($this->table)->find(['to_uid' => $uid, 'level' => $level]);
  70. }
  71. # 获取某个用户的所有上级数据
  72. public function getParentAll($uid, $level = false)
  73. {
  74. $where['to_uid'] = $uid;
  75. if ($level) {
  76. $where['level'] = $level;
  77. }
  78. return Dever::db($this->table)->select($where);
  79. }
  80. # 获取某个用户的下级数据
  81. public function getChild($uid, $level = 1, $page = 10)
  82. {
  83. $where['uid'] = $uid;
  84. if ($level) {
  85. $where['level'] = $level;
  86. }
  87. $set = [];
  88. if ($page) {
  89. $set['num'] = $page;
  90. }
  91. return Dever::db($this->table)->select($where, $set);
  92. }
  93. # 获取某个用户在x小时之内的下级数据
  94. public function getChildNum($uid, $level = 1, $start = false, $end = false, $method = 'count')
  95. {
  96. $where['uid'] = $uid;
  97. if ($level) {
  98. $where['level'] = $level;
  99. }
  100. if ($start) {
  101. $where['cdate#'] = ['>=', strtotime($start)];
  102. }
  103. if ($end) {
  104. $where['cdate##'] = ['<=', strtotime($end)];
  105. }
  106. if ($method == 'count') {
  107. $method = 'count';
  108. } else {
  109. $method = 'select';
  110. }
  111. return Dever::db($this->table)->$method($where);
  112. }
  113. # 插入数据
  114. public function add($uid, $to_uid, $level = 1)
  115. {
  116. $data['uid'] = $uid;
  117. $data['to_uid'] = $to_uid;
  118. $data['level'] = $level;
  119. $info = Dever::db($this->table)->find($data);
  120. if (!$info) {
  121. return Dever::db($this->table)->insert($data);
  122. }
  123. return false;
  124. }
  125. }