TreeRecommend.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\models\tree;
  3. use app\models\user\User;
  4. use app\models\user\UserBill;
  5. use crmeb\basic\BaseModel;
  6. use crmeb\traits\ModelTrait;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. class TreeRecommend extends BaseModel
  11. {
  12. /**
  13. * 数据表主键
  14. * @var string
  15. */
  16. protected $pk = 'id';
  17. /**
  18. * 模型名称
  19. * @var string
  20. */
  21. protected $name = 'tree_recommend';
  22. private static $layers;
  23. private static $get;
  24. use ModelTrait;
  25. public function __construct(array $data = [])
  26. {
  27. parent::__construct($data);
  28. self::$layers = 12;
  29. self::$get = 130;
  30. }
  31. /**
  32. * 排点
  33. * @param int $uid
  34. * @param int $spread_uid
  35. * @return array|bool|int[]
  36. * @throws DataNotFoundException
  37. * @throws DbException
  38. * @throws ModelNotFoundException
  39. */
  40. public static function getTreePoint(int $spread_uid = 0)
  41. {
  42. if (!self::count()) {
  43. return [0, 1];//父节点,线路
  44. }
  45. $parent_last_point = self::where('uid', $spread_uid)->order('add_time', 'desc')->find();
  46. if ($spread_uid == 0) {
  47. $parent_last_point = self::order('add_time', 'asc')->find();
  48. }
  49. while (!$parent_last_point) {
  50. $spread_uid = User::where('uid', $spread_uid)->value('spread_uid');
  51. if (!$spread_uid)
  52. $parent_last_point = self::order('add_time', 'asc')->find();
  53. else
  54. $parent_last_point = self::where('uid', $spread_uid)->order('add_time', 'desc')->find();
  55. }
  56. $way = 1;
  57. while (1) {
  58. //看线
  59. $res = self::where('way', $way)->where('parent_id', $parent_last_point['id'])->find();
  60. if ($res) {
  61. for ($i = 0; ; $i++) {
  62. if ($i == self::$layers - 1) {
  63. $spreads = User::where('spread_uid', $parent_last_point['uid'])->column('uid');
  64. if (count(self::where('uid', 'in', $spreads)->group('uid')->field('uid,COUNT(uid)')->select()) >= $way) {
  65. break;
  66. }
  67. }
  68. $id = $res['id'];
  69. $res = self::where('way', 1)->where('parent_id', $id)->find();
  70. if (!$res) {
  71. return [$id, 1];
  72. }
  73. }
  74. $way++;
  75. } else {
  76. return [$parent_last_point['id'], $way];
  77. }
  78. }
  79. }
  80. /**
  81. * 进场
  82. * @param int $uid
  83. * @param int $spread_uid
  84. * @return bool
  85. * @throws DataNotFoundException
  86. * @throws DbException
  87. * @throws ModelNotFoundException
  88. */
  89. public static function insertTree(int $uid, int $spread_uid = 0): bool
  90. {
  91. $res = self::getTreePoint($spread_uid);
  92. if (!$res) return self::setErrorInfo(self::getErrorInfo());
  93. list($tree_leader, $way) = $res;
  94. self::beginTrans();
  95. try {
  96. $res = self::create([
  97. 'uid' => $uid,
  98. 'parent_id' => $tree_leader,
  99. 'way' => $way,
  100. 'add_time' => time(),
  101. ]);
  102. $res = $res && self::sendToUper($tree_leader, $res->id);
  103. self::checkTrans($res);
  104. if ($res) {
  105. return true;
  106. } else {
  107. return self::setErrorInfo('加入失败');
  108. }
  109. } catch (\Exception $e) {
  110. self::rollbackTrans();
  111. return self::setErrorInfo($e->getMessage());
  112. }
  113. }
  114. /**
  115. * 发奖
  116. * @param int $point
  117. * @return bool
  118. * @throws DataNotFoundException
  119. * @throws DbException
  120. * @throws ModelNotFoundException
  121. */
  122. public static function sendToUper(int $point, $link_id): bool
  123. {
  124. $info = self::where('id', $point)->find();
  125. $res = true;
  126. $i = 0;
  127. while ($info && $i < self::$layers) {
  128. $info->get += self::$get;
  129. $res = $res && $info->save();
  130. $balance = User::where('uid', $info['uid'])->value('brokerage_price');
  131. $res = $res && UserBill::income('下级参与', $info['uid'], 'now_money', 'brokerage', self::$get, $link_id, $balance + self::$get, '下级用户参与分红,获得分红' . self::$get);
  132. $res = $res && User::where('uid', $info['uid'])->inc('brokerage_price', self::$get)->update();
  133. $info = self::where('id', $info['parent_id'])->find();
  134. $i++;
  135. }
  136. return $res;
  137. }
  138. }