TreeRecommend.php 3.9 KB

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