TreeRecommend.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. while (!$parent_last_point) {
  38. $spread_uid = User::where('uid', $spread_uid)->value('spread_uid');
  39. if (!$spread_uid)
  40. $parent_last_point = self::order('add_time', 'asc')->find();
  41. else
  42. $parent_last_point = self::where('uid', $spread_uid)->order('add_time', 'desc')->find();
  43. }
  44. $parent_last_point = $parent_last_point['id'];
  45. $way = 1;
  46. while (1) {
  47. //看线
  48. $res = self::where('way', $way)->where('parent_id', $parent_last_point['id'])->find();
  49. if ($res) {
  50. for ($i = 0; ; $i++) {
  51. if ($i == 10) {
  52. $spreads = User::where('spread_uid', $parent_last_point['uid'])->column('uid');
  53. if (count(self::where('uid', 'in', $spreads)->group('uid')->field('uid,COUNT(uid)')->select()) >= $way) {
  54. break;
  55. }
  56. }
  57. $id = $res['id'];
  58. $res = self::where('way', 1)->where('parent_id', $id)->find();
  59. if (!$res) {
  60. return [$id, 1];
  61. }
  62. }
  63. $way++;
  64. } else {
  65. return [$parent_last_point['id'], $way];
  66. }
  67. }
  68. }
  69. /**
  70. * 进场
  71. * @param int $uid
  72. * @param int $spread_uid
  73. * @return bool
  74. * @throws DataNotFoundException
  75. * @throws DbException
  76. * @throws ModelNotFoundException
  77. */
  78. public static function insertTree(int $uid, int $spread_uid = 0): bool
  79. {
  80. $res = self::getTreePoint($spread_uid);
  81. if (!$res) return self::setErrorInfo(self::getErrorInfo());
  82. list($tree_leader, $way) = $res;
  83. self::beginTrans();
  84. try {
  85. $res = self::create([
  86. 'uid' => $uid,
  87. 'parent_id' => $tree_leader,
  88. 'way' => $way,
  89. 'add_time' => time(),
  90. ]);
  91. $res = $res && self::sendToUper($tree_leader);
  92. self::checkTrans($res);
  93. if ($res) {
  94. return true;
  95. } else {
  96. return self::setErrorInfo('加入失败');
  97. }
  98. } catch (\Exception $e) {
  99. self::rollbackTrans();
  100. return self::setErrorInfo($e->getMessage());
  101. }
  102. }
  103. /**
  104. * 发奖
  105. * @param int $point
  106. * @return bool
  107. * @throws DataNotFoundException
  108. * @throws DbException
  109. * @throws ModelNotFoundException
  110. */
  111. public static function sendToUper(int $point): bool
  112. {
  113. $info = self::where('id', $point)->find();
  114. $res = true;
  115. while ($info) {
  116. $info->get += 130;
  117. $res = $res && $info->save();
  118. $info = self::where('id', $info['parent_id'])->find();
  119. }
  120. return $res;
  121. }
  122. }