|
|
|
@ -128,4 +128,108 @@ public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void importStudentsByExcel(MultipartFile file) {
|
|
|
|
|
try {
|
|
|
|
|
ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
|
|
|
|
|
reader .addHeaderAlias("学号", "no")
|
|
|
|
|
.addHeaderAlias("姓名", "name")
|
|
|
|
|
.addHeaderAlias("性别", "gender")
|
|
|
|
|
.addHeaderAlias("班级", "className")
|
|
|
|
|
.addHeaderAlias("积分", "credits")
|
|
|
|
|
.addHeaderAlias("创建时间", "createTime")
|
|
|
|
|
.addHeaderAlias("最后操作时间", "updateTime");
|
|
|
|
|
List<Map<String, Object>> rows = reader.readAll();
|
|
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
List<Student> studentList = rows.stream().map(row -> {
|
|
|
|
|
Student student = new Student();
|
|
|
|
|
student.setNo(row.get("no").toString());
|
|
|
|
|
student.setName(row.get("name").toString());
|
|
|
|
|
student.setGender("男".equals(row.get("gender")) ? (short) 0 : (short) 1);
|
|
|
|
|
student.setClassName(row.get("className").toString());
|
|
|
|
|
student.setCreator(Integer.valueOf(BaseContext.getCurrentId().toString()));
|
|
|
|
|
if (row.get("credits") == null) {
|
|
|
|
|
student.setPoints(0.00);
|
|
|
|
|
} else {
|
|
|
|
|
student.setPoints(Double.valueOf(row.get("credits").toString()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (row.get("createTime") == null) {
|
|
|
|
|
student.setCreateTime(now);
|
|
|
|
|
} else {
|
|
|
|
|
String createTimeStr = row.get("createTime").toString();
|
|
|
|
|
student.setCreateTime(LocalDateTime.parse(createTimeStr, formatter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (row.get("updateTime") == null) {
|
|
|
|
|
student.setUpdateTime(now);
|
|
|
|
|
} else {
|
|
|
|
|
String updateTimeStr = row.get("updateTime").toString();
|
|
|
|
|
student.setUpdateTime(LocalDateTime.parse(updateTimeStr, formatter));
|
|
|
|
|
}
|
|
|
|
|
return student;
|
|
|
|
|
}).toList();
|
|
|
|
|
try {
|
|
|
|
|
baseMapper.insertOrUpdate(studentList);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.info(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void exportStudentsToExcel(HttpServletResponse response) {
|
|
|
|
|
try {
|
|
|
|
|
LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
wrapper.eq(Student::getCreator, BaseContext.getCurrentId());
|
|
|
|
|
List<Student> studentList = baseMapper.selectList(wrapper);
|
|
|
|
|
|
|
|
|
|
// 创建一个Excel写入器
|
|
|
|
|
ExcelWriter writer = ExcelUtil.getWriter(true);
|
|
|
|
|
|
|
|
|
|
// 设置表头别名
|
|
|
|
|
writer.addHeaderAlias("no", "学号");
|
|
|
|
|
writer.addHeaderAlias("name", "姓名");
|
|
|
|
|
writer.addHeaderAlias("gender", "性别");
|
|
|
|
|
writer.addHeaderAlias("className", "班级");
|
|
|
|
|
writer.addHeaderAlias("points", "积分");
|
|
|
|
|
writer.addHeaderAlias("createTime", "创建时间");
|
|
|
|
|
writer.addHeaderAlias("updateTime", "最后操作时间");
|
|
|
|
|
|
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
// 将性别的数字转换为文字
|
|
|
|
|
List<Map<String, Object>> rows = studentList.stream().map(student -> {
|
|
|
|
|
Map<String, Object> row = new HashMap<>();
|
|
|
|
|
row.put("no", student.getNo());
|
|
|
|
|
row.put("name", student.getName());
|
|
|
|
|
row.put("gender", student.getGender() == 0 ? "男" : "女");
|
|
|
|
|
row.put("className", student.getClassName());
|
|
|
|
|
row.put("points", student.getPoints());
|
|
|
|
|
row.put("createTime", student.getCreateTime().format(formatter));
|
|
|
|
|
row.put("updateTime", student.getUpdateTime().format(formatter));
|
|
|
|
|
return row;
|
|
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
// 写入数据
|
|
|
|
|
writer.write(rows, true);
|
|
|
|
|
|
|
|
|
|
// 设置响应头
|
|
|
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
|
|
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=students.xlsx");
|
|
|
|
|
|
|
|
|
|
// 输出Excel文件
|
|
|
|
|
ServletOutputStream out = response.getOutputStream();
|
|
|
|
|
writer.flush(out, true);
|
|
|
|
|
writer.close();
|
|
|
|
|
IoUtil.close(out);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("导出Excel失败", e);
|
|
|
|
|
throw new BaseException(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|