2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-06-02 93b045e2f659a8dbd424bd8c9beb6525e7a80c05
ximon-admin/src/main/java/com/sandu/ximon/admin/service/ClientService.java
@@ -6,21 +6,24 @@
import com.sandu.common.execption.BusinessException;
import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.param.AddClientPrarm;
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;
import com.sandu.ximon.dao.domain.Role;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.dao.bo.MenuNode;
import com.sandu.ximon.dao.domain.*;
import com.sandu.ximon.dao.enums.AdministratorEnums;
import com.sandu.ximon.dao.mapper.ClientMapper;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Service
@AllArgsConstructor
@@ -37,17 +40,17 @@
        if (getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, addClientPrarm.getClientName().trim())) != null) {
            throw new BusinessException("该用户名已存在!");
        }
//        Long userId = SecurityUtils.getUserId();
        Long userId = SecurityUtils.getUserId();
        boolean clientId = findClientId();
        //判断是否为二级客户  如果是二级客户则不能继续添加
        if (SecurityUtils.getClientId() != null && !clientId) {
        //判断是否为二级客户  如果是二级客户则不能继续添加   一级客户为FALSE
        if (SecurityUtils.getClientId() != null && clientId) {
            throw new BusinessException("权限不足,二级客户不能新增用户!");
        }
        Client client = new Client();
        if (SecurityUtils.getClientId() != null && clientId) {
        if (SecurityUtils.getClientId() != null && !clientId) {
            //一级客户新增用户时 默认为自己名下的二级客户
            client.setSuperiorId(SecurityUtils.getUserId());
            client.setClientSuperior(addClientPrarm.getClientSuperior());
@@ -70,8 +73,8 @@
        if (role == null) {
            throw new BusinessException("角色不存在");
        }
        if (!RoleLevelStatus.NORMAL.getCode().equals(role.getLevel())) {
            throw new BusinessException("无法添加超级管理员或用户管理员");
        if (RoleLevelStatus.SUPER.getCode().equals(role.getLevel())) {
            throw new BusinessException("无法添加超级管理员");
        }
@@ -92,6 +95,15 @@
            throw new BusinessException("添加管理员角色失败");
        }
        /**
         * 添加普通用户 日志记录开始
         */
        String content = "新注册用户:" + addClientPrarm.getClientName();
        StoreOperationRecordsUtils.storeOperationData(null, null, "添加普通用户", content);
        /**
         * 添加普通用户 日志记录结束
         */
        return flag;
    }
@@ -108,17 +120,19 @@
        if (client1 != null && !client1.getId().equals(one.getId())) {
            throw new BusinessException("该用户名已存在!");
        }
        //判断上级用户是否存在
        boolean clientId = findClientId();
        Long userId = SecurityUtils.getUserId();
        //判断更改的用户是否是属于自己名下的二级客户
        if(SecurityUtils.getClientId() != null && clientId){
            if(!Objects.equals(one.getSuperiorId(), SecurityUtils.getUserId())){
        if (SecurityUtils.getClientId() != null && !clientId) {
            if (SecurityUtils.getUserId() != one.getSuperiorId()) {
                throw new BusinessException("权限不足,不能更改其他客户的所属客户信息!");
            }
        }
        Client client = new Client();
        client.setClientName(updateClientPrarm.getClientName());
        if (SecurityUtils.getClientId() != null && clientId) {
            //一级客户新增用户时 默认为自己名下的二级客户
            client.setSuperiorId(SecurityUtils.getUserId());
@@ -142,7 +156,14 @@
        client.setLinkMan(updateClientPrarm.getLinkMan());
        client.setMobile(updateClientPrarm.getMobile());
        // update(client);
        /**
         * 编辑普通用户 日志记录开始
         */
        String content = "编辑普通用户:" + client.getClientName() + "用户id:" + id;
        StoreOperationRecordsUtils.storeOperationData(null, null, "编辑普通用户", content);
        /**
         * 编辑普通用户 日志记录结束
         */
        return updateById(client);
    }
@@ -188,11 +209,21 @@
        return updateById(client);
    }
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteClient(Long id) {
        boolean flag = false;
        //判断删除用户是否存在
        Client one = getById(id);
        if (one == null) {
        Client client = getById(id);
        if (client == null) {
            throw new BusinessException("该客户不存在");
        }
        //只有超管能删除  或  上级客户删除自己的下级用户
        if (!AdministratorEnums.ADMIN.getCode().equals(SecurityUtils.getUserDetails().getAdministratorType())) {
            Client temp = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getSuperiorId, SecurityUtils.getUserId()).eq(Client::getId, id));
            if (temp == null) {
                throw new BusinessException("删除的用户不存在或该用户不是你的下级用户");
            }
        }
        //判断删除的用户有无下级用户
@@ -202,7 +233,31 @@
            throw new BusinessException("删除的用户下有下级用户,不允许删除");
        }
        return removeById(id);
        //管理员角色关系表
        ClientRoleRelationService clientRoleRelationService = SpringContextHolder.getBean(ClientRoleRelationService.class);
        //有用户必定有用户与角色的关系数据
        ClientRoleRelation one = clientRoleRelationService.getOne(Wrappers.lambdaQuery(ClientRoleRelation.class).eq(ClientRoleRelation::getClientId, client.getId()));
        //删用户
        if (removeById(id)) {
            //删角色关系表信息
            flag = clientRoleRelationService.removeById(one);
            //删除失败回滚数据
            if (!flag) {
                throw new BusinessException("删除用户失败!");
            }
        }
        /**
         * 删除普通用户 日志记录开始
         */
        String content = "删除普通用户:" + client.getClientName() + "用户id:" + id;
        StoreOperationRecordsUtils.storeOperationData(null, null, "删除普通用户", content);
        /**
         * 删除普通用户 日志记录结束
         */
        return flag;
    }
    public List<Client> clientList(Long userId, BaseConditionVO baseConditionVO) {
@@ -293,4 +348,34 @@
        one.setIcon(IconUrl);
        return updateById(one);
    }
    /**
     * 获取用户权限列表地
     */
    public List<MenuNode> getUserPermissionList() {
        //通过用户UserID获取用户角色
        ClientRoleRelation one = SpringContextHolder.getBean(ClientRoleRelationService.class).
                getOne(Wrappers.lambdaQuery(ClientRoleRelation.class).eq(ClientRoleRelation::getClientId, SecurityUtils.getUserId()));
        //判空
        if (one == null) {
            throw new BusinessException("该用户未绑定角色");
        }
        //通过RoleID获取MeunId列表
        List<RoleMenuRelation> menuIdList = SpringContextHolder.getBean(RoleMenuRelationService.class)
                .list(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId, one.getRoleId()));
        //判空
        if (menuIdList.isEmpty()) {
            return new ArrayList<>();
        }
        List<Long> menuIds = new ArrayList<>(menuIdList.size());
        for (RoleMenuRelation bean : menuIdList) {
            menuIds.add(bean.getMenuId());
        }
        List<MenuNode> resultList = SpringContextHolder.getBean(MenuService.class).getUserPermissionListById(menuIds);
        return resultList;
    }
}