getUserDetail($id); } public static function levelset($post) { $level = $post['level']; if ($level > 4) { return '最高等级4级'; } $user = User::getUserOrEmptyById($post['id']); if (empty($user)) return '用户不存在'; $level_type = 1; if ($level > $user['level']) { $level_type = 2; } Db::startTrans(); try { $userData = ['level' => $level, 'level_type' => $level_type]; User::update($post['id'], $userData); Db::commit(); } catch (\Exception $e) { Db::rollback(); return '失败' . $e->getMessage(); } return true; } public static function lookpidlevel($id) { $path = Db::name('user')->where('id', $id)->value('path'); if (empty($path)) { return [0, []]; } $arr = explode(',', $path); $ids = $arr; $ids = implode(',', $ids); $order = 'field(id,' . $ids . ')'; $where = array(); $where[] = ['uid', 'in', $arr]; sr_log($where); $user = new UserModel(); $count = $user ->withJoin('userData', 'INNER') ->where($where) ->count(); $list = $user ->withJoin('userData', 'INNER') ->where($where) ->order(Db::raw($order)) ->select(); return [$count, $list]; } public static function getExportList($where, $page, $limit) { return (new UserModel()) ->where($where) ->withJoin('userData', 'INNER') ->where($where) ->page($page, $limit) ->order('id', 'desc') ->select() ->toArray(); } public static function recycleCountSet($post) { $user = User::getUserById($post['id']); if (empty($user)) return '用户不存在'; if (empty($post['recycle_count']) || $post['recycle_count'] < 0 || $post['recycle_count'] > 500) { return '回收卡数量格式有误'; } try { $updateData = ['recycle_count' => $post['recycle_count']]; $result = User::update($user['id'], $updateData); if (!$result) return "更新临时卡数量失败"; } catch (\Exception $e) { return '失败' . $e->getMessage(); } return true; } public function getList($page, $limit, $where, $sort, $userMap) { $where[] = ['status', '<>', 3]; $userDao = new User(); $count = $userDao->getCount($where, $userMap); $list = $userDao->getPageList($page, $limit, $where, $sort, $userMap); foreach ($list as &$item) { //团队会员(包括1、2级和自身)的积分账户合计、余额账户合计 // 计算团队成员(包括1、2级和自身)ID $teamIds = $this->getTeamMoneyAndScore($item['id']); // 积分账户合计 $item['score_total'] = $teamIds['score']; // 余额账户合计 // $item['money_total'] = $teamIds['money']; // 绿色积分合计 $item['green_score_total'] = $teamIds['green_score']; } $data = [ 'code' => 0, 'msg' => Db::getLastSql(), 'count' => $count, 'data' => $list, ]; return json($data); } /** * 获取团队自身,一级、二级自己的余额/积分 * @param $uid */ private function getTeamMoneyAndScore($uid) { $cacheKey = 'userLogicTeamIds_' . $uid; if (!Cache::has($cacheKey)) { // 自身 $user = Db::table(User::$table) ->where('id', $uid) ->field(['id', 'score', 'money','green_score']) ->find(); $totalScore = $user['score']; $totalMoney = $user['money']; $totalGreenScore = $user['green_score']; // 一级子用户 Db::table(User::$table) ->where('pid', $uid) ->field(['id', 'score', 'money','green_score']) ->chunk(100, function ($users) use (&$totalScore, &$totalMoney, &$totalGreenScore) { foreach ($users as $user) { $uids[] = $user['id']; $totalScore += $user['score']; $totalMoney += $user['money']; $totalGreenScore += $user['green_score']; } // 二级子用户 Db::table(User::$table) ->whereIn('pid', $uids) ->field(['id', 'score', 'money','green_score']) ->chunk(100, function ($users) use (&$totalScore, &$totalMoney, &$totalGreenScore) { foreach ($users as $user) { $totalScore += $user['score']; $totalMoney += $user['money']; $totalGreenScore += $user['green_score']; } }); }, ['score', 'money','green_score']); Cache::set($cacheKey, ['score' => $totalScore,'green_score'=>$totalGreenScore, 'money' => sprintf("%.2f", $totalMoney)], 5 * 60); } return Cache::get($cacheKey); } /** * 修改用户手机号码 * @param $uid * @param $phone */ public function modifyPhone($uid, $phone) { // 判断手机号码是否使用过 $user = User::getUserById($uid); if (empty($user)) return '用户不存在'; if ($user['mobile'] == $phone) return "请输入新的手机号码"; if (User::getUserByMobile($phone)) return "该手机号码已注册"; // 修改用户信息 try { $updateData = [ 'mobile' => $phone, ]; // 按照账号原规则,手机号码作为用户名,同时修改新的用户名 if ($user['mobile'] == $user['user_name']) { $updateData['user_name'] = $phone; } $result = User::update($uid, $updateData); if (!$result) return "更新手机号码失败,请稍后重试"; } catch (\Exception $e) { return '失败' . $e->getMessage(); } return true; } /** * 修改用户所属上级 * @param $uid * @param $pid */ public function modifypid($uid, $pid) { $user = User::getUserById($uid); if (empty($user)) return '用户不存在'; // 查询pid是否存在用户path中 if ($uid == $pid) { return "不能修改自己为上级"; } if ($user['pid'] == $pid) { return "该用户已所属待变更的上级,不能修改"; } $parentUser = User::getUserById($pid); if (empty($parentUser)) { return '所属上级不存在'; } // 判断降级或升级 // 判断升级,降级、插入 list($operateType, $prefixPath) = $this->getOperateType($user['id'], $user['path'], $pid, $parentUser['path']); Db::startTrans(); try { if ($operateType) { // 死循环,分支 $result = $this->modifypidByResetTopLevel($user['id'], $user['path'], $pid, $parentUser['path'], $prefixPath); } else { // 正常,迭代修改下级 $result = $this->modifypidByUpdateNextLevel($user['id'], $user['path'], $pid, $parentUser['path']); } if (!$result) { Db::rollback(); return "修改用户所属上级失败,请确认用户的层级关系"; } Db::commit(); } catch (\Exception $exception) { Db::rollback(); return "失败:" . $exception->getMessage(); } return true; // 升级,abcdef,e升级到b后 分为两种情况 1.abef 2.abcd // 降级 abcdef,c降级到e后 分为两种情况 1. ecf 2.abcd // 插入 abcdef,插入j到b后 分为两种情况 1.abj 2.abcdef } /** * 修改余额 * @param $post */ public function ModifyMoney($post) { $user = User::getUserById($post['uid']); if (empty($user)) { return "用户不存在"; } Db::startTrans(); try { if ($post['state'] == 1) { $afterMoney = $user['money'] + $post['money']; } else { $afterMoney = $user['money'] - $post['money']; if ($afterMoney < 0) { return "账号余额不足"; } } $afterMoney = round($afterMoney, 2); $moneyLog = [ 'uid' => $post['uid'], 'type' => $post['type'] ?? 0, 'money' => $post['money'], 'create_at' => date('Y-m-d H:i:s'), 'state' => $post['state'] ?? '0', 'from_id' => $post['from_id'] ?? '0', 'before_money' => $user['money'], 'after_money' => $afterMoney, 'uid2' => $post['uid2'] ?? 0, 'free_type' => $post['free_type'] ?? '', 'remark' => $post['remark'], ]; // 子级path变更 MoneyLog::AddMoneyLog($moneyLog); User::UpdateUserMoney($user['id'], $afterMoney); Db::commit(); } catch (\Exception $exception) { Db::rollback(); return "失败:" . $exception->getMessage(); } return true; } /** * 修改积分 * @param $post */ public function ModifyScore($post) { $user = User::getUserById($post['uid']); if (empty($user)) { return "用户不存在"; } Db::startTrans(); try { if ($post['state'] == 1) { $afterScore = $user['score'] + $post['score']; } else { $afterScore = $user['score'] - $post['score']; if ($afterScore < 0) { return "账号积分不足"; } } $moneyLog = [ 'uid' => $post['uid'], 'type' => $post['type'] ?? 0, 'score' => $post['score'], 'create_at' => date('Y-m-d H:i:s'), 'scene' => $post['scene'] ?? 0, 'from_id' => $post['from_id'] ?? '0', 'state' => $post['state'] ?? '0', 'before_score' => $user['score'], 'after_score' => $afterScore, 'uid2' => $post['uid2'] ?? 0, ]; // 子级path变更 ScoreLog::AddScoreLog($moneyLog); User::UpdateUserScore($user['id'], $afterScore); Db::commit(); } catch (\Exception $exception) { Db::rollback(); return "失败:" . $exception->getMessage(); } return true; } /** * 修改绿色积分 * @param $post */ public function ModifyGreenScore($post) { $user = User::getUserById($post['uid']); if (empty($user)) { return "用户不存在"; } Db::startTrans(); try { if ($post['state'] == 1) { $afterScore = $user['green_score'] + $post['score']; } else { $afterScore = $user['green_score'] - $post['score']; if ($afterScore < 0) { return "账号绿色积分不足"; } } $moneyLog = [ 'uid' => $post['uid'], 'type' => $post['type'] ?? 0, 'score' => $post['score'], 'create_at' => date('Y-m-d H:i:s'), 'scene' => $post['scene'] ?? 0, 'from_id' => $post['from_id'] ?? '0', 'state' => $post['state'] ?? '0', 'before_score' => $user['green_score'], 'after_score' => $afterScore, 'uid2' => $post['uid2'] ?? 0, 'remark' => $post['remark'] ?? '', ]; // GreenScoreLogModel::insertgetId($moneyLog); User::UpdateUserGreenScore($user['id'], $afterScore); Db::commit(); } catch (\Exception $exception) { Db::rollback(); return "失败:" . $exception->getMessage(); } return true; } /** * 删除用户 * @param $id */ public function delUser($id) { $user = User::getUserById($id); if (empty($user)) { return "用户不存在"; } if (User::updateStatus($id, 3) !== 1) { return false; } return true; } /* 注册用户数量统计 * @param string $time 时间节点如:2023-03-01 * @return * @throws \think\db\exception\DbException */ public static function getCountByTime($time = '0') { $cacheKey = "caches:user:counts_{$time}"; if (!Cache::has($cacheKey)) { $data = UserModel::where(['status' => 1]) ->where(function ($query) use ($time) { if ($time) { $query->where('reg_time', '>=', $time); } })->count('id'); if ($data) { Cache::set($cacheKey, $data, rand(3, 5)); } return $data; } return Cache::get($cacheKey); } /** * 判断操作类型,true-死循环 false-正常 * @param $pid * @param $path * @return int */ private function getOperateType($uid, $path, $pid, $pUserPath) { // $pUserPathArr = explode(',', $pUserPath); list($isFallLevel, $prefixPath) = $this->isFallLevel($uid, $pid); if ($isFallLevel) { return [true, $prefixPath]; } return [false, null]; } private function modifypidByUpdateNextLevel($id, $path, $pid, $parentUserPath) { $parentUserPathArr = explode(',', $parentUserPath); $newUserPathArr = $parentUserPathArr; $newUserPathArr[] = $pid; $result = $this->iteraMidifyPathByPid($newUserPathArr, $id); if (!$result) return false; // 更新用户Pid,Path return User::modifyUserPidAndPath($id, $pid, implode(',', $newUserPathArr)); } private function iteraMidifyPathByPid($newUserPathArr, $pid) { $newUserPathArr[] = $pid; $result = Db::table(User::$table) ->where([ 'pid' => $pid ]) ->field(['id', 'path']) ->chunk(100, function ($users) use ($pid, $newUserPathArr) { foreach ($users as $user) { User::modifyUserPath($user['id'], implode(',', $newUserPathArr)); $this->iteraMidifyPathByPid($newUserPathArr, $user['id']); } }); return $result; } private function modifypidByResetTopLevel($id, $path, $pid, $parentUserPath, $prefixPath) { $newUserPathArr[] = $pid; $this->updatePidAndPathByPath($id, $prefixPath); $parentUser = User::getUserById($pid); $newUserPathArr = explode(',', $parentUser['path']); $newUserPathArr[] = $pid; User::modifyUserPidAndPath($id, $pid, implode(',', $newUserPathArr)); $this->iteraMidifyPathByPid($newUserPathArr, $id); return true; // $result = User::modifyUserPidAndPath($pid, 0, ''); // if (!$result) return false; // return User::modifyUserPidAndPath($id, $pid, implode(',', $newUserPathArr)); } private function isFallLevel($uid, $pid) { $user = Db::query(sprintf("SELECT id,path,pid FROM db_user WHERE FIND_IN_SET('%s',path) and FIND_IN_SET('%s',path) LIMIT 1;", $uid, $pid)); if (empty($user)) { return [false, null]; } $userPath = explode(',', $user[0]['path']); $uidIndex = array_search($uid, $userPath); if ($uidIndex < array_search($pid, $userPath)) { $returnPath = []; if (isset($userPath[$uidIndex - 1])) { $returnPath[] = $userPath[$uidIndex - 1]; } $returnPath[] = $userPath[$uidIndex]; $returnPath[] = $userPath[$uidIndex + 1]; return [true, $returnPath]; } return [false, null]; } private function updatePidAndPathByPath($uid, $prefixPath) { $newTopUid = $prefixPath[count($prefixPath) - 1]; // 从阶段处更新,分支出来作为第一层 User::modifyUserPidAndPath($newTopUid, 0, ''); User::modifyUserPidAndPathByPid($uid, 0, ''); $newUserPathArr = []; return $this->iteraMidifyPathByPid($newUserPathArr, $newTopUid); } }