Relation.php 3.9 KB

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