2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-05-12 9f21969b69703a35bde9d659be19f3e39795010d
ximon-admin/src/main/java/com/sandu/ximon/admin/service/ClientService.java
@@ -7,7 +7,9 @@
import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.ximon.admin.param.AddClientPrarm;
import com.sandu.ximon.admin.param.ClientPrarm;
import com.sandu.ximon.admin.param.PwdParam;
import com.sandu.ximon.admin.param.UpdateClientPrarm;
import com.sandu.ximon.admin.param.UserPwsParm;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.dao.domain.Client;
import com.sandu.ximon.dao.domain.ClientRoleRelation;
@@ -34,12 +36,30 @@
        if (getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, addClientPrarm.getClientName().trim())) != null) {
            throw new BusinessException("该用户名已存在!");
        }
        Long userId = SecurityUtils.getUserId();
        boolean clientId = findClientId();
        //判断是否为二级客户  如果是二级客户则不能继续添加   一级客户为FALSE
        if (SecurityUtils.getClientId() != null && clientId) {
            throw new BusinessException("权限不足,二级客户不能新增用户!");
        }
        Client client = new Client();
        if (addClientPrarm.getClientSuperior() != null && !"".equals(addClientPrarm.getClientSuperior())) {
        if (SecurityUtils.getClientId() != null && !clientId) {
            //一级客户新增用户时 默认为自己名下的二级客户
            client.setSuperiorId(SecurityUtils.getUserId());
            client.setClientSuperior(addClientPrarm.getClientSuperior());
        } else if (addClientPrarm.getClientSuperior() != null && !"".equals(addClientPrarm.getClientSuperior())) {
            //超管
            Client one = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, addClientPrarm.getClientSuperior()));
            if (one != null) {
                client.setSuperiorId(one.getId());
                if (one.getSuperiorId() == null) {
                    client.setSuperiorId(one.getId());
                    client.setClientSuperior(addClientPrarm.getClientSuperior());
                } else {
                    throw new BusinessException("上级客户不能为二级用户");
                }
            } else {
                throw new BusinessException("上级客户不存在");
            }
@@ -56,8 +76,11 @@
        client.setClientName(addClientPrarm.getClientName());
        client.setLinkMan(addClientPrarm.getLinkMan());
//        client.setClientSuperior(addClientPrarm.getClientSuperior());
        client.setMobile(addClientPrarm.getMobile());
        client.setClientSuperior(addClientPrarm.getClientSuperior());
        if (SecurityUtils.getClientId() != null && clientId) {
            client.setClientSuperior(SecurityUtils.getUsername());
        }
        client.setPassword(passwordEncoder.encode(addClientPrarm.getPassword()));
        boolean flag = save(client);
@@ -72,7 +95,7 @@
    }
    public boolean updateClient(Long id, ClientPrarm clientPrarm) {
    public boolean updateClient(Long id, UpdateClientPrarm updateClientPrarm) {
        //判断用户是否存在
        Client one = getById(id);
        if (one == null) {
@@ -80,31 +103,88 @@
        }
        //判断用户名是否重复
        Client client1 = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, clientPrarm.getClientName().trim()));
        Client client1 = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, updateClientPrarm.getClientName().trim()));
        if (client1 != null && !client1.getId().equals(one.getId())) {
            throw new BusinessException("该用户名已存在!");
        }
        //判断上级用户是否存在
        boolean clientId = findClientId();
        Long userId = SecurityUtils.getUserId();
        //判断更改的用户是否是属于自己名下的二级客户
        if (SecurityUtils.getClientId() != null && !clientId) {
            if (SecurityUtils.getUserId() != one.getSuperiorId()) {
                throw new BusinessException("权限不足,不能更改其他客户的所属客户信息!");
            }
        }
        Client client = new Client();
        if (clientPrarm.getClientSuperior() != null && clientPrarm.getClientSuperior().trim().length() != 0) {
            Client superior = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, clientPrarm.getClientSuperior()));
        client.setClientName(updateClientPrarm.getClientName());
        if (SecurityUtils.getClientId() != null && clientId) {
            //一级客户新增用户时 默认为自己名下的二级客户
            client.setSuperiorId(SecurityUtils.getUserId());
            client.setClientSuperior(updateClientPrarm.getClientSuperior());
        } else if (updateClientPrarm.getClientSuperior() != null && updateClientPrarm.getClientSuperior().trim().length() != 0) {
            //超管
            Client superior = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, updateClientPrarm.getClientSuperior()));
            if (superior != null) {
                if (superior.getId().equals(one.getId())) {
                    throw new BusinessException("上级客户为本账户!");
                } else {
                    client.setSuperiorId(superior.getId());
                    client.setClientSuperior(clientPrarm.getClientSuperior());
                    client.setClientSuperior(updateClientPrarm.getClientSuperior());
                }
            } else {
                throw new BusinessException("上级客户不存在");
            }
        }
        client.setId(id);
        client.setClientName(clientPrarm.getClientName());
        client.setLinkMan(clientPrarm.getLinkMan());
        client.setMobile(clientPrarm.getMobile());
//        client.setClientName(updateClientPrarm.getClientName());
        client.setLinkMan(updateClientPrarm.getLinkMan());
        client.setMobile(updateClientPrarm.getMobile());
        // update(client);
        return updateById(client);
    }
    /**
     * 修改当前登录用户密码
     *
     * @param param
     * @return
     */
    public boolean resetPassword(PwdParam param) {
        Client client = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, SecurityUtils.getUserDetails().getUserId()));
        if (client == null) {
            throw new BusinessException("该用户不存在!");
        }
        //判断旧密码与数据库是否一致
        if (passwordEncoder.matches(param.getOldPass(), client.getPassword())) {
            //加密新密码
            String encode = passwordEncoder.encode(param.getNewPass());
            client.setPassword(encode);
            return updateById(client);
        } else {
            throw new BusinessException("旧密码不正确,请重新确认密码!");
        }
    }
    /**
     * 修改管理员的密码
     *
     * @param param
     * @return
     */
    public boolean updateAdminPassword(UserPwsParm param) {
        Client client = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, param.getUserid()));
        if (client == null) {
            throw new BusinessException("用户不存在");
        }
        //加密新密码
        client.setPassword(passwordEncoder.encode(param.getNewPass()));
        return updateById(client);
    }
@@ -152,6 +232,23 @@
    }
    /**
     * \
     * 其他类用来查找客户id使用  如果没有上级客户 这返回用户ID
     *
     * @param
     * @return
     */
    public Long getClientId(Long userId) {
        Client one = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, userId));
        if (one != null && one.getSuperiorId() != null) {
            return one.getSuperiorId();
        } else {
            return userId;
        }
    }
    /**
     * 一级客户返回false  二级客户返回true
     *
     * @return
@@ -166,4 +263,34 @@
        }
    }
    /**
     * 一级客户返回false  二级客户返回true
     *
     * @return
     */
    public boolean findClientId(Long userId) {
        Client one = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, userId));
        if (one != null && one.getSuperiorId() != null) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 修改普通用户头像
     *
     * @param userId
     * @param IconUrl
     * @return
     */
    public boolean updateIcon(Long userId, String IconUrl) {
        Client one = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, userId));
        if (one == null) {
            throw new BusinessException("用户不存在");
        }
        one.setIcon(IconUrl);
        return updateById(one);
    }
}