com.ruoyi.common.exception.BusinessException Java Examples

The following examples show how to use com.ruoyi.common.exception.BusinessException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SysDeptServiceImpl.java    From ruoyiplus with MIT License 6 votes vote down vote up
/**
 * 新增保存部门信息
 * 
 * @param dept 部门信息
 * @return 结果
 */
@Override
public int insertDept(SysDept dept)
{
    if(dept.getParentId()==0){
        dept.setAncestors("0");
        return deptMapper.insertDept(dept);
    }else {
        SysDept info = deptMapper.selectDeptById(dept.getParentId());
        // 如果父节点不为"正常"状态,则不允许新增子节点
        if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
            throw new BusinessException("部门停用,不允许新增");
        }
        dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
        return deptMapper.insertDept(dept);
    }
}
 
Example #2
Source File: CommonController.java    From RuoYi with Apache License 2.0 6 votes vote down vote up
@RequestMapping("common/download")
@ApiOperation(value = "通用下载文件")
@ApiImplicitParams({
        @ApiImplicitParam(name = "fileName",value = "文件名",required = true),
        @ApiImplicitParam(name = "delete",value = "是否删除临时文件",required = true,dataType ="boolean")
})
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
    String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf('_') + 1);
    try {
        if (!FileUtils.isValidFilename(fileName)){
            throw new BusinessException(String.format(" 文件名称(%s)非法,不允许下载。 ", fileName));
        }
        String filePath = Global.getDownloadPath() + fileName;

        response.setCharacterEncoding(CharsetKit.UTF8);
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
        FileUtils.writeBytes(filePath, response.getOutputStream());
        if (delete) {
            FileUtils.deleteFile(filePath);
        }
    } catch (Exception e) {
        log.error("下载文件失败", e);
    }
}
 
Example #3
Source File: SysDictTypeServiceImpl.java    From ruoyiplus with MIT License 6 votes vote down vote up
/**
 * 批量删除字典类型
 * 
 * @param ids 需要删除的数据
 * @return 结果
 */
@Override
public int deleteDictTypeByIds(String ids) throws BusinessException
{
    Long[] dictIds = Convert.toLongArray(ids);
    for (Long dictId : dictIds)
    {
        SysDictType dictType = selectDictTypeById(dictId);
        if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0)
        {
            throw new BusinessException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
        }
    }

    return dictTypeMapper.deleteDictTypeByIds(dictIds);
}
 
Example #4
Source File: GenTableServiceImpl.java    From supplierShop with MIT License 6 votes vote down vote up
/**
 * 修改保存参数校验
 * 
 * @param genTable 业务信息
 */
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 BusinessException("树编码字段不能为空");
        }
        else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE)))
        {
            throw new BusinessException("树父编码字段不能为空");
        }
        else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME)))
        {
            throw new BusinessException("树名称字段不能为空");
        }
    }
}
 
Example #5
Source File: SysDictTypeServiceImpl.java    From supplierShop with MIT License 6 votes vote down vote up
/**
 * 批量删除字典类型
 * 
 * @param ids 需要删除的数据
 * @return 结果
 */
@Override
public int deleteDictTypeByIds(String ids) throws BusinessException
{
    Long[] dictIds = Convert.toLongArray(ids);
    for (Long dictId : dictIds)
    {
        SysDictType dictType = selectDictTypeById(dictId);
        if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0)
        {
            throw new BusinessException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
        }
    }

    return dictTypeMapper.deleteDictTypeByIds(dictIds);
}
 
Example #6
Source File: GlobalExceptionHandler.java    From supplierShop with MIT License 6 votes vote down vote up
/**
 * 业务异常
 */
@ExceptionHandler(BusinessException.class)
public Object businessException(HttpServletRequest request, BusinessException e)
{
    log.error(e.getMessage(), e);
    if (ServletUtils.isAjaxRequest(request))
    {
        return AjaxResult.error(e.getMessage());
    }
    else
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("errorMessage", e.getMessage());
        modelAndView.setViewName("error/business");
        return modelAndView;
    }
}
 
Example #7
Source File: SysPostServiceImpl.java    From ruoyiplus with MIT License 5 votes vote down vote up
/**
 * 批量删除岗位信息
 * 
 * @param ids 需要删除的数据ID
 * @throws Exception
 */
@Override
public int deletePostByIds(String ids) throws BusinessException
{
    Long[] postIds = Convert.toLongArray(ids);
    for (Long postId : postIds)
    {
        SysPost post = selectPostById(postId);
        if (countUserPostById(postId) > 0)
        {
            throw new BusinessException(String.format("%1$s已分配,不能删除", post.getPostName()));
        }
    }
    return postMapper.deletePostByIds(postIds);
}
 
Example #8
Source File: SysPostServiceImpl.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
/**
 * 批量删除岗位信息
 *
 * @param ids 需要删除的数据ID
 * @throws BusinessException 异常
 */
@Override
public int deletePostByIds(String ids) throws BusinessException {
    Long[] postIds = Convert.toLongArray(ids);
    for (Long postId : postIds) {
        SysPost post = selectPostById(postId);
        if (countUserPostById(postId) > 0) {
            throw new BusinessException(String.format("%1$s已分配,不能删除", post.getPostName()));
        }
    }
    return postMapper.deletePostByIds(postIds);
}
 
Example #9
Source File: SysDictTypeServiceImpl.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
/**
 * 批量删除字典类型
 *
 * @param ids 需要删除的数据
 * @return 结果
 */
@Override
public int deleteDictTypeByIds(String ids){
    Long[] dictIds = Convert.toLongArray(ids);
    for (Long dictId : dictIds) {
        SysDictType dictType = selectDictTypeById(dictId);
        if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0) {
            throw new BusinessException(String.format("%1$s已分配,不能删除" , dictType.getDictName()));
        }
    }
    return dictTypeMapper.deleteDictTypeByIds(dictIds);
}
 
Example #10
Source File: SysRoleServiceImpl.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
/**
 * 批量删除角色信息
 *
 * @param ids 需要删除的数据ID
 * @throws BusinessException 异常
 */
@Override
public int deleteRoleByIds(String ids) {
    Long[] roleIds = Convert.toLongArray(ids);
    for (Long roleId : roleIds) {
        SysRole role = selectRoleById(roleId);
        if (countUserRoleByRoleId(roleId) > 0) {
            throw new BusinessException(String.format("%1$s已分配,不能删除", role.getRoleName()));
        }
    }
    return roleMapper.deleteRoleByIds(roleIds);
}
 
Example #11
Source File: SysDeptServiceImpl.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
/**
 * 新增保存部门信息
 *
 * @param dept 部门信息
 * @return 结果
 */
@Override
public int insertDept(SysDept dept) {
    SysDept info = deptMapper.selectDeptById(dept.getParentId());
    //如果父节点不为"正常"状态,则不允许新增子节点
    if(!UserConstants.DEPT_NORMAL.equals(info.getStatus())){
        throw new BusinessException("上级部门不为正常状态,新增失败!");
    }
    dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
    return deptMapper.insertDept(dept);
}
 
Example #12
Source File: SysUserServiceImpl.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
/**
 * 修改用户状态
 *
 * @param user 用户
 * @return 结果
 */
@Override
public int changeStatus(SysUser user) {
    if (SysUser.isAdmin(user.getUserId())) {
        throw new BusinessException("不允许修改超级管理员用户");
    }
    return userMapper.updateUser(user);
}
 
Example #13
Source File: SysUserServiceImpl.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
/**
 * 批量删除用户信息
 *
 * @param ids 需要删除的数据ID
 * @return 结果
 */
@Override
public int deleteUserByIds(String ids) {
    Long[] userIds = Convert.toLongArray(ids);
    for (Long userId : userIds) {
        if (SysUser.isAdmin(userId)) {
            throw new BusinessException("不允许删除超级管理员用户");
        }
    }
    return userMapper.deleteUserByIds(userIds);
}
 
Example #14
Source File: ExcelUtil.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
/**
 * 对list数据源将其里面的数据导入到excel表单
 *
 * @return 结果
 */
public AjaxResult exportExcel() {
    OutputStream out = null;
    // 产生工作薄对象
    try{
        // 取出一共有多少个sheet.
        double sheetNo = Math.ceil(list.size() / SHEET_SIZE);
        for (int index = 0; index <= sheetNo; index++) {
            createSheet(sheetNo, index);
            // 产生一行
            Row row = sheet.createRow(0);
            // 写入各个字段的列头名称
            this.setCellTitle(fields, row);
            if (Excel.Type.EXPORT.equals(type)){
                fillExcelData(index);
            }
        }
        String filename = encodingFilename(sheetName);
        out = new FileOutputStream(getAbsoluteFile(filename));
        workbook.write(out);
        return AjaxResult.success(filename);
    } catch (Exception e) {
        log.error("导出Excel异常", e);
        throw new BusinessException("导出Excel失败,请联系网站管理员!");
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e1) {
                log.error(e1.getMessage(), e1);
            }
        }
    }
}
 
Example #15
Source File: GlobalExceptionHandler.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
/**
 * 业务异常
 */
@ExceptionHandler(BusinessException.class)
public Object businessException(HttpServletRequest request, BusinessException e) {
    log.error(e.getMessage(), e);
    if (ServletUtils.isAjaxRequest(request)){
        return AjaxResult.error(e.getMessage());
    }else{
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("errorMessage", e.getMessage());
        modelAndView.setViewName("error/business");
        return modelAndView;
    }
}
 
Example #16
Source File: SysUserServiceImpl.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 用户状态修改
 * 
 * @param user 用户信息
 * @return 结果
 */
@Override
public int changeStatus(SysUser user)
{
    if (SysUser.isAdmin(user.getUserId()))
    {
        throw new BusinessException("不允许修改超级管理员用户");
    }
    return userMapper.updateUser(user);
}
 
Example #17
Source File: StoreGoodsCateServiceImpl.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 删除商城商品分类信息
 * 
 * @param id 商城商品分类ID
 * @return 结果
 */
public int deleteStoreGoodsCateById(Long id)
{
    StoreGoodsCate goodsCate = new StoreGoodsCate();
    goodsCate.setPid(id);
    List<StoreGoodsCate> goodsCates = storeGoodsCateMapper
            .selectStoreGoodsCateList(goodsCate);
    if(!goodsCates.isEmpty())  throw new BusinessException("请先删除子分类");
    return storeGoodsCateMapper.deleteStoreGoodsCateById(id);
}
 
Example #18
Source File: SysRoleServiceImpl.java    From ruoyiplus with MIT License 5 votes vote down vote up
/**
 * 批量删除角色信息
 * 
 * @param ids 需要删除的数据ID
 * @throws Exception
 */
@Override
public int deleteRoleByIds(String ids) throws BusinessException
{
    Long[] roleIds = Convert.toLongArray(ids);
    for (Long roleId : roleIds)
    {
        SysRole role = selectRoleById(roleId);
        if (countUserRoleByRoleId(roleId) > 0)
        {
            throw new BusinessException(String.format("%1$s已分配,不能删除", role.getRoleName()));
        }
    }
    return roleMapper.deleteRoleByIds(roleIds);
}
 
Example #19
Source File: SysUserServiceImpl.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 批量删除用户信息
 * 
 * @param ids 需要删除的数据ID
 * @return 结果
 */
@Override
public int deleteUserByIds(String ids) throws BusinessException
{
    Long[] userIds = Convert.toLongArray(ids);
    for (Long userId : userIds)
    {
        if (SysUser.isAdmin(userId))
        {
            throw new BusinessException("不允许删除超级管理员用户");
        }
    }
    return userMapper.deleteUserByIds(userIds);
}
 
Example #20
Source File: SysUserServiceImpl.java    From ruoyiplus with MIT License 5 votes vote down vote up
/**
 * 用户状态修改
 * 
 * @param user 用户信息
 * @return 结果
 */
@Override
public int changeStatus(SysUser user)
{
    if (SysUser.isAdmin(user.getUserId()))
    {
        throw new BusinessException("不允许修改超级管理员用户");
    }
    return userMapper.updateUser(user);
}
 
Example #21
Source File: SysUserServiceImpl.java    From ruoyiplus with MIT License 5 votes vote down vote up
/**
 * 批量删除用户信息
 * 
 * @param ids 需要删除的数据ID
 * @return 结果
 */
@Override
public int deleteUserByIds(String ids) throws BusinessException
{
    Long[] userIds = Convert.toLongArray(ids);
    for (Long userId : userIds)
    {
        if (SysUser.isAdmin(userId))
        {
            throw new BusinessException("不允许删除超级管理员用户");
        }
    }
    return userMapper.deleteUserByIds(userIds);
}
 
Example #22
Source File: DefaultExceptionHandler.java    From ruoyiplus with MIT License 5 votes vote down vote up
/**
 * 业务异常
 */
@ExceptionHandler(BusinessException.class)
public AjaxResult businessException(BusinessException e)
{
    log.error(e.getMessage(), e);
    return AjaxResult.error(e.getMessage());
}
 
Example #23
Source File: SysPostServiceImpl.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 批量删除岗位信息
 * 
 * @param ids 需要删除的数据ID
 * @throws Exception
 */
@Override
public int deletePostByIds(String ids) throws BusinessException
{
    Long[] postIds = Convert.toLongArray(ids);
    for (Long postId : postIds)
    {
        SysPost post = selectPostById(postId);
        if (countUserPostById(postId) > 0)
        {
            throw new BusinessException(String.format("%1$s已分配,不能删除", post.getPostName()));
        }
    }
    return postMapper.deletePostByIds(postIds);
}
 
Example #24
Source File: SysRoleServiceImpl.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 批量删除角色信息
 * 
 * @param ids 需要删除的数据ID
 * @throws Exception
 */
@Override
public int deleteRoleByIds(String ids) throws BusinessException
{
    Long[] roleIds = Convert.toLongArray(ids);
    for (Long roleId : roleIds)
    {
        SysRole role = selectRoleById(roleId);
        if (countUserRoleByRoleId(roleId) > 0)
        {
            throw new BusinessException(String.format("%1$s已分配,不能删除", role.getRoleName()));
        }
    }
    return roleMapper.deleteRoleByIds(roleIds);
}
 
Example #25
Source File: SysDeptServiceImpl.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 新增保存部门信息
 * 
 * @param dept 部门信息
 * @return 结果
 */
@Override
public int insertDept(SysDept dept)
{
    SysDept info = deptMapper.selectDeptById(dept.getParentId());
    // 如果父节点不为"正常"状态,则不允许新增子节点
    if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
    {
        throw new BusinessException("部门停用,不允许新增");
    }
    dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
    return deptMapper.insertDept(dept);
}
 
Example #26
Source File: SysUserServiceImpl.java    From ruoyiplus with MIT License 4 votes vote down vote up
/**
 * 导入用户数据
 * 
 * @param userList 用户数据列表
 * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
 * @param operName 操作用户
 * @return 结果
 */
@Override
public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName)
{
    if (StringUtils.isNull(userList) || userList.size() == 0)
    {
        throw new BusinessException("导入用户数据不能为空!");
    }
    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
    String password = configService.selectConfigByKey("sys.user.initPassword");
    for (SysUser user : userList)
    {
        try
        {
            // 验证是否存在这个用户
            SysUser u = userMapper.selectUserByLoginName(user.getLoginName());
            if (StringUtils.isNull(u))
            {
                user.setPassword(Md5Utils.hash(user.getLoginName() + password));
                user.setCreateBy(operName);
                this.insertUser(user);
                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 导入成功");
            }
            else if (isUpdateSupport)
            {
                user.setUpdateBy(operName);
                this.updateUser(user);
                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 更新成功");
            }
            else
            {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、账号 " + user.getLoginName() + " 已存在");
            }
        }
        catch (Exception e)
        {
            failureNum++;
            String msg = "<br/>" + failureNum + "、账号 " + user.getLoginName() + " 导入失败:";
            failureMsg.append(msg + e.getMessage());
            log.error(msg, e);
        }
    }
    if (failureNum > 0)
    {
        failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
        throw new BusinessException(failureMsg.toString());
    }
    else
    {
        successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
    }
    return successMsg.toString();
}
 
Example #27
Source File: SysUserServiceImpl.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 导入用户数据
 *
 * @param userList      导入的用户数据列表
 * @param updateSupport 是否更新支持,如果已存在,则进行更新数据
 * @param loginUser     操作用户
 * @return 结果
 */
@Override
public String importUser(List<SysUser> userList, Boolean updateSupport, SysUser loginUser) {
    if (CollectionUtils.isEmpty(userList)) {
        throw new BusinessException("导入用户数据不能为空!");
    }
    int successNum = 0;
    int failureNum = 0;
    final String br = "<br/>";
    //当然操作用户
    String operName = loginUser.getLoginName();
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
    String password = configService.selectConfigByKey("sys.user.initPassword");
    for (SysUser user : userList) {
        try {
            // 验证是否存在这个用户
            SysUser u = userMapper.selectUserByLoginName(user.getLoginName());
            if (ObjectUtil.isNull(u)) {
                user.setPassword(Md5Utils.hash(user.getLoginName() + password));
                user.setCreateBy(operName);
                this.insertUser(user);
                successNum++;
                successMsg.append(br).append(successNum).append("、账号 ").append(user.getLoginName()).append(" 导入成功");
            } else if (updateSupport) {
                user.setUpdateBy(operName);
                this.updateUser(user);
                successNum++;
                successMsg.append(br).append(successNum).append("、账号 ").append(user.getLoginName()).append(" 更新成功");
            } else {
                failureNum++;
                failureMsg.append(br).append(failureNum).append("、账号 ").append(user.getLoginName()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = br + failureNum + "、账号 " + user.getLoginName() + " 导入失败:";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }
    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
        throw new BusinessException(failureMsg.toString());
    } else {
        successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
    }
    return successMsg.toString();
}
 
Example #28
Source File: DemoOperateController.java    From supplierShop with MIT License 4 votes vote down vote up
/**
 * 导入用户数据
 * 
 * @param userList 用户数据列表
 * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
 * @return 结果
 */
public String importUser(List<UserOperateModel> userList, Boolean isUpdateSupport)
{
    if (StringUtils.isNull(userList) || userList.size() == 0)
    {
        throw new BusinessException("导入用户数据不能为空!");
    }
    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
    for (UserOperateModel user : userList)
    {
        try
        {
            // 验证是否存在这个用户
            boolean userFlag = false;
            for (Map.Entry<Integer, UserOperateModel> entry : users.entrySet())
            {
                if (entry.getValue().getUserName().equals(user.getUserName()))
                {
                    userFlag = true;
                    break;
                }
            }
            if (!userFlag)
            {
                Integer userId = users.size() + 1;
                user.setUserId(userId);
                users.put(userId, user);
                successNum++;
                successMsg.append("<br/>" + successNum + "、用户 " + user.getUserName() + " 导入成功");
            }
            else if (isUpdateSupport)
            {
                users.put(user.getUserId(), user);
                successNum++;
                successMsg.append("<br/>" + successNum + "、用户 " + user.getUserName() + " 更新成功");
            }
            else
            {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、用户 " + user.getUserName() + " 已存在");
            }
        }
        catch (Exception e)
        {
            failureNum++;
            String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败:";
            failureMsg.append(msg + e.getMessage());
        }
    }
    if (failureNum > 0)
    {
        failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
        throw new BusinessException(failureMsg.toString());
    }
    else
    {
        successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
    }
    return successMsg.toString();
}
 
Example #29
Source File: SysUserServiceImpl.java    From supplierShop with MIT License 4 votes vote down vote up
/**
 * 导入用户数据
 * 
 * @param userList 用户数据列表
 * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
 * @param operName 操作用户
 * @return 结果
 */
@Override
public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName)
{
    if (StringUtils.isNull(userList) || userList.size() == 0)
    {
        throw new BusinessException("导入用户数据不能为空!");
    }
    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
    String password = configService.selectConfigByKey("sys.user.initPassword");
    for (SysUser user : userList)
    {
        try
        {
            // 验证是否存在这个用户
            SysUser u = userMapper.selectUserByLoginName(user.getLoginName());
            if (StringUtils.isNull(u))
            {
                user.setPassword(Md5Utils.hash(user.getLoginName() + password));
                user.setCreateBy(operName);
                this.insertUser(user);
                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 导入成功");
            }
            else if (isUpdateSupport)
            {
                user.setUpdateBy(operName);
                this.updateUser(user);
                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 更新成功");
            }
            else
            {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、账号 " + user.getLoginName() + " 已存在");
            }
        }
        catch (Exception e)
        {
            failureNum++;
            String msg = "<br/>" + failureNum + "、账号 " + user.getLoginName() + " 导入失败:";
            failureMsg.append(msg + e.getMessage());
            log.error(msg, e);
        }
    }
    if (failureNum > 0)
    {
        failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
        throw new BusinessException(failureMsg.toString());
    }
    else
    {
        successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
    }
    return successMsg.toString();
}
 
Example #30
Source File: DemoOperateController.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 导入用户数据
 *
 * @param userList        用户数据列表
 * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
 * @return 结果
 */
private String importUser(List<UserOperateModel> userList, Boolean isUpdateSupport) {
    if (CollectionUtil.isEmpty(userList)) {
        throw new BusinessException("导入用户数据不能为空!");
    }
    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
    for (UserOperateModel user : userList) {
        try {
            // 验证是否存在这个用户
            boolean userFlag = false;
            for (Map.Entry<Integer, UserOperateModel> entry : USERS.entrySet()) {
                if (entry.getValue().getUserName().equals(user.getUserName())) {
                    userFlag = true;
                    break;
                }
            }
            if (!userFlag) {
                int userId = USERS.size() + 1;
                user.setUserId(userId);
                USERS.put(userId, user);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、用户 ").append(user.getUserName()).append(" 导入成功");
            } else if (isUpdateSupport) {
                USERS.put(user.getUserId(), user);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、用户 ").append(user.getUserName()).append(" 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、用户 ").append(user.getUserName()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败:";
            failureMsg.append(msg).append(e.getMessage());
        }
    }
    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
        throw new BusinessException(failureMsg.toString());
    } else {
        successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
    }
    return successMsg.toString();
}