Search in sources :

Example 1 with CustomException

use of com.ruoyi.common.exception.CustomException in project hocassian-media-matrix by hokaso.

the class GenTableServiceImpl method synchDb.

/**
 * 同步数据库
 *
 * @param tableName 表名称
 */
@Override
@Transactional
public void synchDb(String tableName) {
    GenTable table = genTableMapper.selectGenTableByName(tableName);
    List<GenTableColumn> tableColumns = table.getColumns();
    List<String> tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
    List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
    if (StringUtils.isEmpty(dbTableColumns)) {
        throw new CustomException("同步数据失败,原表结构不存在");
    }
    List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
    dbTableColumns.forEach(column -> {
        if (!tableColumnNames.contains(column.getColumnName())) {
            GenUtils.initColumnField(column, table);
            genTableColumnMapper.insertGenTableColumn(column);
        }
    });
    List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
    if (StringUtils.isNotEmpty(delColumns)) {
        genTableColumnMapper.deleteGenTableColumns(delColumns);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenTableColumn(com.ruoyi.generator.domain.GenTableColumn) VelocityUtils(com.ruoyi.generator.util.VelocityUtils) CharsetKit(com.ruoyi.common.core.text.CharsetKit) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CustomException(com.ruoyi.common.exception.CustomException) Template(org.apache.velocity.Template) LinkedHashMap(java.util.LinkedHashMap) StringUtils(com.ruoyi.common.utils.StringUtils) Service(org.springframework.stereotype.Service) Map(java.util.Map) SecurityUtils(com.ruoyi.common.utils.SecurityUtils) ZipEntry(java.util.zip.ZipEntry) FileUtils(com.ruoyi.common.utils.file.FileUtils) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) GenConstants(com.ruoyi.common.constant.GenConstants) IOException(java.io.IOException) GenUtils(com.ruoyi.generator.util.GenUtils) Collectors(java.util.stream.Collectors) VelocityContext(org.apache.velocity.VelocityContext) GenTableMapper(com.ruoyi.generator.mapper.GenTableMapper) File(java.io.File) VelocityInitializer(com.ruoyi.generator.util.VelocityInitializer) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) JSON(com.alibaba.fastjson.JSON) GenTable(com.ruoyi.generator.domain.GenTable) JSONObject(com.alibaba.fastjson.JSONObject) GenTableColumnMapper(com.ruoyi.generator.mapper.GenTableColumnMapper) Velocity(org.apache.velocity.app.Velocity) Transactional(org.springframework.transaction.annotation.Transactional) Constants(com.ruoyi.common.constant.Constants) GenTable(com.ruoyi.generator.domain.GenTable) GenTableColumn(com.ruoyi.generator.domain.GenTableColumn) CustomException(com.ruoyi.common.exception.CustomException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with CustomException

use of com.ruoyi.common.exception.CustomException in project hocassian-media-matrix by hokaso.

the class GenTableServiceImpl method generatorCode.

/**
 * 生成代码(自定义路径)
 *
 * @param tableName 表名称
 */
@Override
public void generatorCode(String tableName) {
    // 查询表信息
    GenTable table = genTableMapper.selectGenTableByName(tableName);
    // 设置主子表信息
    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 CustomException("渲染模板失败,表名:" + table.getTableName());
            }
        }
    }
}
Also used : StringWriter(java.io.StringWriter) GenTable(com.ruoyi.generator.domain.GenTable) VelocityContext(org.apache.velocity.VelocityContext) IOException(java.io.IOException) CustomException(com.ruoyi.common.exception.CustomException) File(java.io.File) Template(org.apache.velocity.Template)

Example 3 with CustomException

use of com.ruoyi.common.exception.CustomException in project hocassian-media-matrix by hokaso.

the class ExcelUtil method exportExcel.

/**
 * 对list数据源将其里面的数据导入到excel表单
 *
 * @return 结果
 */
public AjaxResult exportExcel() {
    OutputStream out = null;
    try {
        // 取出一共有多少个sheet.
        double sheetNo = Math.ceil(list.size() / sheetSize);
        for (int index = 0; index <= sheetNo; index++) {
            createSheet(sheetNo, index);
            // 产生一行
            Row row = sheet.createRow(0);
            int column = 0;
            // 写入各个字段的列头名称
            for (Object[] os : fields) {
                Excel excel = (Excel) os[1];
                this.createCell(excel, row, column++);
            }
            if (Type.EXPORT.equals(type)) {
                fillExcelData(index, row);
                addStatisticsRow();
            }
        }
        String filename = encodingFilename(sheetName);
        out = new FileOutputStream(getAbsoluteFile(filename));
        wb.write(out);
        return AjaxResult.success(filename);
    } catch (Exception e) {
        log.error("导出Excel异常{}", e.getMessage());
        throw new CustomException("导出Excel失败,请联系网站管理员!");
    } finally {
        if (wb != null) {
            try {
                wb.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}
Also used : Excel(com.ruoyi.common.annotation.Excel) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Row(org.apache.poi.ss.usermodel.Row) CustomException(com.ruoyi.common.exception.CustomException) IOException(java.io.IOException) DataValidationConstraint(org.apache.poi.ss.usermodel.DataValidationConstraint) CustomException(com.ruoyi.common.exception.CustomException) IOException(java.io.IOException)

Example 4 with CustomException

use of com.ruoyi.common.exception.CustomException in project hocassian-media-matrix by hokaso.

the class SysDeptServiceImpl method insertDept.

/**
 * 新增保存部门信息
 *
 * @param dept 部门信息
 * @return 结果
 */
@Override
public int insertDept(SysDept dept) {
    SysDept info = deptMapper.selectDeptById(dept.getParentId());
    // 如果父节点不为正常状态,则不允许新增子节点
    if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
        throw new CustomException("部门停用,不允许新增");
    }
    dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
    return deptMapper.insertDept(dept);
}
Also used : SysDept(com.ruoyi.common.core.domain.entity.SysDept) CustomException(com.ruoyi.common.exception.CustomException)

Example 5 with CustomException

use of com.ruoyi.common.exception.CustomException in project hocassian-media-matrix by hokaso.

the class SysRoleServiceImpl method deleteRoleByIds.

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

Aggregations

CustomException (com.ruoyi.common.exception.CustomException)10 IOException (java.io.IOException)4 GenTable (com.ruoyi.generator.domain.GenTable)3 Transactional (org.springframework.transaction.annotation.Transactional)3 JSONObject (com.alibaba.fastjson.JSONObject)2 GenTableColumn (com.ruoyi.generator.domain.GenTableColumn)2 File (java.io.File)2 StringWriter (java.io.StringWriter)2 Template (org.apache.velocity.Template)2 VelocityContext (org.apache.velocity.VelocityContext)2 JSON (com.alibaba.fastjson.JSON)1 Excel (com.ruoyi.common.annotation.Excel)1 Constants (com.ruoyi.common.constant.Constants)1 GenConstants (com.ruoyi.common.constant.GenConstants)1 SysDept (com.ruoyi.common.core.domain.entity.SysDept)1 SysRole (com.ruoyi.common.core.domain.entity.SysRole)1 SysUser (com.ruoyi.common.core.domain.entity.SysUser)1 LoginUser (com.ruoyi.common.core.domain.model.LoginUser)1 CharsetKit (com.ruoyi.common.core.text.CharsetKit)1 CaptchaException (com.ruoyi.common.exception.user.CaptchaException)1