Search in sources :

Example 66 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Oracle by yangzongzhuan.

the class GenTableServiceImpl method generatorCode.

/**
 * 生成代码(自定义路径)
 *
 * @param tableName 表名称
 */
@Override
public void generatorCode(String tableName) {
    // 查询表信息
    GenTable table = genTableMapper.selectGenTableByName(tableName);
    // 获取菜单id序列,用于生成菜单sql语句
    long menuId = genTableMapper.selectMenuId();
    table.setMenuId(menuId);
    // 设置主子表信息
    setSubTable(table);
    // 设置主键列信息
    setPkColumn(table);
    VelocityInitializer.initVelocity();
    VelocityContext context = VelocityUtils.prepareContext(table);
    // 获取模板列表
    List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
    for (String template : templates) {
        if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm")) {
            // 渲染模板
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, Constants.UTF8);
            tpl.merge(context, sw);
            try {
                String path = getGenPath(table, template);
                FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8);
            } catch (IOException e) {
                throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
            }
        }
    }
}
Also used : StringWriter(java.io.StringWriter) ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.project.tool.gen.domain.GenTable) VelocityContext(org.apache.velocity.VelocityContext) IOException(java.io.IOException) File(java.io.File) Template(org.apache.velocity.Template)

Example 67 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Oracle by yangzongzhuan.

the class GenTableServiceImpl method validateEdit.

/**
 * 修改保存参数校验
 *
 * @param genTable 业务信息
 */
@Override
public void validateEdit(GenTable genTable) {
    if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) {
        String options = JSON.toJSONString(genTable.getParams());
        JSONObject paramsObj = JSONObject.parseObject(options);
        if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE))) {
            throw new ServiceException("树编码字段不能为空");
        } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE))) {
            throw new ServiceException("树父编码字段不能为空");
        } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME))) {
            throw new ServiceException("树名称字段不能为空");
        }
    } else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory())) {
        if (StringUtils.isEmpty(genTable.getSubTableName())) {
            throw new ServiceException("关联子表的表名不能为空");
        } else if (StringUtils.isEmpty(genTable.getSubTableFkName())) {
            throw new ServiceException("子表关联的外键名不能为空");
        }
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) ServiceException(com.ruoyi.common.exception.ServiceException)

Example 68 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Oracle by yangzongzhuan.

the class GenTableServiceImpl method importGenTable.

/**
 * 导入表结构
 *
 * @param tableList 导入表列表
 */
@Override
@Transactional
public void importGenTable(List<GenTable> tableList) {
    String operName = SecurityUtils.getUsername();
    try {
        for (GenTable table : tableList) {
            String tableName = table.getTableName();
            GenUtils.initTable(table, operName);
            int row = genTableMapper.insertGenTable(table);
            if (row > 0) {
                // 保存列信息
                List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
                for (GenTableColumn column : genTableColumns) {
                    GenUtils.initColumnField(column, table);
                    genTableColumnMapper.insertGenTableColumn(column);
                }
            }
        }
    } catch (Exception e) {
        throw new ServiceException("导入失败:" + e.getMessage());
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.project.tool.gen.domain.GenTable) GenTableColumn(com.ruoyi.project.tool.gen.domain.GenTableColumn) ServiceException(com.ruoyi.common.exception.ServiceException) IOException(java.io.IOException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 69 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-fast by yangzongzhuan.

the class SysRoleServiceImpl method checkRoleDataScope.

/**
 * 校验角色是否有数据权限
 *
 * @param roleId 角色id
 */
@Override
public void checkRoleDataScope(Long roleId) {
    if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
        SysRole role = new SysRole();
        role.setRoleId(roleId);
        List<SysRole> roles = SpringUtils.getAopProxy(this).selectRoleList(role);
        if (StringUtils.isEmpty(roles)) {
            throw new ServiceException("没有权限访问角色数据!");
        }
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) SysRole(com.ruoyi.project.system.domain.SysRole)

Example 70 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-fast by yangzongzhuan.

the class SysRoleServiceImpl method deleteRoleByIds.

/**
 * 批量删除角色信息
 *
 * @param roleIds 需要删除的角色ID
 * @return 结果
 */
@Override
@Transactional
public int deleteRoleByIds(Long[] roleIds) {
    for (Long roleId : roleIds) {
        checkRoleAllowed(new SysRole(roleId));
        checkRoleDataScope(roleId);
        SysRole role = selectRoleById(roleId);
        if (countUserRoleByRoleId(roleId) > 0) {
            throw new ServiceException(String.format("%1$s已分配,不能删除", role.getRoleName()));
        }
    }
    // 删除角色与菜单关联
    roleMenuMapper.deleteRoleMenu(roleIds);
    // 删除角色与部门关联
    roleDeptMapper.deleteRoleDept(roleIds);
    return roleMapper.deleteRoleByIds(roleIds);
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) SysRole(com.ruoyi.project.system.domain.SysRole) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ServiceException (com.ruoyi.common.exception.ServiceException)109 IOException (java.io.IOException)21 Transactional (org.springframework.transaction.annotation.Transactional)21 GenTable (com.ruoyi.generator.domain.GenTable)12 StringWriter (java.io.StringWriter)12 Template (org.apache.velocity.Template)12 VelocityContext (org.apache.velocity.VelocityContext)12 SysRole (com.ruoyi.common.core.domain.entity.SysRole)10 File (java.io.File)10 SysUser (com.ruoyi.common.core.domain.entity.SysUser)9 GenTableColumn (com.ruoyi.generator.domain.GenTableColumn)8 Before (org.aspectj.lang.annotation.Before)8 JSONObject (com.alibaba.fastjson.JSONObject)6 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)6 SysDept (com.ruoyi.common.core.domain.entity.SysDept)6 LoginUser (com.ruoyi.common.core.domain.model.LoginUser)6 Constants (com.ruoyi.common.constant.Constants)5 GenConstants (com.ruoyi.common.constant.GenConstants)5 StringUtils (com.ruoyi.common.utils.StringUtils)5 SysOss (com.ruoyi.system.domain.SysOss)5