|
@@ -1,20 +1,18 @@
|
|
|
-package cn.com.qmth.examcloud.tool.service.batch_update_user;
|
|
|
+package cn.com.qmth.examcloud.tool.service.batch_create_user;
|
|
|
|
|
|
import cn.com.qmth.examcloud.tool.cache.LoginSessionManager;
|
|
|
import cn.com.qmth.examcloud.tool.config.SysProperty;
|
|
|
import cn.com.qmth.examcloud.tool.entity.TaskEntity;
|
|
|
import cn.com.qmth.examcloud.tool.service.CommonService;
|
|
|
import cn.com.qmth.examcloud.tool.service.TaskService;
|
|
|
-import cn.com.qmth.examcloud.tool.service.batch_update_user.vo.RoleInfo;
|
|
|
-import cn.com.qmth.examcloud.tool.service.batch_update_user.vo.UserInfo;
|
|
|
-import cn.com.qmth.examcloud.tool.service.batch_update_user.vo.UserInfoListener;
|
|
|
+import cn.com.qmth.examcloud.tool.service.batch_create_user.vo.UserInfo;
|
|
|
+import cn.com.qmth.examcloud.tool.service.batch_create_user.vo.UserInfoListener;
|
|
|
import cn.com.qmth.examcloud.tool.utils.HttpHelper;
|
|
|
import cn.com.qmth.examcloud.tool.utils.JsonMapper;
|
|
|
import cn.com.qmth.examcloud.tool.utils.StatusException;
|
|
|
import cn.com.qmth.examcloud.tool.vo.user.User;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
-import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -53,53 +51,60 @@ public class BatchCreateUserTask implements TaskService {
|
|
|
throw new StatusException("任务参数解析错误!");
|
|
|
}
|
|
|
|
|
|
- String filePath = jsonParams.get("filePath").asText("");
|
|
|
+ String dataFilePath = jsonParams.get("dataFilePath").asText();
|
|
|
+ if (StringUtils.isBlank(dataFilePath)) {
|
|
|
+ throw new StatusException("参数dataFilePath不能为空!");
|
|
|
+ }
|
|
|
+
|
|
|
UserInfoListener dataListener = new UserInfoListener();
|
|
|
- EasyExcel.read(filePath, UserInfo.class, dataListener).sheet().doRead();
|
|
|
- List<UserInfo> userList = dataListener.getList();
|
|
|
+ EasyExcel.read(dataFilePath, UserInfo.class, dataListener).sheet().doRead();
|
|
|
+ List<UserInfo> dataList = dataListener.getList();
|
|
|
|
|
|
- Map<String, Long> roleMaps = this.queryRoles(loginUser);
|
|
|
+ int total = dataList.size();
|
|
|
+ log.info("共{}条数据!", total);
|
|
|
|
|
|
- int total = userList.size();
|
|
|
- boolean hasError = false;
|
|
|
+ // 检验数据
|
|
|
+ Map<String, Long> orgMaps = new HashMap<>();
|
|
|
+ Map<String, Long> courseMaps = new HashMap<>();
|
|
|
+ Map<String, Long> roleMaps = commonService.queryRoles(loginUser);
|
|
|
+
|
|
|
+ List<String> errMessages = new ArrayList<>();
|
|
|
for (int i = 0; i < total; i++) {
|
|
|
- UserInfo user = userList.get(i);
|
|
|
+ UserInfo user = dataList.get(i);
|
|
|
|
|
|
if (StringUtils.isBlank(user.getLoginName())) {
|
|
|
- log.warn("共{}条 第{}条 登录名不能为空!", total, i + 1);
|
|
|
- hasError = true;
|
|
|
+ errMessages.add("第" + (i + 1) + "条 登录名不能为空!");
|
|
|
+ } else {
|
|
|
+ user.setLoginName(user.getLoginName().trim());
|
|
|
}
|
|
|
|
|
|
if (StringUtils.isBlank(user.getUserName())) {
|
|
|
- log.warn("共{}条 第{}条 姓名不能为空!{}", total, i + 1, user.getLoginName());
|
|
|
- hasError = true;
|
|
|
+ errMessages.add("第" + (i + 1) + "条 姓名不能为空!");
|
|
|
+ } else {
|
|
|
+ user.setUserName(user.getUserName().trim());
|
|
|
}
|
|
|
|
|
|
if (StringUtils.isBlank(user.getPsw())) {
|
|
|
- log.warn("共{}条 第{}条 密码不能为空!{}", total, i + 1, user.getLoginName());
|
|
|
- hasError = true;
|
|
|
+ errMessages.add("第" + (i + 1) + "条 密码不能为空!");
|
|
|
+ } else {
|
|
|
+ user.setPsw(user.getPsw().trim());
|
|
|
}
|
|
|
|
|
|
if (!roleMaps.containsKey(user.getRoleName())) {
|
|
|
- log.warn("共{}条 第{}条 角色内容有误!{}", total, i + 1, user.getLoginName());
|
|
|
- hasError = true;
|
|
|
+ errMessages.add("第" + (i + 1) + "条 角色内容有误!");
|
|
|
}
|
|
|
-
|
|
|
- user.setUserName(user.getUserName().trim());
|
|
|
- user.setLoginName(user.getLoginName().trim());
|
|
|
- user.setPsw(user.getPsw().trim());
|
|
|
}
|
|
|
|
|
|
- if (hasError) {
|
|
|
- return;
|
|
|
+ if (!errMessages.isEmpty()) {
|
|
|
+ throw new StatusException(StringUtils.join(errMessages, "\n"));
|
|
|
}
|
|
|
|
|
|
int failCount = 0;
|
|
|
for (int i = 0; i < total; i++) {
|
|
|
- UserInfo user = userList.get(i);
|
|
|
+ UserInfo user = dataList.get(i);
|
|
|
this.createUser(loginUser, user, roleMaps);
|
|
|
|
|
|
- UserInfo existUser = this.queryUserByLoginName(loginUser, user.getLoginName());
|
|
|
+ UserInfo existUser = commonService.queryUserByLoginName(loginUser, user.getLoginName());
|
|
|
if (existUser == null) {
|
|
|
log.warn("共{}条 第{}条 用户创建失败!{}", total, i + 1, user.getLoginName());
|
|
|
failCount++;
|
|
@@ -118,51 +123,6 @@ public class BatchCreateUserTask implements TaskService {
|
|
|
task.setDescription(msg);
|
|
|
}
|
|
|
|
|
|
- private Map<String, Long> queryRoles(User loginUser) {
|
|
|
- String url = loginUser.getServerUrl() + "/api/ecs_core/rolePrivilege/getRoles?includeSuperAdmin=true";
|
|
|
- Map<String, String> headers = new HashMap<>();
|
|
|
- headers.put("key", loginUser.getKey());
|
|
|
- headers.put("token", loginUser.getToken());
|
|
|
-
|
|
|
- String result = HttpHelper.post(url, headers, null);
|
|
|
- log.info(result);
|
|
|
-
|
|
|
- List<RoleInfo> roles = new JsonMapper().toList(result, RoleInfo.class);
|
|
|
- Map<String, Long> maps = new HashMap<>();
|
|
|
- if (CollectionUtils.isNotEmpty(roles)) {
|
|
|
- for (RoleInfo role : roles) {
|
|
|
- maps.put(role.getRoleName(), role.getRoleId());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return maps;
|
|
|
- }
|
|
|
-
|
|
|
- private UserInfo queryUserByLoginName(User loginUser, String loginName) {
|
|
|
- String url = loginUser.getServerUrl() + "/api/ecs_core/user/query";
|
|
|
- Map<String, String> headers = new HashMap<>();
|
|
|
- headers.put("key", loginUser.getKey());
|
|
|
- headers.put("token", loginUser.getToken());
|
|
|
-
|
|
|
- Map<String, String> params = new HashMap<>();
|
|
|
- params.put("rootOrgId", String.valueOf(loginUser.getRootOrgId()));
|
|
|
- params.put("loginName", loginName);
|
|
|
-
|
|
|
- String result = HttpHelper.get(url, headers, params);
|
|
|
- // log.info(result);
|
|
|
-
|
|
|
- List<UserInfo> users = new JsonMapper().toList(result, UserInfo.class);
|
|
|
- if (CollectionUtils.isNotEmpty(users)) {
|
|
|
- for (UserInfo user : users) {
|
|
|
- if (user.getLoginName().equals(loginName)) {
|
|
|
- return user;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
private void createUser(User loginUser, UserInfo user, Map<String, Long> roleMaps) {
|
|
|
String url = loginUser.getServerUrl() + "/api/ecs_core/user";
|
|
|
Map<String, String> headers = new HashMap<>();
|