Tree.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Tree extends BaseModel
  11. {
  12. use ModelTrait;
  13. protected $name = 'tree';
  14. /**
  15. * 排点
  16. * @param int $uid
  17. * @param int $spread_uid
  18. * @return array|bool|int[]
  19. * @throws DataNotFoundException
  20. * @throws DbException
  21. * @throws ModelNotFoundException
  22. */
  23. public static function getTreePoint()
  24. {
  25. return self::max('tree_num') + 1;
  26. }
  27. /**
  28. * 进场
  29. * @param int $uid
  30. * @param int $spread_uid
  31. * @return bool
  32. * @throws DataNotFoundException
  33. * @throws DbException
  34. * @throws ModelNotFoundException
  35. */
  36. public static function insertTree(int $uid): bool
  37. {
  38. $point = self::getTreePoint();
  39. if (!$point) return self::setErrorInfo(self::getErrorInfo());
  40. self::beginTrans();
  41. try {
  42. $res = self::create([
  43. 'uid' => $uid,
  44. 'tree_num' => $point,
  45. 'add_time' => time(),
  46. ]);
  47. $res = $res && self::sendToUper($point);
  48. self::checkTrans($res);
  49. if ($res) {
  50. return true;
  51. } else {
  52. return self::setErrorInfo('加入失败');
  53. }
  54. } catch (\Exception $e) {
  55. self::rollbackTrans();
  56. return self::setErrorInfo($e->getMessage());
  57. }
  58. }
  59. /**
  60. * 发奖
  61. * @param int $point
  62. * @return bool
  63. * @throws DataNotFoundException
  64. * @throws DbException
  65. * @throws ModelNotFoundException
  66. */
  67. public static function sendToUper(int $point): bool
  68. {
  69. $get = 132;
  70. $info = self::where('tree_num', $point - 1)->find();
  71. $layer = 0;
  72. $up_point = $point - 1;
  73. $res = true;
  74. while ($info && $layer < 12) {
  75. $balance = User::where('uid', $info['uid'])->value('brokerage_price');
  76. $res = $res && UserBill::income('市场分红(佣金)', $info['uid'], 'now_money', 'brokerage', bcmul($get, 0.5, 2), $point, bcadd($balance, bcmul($get, 0.5, 2), 2), '下级用户参与分红,获得分红' . bcmul($get, 0.5, 2));
  77. $res = $res && User::where('uid', $info['uid'])->inc('brokerage_price', bcmul($get, 0.5, 2))->update();
  78. $balance2 = User::where('uid', $info['uid'])->value('integral');
  79. $res = $res && UserBill::income('市场分红(消费券)', $info['uid'], 'integral', 'brokerage_integral', $get, $point, bcadd($balance2, $get, 2), '下级用户参与分红,获得分红' . $get);
  80. $res = $res && User::where('uid', $info['uid'])->inc('integral', $get)->update();
  81. $info->get += $get;
  82. $res = $res && $info->save();
  83. $up_point = $up_point - 1;
  84. $info = self::where('tree_num', $up_point)->find();
  85. $layer++;
  86. }
  87. return $res;
  88. }
  89. }