Commit 3a4bf0e2 authored by Ryan Loong's avatar Ryan Loong

优化学生查询逻辑

parent 8b602ffa
......@@ -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;
}
}
......@@ -8,9 +8,11 @@ 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.SqlUtil;
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.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.ImmutableMap;
......@@ -157,10 +159,10 @@ public class StudentInfoService {
StudentInfoExample.Criteria criteria = example.createCriteria();
criteria.andDeletedEqualTo(false);
if (!StringUtils.isEmpty(studentName)) {
criteria.andNameLike("%" + studentName + "%");
criteria.andNameLike(SqlUtil.likeStr(studentName));
}
if (!StringUtils.isEmpty(studentId)) {
criteria.andStudentUniqueIdLike("%" + studentId + "%");
criteria.andStudentUniqueIdLike(SqlUtil.likeStr(studentId));
}
if (idList != null && idList.size() > 0) {
criteria.andIdIn(idList);
......@@ -169,34 +171,44 @@ public class StudentInfoService {
criteria.andClassIdEqualTo(classId);
}
PageHelper.startPage(page, limit);
example.setOrderByClause(sort + " " + order);
Map<Integer, Integer> callTimeMap = studentPointService.countByClassGroupByStudent(classId);
if (Objects.nonNull(minCall) || Objects.nonNull(maxCall)) {
List<Integer> excluded = callTimeMap.entrySet().stream()
.filter(entry -> (Objects.nonNull(minCall) && entry.getValue() < minCall)
|| (Objects.nonNull(maxCall) && entry.getValue() > maxCall))
.map(Map.Entry::getKey).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(excluded)) {
criteria.andIdNotIn(excluded);
}
}
List<StudentInfo> studentInfoList = studentInfoMapper.selectByExample(example);
try (Page<?> ignored = PageHelper.startPage(page, limit, sort + " " + order)) {
List<StudentInfo> studentInfoList = studentInfoMapper.selectByExample(example);
if (CollectionUtils.isEmpty(studentInfoList)) {
return ImmutableMap.of("total", 0 , "items", Collections.emptyList());
}
if (CollectionUtils.isEmpty(studentInfoList)) {
return ImmutableMap.of("total", 0 , "items", Collections.emptyList());
}
Map<Integer, String> classMap = classService.getClassNameMap();
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()));
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();
return ImmutableMap.of("total", total, "items", dataList);
}
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();
return new HashMap(){{
put("total", total);
put("items", dataList);
}};
}
// /**
......
......@@ -101,6 +101,10 @@ public class StudentPointService {
.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(){{
......
package cn.exploring.engine.server.db.util;
import org.apache.commons.lang3.StringUtils;
import java.util.Optional;
public class SqlUtil {
public static String likeStr(String str) {
return Optional.ofNullable(str)
.filter(StringUtils::isNotBlank)
.map(String::trim)
.map(s -> StrUtil.format("%{}%", s.replaceAll("\\s+", "%")))
.orElse("%");
}
}
package cn.exploring.engine.server.db.util;
import org.apache.logging.log4j.message.ParameterizedMessage;
import java.util.Objects;
public class StrUtil {
public static String format(String messagePattern, Object... arguments) {
if (Objects.nonNull(arguments) && arguments.length == 1 && Objects.nonNull(arguments[0]) && arguments[0].getClass().isArray()) {
arguments = (Object[]) arguments[0];
}
return ParameterizedMessage.format(messagePattern, arguments);
}
}
......@@ -69,12 +69,6 @@ public class PointController {
}
// @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);
......
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