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

add 导出成绩到excel

parent 41ed864a
package cn.exploring.engine.server.db.domain.vo; package cn.exploring.engine.server.db.domain.vo;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
import org.springframework.beans.BeanUtils;
import java.time.LocalDateTime; import java.time.LocalDateTime;
public class StudentPointExcelExportVo { public class StudentPointExcelExportVo {
@ExcelProperty("学号") @ExcelProperty("学号")
private String studentUid; private String studentUniqueId;
@ExcelProperty("姓名") @ExcelProperty("姓名")
private String studentName; private String name;
@ExcelProperty("成绩") @ExcelProperty("班级")
private Integer score; private String className;
@ExcelProperty("记录创建时间") @ExcelProperty("点名时间")
private LocalDateTime addTime; private LocalDateTime recordTime;
public String getStudentUid() { @ExcelProperty("分数")
return studentUid; 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) { public void setName(String name) {
this.studentUid = studentUid; this.name = name;
} }
public String getStudentName() { public String getClassName() {
return studentName; return className;
} }
public void setStudentName(String studentName) { public void setClassName(String className) {
this.studentName = studentName; this.className = className;
} }
public Integer getScore() { public LocalDateTime getRecordTime() {
return score; return recordTime;
} }
public void setScore(Integer score) { public void setRecordTime(LocalDateTime recordTime) {
this.score = score; this.recordTime = recordTime;
} }
public LocalDateTime getAddTime() { public Integer getPoint() {
return addTime; return point;
} }
public void setAddTime(LocalDateTime addTime) { public void setPoint(Integer point) {
this.addTime = addTime; this.point = point;
} }
} }
...@@ -373,4 +373,12 @@ public class StudentInfoService { ...@@ -373,4 +373,12 @@ public class StudentInfoService {
return null; 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; ...@@ -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.SpecialSqlMapper;
import cn.exploring.engine.server.db.dao.StudentPointInfoMapper; 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.StudentPointInfo;
import cn.exploring.engine.server.db.domain.StudentPointInfoExample; import cn.exploring.engine.server.db.domain.StudentPointInfoExample;
import cn.exploring.engine.server.db.domain.vo.StudentPointExcelExportVo; import cn.exploring.engine.server.db.domain.vo.StudentPointExcelExportVo;
...@@ -10,15 +9,15 @@ import cn.exploring.engine.server.db.domain.vo.StudentPointWithStudentInfoVo; ...@@ -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.DateTimeUtil;
import cn.exploring.engine.server.db.util.SqlUtil; import cn.exploring.engine.server.db.util.SqlUtil;
import cn.exploring.engine.server.db.util.StrUtil; 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.BeanSearcher;
import cn.zhxu.bs.SearchResult; 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.MapBuilder;
import cn.zhxu.bs.util.MapUtils; import cn.zhxu.bs.util.MapUtils;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
...@@ -27,15 +26,15 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -27,15 +26,15 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.util.List;
import java.util.*; import java.util.Map;
import java.util.function.Function; import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
...@@ -56,19 +55,29 @@ public class StudentPointService { ...@@ -56,19 +55,29 @@ public class StudentPointService {
BeanSearcher beanSearcher; 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) { 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 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); 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::getDeleted, StudentPointInfo.NOT_DELETED).op(Equal.class)
.field(StudentPointWithStudentInfoVo::getName, studentName).op(Contain.class) .field(StudentPointWithStudentInfoVo::getName, studentName).op(Contain.class)
.field(StudentPointWithStudentInfoVo::getStudentUniqueId, studentUniqueId).op(Contain.class) .field(StudentPointWithStudentInfoVo::getStudentUniqueId, studentUniqueId).op(Contain.class)
.field(StudentPointWithStudentInfoVo::getClassId, classId).op(Equal.class) .field(StudentPointWithStudentInfoVo::getClassId, classId).op(Equal.class)
.field(StudentPointWithStudentInfoVo::getRecordTime, start).op(GreaterEqual.class) .field(StudentPointWithStudentInfoVo::getRecordTime, start).op(GreaterEqual.class)
.field(StudentPointWithStudentInfoVo::getRecordTime, end).op(LessEqual.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 { ...@@ -87,13 +96,14 @@ public class StudentPointService {
} }
@Transactional(rollbackFor = Exception.class)
public String addPoint(StudentPointInfo studentPoint) { public String addPoint(StudentPointInfo studentPoint) {
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
studentPoint.setRecordTime(now); studentPoint.setRecordTime(now);
studentPoint.setUpdateTime(now); studentPoint.setUpdateTime(now);
studentPoint.setAddTime(now); studentPoint.setAddTime(now);
studentPoint.setDeleted(false); studentPoint.setDeleted(false);
studentPointInfoMapper.insertSelective(studentPoint); studentPointInfoMapper.insert(studentPoint);
return "add OK"; return "add OK";
} }
...@@ -163,22 +173,22 @@ public class StudentPointService { ...@@ -163,22 +173,22 @@ public class StudentPointService {
.example()); .example());
} }
public List<StudentPointExcelExportVo> transToExcelExportVo(Collection<StudentPointInfo> list) { // public List<StudentPointExcelExportVo> transToExcelExportVo(Collection<StudentPointInfo> list) {
if (CollectionUtils.isEmpty(list)) { // if (CollectionUtils.isEmpty(list)) {
return Collections.emptyList(); // return Collections.emptyList();
} // }
//
Map<Integer, StudentInfo> studentInfoMap = studentInfoService.selectByPrimaryKey(list.stream().map(StudentPointInfo::getStudentId).collect(Collectors.toSet())) // 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)); // .stream().collect(Collectors.toMap(StudentInfo::getId, x -> x, (k1, k2) -> k1));
//
return list.stream().map(pointInfo -> new StudentPointExcelExportVo() {{ // return list.stream().map(pointInfo -> new StudentPointExcelExportVo() {{
StudentInfo studentInfo = studentInfoMap.getOrDefault(pointInfo.getStudentId(), new StudentInfo()); // StudentInfo studentInfo = studentInfoMap.getOrDefault(pointInfo.getStudentId(), new StudentInfo());
setStudentName(studentInfo.getName()); // setStudentName(studentInfo.getName());
setStudentUid(studentInfo.getStudentUniqueId()); // setStudentUid(studentInfo.getStudentUniqueId());
setScore(pointInfo.getPoint()); // setScore(pointInfo.getPoint());
setAddTime(pointInfo.getAddTime()); // setAddTime(pointInfo.getAddTime());
}}).collect(Collectors.toList()); // }}).collect(Collectors.toList());
} // }
public File transToExcelFile(List<StudentPointExcelExportVo> voList) { public File transToExcelFile(List<StudentPointExcelExportVo> voList) {
File file = null; File file = null;
......
...@@ -6,7 +6,6 @@ import cn.exploring.engine.server.db.domain.StudentPointInfo; ...@@ -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.domain.vo.IdVo;
import cn.exploring.engine.server.db.service.StudentInfoService; import cn.exploring.engine.server.db.service.StudentInfoService;
import cn.exploring.engine.server.db.service.StudentPointService; import cn.exploring.engine.server.db.service.StudentPointService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -44,6 +43,31 @@ public class PointController { ...@@ -44,6 +43,31 @@ public class PointController {
return ResponseUtil.ok(pointService.selectPointByWeb(studentName, studentUniqueId, classId, startTime, endTime, page, limit, sort, order)); 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") @PostMapping("add")
public Object add(@RequestBody StudentPointInfo pointInfo) { public Object add(@RequestBody StudentPointInfo pointInfo) {
...@@ -76,28 +100,28 @@ public class PointController { ...@@ -76,28 +100,28 @@ public class PointController {
} }
@GetMapping("exportByClass") // @GetMapping("exportByClass")
public Object exportByClass(Integer classId) { // public Object exportByClass(Integer classId) {
List<StudentPointInfo> list = pointService.listByClassId(classId); // List<StudentPointInfo> list = pointService.listByClassId(classId);
if (CollectionUtils.isEmpty(list)) { // if (CollectionUtils.isEmpty(list)) {
return ResponseUtil.fail(402, "暂无数据"); // return ResponseUtil.fail(402, "暂无数据");
} // }
//
File file = pointService.transToExcelFile(pointService.transToExcelExportVo(list)); // File file = pointService.transToExcelFile(pointService.transToExcelExportVo(list));
FileInputStream in = null; // FileInputStream in = null;
try { // try {
in = new FileInputStream(file); // in = new FileInputStream(file);
return ResponseUtil.ok(storageService.storageStore(in, file.length(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.getName(), storageService.generateKey(file.getName()))); // return ResponseUtil.ok(storageService.storageStore(in, file.length(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.getName(), storageService.generateKey(file.getName())));
} catch (FileNotFoundException e) { // } catch (FileNotFoundException e) {
throw new RuntimeException(e); // throw new RuntimeException(e);
} finally { // } finally {
if (Objects.nonNull(in)) { // if (Objects.nonNull(in)) {
try { // try {
in.close(); // in.close();
} catch (IOException ignored) { // } 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