Relation.php 4.8 KB

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