TreeNew.php 4.8 KB

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