From e4020faff32cb45976bb2f4f10f606e06eeb9655 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期六, 27 十一月 2021 12:57:21 +0800 Subject: [PATCH] update 使用导入新写法 重构系统用户导入 --- ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java | 9 ++-- ruoyi-system/src/main/java/com/ruoyi/system/listener/SysUserImportListener.java | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 5 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java index 51027f1..194c993 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java @@ -12,11 +12,13 @@ import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.enums.BusinessType; +import com.ruoyi.common.excel.ExcelResult; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.system.domain.vo.SysUserExportVo; import com.ruoyi.system.domain.vo.SysUserImportVo; +import com.ruoyi.system.listener.SysUserImportListener; import com.ruoyi.system.service.ISysPostService; import com.ruoyi.system.service.ISysRoleService; import com.ruoyi.system.service.ISysUserService; @@ -87,11 +89,8 @@ @PreAuthorize("@ss.hasPermi('system:user:import')") @PostMapping("/importData") public AjaxResult<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception { - List<SysUserImportVo> userListVo = ExcelUtil.importExcel(file.getInputStream(), SysUserImportVo.class); - List<SysUser> userList = BeanUtil.copyToList(userListVo, SysUser.class); - String operName = getUsername(); - String message = userService.importUser(userList, updateSupport, operName); - return AjaxResult.success(message); + ExcelResult<SysUserImportVo> result = ExcelUtil.importExcel(file.getInputStream(), SysUserImportVo.class, new SysUserImportListener(updateSupport)); + return AjaxResult.success(result.getAnalysis()); } @ApiOperation("涓嬭浇瀵煎叆妯℃澘") diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/listener/SysUserImportListener.java b/ruoyi-system/src/main/java/com/ruoyi/system/listener/SysUserImportListener.java new file mode 100644 index 0000000..d0fd7d0 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/listener/SysUserImportListener.java @@ -0,0 +1,109 @@ +package com.ruoyi.system.listener; + +import cn.hutool.core.bean.BeanUtil; +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.ruoyi.common.core.domain.entity.SysUser; +import com.ruoyi.common.excel.ExcelListener; +import com.ruoyi.common.excel.ExcelResult; +import com.ruoyi.common.exception.ServiceException; +import com.ruoyi.common.utils.SecurityUtils; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.spring.SpringUtils; +import com.ruoyi.system.domain.vo.SysUserImportVo; +import com.ruoyi.system.service.ISysConfigService; +import com.ruoyi.system.service.ISysUserService; +import lombok.extern.slf4j.Slf4j; + +import java.util.List; + +/** + * 绯荤粺鐢ㄦ埛鑷畾涔夊鍏� + * + * @author Lion Li + */ +@Slf4j +public class SysUserImportListener extends AnalysisEventListener<SysUserImportVo> implements ExcelListener<SysUserImportVo> { + + private final ISysUserService userService; + + private final String password; + + private final Boolean isUpdateSupport; + + private final String operName; + + private int successNum = 0; + private int failureNum = 0; + private final StringBuilder successMsg = new StringBuilder(); + private final StringBuilder failureMsg = new StringBuilder(); + + public SysUserImportListener(Boolean isUpdateSupport) { + this.userService = SpringUtils.getBean(ISysUserService.class); + this.password = SpringUtils.getBean(ISysConfigService.class) + .selectConfigByKey("sys.user.initPassword"); + this.isUpdateSupport = isUpdateSupport; + this.operName = SecurityUtils.getUsername(); + } + + @Override + public void invoke(SysUserImportVo userVo, AnalysisContext context) { + SysUser user = this.userService.selectUserByUserName(userVo.getUserName()); + try { + // 楠岃瘉鏄惁瀛樺湪杩欎釜鐢ㄦ埛 + if (StringUtils.isNull(user)) { + user = BeanUtil.toBean(userVo, SysUser.class); + user.setPassword(SecurityUtils.encryptPassword(password)); + user.setCreateBy(operName); + userService.insertUser(user); + successNum++; + successMsg.append("<br/>").append(successNum).append("銆佽处鍙� ").append(user.getUserName()).append(" 瀵煎叆鎴愬姛"); + } else if (isUpdateSupport) { + user.setUpdateBy(operName); + userService.updateUser(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()); + log.error(msg, e); + } + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + + } + + @Override + public ExcelResult<SysUserImportVo> getExcelResult() { + return new ExcelResult<SysUserImportVo>() { + + @Override + public String getAnalysis() { + if (failureNum > 0) { + failureMsg.insert(0, "寰堟姳姝夛紝瀵煎叆澶辫触锛佸叡 " + failureNum + " 鏉℃暟鎹牸寮忎笉姝g‘锛岄敊璇涓嬶細"); + throw new ServiceException(failureMsg.toString()); + } else { + successMsg.insert(0, "鎭枩鎮紝鏁版嵁宸插叏閮ㄥ鍏ユ垚鍔燂紒鍏� " + successNum + " 鏉★紝鏁版嵁濡備笅锛�"); + } + return successMsg.toString(); + } + + @Override + public List<SysUserImportVo> getList() { + return null; + } + + @Override + public List<String> getErrorList() { + return null; + } + }; + } +} -- Gitblit v1.9.3