|
@@ -1,6 +1,5 @@
|
|
|
package org.eco.system.service.impl;
|
|
|
|
|
|
-import cn.dev33.satoken.secure.BCrypt;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.mybatisflex.core.paginate.Page;
|
|
|
import com.mybatisflex.core.query.QueryMethods;
|
|
@@ -11,13 +10,15 @@ import jakarta.validation.Validator;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.eco.common.core.constant.CacheNames;
|
|
|
import org.eco.common.core.constant.UserConstants;
|
|
|
+import org.eco.common.core.core.domain.model.LoginUser;
|
|
|
import org.eco.common.core.core.page.PageResult;
|
|
|
import org.eco.common.core.exception.BusinessException;
|
|
|
import org.eco.common.core.service.UserService;
|
|
|
import org.eco.common.core.utils.MapstructUtils;
|
|
|
import org.eco.common.core.utils.SpringUtils;
|
|
|
import org.eco.common.core.utils.StringUtils;
|
|
|
-import org.eco.common.core.utils.bean.BeanValidators;
|
|
|
+import org.eco.common.excel.core.ExcelResult;
|
|
|
+import org.eco.common.excel.utils.ExcelUtil;
|
|
|
import org.eco.common.orm.core.page.PageQuery;
|
|
|
import org.eco.common.orm.core.service.impl.BaseServiceImpl;
|
|
|
import org.eco.common.security.utils.LoginHelper;
|
|
@@ -27,7 +28,9 @@ import org.eco.system.domain.SysUserPost;
|
|
|
import org.eco.system.domain.bo.SysUserBo;
|
|
|
import org.eco.system.domain.vo.SysPostVo;
|
|
|
import org.eco.system.domain.vo.SysRoleVo;
|
|
|
+import org.eco.system.domain.vo.SysUserImportVo;
|
|
|
import org.eco.system.domain.vo.SysUserVo;
|
|
|
+import org.eco.system.listener.SysUserImportListener;
|
|
|
import org.eco.system.mapper.SysUserMapper;
|
|
|
import org.eco.system.service.ISysConfigService;
|
|
|
import org.eco.system.service.ISysDataScopeService;
|
|
@@ -37,10 +40,12 @@ import org.eco.system.service.ISysUserPostService;
|
|
|
import org.eco.system.service.ISysUserRoleService;
|
|
|
import org.eco.system.service.ISysUserService;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
+import java.io.InputStream;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
@@ -444,8 +449,14 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
|
|
|
@Transactional
|
|
|
public boolean insertUser(SysUserBo userBo) {
|
|
|
SysUser user = MapstructUtils.convert(userBo, SysUser.class);
|
|
|
- // 新增用户信息
|
|
|
- boolean saved = this.save(user);
|
|
|
+ boolean saved;
|
|
|
+ if (ObjectUtil.isNotNull(user.getTenantId())) {
|
|
|
+ // 不会覆盖租户
|
|
|
+ saved = TenantHelper.ignore(() -> this.save(user));
|
|
|
+ } else {
|
|
|
+ // 新增用户信息
|
|
|
+ saved = this.save(user);
|
|
|
+ }
|
|
|
if (saved) {
|
|
|
// 新增用户岗位关联
|
|
|
insertUserPost(user);
|
|
@@ -626,65 +637,6 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
|
|
|
return this.removeByIds(Arrays.asList(userIds));
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 导入用户数据
|
|
|
- *
|
|
|
- * @param userList 用户数据列表
|
|
|
- * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
|
|
|
- * @param operId 操作用户ID
|
|
|
- * @return 结果
|
|
|
- */
|
|
|
- @Override
|
|
|
- public String importUser(List<SysUser> userList, Boolean isUpdateSupport, Long operId) {
|
|
|
- if (StringUtils.isNull(userList) || userList.isEmpty()) {
|
|
|
- throw new BusinessException("导入用户数据不能为空!");
|
|
|
- }
|
|
|
- int successNum = 0;
|
|
|
- int failureNum = 0;
|
|
|
- StringBuilder successMsg = new StringBuilder();
|
|
|
- StringBuilder failureMsg = new StringBuilder();
|
|
|
- String initPassword = configService.selectConfigByKey("sys.user.initPassword").getConfigValue();
|
|
|
- for (SysUser user : userList) {
|
|
|
- try {
|
|
|
- // 验证是否存在这个用户
|
|
|
- SysUserVo u = selectUserByUserName(user.getUserName());
|
|
|
- if (StringUtils.isNull(u)) {
|
|
|
- BeanValidators.validateWithException(validator, user);
|
|
|
- user.setPassword(BCrypt.hashpw(initPassword));
|
|
|
- user.setCreateBy(operId);
|
|
|
- this.save(user);
|
|
|
- successNum++;
|
|
|
- successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
|
|
|
- } else if (isUpdateSupport) {
|
|
|
- BeanValidators.validateWithException(validator, user);
|
|
|
- checkUserAllowed(u.getUserId());
|
|
|
- checkUserDataScope(u.getUserId());
|
|
|
- user.setVersion(u.getVersion());
|
|
|
- user.setUserId(u.getUserId());
|
|
|
- user.setUpdateBy(operId);
|
|
|
- this.updateById(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());
|
|
|
- log.error(msg, e);
|
|
|
- }
|
|
|
- }
|
|
|
- if (failureNum > 0) {
|
|
|
- failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
|
|
|
- throw new BusinessException(failureMsg.toString());
|
|
|
- } else {
|
|
|
- successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
|
|
|
- }
|
|
|
- return successMsg.toString();
|
|
|
- }
|
|
|
-
|
|
|
@Cacheable(cacheNames = CacheNames.SYS_USER_NAME, key = "#userId")
|
|
|
@Override
|
|
|
public String selectUserNameById(Long userId) {
|
|
@@ -703,4 +655,12 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
|
|
|
QueryWrapper queryWrapper = query().where(SYS_USER.DEPT_ID.eq(deptId)).and(SYS_USER.DEL_FLAG.eq(0));
|
|
|
return this.listAs(queryWrapper, SysUserVo.class);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Async
|
|
|
+ public void asyncImportData(InputStream inputStream, boolean updateSupport, LoginUser loginUser) {
|
|
|
+ ExcelResult<SysUserImportVo> result = ExcelUtil.importExcel(inputStream, SysUserImportVo.class, new SysUserImportListener(updateSupport, loginUser));
|
|
|
+ log.info("错误信息:{}", result.getAnalysis().getMsg());
|
|
|
+ log.info("状态:{}", result.getAnalysis().getStatus());
|
|
|
+ }
|
|
|
}
|