【新增】 学生模板导入
This commit is contained in:
@@ -227,6 +227,18 @@ public class UserController {
|
||||
ExcelUtils.write(response, "用户导入模板.xls", "用户列表", UserImportExcelVO.class, list);
|
||||
}
|
||||
|
||||
@GetMapping("/get-import-template-stu")
|
||||
@Operation(summary = "获得导入学生模板")
|
||||
public void importTemplateStu(HttpServletResponse response) throws IOException {
|
||||
// 手动创建导出 demo
|
||||
List<UserStuImportExcelVO> list = Arrays.asList(
|
||||
UserStuImportExcelVO.builder().userType("学生").username("xueshen").classId("三年二班").email("xueshen@exam.cn").mobile("xxxxxxxx")
|
||||
.nickname("xxxx").sfz("xxxxxxxxxxxxx").status(CommonStatusEnum.ENABLE.getStatus()).sex(SexEnum.MALE.getSex()).build()
|
||||
);
|
||||
// 输出
|
||||
ExcelUtils.write(response, "学生导入模板.xls", "学生列表", UserStuImportExcelVO.class, list);
|
||||
}
|
||||
|
||||
@PostMapping("/import")
|
||||
@Operation(summary = "导入用户")
|
||||
@Parameters({
|
||||
@@ -240,6 +252,18 @@ public class UserController {
|
||||
return success(userService.importUserList(list, updateSupport));
|
||||
}
|
||||
|
||||
@PostMapping("/importStu")
|
||||
@Operation(summary = "导入学生")
|
||||
@Parameters({
|
||||
@Parameter(name = "file", description = "Excel 文件", required = true),
|
||||
@Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
|
||||
})
|
||||
public CommonResult<UserImportRespVO> importStuExcel(@RequestParam("file") MultipartFile file,
|
||||
@RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
|
||||
List<UserStuImportExcelVO> list = ExcelUtils.read(file, UserStuImportExcelVO.class);
|
||||
return success(userService.importUserStuList(list, updateSupport));
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/getSpeciatListByUser")
|
||||
@Operation(summary = "获取专业信息,判断用户类型")
|
||||
|
@@ -0,0 +1,51 @@
|
||||
package pc.exam.pp.module.system.controller.admin.user.vo.user;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import pc.exam.pp.framework.excel.core.annotations.DictFormat;
|
||||
import pc.exam.pp.framework.excel.core.convert.DictConvert;
|
||||
import pc.exam.pp.module.system.enums.DictTypeConstants;
|
||||
|
||||
/**
|
||||
* 用户 Excel 导入 VO
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = false) // 设置 chain = false,避免用户导入有问题
|
||||
public class UserStuImportExcelVO {
|
||||
@ExcelProperty("属性(学生)")
|
||||
private String userType;
|
||||
|
||||
@ExcelProperty("学生账号")
|
||||
private String username;
|
||||
|
||||
@ExcelProperty("姓名")
|
||||
private String nickname;
|
||||
|
||||
@ExcelProperty("班级")
|
||||
private String classId;
|
||||
|
||||
@ExcelProperty("用户邮箱")
|
||||
private String email;
|
||||
|
||||
@ExcelProperty("手机号码")
|
||||
private String mobile;
|
||||
|
||||
@ExcelProperty("身份证")
|
||||
private String sfz;
|
||||
|
||||
@ExcelProperty(value = "用户性别", converter = DictConvert.class)
|
||||
@DictFormat(DictTypeConstants.USER_SEX)
|
||||
private Integer sex;
|
||||
|
||||
@ExcelProperty(value = "账号状态", converter = DictConvert.class)
|
||||
@DictFormat(DictTypeConstants.COMMON_STATUS)
|
||||
private Integer status;
|
||||
|
||||
}
|
@@ -242,6 +242,15 @@ public interface AdminUserService {
|
||||
*/
|
||||
UserImportRespVO importUserList(List<UserImportExcelVO> importUsers, boolean isUpdateSupport);
|
||||
|
||||
/**
|
||||
* 批量导入学生
|
||||
*
|
||||
* @param importUsers 导入学生列表
|
||||
* @param isUpdateSupport 是否支持更新
|
||||
* @return 导入结果
|
||||
*/
|
||||
UserImportRespVO importUserStuList(List<UserStuImportExcelVO> importUsers, boolean isUpdateSupport);
|
||||
|
||||
/**
|
||||
* 获得指定状态的用户们
|
||||
*
|
||||
|
@@ -735,6 +735,73 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
return respVO;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
|
||||
public UserImportRespVO importUserStuList(List<UserStuImportExcelVO> importUsers, boolean isUpdateSupport) {
|
||||
// 1.1 参数校验
|
||||
if (CollUtil.isEmpty(importUsers)) {
|
||||
throw exception(USER_IMPORT_LIST_IS_EMPTY);
|
||||
}
|
||||
// 1.2 初始化密码不能为空
|
||||
String initPassword = configApi.getConfigValueByKey(USER_INIT_PASSWORD_KEY);
|
||||
if (StrUtil.isEmpty(initPassword)) {
|
||||
throw exception(USER_IMPORT_INIT_PASSWORD);
|
||||
}
|
||||
|
||||
// 2. 遍历,逐个创建 or 更新
|
||||
UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>())
|
||||
.updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build();
|
||||
importUsers.forEach(importUser -> {
|
||||
// 2.1.1 校验字段是否符合要求
|
||||
try {
|
||||
ValidationUtils.validate(BeanUtils.toBean(importUser, UserSaveReqVO.class).setPassword(initPassword));
|
||||
} catch (ConstraintViolationException ex){
|
||||
respVO.getFailureUsernames().put(importUser.getUsername(), ex.getMessage());
|
||||
return;
|
||||
}
|
||||
// 2.1.2 校验,判断是否有不符合的原因
|
||||
// try {
|
||||
// validateUserForCreateOrUpdate(null, null, importUser.getMobile(), importUser.getEmail(),
|
||||
// importUser.getDeptId(), null);
|
||||
// } catch (ServiceException ex) {
|
||||
// respVO.getFailureUsernames().put(importUser.getUsername(), ex.getMessage());
|
||||
// return;
|
||||
// }
|
||||
// 学生 to 2
|
||||
importUser.setUserType("2");
|
||||
// 1.3 根据班级名称查询班级是否存在,如存在返回班级ID,如不存在创建并返回班级ID
|
||||
ClassDO classDO = classMapper.getClassNameOne(importUser.getClassId());
|
||||
if (classDO == null) {
|
||||
// 如果没有查询出来需要创建数据
|
||||
ClassSaveReqVO classSaveReqVO = new ClassSaveReqVO();
|
||||
classSaveReqVO.setName(importUser.getClassId());
|
||||
importUser.setClassId(String.valueOf(classService.createClass(classSaveReqVO)));
|
||||
} else {
|
||||
// 查询出数据后进行赋值
|
||||
importUser.setClassId(String.valueOf(classDO.getId()));
|
||||
}
|
||||
// 2.2.1 判断如果不存在,在进行插入
|
||||
AdminUserDO existUser = userMapper.selectByUsername(importUser.getUsername());
|
||||
if (existUser == null) {
|
||||
userMapper.insert(BeanUtils.toBean(importUser, AdminUserDO.class)
|
||||
.setPassword(encodePassword(initPassword)).setPostIds(new HashSet<>())); // 设置默认密码及空岗位编号数组
|
||||
respVO.getCreateUsernames().add(importUser.getUsername());
|
||||
return;
|
||||
}
|
||||
// 2.2.2 如果存在,判断是否允许更新
|
||||
if (!isUpdateSupport) {
|
||||
respVO.getFailureUsernames().put(importUser.getUsername(), USER_USERNAME_EXISTS.getMsg());
|
||||
return;
|
||||
}
|
||||
AdminUserDO updateUser = BeanUtils.toBean(importUser, AdminUserDO.class);
|
||||
updateUser.setId(existUser.getId());
|
||||
userMapper.updateById(updateUser);
|
||||
respVO.getUpdateUsernames().add(importUser.getUsername());
|
||||
});
|
||||
return respVO;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<AdminUserDO> getUserListByStatus(Integer status) {
|
||||
return userMapper.selectListByStatus(status);
|
||||
|
Reference in New Issue
Block a user