代理层级的处理

2021-05-04  本文已影响0人  云龙789
   /**
     * 获取某个代理下的所有用户
     * @param $agentInfo array
     * @param $topId
     * @return bool
     */
    public function updateAllMyUserAgentPath($agentInfo, $topId)
    {
        $userList = $db->select($this->fields)
            ->where('agentId=:agentId', array(':agentId' => $agentInfo['id']))
            ->from(self::TABLE)->queryAll();
        if (!$userList) {
            return true;
        }

        foreach ($userList as $user) {
            // 1. 更新自己的代理路径
            if ($agentInfo['agentPath']) {
                $path = $agentInfo['agentPath'];
            } else {
                $path = $agentInfo['username'];
            }
            $updatePath = $this->updateAgentPath($user, $topId, $path);
            if ($updatePath) {
                //如果代理线更新成功,则需要更新 $user  变量的 agentPath 值,$topId 不需要更新是因为 updateAgentPath()  方法没有改变这个值
                $user['agentPath'] = $updatePath;
            }
            // 2. 处理自己的下级代理层级
            $this->updateAllMyUserAgentPath($user, $topId);
        }
        return true;
    }

    /**
     * 更新用户的代理路径和顶级代理ID
     * @param $user User
     * @param $topId int
     * @param $agentPath string
     * @return bool|string
     */
    private function updateAgentPath($user, $topId, $agentPath)
    {
        if ($user['agentId'] == 0) {
            // 顶级和路径都是自己
            $this->update($user['id'], ['agentPath' => $user['username'], 'topAgentId' => $user['id']]);
            return false;
        }

        //  代理是自己的 表示有问题
        if ($user['agentId'] == $user['id']) {
            return false;
        }
        $newPath = $agentPath . '-' . $user['username'];
        $arr = explode('-', $newPath);
        $arr = array_unique($arr);
        $newPath = implode('-', $arr);
        $this->update($user['id'], ['agentPath' => $newPath, 'topAgentId' => $topId]);
        return $newPath;
    }

上一篇 下一篇

猜你喜欢

热点阅读