Commit 72caa0e2 authored by Ryan Loong's avatar Ryan Loong

add 导出成绩到excel

parent 41ed864a
package cn.exploring.engine.server.db.domain.vo;
import com.alibaba.excel.annotation.ExcelProperty;
import org.springframework.beans.BeanUtils;
import java.time.LocalDateTime;
public class StudentPointExcelExportVo {
@ExcelProperty("学号")
private String studentUid;
private String studentUniqueId;
@ExcelProperty("姓名")
private String studentName;
private String name;
@ExcelProperty("成绩")
private Integer score;
@ExcelProperty("班级")
private String className;
@ExcelProperty("记录创建时间")
private LocalDateTime addTime;
@ExcelProperty("点名时间")
private LocalDateTime recordTime;
public String getStudentUid() {
return studentUid;
@ExcelProperty("分数")
private Integer point;
public StudentPointExcelExportVo() {
}
public StudentPointExcelExportVo(String studentUniqueId, String name, String className, LocalDateTime recordTime, Integer point) {
this.studentUniqueId = studentUniqueId;
this.name = name;
this.className = className;
this.recordTime = recordTime;
this.point = point;
}
public StudentPointExcelExportVo(StudentPointWithStudentInfoVo studentPointWithStudentInfoVo) {
BeanUtils.copyProperties(studentPointWithStudentInfoVo, this);
}
public String getStudentUniqueId() {
return studentUniqueId;
}
public void setStudentUniqueId(String studentUniqueId) {
this.studentUniqueId = studentUniqueId;
}
public String getName() {
return name;
}
public void setStudentUid(String studentUid) {
this.studentUid = studentUid;
public void setName(String name) {
this.name = name;
}
public String getStudentName() {
return studentName;
public String getClassName() {
return className;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
public void setClassName(String className) {
this.className = className;
}
public Integer getScore() {
return score;
public LocalDateTime getRecordTime() {
return recordTime;
}
public void setScore(Integer score) {
this.score = score;
public void setRecordTime(LocalDateTime recordTime) {
this.recordTime = recordTime;
}
public LocalDateTime getAddTime() {
return addTime;
public Integer getPoint() {
return point;
}
public void setAddTime(LocalDateTime addTime) {
this.addTime = addTime;
public void setPoint(Integer point) {
this.point = point;
}
}
......@@ -373,4 +373,12 @@ public class StudentInfoService {
return null;
}
@Transactional(rollbackFor = Exception.class)
public void increaseCallTimeByPrimaryKey(Integer id) {
studentInfoMapper.updateByPrimaryKeySelective(
StudentInfo.builder().id(id).updateTime(LocalDateTime.now()).build().increment(StudentInfo.Increment.callTimes.inc(1)),
StudentInfo.Column.callTimes, StudentInfo.Column.updateTime
);
}
}
......@@ -2,7 +2,6 @@ package cn.exploring.engine.server.db.service;
import cn.exploring.engine.server.db.dao.SpecialSqlMapper;
import cn.exploring.engine.server.db.dao.StudentPointInfoMapper;
import cn.exploring.engine.server.db.domain.StudentInfo;
import cn.exploring.engine.server.db.domain.StudentPointInfo;
import cn.exploring.engine.server.db.domain.StudentPointInfoExample;
import cn.exploring.engine.server.db.domain.vo.StudentPointExcelExportVo;
......@@ -10,15 +9,15 @@ import cn.exploring.engine.server.db.domain.vo.StudentPointWithStudentInfoVo;
import cn.exploring.engine.server.db.util.DateTimeUtil;
import cn.exploring.engine.server.db.util.SqlUtil;
import cn.exploring.engine.server.db.util.StrUtil;
import cn.exploring.engine.server.db.util.Util;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.*;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.operator.GreaterEqual;
import cn.zhxu.bs.operator.LessEqual;
import cn.zhxu.bs.util.MapBuilder;
import cn.zhxu.bs.util.MapUtils;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
......@@ -27,15 +26,15 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
......@@ -56,19 +55,29 @@ public class StudentPointService {
BeanSearcher beanSearcher;
public Map<String, Object> selectPointByWeb( String studentName, String studentUniqueId, Integer classId, String startTime, String endTime, Integer page, Integer limit, String sort, String order) {
SearchResult<StudentPointWithStudentInfoVo> result = beanSearcher.search(StudentPointWithStudentInfoVo.class, SqlUtil.page(voWrapper(studentName, studentUniqueId, classId, startTime, endTime), page, limit, sort, order).build());
return ImmutableMap.of("total", result.getTotalCount().longValue(), "items", result.getDataList());
}
public File exportList(String studentName, String studentUniqueId, Integer classId, String startTime, String endTime) {
List<StudentPointExcelExportVo> list = beanSearcher.searchAll(StudentPointWithStudentInfoVo.class, voWrapper(studentName, studentUniqueId, classId, startTime, endTime).build())
.stream().map(StudentPointExcelExportVo::new).collect(Collectors.toList());
return transToExcelFile(list);
}
public MapBuilder voWrapper(String studentName, String studentUniqueId, Integer classId, String startTime, String endTime) {
LocalDateTime start = Optional.ofNullable(startTime).filter(StringUtils::isNotBlank).map(s -> LocalDateTime.parse(s, DateTimeUtil.DATE_TIME_FORMATTER3)).orElse(null);
LocalDateTime end = Optional.ofNullable(endTime).filter(StringUtils::isNotBlank).map(s -> LocalDateTime.parse(s, DateTimeUtil.DATE_TIME_FORMATTER3)).orElse(null);
MapBuilder wrapper = MapUtils.builder()
return MapUtils.builder()
.field(StudentPointWithStudentInfoVo::getDeleted, StudentPointInfo.NOT_DELETED).op(Equal.class)
.field(StudentPointWithStudentInfoVo::getName, studentName).op(Contain.class)
.field(StudentPointWithStudentInfoVo::getStudentUniqueId, studentUniqueId).op(Contain.class)
.field(StudentPointWithStudentInfoVo::getClassId, classId).op(Equal.class)
.field(StudentPointWithStudentInfoVo::getRecordTime, start).op(GreaterEqual.class)
.field(StudentPointWithStudentInfoVo::getRecordTime, end).op(LessEqual.class);
SearchResult<StudentPointWithStudentInfoVo> result = beanSearcher.search(StudentPointWithStudentInfoVo.class, SqlUtil.page(wrapper, page, limit, sort, order).build());
return ImmutableMap.of("total", result.getTotalCount().longValue(), "items", result.getDataList());
}
/**
......@@ -87,13 +96,14 @@ public class StudentPointService {
}
@Transactional(rollbackFor = Exception.class)
public String addPoint(StudentPointInfo studentPoint) {
LocalDateTime now = LocalDateTime.now();
studentPoint.setRecordTime(now);
studentPoint.setUpdateTime(now);
studentPoint.setAddTime(now);
studentPoint.setDeleted(false);
studentPointInfoMapper.insertSelective(studentPoint);
studentPointInfoMapper.insert(studentPoint);
return "add OK";
}
......@@ -163,22 +173,22 @@ public class StudentPointService {
.example());
}
public List<StudentPointExcelExportVo> transToExcelExportVo(Collection<StudentPointInfo> list) {
if (CollectionUtils.isEmpty(list)) {
return Collections.emptyList();
}
Map<Integer, StudentInfo> studentInfoMap = studentInfoService.selectByPrimaryKey(list.stream().map(StudentPointInfo::getStudentId).collect(Collectors.toSet()))
.stream().collect(Collectors.toMap(StudentInfo::getId, x -> x, (k1, k2) -> k1));
return list.stream().map(pointInfo -> new StudentPointExcelExportVo() {{
StudentInfo studentInfo = studentInfoMap.getOrDefault(pointInfo.getStudentId(), new StudentInfo());
setStudentName(studentInfo.getName());
setStudentUid(studentInfo.getStudentUniqueId());
setScore(pointInfo.getPoint());
setAddTime(pointInfo.getAddTime());
}}).collect(Collectors.toList());
}
// public List<StudentPointExcelExportVo> transToExcelExportVo(Collection<StudentPointInfo> list) {
// if (CollectionUtils.isEmpty(list)) {
// return Collections.emptyList();
// }
//
// Map<Integer, StudentInfo> studentInfoMap = studentInfoService.selectByPrimaryKey(list.stream().map(StudentPointInfo::getStudentId).collect(Collectors.toSet()))
// .stream().collect(Collectors.toMap(StudentInfo::getId, x -> x, (k1, k2) -> k1));
//
// return list.stream().map(pointInfo -> new StudentPointExcelExportVo() {{
// StudentInfo studentInfo = studentInfoMap.getOrDefault(pointInfo.getStudentId(), new StudentInfo());
// setStudentName(studentInfo.getName());
// setStudentUid(studentInfo.getStudentUniqueId());
// setScore(pointInfo.getPoint());
// setAddTime(pointInfo.getAddTime());
// }}).collect(Collectors.toList());
// }
public File transToExcelFile(List<StudentPointExcelExportVo> voList) {
File file = null;
......
......@@ -6,7 +6,6 @@ import cn.exploring.engine.server.db.domain.StudentPointInfo;
import cn.exploring.engine.server.db.domain.vo.IdVo;
import cn.exploring.engine.server.db.service.StudentInfoService;
import cn.exploring.engine.server.db.service.StudentPointService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
......@@ -44,6 +43,31 @@ public class PointController {
return ResponseUtil.ok(pointService.selectPointByWeb(studentName, studentUniqueId, classId, startTime, endTime, page, limit, sort, order));
}
@GetMapping("export")
public Object export(@RequestParam String studentName, @RequestParam String studentUniqueId, @RequestParam Integer classId, @RequestParam String startTime, @RequestParam String endTime,
@RequestParam(required = false, defaultValue = "addTime") String sort, @RequestParam(required = false, defaultValue = "desc") String order) {
studentUniqueId = StringUtils.isEmpty(studentUniqueId) ? null : studentUniqueId;
studentName = StringUtils.isEmpty(studentName) ? null : studentName;
startTime = StringUtils.isEmpty(startTime) ? null : startTime;
endTime = StringUtils.isEmpty(endTime) ? null : endTime;
File file = pointService.exportList(studentName, studentUniqueId, classId, startTime, endTime);
FileInputStream in = null;
try {
in = new FileInputStream(file);
return ResponseUtil.ok(storageService.storageStore(in, file.length(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.getName(), storageService.generateKey(file.getName())));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} finally {
if (Objects.nonNull(in)) {
try {
in.close();
} catch (IOException ignored) {
}
}
}
}
@PostMapping("add")
public Object add(@RequestBody StudentPointInfo pointInfo) {
......@@ -76,28 +100,28 @@ public class PointController {
}
@GetMapping("exportByClass")
public Object exportByClass(Integer classId) {
List<StudentPointInfo> list = pointService.listByClassId(classId);
if (CollectionUtils.isEmpty(list)) {
return ResponseUtil.fail(402, "暂无数据");
}
File file = pointService.transToExcelFile(pointService.transToExcelExportVo(list));
FileInputStream in = null;
try {
in = new FileInputStream(file);
return ResponseUtil.ok(storageService.storageStore(in, file.length(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.getName(), storageService.generateKey(file.getName())));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} finally {
if (Objects.nonNull(in)) {
try {
in.close();
} catch (IOException ignored) {
}
}
}
}
// @GetMapping("exportByClass")
// public Object exportByClass(Integer classId) {
// List<StudentPointInfo> list = pointService.listByClassId(classId);
// if (CollectionUtils.isEmpty(list)) {
// return ResponseUtil.fail(402, "暂无数据");
// }
//
// File file = pointService.transToExcelFile(pointService.transToExcelExportVo(list));
// FileInputStream in = null;
// try {
// in = new FileInputStream(file);
// return ResponseUtil.ok(storageService.storageStore(in, file.length(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.getName(), storageService.generateKey(file.getName())));
// } catch (FileNotFoundException e) {
// throw new RuntimeException(e);
// } finally {
// if (Objects.nonNull(in)) {
// try {
// in.close();
// } catch (IOException ignored) {
// }
// }
// }
// }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment