|
|
@@ -4,6 +4,7 @@
|
|
|
namespace app\admin\logic;
|
|
|
|
|
|
|
|
|
+use app\admin\model\dao\ShopOrder;
|
|
|
use app\admin\model\dao\User;
|
|
|
use app\common\model\User as UserModel;
|
|
|
use think\facade\Cache;
|
|
|
@@ -122,4 +123,93 @@ class UserLogic
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 修改用户所属上级
|
|
|
+ * @param mixed $uid
|
|
|
+ * @param mixed $pid
|
|
|
+ */
|
|
|
+ public function modifypid($uid, $pid)
|
|
|
+ {
|
|
|
+ // 查询pid是否存在用户path中
|
|
|
+ $user = User::getUserById($uid);
|
|
|
+
|
|
|
+ if ($uid == $pid) {
|
|
|
+ return "不能修改自己为上级";
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($user['pid'] == $pid) {
|
|
|
+ return "该用户已所属待变更的上级,不能修改";
|
|
|
+ }
|
|
|
+
|
|
|
+ // 当前用户关系层级链路
|
|
|
+ $newUserPath = "";
|
|
|
+
|
|
|
+ if ($pid == 0) {
|
|
|
+ $newPathPrefix = $user['id'];
|
|
|
+
|
|
|
+ } else {
|
|
|
+ $pUser = User::getUserById($pid);
|
|
|
+ if (empty($pUser)) {
|
|
|
+ return "待变更的上级用户不存在";
|
|
|
+ }
|
|
|
+
|
|
|
+ $userPathArr = explode(',', $user['path']);
|
|
|
+ if (in_array($pid, $userPathArr)) {
|
|
|
+ $pidIndex = array_search($pid, $userPathArr);
|
|
|
+ $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex + 1);
|
|
|
+ } else {
|
|
|
+ $pidIndex = array_search($user['pid'], $userPathArr);
|
|
|
+ $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex);
|
|
|
+ $userPathArrPrefix[] = $pid;
|
|
|
+ }
|
|
|
+ $newUserPath = implode(',', $userPathArrPrefix);
|
|
|
+
|
|
|
+ // 子级用户Path待替换关系层级前部分链表
|
|
|
+ $userPathArrPrefix[] = $user['id'];
|
|
|
+ $newPathPrefix = implode(',', $userPathArrPrefix);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 子级用户Path被替换关系层级前部分链表
|
|
|
+ $oldPathPrefix = $user['path'] == 0 ? $uid : $user['path'] . ',' . $uid;
|
|
|
+
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ // 子级path变更
|
|
|
+ $result = Db::table(User::$table)
|
|
|
+ ->where([
|
|
|
+ ['path', 'like', $oldPathPrefix . '%'],
|
|
|
+ ['id', '<>', $uid]
|
|
|
+ ])
|
|
|
+ ->field(['id', 'path'])
|
|
|
+ ->chunk(100, function ($users) use ($pid, $oldPathPrefix, $newPathPrefix, $uid) {
|
|
|
+ foreach ($users as $user) {
|
|
|
+
|
|
|
+ $userPathArr = explode(',', $user['path']);
|
|
|
+ $uidIndex = array_search($uid, $userPathArr);
|
|
|
+ $userPathArrSub = array_slice($userPathArr, $uidIndex);
|
|
|
+ if (in_array($uid, $userPathArrSub)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $newPath = str_replace($oldPathPrefix, $newPathPrefix, $user['path']);
|
|
|
+ User::modifyUserPath($user['id'], $newPath);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (!$result) {
|
|
|
+ Db::rollback();
|
|
|
+ return "修改用户所属上级失败,请确认用户的层级关系";
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新用户Pid,Path
|
|
|
+ User::modifyUserPidAndPath($uid, $pid, $newUserPath);
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+ } catch (\Exception $exception) {
|
|
|
+ Db::rollback();
|
|
|
+ return "失败:" . $exception->getMessage();
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }
|
|
|
}
|