Relation.php 4.1 KB

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