Search in sources :

Example 26 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Plus by JavaLionLi.

the class GenTableServiceImpl method generatorCode.

/**
 * 生成代码(自定义路径)
 *
 * @param tableName 表名称
 */
@Override
public void generatorCode(String tableName) {
    // 查询表信息
    GenTable table = baseMapper.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.writeUtf8String(sw.toString(), path);
            } catch (Exception e) {
                throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
            }
        }
    }
}
Also used : StringWriter(java.io.StringWriter) ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.generator.domain.GenTable) VelocityContext(org.apache.velocity.VelocityContext) ServiceException(com.ruoyi.common.exception.ServiceException) IOException(java.io.IOException) Template(org.apache.velocity.Template)

Example 27 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Plus by JavaLionLi.

the class GenTableServiceImpl method synchDb.

/**
 * 同步数据库
 *
 * @param tableName 表名称
 */
@Override
@Transactional(rollbackFor = Exception.class)
public void synchDb(String tableName) {
    GenTable table = baseMapper.selectGenTableByName(tableName);
    List<GenTableColumn> tableColumns = table.getColumns();
    Map<String, GenTableColumn> tableColumnMap = tableColumns.stream().collect(Collectors.toMap(GenTableColumn::getColumnName, Function.identity()));
    List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
    if (CollUtil.isEmpty(dbTableColumns)) {
        throw new ServiceException("同步数据失败,原表结构不存在");
    }
    List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
    List<GenTableColumn> saveColumns = new ArrayList<>();
    dbTableColumns.forEach(column -> {
        GenUtils.initColumnField(column, table);
        if (tableColumnMap.containsKey(column.getColumnName())) {
            GenTableColumn prevColumn = tableColumnMap.get(column.getColumnName());
            column.setColumnId(prevColumn.getColumnId());
            if (column.isList()) {
                // 如果是列表,继续保留查询方式/字典类型选项
                column.setDictType(prevColumn.getDictType());
                column.setQueryType(prevColumn.getQueryType());
            }
            if (StringUtils.isNotEmpty(prevColumn.getIsRequired()) && !column.isPk() && (column.isInsert() || column.isEdit()) && ((column.isUsableColumn()) || (!column.isSuperColumn()))) {
                // 如果是(新增/修改&非主键/非忽略及父属性),继续保留必填/显示类型选项
                column.setIsRequired(prevColumn.getIsRequired());
                column.setHtmlType(prevColumn.getHtmlType());
            }
            genTableColumnMapper.updateById(column);
        } else {
            genTableColumnMapper.insert(column);
        }
    });
    if (CollUtil.isNotEmpty(saveColumns)) {
        genTableColumnMapper.insertBatch(saveColumns);
    }
    List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
    if (CollUtil.isNotEmpty(delColumns)) {
        List<Long> ids = delColumns.stream().map(GenTableColumn::getColumnId).collect(Collectors.toList());
        genTableColumnMapper.deleteBatchIds(ids);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) JsonUtils(com.ruoyi.common.utils.JsonUtils) java.util(java.util) TableDataInfo(com.ruoyi.common.core.page.TableDataInfo) GenTableColumn(com.ruoyi.generator.domain.GenTableColumn) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectUtil(cn.hutool.core.util.ObjectUtil) VelocityUtils(com.ruoyi.generator.util.VelocityUtils) ServiceException(com.ruoyi.common.exception.ServiceException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Function(java.util.function.Function) PageQuery(com.ruoyi.common.core.domain.PageQuery) Template(org.apache.velocity.Template) StringUtils(com.ruoyi.common.utils.StringUtils) Service(org.springframework.stereotype.Service) ZipEntry(java.util.zip.ZipEntry) FileUtils(com.ruoyi.common.utils.file.FileUtils) IoUtil(cn.hutool.core.io.IoUtil) GenConstants(com.ruoyi.common.constant.GenConstants) StringWriter(java.io.StringWriter) Dict(cn.hutool.core.lang.Dict) IOException(java.io.IOException) GenUtils(com.ruoyi.generator.util.GenUtils) LoginHelper(com.ruoyi.common.helper.LoginHelper) GenTableMapper(com.ruoyi.generator.mapper.GenTableMapper) VelocityContext(org.apache.velocity.VelocityContext) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) VelocityInitializer(com.ruoyi.generator.util.VelocityInitializer) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) CollUtil(cn.hutool.core.collection.CollUtil) Slf4j(lombok.extern.slf4j.Slf4j) GenTable(com.ruoyi.generator.domain.GenTable) GenTableColumnMapper(com.ruoyi.generator.mapper.GenTableColumnMapper) Velocity(org.apache.velocity.app.Velocity) Constants(com.ruoyi.common.constant.Constants) Transactional(org.springframework.transaction.annotation.Transactional) ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.generator.domain.GenTable) GenTableColumn(com.ruoyi.generator.domain.GenTableColumn) Transactional(org.springframework.transaction.annotation.Transactional)

Example 28 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Vue-Plus by JavaLionLi.

the class SysOssController method upload.

/**
 * 上传OSS对象存储
 */
@ApiOperation("上传OSS对象存储")
@ApiImplicitParams({ @ApiImplicitParam(name = "file", value = "文件", paramType = "query", dataTypeClass = File.class, required = true) })
@SaCheckPermission("system:oss:upload")
@Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
@PostMapping("/upload")
public R<Map<String, String>> upload(@RequestPart("file") MultipartFile file) {
    if (ObjectUtil.isNull(file)) {
        throw new ServiceException("上传文件不能为空");
    }
    SysOss oss = iSysOssService.upload(file);
    Map<String, String> map = new HashMap<>(2);
    map.put("url", oss.getUrl());
    map.put("fileName", oss.getFileName());
    return R.ok(map);
}
Also used : SysOss(com.ruoyi.system.domain.SysOss) ServiceException(com.ruoyi.common.exception.ServiceException) HashMap(java.util.HashMap) Log(com.ruoyi.common.annotation.Log) SaCheckPermission(cn.dev33.satoken.annotation.SaCheckPermission)

Example 29 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project wumei-smart by kerwincui.

the class RateLimiterAspect method doBefore.

@Before("@annotation(rateLimiter)")
public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable {
    String key = rateLimiter.key();
    int time = rateLimiter.time();
    int count = rateLimiter.count();
    String combineKey = getCombineKey(rateLimiter, point);
    List<Object> keys = Collections.singletonList(combineKey);
    try {
        Long number = redisTemplate.execute(limitScript, keys, count, time);
        if (StringUtils.isNull(number) || number.intValue() > count) {
            throw new ServiceException("访问过于频繁,请稍候再试");
        }
        log.info("限制请求'{}',当前请求'{}',缓存key'{}'", count, number.intValue(), key);
    } catch (ServiceException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("服务器限流异常,请稍候再试");
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) JoinPoint(org.aspectj.lang.JoinPoint) ServiceException(com.ruoyi.common.exception.ServiceException) Before(org.aspectj.lang.annotation.Before)

Example 30 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project wumei-smart by kerwincui.

the class UserDetailsServiceImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    SysUser user = userService.selectUserByUserName(username);
    if (StringUtils.isNull(user)) {
        log.info("登录用户:{} 不存在.", username);
        throw new ServiceException("登录用户:" + username + " 不存在");
    } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
        log.info("登录用户:{} 已被删除.", username);
        throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
    } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
        log.info("登录用户:{} 已被停用.", username);
        throw new ServiceException("对不起,您的账号:" + username + " 已停用");
    }
    UserDetails details = createLoginUser(user);
    return createLoginUser(user);
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) SysUser(com.ruoyi.common.core.domain.entity.SysUser) ServiceException(com.ruoyi.common.exception.ServiceException)

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