Commit 2e15b0eb authored by Ryan Loong's avatar Ryan Loong

add 上传相关接口 && update 学生查询接口

parent 39b2156a
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
......
......@@ -2,7 +2,6 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/engine-class-work.iml" filepath="$PROJECT_DIR$/.idea/engine-class-work.iml" />
<module fileurl="file://$PROJECT_DIR$/server-admin-api/server-admin-api.iml" filepath="$PROJECT_DIR$/server-admin-api/server-admin-api.iml" />
<module fileurl="file://$PROJECT_DIR$/server-all/server-all.iml" filepath="$PROJECT_DIR$/server-all/server-all.iml" />
<module fileurl="file://$PROJECT_DIR$/server-core/server-core.iml" filepath="$PROJECT_DIR$/server-core/server-core.iml" />
......
......@@ -27,11 +27,11 @@
<artifactId>shiro-spring-boot-web-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.poi</groupId>-->
<!-- <artifactId>poi</artifactId>-->
<!-- <version>4.1.0</version>-->
<!-- </dependency>-->
</dependencies>
......
......@@ -74,6 +74,14 @@ public class ResponseUtil {
return obj;
}
public static Object fail(int errno, String errmsg, Object data) {
Map<String, Object> obj = new HashMap<String, Object>();
obj.put("errno", errno);
obj.put("errmsg", errmsg);
obj.put("data", data);
return obj;
}
public static Object badArgument() {
return fail(401, "参数不对");
}
......
......@@ -82,6 +82,13 @@
<artifactId>janino</artifactId>
<version>3.0.7</version>
</dependency>
<!-- Alibaba EasyExcel 用于处理Excel的读写 https://github.com/alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
......
package cn.exploring.engine.server.db.domain.vo;
import cn.exploring.engine.server.db.domain.StudentInfo;
import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.Objects;
public class StudentInfoExcelImportResultVo extends StudentInfoExcelImportVo {
private Integer id;
private List<String> errMsg;
public StudentInfoExcelImportResultVo() {
super();
}
public StudentInfoExcelImportResultVo(StudentInfoExcelImportVo vo) {
if (Objects.nonNull(vo)) {
BeanUtils.copyProperties(vo, this);
}
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<String> getErrMsg() {
return errMsg;
}
public void setErrMsg(List<String> errMsg) {
this.errMsg = errMsg;
}
}
package cn.exploring.engine.server.db.domain.vo;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import java.util.List;
public class StudentInfoExcelImportVo {
@ExcelProperty("学号")
private String studentUid;
@ExcelProperty("姓名")
private String name;
@ExcelIgnore
private Integer rowNumber;
public String getStudentUid() {
return studentUid;
}
public void setStudentUid(String studentUid) {
this.studentUid = studentUid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getRowNumber() {
return rowNumber;
}
public void setRowNumber(Integer rowNumber) {
this.rowNumber = rowNumber;
}
}
package cn.exploring.engine.server.db.domain.vo;
import com.alibaba.excel.annotation.ExcelProperty;
import java.time.LocalDateTime;
public class StudentPointExcelExportVo {
@ExcelProperty("学号")
private String studentUid;
@ExcelProperty("姓名")
private String studentName;
@ExcelProperty("成绩")
private Integer score;
@ExcelProperty("记录创建时间")
private LocalDateTime addTime;
public String getStudentUid() {
return studentUid;
}
public void setStudentUid(String studentUid) {
this.studentUid = studentUid;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public LocalDateTime getAddTime() {
return addTime;
}
public void setAddTime(LocalDateTime addTime) {
this.addTime = addTime;
}
}
......@@ -7,6 +7,7 @@ import cn.exploring.engine.server.db.domain.StudentInfo;
*/
public class StudentVo extends StudentInfo {
public String ClassName;
private Integer callTimes;
public String getClassName() {
return ClassName;
......@@ -15,4 +16,12 @@ public class StudentVo extends StudentInfo {
public void setClassName(String className) {
ClassName = className;
}
public Integer getCallTimes() {
return callTimes;
}
public void setCallTimes(Integer callTimes) {
this.callTimes = callTimes;
}
}
......@@ -6,6 +6,8 @@ import cn.exploring.engine.server.db.domain.ClassInfoExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
......@@ -13,6 +15,7 @@ import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Service
public class ClassService {
......@@ -101,5 +104,20 @@ public class ClassService {
return "batch deleted OK";
}
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public List<ClassInfo> listAll() {
return classInfoMapper.selectByExample(new ClassInfoExample().createCriteria()
.andDeletedEqualTo(ClassInfo.NOT_DELETED)
.example());
}
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public ClassInfo selectByPrimaryKey(Integer id) {
return Objects.isNull(id) ? null
: classInfoMapper.selectByExample(new ClassInfoExample().createCriteria()
.andDeletedEqualTo(ClassInfo.NOT_DELETED)
.andIdEqualTo(id)
.example()).stream().findFirst().orElse(null);
}
}
......@@ -2,21 +2,33 @@ package cn.exploring.engine.server.db.service;
import cn.exploring.engine.server.db.dao.SpecialSqlMapper;
import cn.exploring.engine.server.db.dao.StudentInfoMapper;
import cn.exploring.engine.server.db.domain.ClassInfo;
import cn.exploring.engine.server.db.domain.StudentInfo;
import cn.exploring.engine.server.db.domain.StudentInfoExample;
import cn.exploring.engine.server.db.domain.vo.StudentInfoExcelImportResultVo;
import cn.exploring.engine.server.db.domain.vo.StudentInfoExcelImportVo;
import cn.exploring.engine.server.db.domain.vo.StudentVo;
import cn.exploring.engine.server.db.util.excel.ExcelReaderListener;
import cn.exploring.engine.server.db.util.excel.StudentInfoExcelReaderListener;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOError;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class StudentInfoService {
......@@ -30,7 +42,12 @@ public class StudentInfoService {
@Resource
SpecialSqlMapper specialSqlMapper;
@Resource
StudentPointService studentPointService;
public static final String STRING_DEFAULT_HEAD = "{{default}}";
public static final String URL_DEFAULT_HEAD = "https://storage.exploring.cn/class-work/static/img/head-student-default.png";
/**
* 添加
......@@ -99,6 +116,14 @@ public class StudentInfoService {
}
public List<StudentInfo> selectStudentInfoByUniqueId(Collection<String> uniqueIdList) {
StudentInfoExample example = new StudentInfoExample();
StudentInfoExample.Criteria criteria = example.createCriteria();
criteria.andDeletedEqualTo(false).andStudentUniqueIdIn(new ArrayList<>(uniqueIdList));
return studentInfoMapper.selectByExample(example);
}
/**
* 检索学生相关的信息
* @param studentName
......@@ -127,7 +152,7 @@ public class StudentInfoService {
* @param studentId
* @return
*/
public Map selectStudentInfoByWeb(String studentName, String studentId, List<Integer> idList, Integer classId, Integer page, Integer limit, String sort, String order) {
public Map selectStudentInfoByWeb(String studentName, String studentId, List<Integer> idList, Integer classId, Integer page, Integer limit, String sort, String order, Integer minCall, Integer maxCall) {
StudentInfoExample example = new StudentInfoExample();
StudentInfoExample.Criteria criteria = example.createCriteria();
criteria.andDeletedEqualTo(false);
......@@ -143,14 +168,38 @@ public class StudentInfoService {
if (classId != null) {
criteria.andClassIdEqualTo(classId);
}
Map<Integer, Integer> callTimeMap = studentPointService.countByClassGroupByStudent(classId);
if (Objects.nonNull(minCall) || Objects.nonNull(maxCall)) {
criteria.andIdNotIn(callTimeMap.entrySet().stream()
.filter(entry -> (Objects.nonNull(minCall) && entry.getValue() < minCall)
|| (Objects.nonNull(maxCall) && entry.getValue() > maxCall))
.map(Map.Entry::getKey).collect(Collectors.toList()));
}
PageHelper.startPage(page, limit);
example.setOrderByClause(sort + " " + order);
List<StudentInfo> studentInfoList = studentInfoMapper.selectByExample(example);
if (CollectionUtils.isEmpty(studentInfoList)) {
return ImmutableMap.of("total", 0 , "items", Collections.emptyList());
}
Map<Integer, String> classMap = classService.getClassNameMap();
List<StudentVo> dataList = studentInfoList.stream().map(x -> {
StudentVo voData = new StudentVo();
BeanUtils.copyProperties(x, voData);
voData.setClassName(classMap.get(x.getClassId()));
voData.setCallTimes(callTimeMap.getOrDefault(x.getId(), 0));
if (StringUtils.isEmpty(x.getHead()) || x.getHead().trim().equalsIgnoreCase(STRING_DEFAULT_HEAD)) {
voData.setHead(URL_DEFAULT_HEAD);
}
return voData;
}).collect(Collectors.toList());
Long total = PageInfo.of(studentInfoList).getTotal();
......@@ -160,6 +209,105 @@ public class StudentInfoService {
}};
}
/**
* 查询可以被点名的学生名单
* @param classId 班级ID
* @param maxCall 排除点名达到指定次数的学生(null为不排除)
* @return 查询到的list和总数信息
*/
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public Map<String, Object> listForCall(Integer classId, Integer maxCall) {
Set<Integer> excluded = null;
if (Objects.nonNull(maxCall) && maxCall > 0) {
excluded = studentPointService.countByClassGroupByStudent(classId).entrySet().stream().filter(entry -> entry.getValue() >= maxCall).map(Map.Entry::getKey).collect(Collectors.toSet());
}
StudentInfoExample.Criteria criteria = new StudentInfoExample().createCriteria()
.andDeletedEqualTo(StudentInfo.NOT_DELETED)
.andClassIdEqualTo(classId);
if (Objects.nonNull(maxCall) && maxCall > 0) {
criteria.andIdNotIn(studentPointService.countByClassGroupByStudent(classId).entrySet().stream().filter(entry -> entry.getValue() >= maxCall).map(Map.Entry::getKey).collect(Collectors.toList()));
}
List<StudentInfo> queryList = studentInfoMapper.selectByExample(criteria.example())
.stream().peek(x -> {
if (StringUtils.isEmpty(x.getHead()) || x.getHead().trim().equalsIgnoreCase(STRING_DEFAULT_HEAD)) {
x.setHead(URL_DEFAULT_HEAD);
}
}).collect(Collectors.toList());
return ImmutableMap.of("total", queryList.size(), "items", queryList);
}
public List<StudentInfoExcelImportVo> importByExcel(MultipartFile file) throws IOException {
ExcelReaderListener<StudentInfoExcelImportVo> listener = new StudentInfoExcelReaderListener();
EasyExcel.read(file.getInputStream(), StudentInfoExcelImportResultVo.class, listener).sheet().doRead();
return listener.getAddList();
}
public List<StudentInfoExcelImportResultVo> checkImportByExcel(Collection<StudentInfoExcelImportVo> importVoList, Integer classId) {
Map<String, StudentInfo> existStudent = selectStudentInfoByUniqueId(importVoList.stream().map(StudentInfoExcelImportVo::getStudentUid).filter(org.apache.commons.lang3.StringUtils::isNotBlank).collect(Collectors.toSet()))
.stream().collect(Collectors.toMap(StudentInfo::getStudentUniqueId, x -> x, (k1, k2) -> k1));
Map<Integer, String> classNameMap = classService.listAll().stream().collect(Collectors.toMap(ClassInfo::getId, ClassInfo::getClassName));
return importVoList.stream().map(vo -> {
List<String> err = new ArrayList<>();
if (org.apache.commons.lang3.StringUtils.isBlank(vo.getStudentUid())) {
err.add("学号不能为空");
}
if (org.apache.commons.lang3.StringUtils.isBlank(vo.getName())) {
err.add("姓名不能为空");
}
if (org.apache.commons.lang3.StringUtils.isNotBlank(vo.getStudentUid()) && existStudent.containsKey(vo.getStudentUid())) {
StudentInfo exist = existStudent.get(vo.getStudentUid());
err.add(String.format("学号[%s]已存在,对应学生信息:[班级: '%s', 姓名: '%s']", vo.getStudentUid(), classNameMap.getOrDefault(exist.getClassId(), ""), exist.getName()));
}
if (CollectionUtils.isEmpty(err)) {
return null;
}
StudentInfoExcelImportResultVo result = new StudentInfoExcelImportResultVo(vo);
result.setErrMsg(err);
return result;
}).filter(Objects::nonNull).collect(Collectors.toList());
}
@Transactional(rollbackFor = Exception.class)
public List<StudentInfoExcelImportResultVo> handleImport(Collection<? extends StudentInfoExcelImportVo> voList, Integer classId) {
LocalDateTime now = LocalDateTime.now();
return voList.stream().map(vo -> {
StudentInfo record = new StudentInfo() {{
setName(vo.getName());
setClassId(classId);
setStudentUniqueId(vo.getStudentUid());
setAddTime(now);
setUpdateTime(now);
setDeleted(StudentInfo.NOT_DELETED);
setHead(STRING_DEFAULT_HEAD);
}};
studentInfoMapper.insert(record);
StudentInfoExcelImportResultVo result = new StudentInfoExcelImportResultVo(vo);
result.setId(record.getId());
return result;
}).collect(Collectors.toList());
}
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public List<StudentInfo> selectByPrimaryKey(Collection<Integer> idList) {
return CollectionUtils.isEmpty(idList) ? Collections.emptyList()
: studentInfoMapper.selectByExample(new StudentInfoExample().createCriteria()
.andDeletedEqualTo(StudentInfo.NOT_DELETED)
.andIdIn(new ArrayList<>(idList))
.example());
}
}
......@@ -2,17 +2,25 @@ 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.StudentInfoExcelImportVo;
import cn.exploring.engine.server.db.domain.vo.StudentPointExcelExportVo;
import cn.exploring.engine.server.db.util.Util;
import com.alibaba.excel.EasyExcel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import javax.print.attribute.IntegerSyntax;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
......@@ -25,6 +33,9 @@ public class StudentPointService {
@Resource
SpecialSqlMapper specialSqlMapper;
@Resource
StudentInfoService studentInfoService;
public Map selectPointByWeb( String studentName, String studentUniqueId, Integer classId, String startTime, String endTime, Integer page, Integer limit, String sort, String order) {
List<Map> dataList = specialSqlMapper.selectPoint(studentName, studentUniqueId, classId, startTime, endTime, page, limit, sort, order);
Map<String, String> countInfoData = specialSqlMapper.selectPointCount(studentName, studentUniqueId, classId, startTime, endTime);
......@@ -77,4 +88,70 @@ public class StudentPointService {
return "batch deleted OK";
}
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public Map<Integer, List<StudentPointInfo>> selectByClassGroupByStudent(Integer classId) {
StudentPointInfoExample.Criteria criteria = new StudentPointInfoExample().createCriteria()
.andDeletedEqualTo(StudentPointInfo.NOT_DELETED);
if (Objects.nonNull(classId)) {
criteria.andClassIdEqualTo(classId);
}
return studentPointInfoMapper.selectByExample(criteria.example())
.stream().collect(Collectors.groupingBy(StudentPointInfo::getStudentId, Collectors.toList()));
}
public Map<Integer, Integer> countByClassGroupByStudent(Integer classId) {
return selectByClassGroupByStudent(classId).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().size()));
}
@Transactional(rollbackFor = Exception.class)
public void removeAll() {
studentPointInfoMapper.updateByExampleSelective(new StudentPointInfo(){{
setUpdateTime(LocalDateTime.now());
setDeleted(StudentPointInfo.IS_DELETED);
}} , new StudentPointInfoExample().createCriteria()
.andDeletedEqualTo(StudentPointInfo.NOT_DELETED)
.example());
}
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public List<StudentPointInfo> listByClassId(Integer classId) {
return studentPointInfoMapper.selectByExample(new StudentPointInfoExample().createCriteria()
.andDeletedEqualTo(StudentPointInfo.NOT_DELETED)
.andClassIdEqualTo(classId)
.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 File transToExcelFile(List<StudentPointExcelExportVo> voList) {
File file = null;
try {
file = File.createTempFile("TMP-EXCEL-", ".xlsx");
EasyExcel.write(file, StudentPointExcelExportVo.class).sheet().doWrite(voList);
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
}
package cn.exploring.engine.server.db.util.excel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;
public class ExcelReaderListener<T> extends AnalysisEventListener<T> {
List<T> addList = new ArrayList<>();
public List<T> getAddList() {
return addList;
}
public void setAddList(List<T> addList) {
this.addList = addList;
}
@Override
public void invoke(T t, AnalysisContext analysisContext) {
addList.add(t);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
package cn.exploring.engine.server.db.util.excel;
import cn.exploring.engine.server.db.domain.vo.StudentInfoExcelImportVo;
import com.alibaba.excel.context.AnalysisContext;
public class StudentInfoExcelReaderListener extends ExcelReaderListener<StudentInfoExcelImportVo>{
public void invoke(StudentInfoExcelImportVo t, AnalysisContext analysisContext) {
t.setRowNumber(analysisContext.readRowHolder().getRowIndex());
addList.add(t);
}
}
......@@ -40,6 +40,11 @@ public class ClassController {
return ResponseUtil.ok(classService.batchDeletedData(idList));
}
@GetMapping("/listAll")
public Object listAll() {
return ResponseUtil.ok(classService.listAll());
}
......
package cn.exploring.engine.server.wx.web;
import cn.exploring.engine.server.core.storage.StorageService;
import cn.exploring.engine.server.core.util.ResponseUtil;
import cn.exploring.engine.server.db.domain.StudentPointInfo;
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.*;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
@RestController
@RequestMapping("/wx/point")
......@@ -18,6 +26,12 @@ public class PointController {
@Resource
StudentPointService pointService;
@Resource
StudentInfoService studentInfoService;
@Resource
StorageService storageService;
@GetMapping("selectPoint")
public Object selectPoint(@RequestParam String studentName, @RequestParam String studentUniqueId, @RequestParam Integer classId, @RequestParam String startTime, @RequestParam String endTime,
@RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit,
......@@ -48,9 +62,41 @@ public class PointController {
return ResponseUtil.ok(pointService.batchDeleted(idList));
}
@PostMapping("removeAll")
public Object removeAll() {
pointService.removeAll();
return ResponseUtil.ok();
}
@PostMapping("getStuList")
public Object getStuList(@RequestParam Integer classId, @RequestParam Integer maxCall) {
return ResponseUtil.ok(studentInfoService.listForCall(classId, maxCall));
}
@GetMapping("export")
public Object export(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) {
}
}
}
}
}
package cn.exploring.engine.server.wx.web;
import cn.exploring.engine.server.core.util.ResponseUtil;
import cn.exploring.engine.server.db.domain.ClassInfo;
import cn.exploring.engine.server.db.domain.StudentInfo;
import cn.exploring.engine.server.db.domain.vo.StudentInfoExcelImportResultVo;
import cn.exploring.engine.server.db.domain.vo.StudentInfoExcelImportVo;
import cn.exploring.engine.server.db.service.ClassService;
import cn.exploring.engine.server.db.service.StudentInfoService;
import com.google.common.collect.ImmutableMap;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
@RestController
@RequestMapping("/wx/student")
......@@ -17,12 +27,16 @@ public class StudentController {
@Resource
StudentInfoService studentInfoService;
@Resource
ClassService classService;
@GetMapping("selectStudent")
public Object selectClass(@RequestParam String studentName, @RequestParam String studentUniqueId, @RequestParam Integer classId,
public Object selectClass(@RequestParam String studentName, @RequestParam String studentUniqueId, @Nullable @RequestParam Integer classId,
@Nullable @RequestParam Integer minCall, @Nullable @RequestParam Integer maxCall,
@RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit,
@RequestParam(required = false, defaultValue = "add_time") String sort, @RequestParam(required = false, defaultValue = "desc") String order) {
return ResponseUtil.ok(studentInfoService.selectStudentInfoByWeb(studentName, studentUniqueId, null, classId, page, limit, sort, order));
return ResponseUtil.ok(studentInfoService.selectStudentInfoByWeb(studentName, studentUniqueId, null, classId, page, limit, sort, order, minCall, maxCall));
}
@GetMapping("randomSelect")
......@@ -47,4 +61,33 @@ public class StudentController {
public Object batchDeleted(@RequestBody List<Integer> idList) {
return ResponseUtil.ok(studentInfoService.batchDeleted(idList));
}
@PostMapping("import")
public Object handleImport(@RequestParam("file") MultipartFile file, @RequestParam Integer classId) {
if (Objects.isNull(classService.selectByPrimaryKey(classId))) {
return ResponseUtil.fail(407, "班级不存在");
}
List<StudentInfoExcelImportVo> voList;
try {
voList = studentInfoService.importByExcel(file);
} catch (Exception e) {
e.printStackTrace();
return ResponseUtil.fail(407, "文件打开失败");
}
if (CollectionUtils.isEmpty(voList)) {
return ResponseUtil.fail(407, "未能读取到有效的数据");
}
List<StudentInfoExcelImportResultVo> errors = studentInfoService.checkImportByExcel(voList, classId);
if (!CollectionUtils.isEmpty(errors)) {
return ResponseUtil.fail(408, "文件存在错误,请检查", errors);
}
List<StudentInfoExcelImportResultVo> list = studentInfoService.handleImport(voList, classId);
return ResponseUtil.ok(ImmutableMap.of("total", list.size(), "list", list));
}
}
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