doum
2 天以前 064d703f2907b931085c5f565269289e9fa81012
server/system_service/src/main/java/com/doumee/service/system/impl/SystemPermissionServiceImpl.java
@@ -1,9 +1,11 @@
package com.doumee.service.system.impl;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.dao.system.dto.DeleteSystemPermissionDTO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.service.business.third.model.PageData;
import com.doumee.service.business.third.model.PageWrap;
import com.doumee.dao.system.SystemPermissionMapper;
import com.doumee.dao.system.dto.QuerySystemPermissionDTO;
import com.doumee.dao.system.model.SystemPermission;
@@ -11,11 +13,14 @@
import com.doumee.service.system.SystemPermissionService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/**
@@ -25,6 +30,7 @@
 */
@Service
public class SystemPermissionServiceImpl implements SystemPermissionService {
    @Autowired
    private SystemPermissionMapper systemPermissionMapper;
@@ -53,6 +59,26 @@
    }
    @Override
    public void delete(DeleteSystemPermissionDTO dto) {
        UpdateWrapper<SystemPermission> deleteWrapper = new UpdateWrapper<>();
        deleteWrapper.lambda()
                .set(SystemPermission::getDeleted, Boolean.TRUE)
                .eq(dto.getId() != null, SystemPermission::getId, dto.getId())
                .likeLeft(dto.getModulePrefix() != null, SystemPermission::getModule, dto.getModulePrefix());
        systemPermissionMapper.update(null, deleteWrapper);
    }
    @Override
    public void deleteInBatch(List<DeleteSystemPermissionDTO> dtos) {
        if (CollectionUtils.isEmpty(dtos)) {
            return;
        }
        for (DeleteSystemPermissionDTO dto : dtos) {
            this.delete(dto);
        }
    }
    @Override
    public void updateById(SystemPermission systemPermission) {
        systemPermissionMapper.updateById(systemPermission);
    }
@@ -61,7 +87,7 @@
    @Transactional
    public void updateByIdInBatch(List<SystemPermission> systemPermissions) {
        if (CollectionUtils.isEmpty(systemPermissions)) return;
        for (SystemPermission systemPermission: systemPermissions) {
        for (SystemPermission systemPermission : systemPermissions) {
            this.updateById(systemPermission);
        }
    }
@@ -83,7 +109,8 @@
    @Override
    public SystemPermission findOne(SystemPermission systemPermission) {
        Wrapper<SystemPermission> wrapper = new QueryWrapper<>(systemPermission);
        QueryWrapper<SystemPermission> wrapper = new QueryWrapper<>(systemPermission);
        wrapper.lambda().last("limit 1");
        return systemPermissionMapper.selectOne(wrapper);
    }
@@ -94,9 +121,46 @@
    }
    @Override
    public PageData<SystemPermissionListVO> findPage(PageWrap<QuerySystemPermissionDTO> pageWrap) {
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        return PageData.from(new PageInfo<>(systemPermissionMapper.selectManageList(pageWrap.getModel(), pageWrap.getOrderByClause())));
    public List<SystemPermissionListVO> findTree() {
        List<SystemPermissionListVO> permissionList = systemPermissionMapper.selectManageList();
        List<SystemPermissionListVO> tree = new ArrayList<>();
        for (SystemPermissionListVO permission : permissionList) {
            // 没有模块的权限默认归属为"其它"模块
            if (StringUtils.isBlank(permission.getModule())) {
                permission.setModule("其它");
            }
            // 分解模块
            String[] modulePaths = permission.getModule().split("/");
            SystemPermissionListVO target = null;
            SystemPermissionListVO parent = null;
            List<String> paths = new ArrayList<>();
            for (String path : modulePaths) {
                paths.add(path);
                // 查找模块
                target = this.getTargetNode(tree, paths);
                // 如果模块不存在,则添加一个子模块
                if (target == null) {
                    target = new SystemPermissionListVO();
                    target.setId(Integer.valueOf(RandomStringUtils.randomNumeric(6)) * -1);
                    target.setLevel(paths.size() - 1);
                    target.setType("module");
                    target.setName(path);
                    target.setModulePath(StringUtils.join(paths, "/"));
                    target.setChildren(new ArrayList<>());
                    if (parent != null) {
                        parent.getChildren().add(target);
                    } else {
                        tree.add(target);
                    }
                }
                parent = target;
            }
            // 将权限添加进模块中
            if (target != null) {
                target.getChildren().add(permission);
            }
        }
        return tree;
    }
    @Override
@@ -104,4 +168,28 @@
        Wrapper<SystemPermission> wrapper = new QueryWrapper<>(systemPermission);
        return systemPermissionMapper.selectCount(wrapper);
    }
    /**
     * 查找目标节点
     * @param tree 节点树
     * @param paths 模块路径
     * @return
     */
    private SystemPermissionListVO getTargetNode(List<SystemPermissionListVO> tree, List<String> paths) {
        List<SystemPermissionListVO> list = tree;
        List<SystemPermissionListVO> nodeStack = new ArrayList<>();
        for (String path : paths) {
            if (list == null) {
                break;
            }
            for (SystemPermissionListVO permission : list) {
                if (path.equals(permission.getName()) && "module".equals(permission.getType())) {
                    nodeStack.add(permission);
                    list = permission.getChildren();
                    break;
                }
            }
        }
        return nodeStack.size() == paths.size() ? nodeStack.get(nodeStack.size() - 1) : null;
    }
}