|
|
@@ -0,0 +1,134 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+
|
|
|
+namespace app\models\tree;
|
|
|
+
|
|
|
+
|
|
|
+use app\models\user\User;
|
|
|
+use crmeb\basic\BaseModel;
|
|
|
+use crmeb\traits\ModelTrait;
|
|
|
+use think\db\exception\DataNotFoundException;
|
|
|
+use think\db\exception\DbException;
|
|
|
+use think\db\exception\ModelNotFoundException;
|
|
|
+
|
|
|
+class TreeRecommend extends BaseModel
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 数据表主键
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $pk = 'id';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 模型名称
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $name = 'tree_recommend';
|
|
|
+
|
|
|
+ use ModelTrait;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 排点
|
|
|
+ * @param int $uid
|
|
|
+ * @param int $spread_uid
|
|
|
+ * @return array|bool|int[]
|
|
|
+ * @throws DataNotFoundException
|
|
|
+ * @throws DbException
|
|
|
+ * @throws ModelNotFoundException
|
|
|
+ */
|
|
|
+ public static function getTreePoint(int $spread_uid = 0)
|
|
|
+ {
|
|
|
+ if (!self::count()) {
|
|
|
+ return [0, 1];//父节点,线路
|
|
|
+ }
|
|
|
+ $parent_last_point = self::where('uid', $spread_uid)->order('add_time', 'desc')->find();
|
|
|
+ while (!$parent_last_point) {
|
|
|
+ $spread_uid = User::where('uid', $spread_uid)->value('spread_uid');
|
|
|
+ if (!$spread_uid)
|
|
|
+ $parent_last_point = self::order('add_time', 'asc')->find();
|
|
|
+ else
|
|
|
+ $parent_last_point = self::where('uid', $spread_uid)->order('add_time', 'desc')->find();
|
|
|
+ }
|
|
|
+ $parent_last_point = $parent_last_point['id'];
|
|
|
+ $way = 1;
|
|
|
+ while (1) {
|
|
|
+ //看线
|
|
|
+ $res = self::where('way', $way)->where('parent_id', $parent_last_point['id'])->find();
|
|
|
+ if ($res) {
|
|
|
+ for ($i = 0; ; $i++) {
|
|
|
+ if ($i == 10) {
|
|
|
+ $spreads = User::where('spread_uid', $parent_last_point['uid'])->column('uid');
|
|
|
+ if (count(self::where('uid', 'in', $spreads)->group('uid')->field('uid,COUNT(uid)')->select()) >= $way) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $id = $res['id'];
|
|
|
+ $res = self::where('way', 1)->where('parent_id', $id)->find();
|
|
|
+ if (!$res) {
|
|
|
+ return [$id, 1];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $way++;
|
|
|
+ } else {
|
|
|
+ return [$parent_last_point['id'], $way];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 进场
|
|
|
+ * @param int $uid
|
|
|
+ * @param int $spread_uid
|
|
|
+ * @return bool
|
|
|
+ * @throws DataNotFoundException
|
|
|
+ * @throws DbException
|
|
|
+ * @throws ModelNotFoundException
|
|
|
+ */
|
|
|
+ public static function insertTree(int $uid, int $spread_uid = 0): bool
|
|
|
+ {
|
|
|
+ $res = self::getTreePoint($spread_uid);
|
|
|
+ if (!$res) return self::setErrorInfo(self::getErrorInfo());
|
|
|
+ list($tree_leader, $way) = $res;
|
|
|
+ self::beginTrans();
|
|
|
+ try {
|
|
|
+ $res = self::create([
|
|
|
+ 'uid' => $uid,
|
|
|
+ 'parent_id' => $tree_leader,
|
|
|
+ 'way' => $way,
|
|
|
+ 'add_time' => time(),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $res = $res && self::sendToUper($tree_leader);
|
|
|
+
|
|
|
+ self::checkTrans($res);
|
|
|
+ if ($res) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return self::setErrorInfo('加入失败');
|
|
|
+ }
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ self::rollbackTrans();
|
|
|
+ return self::setErrorInfo($e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发奖
|
|
|
+ * @param int $point
|
|
|
+ * @return bool
|
|
|
+ * @throws DataNotFoundException
|
|
|
+ * @throws DbException
|
|
|
+ * @throws ModelNotFoundException
|
|
|
+ */
|
|
|
+ public static function sendToUper(int $point): bool
|
|
|
+ {
|
|
|
+ $info = self::where('id', $point)->find();
|
|
|
+ $res = true;
|
|
|
+ while ($info) {
|
|
|
+ $info->get += 130;
|
|
|
+ $res = $res && $info->save();
|
|
|
+ $info = self::where('id', $info['parent_id'])->find();
|
|
|
+ }
|
|
|
+ return $res;
|
|
|
+ }
|
|
|
+}
|