order('add_time', 'desc')->find(); if ($spread_uid == 0) { $parent_last_point = self::order('add_time', 'asc')->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(); } $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 == 11) { // $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; $i = 0; while ($info && $i < 12) { $info->get += 130; $res = $res && $info->save(); $info = self::where('id', $info['parent_id'])->find(); $i++; } return $res; } }