Relation.php 3.8 KB

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