TreeRecommend.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 TreeRecommend extends BaseModel
  13. {
  14. /**
  15. * 数据表主键
  16. * @var string
  17. */
  18. protected $pk = 'id';
  19. /**
  20. * 模型名称
  21. * @var string
  22. */
  23. protected $name = 'tree_recommend';
  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, $way = 1)
  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. while (1) {
  63. //看线
  64. $res = self::where('way', $way)->where('parent_id', $parent_last_point['id'])->find();
  65. if ($res) {
  66. for ($i = 0; ; $i++) {
  67. if ($i == self::$layers - 1) {
  68. $spreads = User::where('spread_uid', $parent_last_point['uid'])->column('uid');
  69. if (count(self::where('uid', 'in', $spreads)->group('uid')->field('uid,COUNT(uid)')->select()) + count(self::where('uid', $parent_last_point['uid'])->select()) - 1 >= $way) {
  70. break;
  71. }
  72. }
  73. $id = $res['id'];
  74. $res = self::where('way', 1)->where('parent_id', $id)->find();
  75. if (!$res) {
  76. return [$id, 1];
  77. }
  78. }
  79. $way++;
  80. } else {
  81. return [$parent_last_point['id'], $way];
  82. }
  83. }
  84. }
  85. /**
  86. * 进场
  87. * @param int $uid
  88. * @param int $spread_uid
  89. * @return bool
  90. * @throws DataNotFoundException
  91. * @throws DbException
  92. * @throws ModelNotFoundException
  93. */
  94. public static function insertTree(int $uid, int $spread_uid = 0, $way = 1): bool
  95. {
  96. $res = self::getTreePoint($spread_uid, $way);
  97. if (!$res) return self::setErrorInfo(self::getErrorInfo());
  98. list($tree_leader, $way) = $res;
  99. self::beginTrans();
  100. try {
  101. $res = self::create([
  102. 'uid' => $uid,
  103. 'parent_id' => $tree_leader,
  104. 'way' => $way,
  105. 'add_time' => time(),
  106. ]);
  107. $res = $res && self::sendToUper($tree_leader, $res->id);
  108. self::checkTrans($res);
  109. if ($res) {
  110. return true;
  111. } else {
  112. return self::setErrorInfo('加入失败');
  113. }
  114. } catch (\Exception $e) {
  115. self::rollbackTrans();
  116. return self::setErrorInfo($e->getMessage());
  117. }
  118. }
  119. /**
  120. * 发奖
  121. * @param int $point
  122. * @return bool
  123. * @throws DataNotFoundException
  124. * @throws DbException
  125. * @throws ModelNotFoundException
  126. */
  127. public static function sendToUper(int $point, $link_id): bool
  128. {
  129. $info = self::where('id', $point)->find();
  130. $res = true;
  131. $i = 0;
  132. while ($info && $i < self::$layers) {
  133. $info->get += (float)self::$get;
  134. $res = $res && $info->save();
  135. $balance = User::where('uid', $info['uid'])->value('brokerage_price');
  136. $res = $res && UserBill::income('市场分红', $info['uid'], 'now_money', 'brokerage', self::$get, $link_id, $balance + self::$get, '下级用户参与分红,获得分红' . self::$get);
  137. $res = $res && User::where('uid', $info['uid'])->inc('brokerage_price', self::$get)->update();
  138. $info = self::where('id', $info['parent_id'])->find();
  139. $i++;
  140. }
  141. return $res;
  142. }
  143. }