Relation.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, $call = true)
  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 && $call) {
  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, false);
  41. }
  42. }
  43. } else {
  44. Dever::alert('更新失败');
  45. }
  46. }
  47. return true;
  48. }
  49. # 重置上级 效率较低
  50. public function resetParent($uid, $parent)
  51. {
  52. Dever::db('invite/relation')->delete(array('to_uid' => $uid));
  53. $this->set($parent, $uid);
  54. $child = $this->getChild($uid, 1, false);
  55. if ($child) {
  56. foreach ($child as $k => $v) {
  57. $this->resetParent($v['to_uid'], $uid);
  58. /*
  59. $child_parent = $this->getParentAll($v['to_uid']);
  60. if ($child_parent) {
  61. foreach ($child_parent as $k1 => $v1) {
  62. if ($v1['level'] > $v['level']) {
  63. Dever::db('invite/relation')->update(array('where_id' => $v['id'], 'uid' => $parent));
  64. }
  65. }
  66. }*/
  67. }
  68. }
  69. return true;
  70. }
  71. # 清理邀请关系
  72. public function dropParent($uid, $parent)
  73. {
  74. return Dever::db('invite/relation')->delete(array('to_uid' => $uid, 'uid' => $parent));
  75. }
  76. # 获取某个用户的上级数据
  77. public function getParent($uid, $level = 1)
  78. {
  79. return Dever::db('invite/relation')->one(array('to_uid' => $uid, 'level' => $level));
  80. }
  81. # 获取某个用户的所有上级数据
  82. public function getParentAll($uid, $level = false, $order = 1)
  83. {
  84. $where['to_uid'] = $uid;
  85. if ($level) {
  86. $where['level'] = $level;
  87. }
  88. if ($order == 1) {
  89. return Dever::db('invite/relation')->getParent($where);
  90. } else {
  91. return Dever::db('invite/relation')->getParentLevel($where);
  92. }
  93. }
  94. # 获取某个用户的下级数据
  95. public function getChild($uid, $level = false, $page = true)
  96. {
  97. $where['uid'] = $uid;
  98. if ($level) {
  99. $where['level'] = $level;
  100. }
  101. if ($page) {
  102. return Dever::db('invite/relation')->getChildByPage($where);
  103. } else {
  104. return Dever::db('invite/relation')->getChild($where);
  105. }
  106. }
  107. # 获取某个用户在x小时之内的下级数据
  108. public function getChildNum($uid, $level = 1, $time = false, $curtime = false, $method = 'count')
  109. {
  110. $where['uid'] = $uid;
  111. if ($level) {
  112. $where['level'] = $level;
  113. }
  114. if ($time) {
  115. $time = $time * 3600;
  116. if ($curtime) {
  117. if (strstr($curtime, '-')) {
  118. $curtime = maketime($curtime);
  119. }
  120. $cur = $curtime;
  121. } else {
  122. $cur = time();
  123. }
  124. $where['end'] = $cur + $time;
  125. }
  126. if ($method == 'count') {
  127. $method = 'getChildCount';
  128. } else {
  129. $method = 'getChild';
  130. }
  131. return Dever::db('invite/relation')->$method($where);
  132. }
  133. # 插入数据
  134. public function add($uid, $to_uid, $level = 1)
  135. {
  136. $data['uid'] = $uid;
  137. $data['to_uid'] = $to_uid;
  138. $data['level'] = $level;
  139. $data['clear'] = true;
  140. $info = Dever::db('invite/relation')->one($data);
  141. if (!$info) {
  142. return Dever::db('invite/relation')->insert($data);
  143. }
  144. return false;
  145. }
  146. }