Commit 314dd3c9 authored by Ryan Loong's avatar Ryan Loong

update 删除班级前校验班级是否有学生

parent 192df78f
......@@ -3,11 +3,14 @@ package cn.exploring.engine.server.db.service;
import cn.exploring.engine.server.db.dao.ClassInfoMapper;
import cn.exploring.engine.server.db.domain.ClassInfo;
import cn.exploring.engine.server.db.domain.ClassInfoExample;
import cn.exploring.engine.server.db.domain.StudentInfo;
import cn.exploring.engine.server.db.util.StrUtil;
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.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
......@@ -16,6 +19,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@Service
public class ClassService {
......@@ -23,6 +27,9 @@ public class ClassService {
@Resource
ClassInfoMapper classInfoMapper;
@Resource
private StudentInfoService studentInfoService;
public Map<Integer, String> getClassNameMap() {
ClassInfoExample example = new ClassInfoExample();
ClassInfoExample.Criteria criteria = example.createCriteria();
......@@ -95,13 +102,21 @@ public class ClassService {
classInfoMapper.updateByPrimaryKey(classInfo);
}
public Object batchDeletedData(List<Integer> idList) {
public String batchDeletedData(List<Integer> idList) {
List<ClassInfo> classInfoList = selectClass(null, idList);
Map<Integer, String> classNameMap = classInfoList.stream().collect(Collectors.toMap(ClassInfo::getId, ClassInfo::getClassName));
Map<Integer, List<StudentInfo>> classStudentMap = studentInfoService.selectStudentByClassIdListGroupByClassId(idList);
if (!CollectionUtils.isEmpty(classStudentMap) && classStudentMap.values().stream().anyMatch(list -> !CollectionUtils.isEmpty(list))) {
return classStudentMap.entrySet().stream().filter(entry -> !CollectionUtils.isEmpty(entry.getValue()))
.map(entry -> StrUtil.format("班级[({}){}]存在学生{}名无法删除", entry.getKey(), classNameMap.get(entry.getKey()), entry.getValue().size()))
.collect(Collectors.joining(", "));
}
classInfoList.forEach(x -> {
x.setDeleted(null);
updateClassInfo(x);
});
return "batch deleted OK";
return null;
}
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
......
......@@ -26,6 +26,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
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 org.springframework.web.multipart.MultipartFile;
......@@ -413,4 +414,14 @@ public class StudentInfoService {
throw new RuntimeException("更新点名次数失败");
}
}
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public Map<Integer, List<StudentInfo>> selectStudentByClassIdListGroupByClassId(Collection<Integer> classIdList) {
return CollectionUtils.isEmpty(classIdList) ? ImmutableMap.of()
: studentInfoMapper.selectByExample(new StudentInfoExample().createCriteria()
.andDeletedEqualTo(StudentInfo.NOT_DELETED)
.andClassIdIn(new ArrayList<>(classIdList))
.example())
.stream().collect(Collectors.groupingBy(StudentInfo::getClassId, Collectors.toList()));
}
}
......@@ -3,6 +3,7 @@ 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.service.ClassService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
......@@ -37,7 +38,8 @@ public class ClassController {
@PostMapping("batchDeleted")
public Object batchDeleted(@RequestBody List<Integer> idList) {
return ResponseUtil.ok(classService.batchDeletedData(idList));
String errMsg = classService.batchDeletedData(idList);
return StringUtils.isNotBlank(errMsg) ? ResponseUtil.ok("成功") : ResponseUtil.fail(402, errMsg);
}
@GetMapping("/listAll")
......
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