277 lines
11 KiB
Java
277 lines
11 KiB
Java
package com.yunhai.bschclassmanage.service.impl;
|
|
|
|
import com.yunhai.bschclassmanage.Repository.SchBinSchsystemRepository;
|
|
import com.yunhai.bschclassmanage.entity.BaseSchsystem;
|
|
import com.yunhai.bschclassmanage.entity.SchBinSchsystem;
|
|
import com.yunhai.bschclassmanage.enums.AcademicStage;
|
|
import com.yunhai.bschclassmanage.enums.NodeType;
|
|
import com.yunhai.bschclassmanage.enums.SchType;
|
|
import com.yunhai.bschclassmanage.model.entityField.EntityFieldNames;
|
|
import com.yunhai.bschclassmanage.model.vo.AcademicStageSchsystemVO;
|
|
import com.yunhai.bschclassmanage.model.vo.SchSystemVO;
|
|
import com.yunhai.bschclassmanage.model.vo.SelectIntVO;
|
|
import com.yunhai.bschclassmanage.model.vo.TreeSelectVO;
|
|
import com.yunhai.bschclassmanage.service.IBaseSchsystemGradeService;
|
|
import com.yunhai.bschclassmanage.service.IBaseSchsystemService;
|
|
import com.yunhai.bschclassmanage.service.ISchBinSchsystemService;
|
|
import com.yunhai.bschclassmanage.service.ISyncUserBaseService;
|
|
import com.yunhai.bschclassmanage.utils.RedisConstants;
|
|
import com.yunhai.bschclassmanage.utils.RedisUtil;
|
|
import com.yunhai.platform.utils.exception.DException;
|
|
import com.yunhai.platform.utils.model.BoaGroup;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import javax.persistence.criteria.Predicate;
|
|
import java.lang.reflect.Array;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Service
|
|
public class SchBinSchsystemService implements ISchBinSchsystemService {
|
|
|
|
@Autowired
|
|
private SchBinSchsystemRepository schBinSchsystemRepository;
|
|
|
|
@Autowired
|
|
private IBaseSchsystemService baseSchsystemService;
|
|
|
|
@Autowired
|
|
private ISyncUserBaseService syncUserBaseService;
|
|
|
|
@Autowired
|
|
private RedisUtil redisUtil;
|
|
|
|
public List<SchBinSchsystem> getSchBinSchsystemList(Integer schId){
|
|
Specification specification = (Specification) (root, criteriaQuery, criteriaBuilder) -> {
|
|
List<Predicate> predicates = new ArrayList<>();
|
|
predicates.add(criteriaBuilder.equal(root.get(EntityFieldNames.schId),schId));
|
|
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
};
|
|
return schBinSchsystemRepository.findAll(specification);
|
|
}
|
|
|
|
public List<Integer> getSchIds(){
|
|
return schBinSchsystemRepository.findAll().stream().map(s->s.getSchId()).distinct().collect(Collectors.toList());
|
|
}
|
|
|
|
|
|
public List<Integer> getSchBinSchsystemIds(Integer schId){
|
|
return getSchBinSchsystemList(schId).stream().map(s->s.getSchSystemId()).collect(Collectors.toList());
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 获取学校关联的学段与学制映射(一个学段仅对应一个学制)
|
|
* @param schId 学校ID
|
|
* @return Map<学段ID, 学制ID>
|
|
*/
|
|
public Map<Integer, Integer> getAcademicStageToSchsystemMap(Integer schId) {
|
|
List<Integer> schsystemIds = getSchBinSchsystemIds(schId);
|
|
return baseSchsystemService.getSchsystemList().stream()
|
|
.filter(s -> schsystemIds.contains(s.getId()))
|
|
.collect(Collectors.toMap(
|
|
BaseSchsystem::getAcademicStage, // key: 学段ID
|
|
BaseSchsystem::getId, // value: 学制ID
|
|
(existing, replacement) -> existing // 若有重复,保留第一个(理论上不会发生)
|
|
));
|
|
}
|
|
|
|
public List<AcademicStageSchsystemVO> getAcademicStageSchsystemList(Integer schId) {
|
|
List<AcademicStage> academicStageEnumList = AcademicStage.list();
|
|
List<BaseSchsystem> baseSchSystemList = baseSchsystemService.getSchsystemList();
|
|
Set<Integer> checkedIdSet = new HashSet<>(getSchBinSchsystemIds(schId));
|
|
|
|
Map<Integer, List<BaseSchsystem>> groupByStage = baseSchSystemList.stream()
|
|
.filter(s -> s.getAcademicStage() != null)
|
|
.collect(Collectors.groupingBy(BaseSchsystem::getAcademicStage));
|
|
|
|
return academicStageEnumList.stream()
|
|
.map(stageEnum -> {
|
|
List<BaseSchsystem> stageSystems = groupByStage.getOrDefault(stageEnum.getKey(), Collections.emptyList());
|
|
List<SchSystemVO> schSystemVOList = stageSystems.stream()
|
|
.map(sys -> SchSystemVO.builder()
|
|
.id(sys.getId())
|
|
.name(sys.getName())
|
|
.checked(checkedIdSet.contains(sys.getId()))
|
|
.build())
|
|
.collect(Collectors.toList());
|
|
|
|
return AcademicStageSchsystemVO.builder()
|
|
.academicStageId(stageEnum.getKey())
|
|
.academicStageName(stageEnum.getValue())
|
|
.academicStageList(schSystemVOList)
|
|
.build();
|
|
})
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
public void saveSchBinSchsystem(SchBinSchsystem entity, Integer[] schsystemIds) {
|
|
|
|
Integer schId=entity.getSchId();
|
|
|
|
List<SchBinSchsystem> existingList = getSchBinSchsystemList(schId);
|
|
if (!existingList.isEmpty()) {
|
|
schBinSchsystemRepository.deleteAll(existingList);
|
|
}
|
|
|
|
List<SchBinSchsystem> newList = new ArrayList<>(schsystemIds.length);
|
|
for (Integer systemId : schsystemIds) {
|
|
if (systemId == null) {
|
|
continue;
|
|
}
|
|
SchBinSchsystem newEntity = new SchBinSchsystem(); // ← 每次 new 一个新对象
|
|
// 如果需要复制 entity 的其他属性
|
|
BeanUtils.copyProperties(entity, newEntity);
|
|
newEntity.setSchSystemId(systemId);
|
|
newList.add(newEntity);
|
|
}
|
|
|
|
if (!newList.isEmpty()) {
|
|
schBinSchsystemRepository.saveAll(newList);
|
|
}
|
|
String cacheKey = RedisConstants.SCHOOL_GRAGE+"_"+schId;
|
|
SchGroupInitialization(schId);
|
|
redisUtil.del(cacheKey);
|
|
}
|
|
|
|
|
|
//获取学校类型
|
|
public Integer getSchType(Integer schId) {
|
|
Set<Integer> schsystemIds = getSchBinSchsystemList(schId)
|
|
.stream()
|
|
.map(s -> s.getSchSystemId())
|
|
.collect(Collectors.toSet());
|
|
|
|
Set<Integer> typeIds = baseSchsystemService.getSchsystemList()
|
|
.stream()
|
|
.filter(s -> schsystemIds.contains(s.getId()))
|
|
.map(s -> s.getType())
|
|
.collect(Collectors.toSet());
|
|
|
|
// 校验:类型必须唯一
|
|
if (typeIds.isEmpty()) {
|
|
throw new DException("该学校未绑定任何学制,请先绑定");
|
|
}
|
|
if (typeIds.size() > 1) {
|
|
throw new DException("该学校绑定了多种学制类型(" + typeIds + "),数据异常,请检查");
|
|
}
|
|
|
|
// 此时 typeIds 仅有一个元素,直接获取
|
|
Integer typeId = typeIds.iterator().next();
|
|
|
|
if(typeId.equals(SchType.VOCATIONALEDUCATION.getKey())&&schsystemIds.size()>1){
|
|
throw new DException("该学校只能选择单个学制,请重新选择学制!");
|
|
}
|
|
|
|
// 从枚举中匹配该 typeId
|
|
return SchType.list().stream()
|
|
.filter(s -> s.getKey().equals(typeId))
|
|
.findFirst()
|
|
.orElseThrow(() -> new DException("未找到对应的学制类型,typeId=" + typeId))
|
|
.getKey();
|
|
}
|
|
|
|
|
|
|
|
public List<SelectIntVO> selectSchBinSchsystem(Integer schId) {
|
|
// 1. 获取学校绑定的学制 ID 集合
|
|
List<SchBinSchsystem> binList = getSchBinSchsystemList(schId);
|
|
if (binList == null || binList.isEmpty()) {
|
|
return Collections.emptyList();
|
|
}
|
|
Set<Integer> schsystemIds = binList.stream()
|
|
.map(SchBinSchsystem::getSchSystemId)
|
|
.collect(Collectors.toSet());
|
|
|
|
// 2. 获取全部学制并过滤出绑定的
|
|
List<BaseSchsystem> allSystems = baseSchsystemService.getSchsystemList();
|
|
if (allSystems == null || allSystems.isEmpty()) {
|
|
return Collections.emptyList();
|
|
}
|
|
|
|
// 3. 转换为 SchSystemVO 列表,checked 固定为 true
|
|
return allSystems.stream()
|
|
.filter(s -> schsystemIds.contains(s.getId()))
|
|
.map(s -> {
|
|
SelectIntVO vo = new SelectIntVO();
|
|
vo.setId(s.getId());
|
|
vo.setName(s.getName());
|
|
return vo;
|
|
})
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
|
|
public Map<Integer, String> getSchBinSchsystemMap(Integer schId) {
|
|
List<SelectIntVO> list= selectSchBinSchsystem(schId);
|
|
return list.stream()
|
|
.collect(Collectors.toMap(
|
|
SelectIntVO::getId, // key
|
|
SelectIntVO::getName, // value
|
|
(oldVal, newVal) -> oldVal // 若key冲突,保留旧值(理论上不会冲突)
|
|
));
|
|
}
|
|
|
|
//学制树:第一层学段(value=学段id,title=学段名称),第二层学制(value=学制id,title=学制名称)
|
|
public List<TreeSelectVO> selectSchsystemTree() {
|
|
List<AcademicStageSchsystemVO> cached = getAcademicStageSchsystemList(null);
|
|
List<TreeSelectVO> list = new ArrayList<>();
|
|
for (AcademicStageSchsystemVO item : cached) {
|
|
List<TreeSelectVO> children = new ArrayList<>();
|
|
if (item.getAcademicStageList() != null) {
|
|
for (SchSystemVO sys : item.getAcademicStageList()) {
|
|
children.add(TreeSelectVO.builder()
|
|
.id(sys.getId())
|
|
.label(sys.getName())
|
|
.build());
|
|
}
|
|
}
|
|
list.add(TreeSelectVO.builder()
|
|
.id(item.getAcademicStageId())
|
|
.label(item.getAcademicStageName())
|
|
.children(children)
|
|
.build());
|
|
}
|
|
return list;
|
|
}
|
|
|
|
private void SchGroupInitialization(Integer schId)
|
|
{
|
|
//添加学生组
|
|
BoaGroup teaGroup= syncUserBaseService.addGroup("教师组",schId,1,NodeType.TEACHER);
|
|
BoaGroup stuGroup= syncUserBaseService.addGroup("学生组",schId,2,NodeType.STUDENT);
|
|
|
|
//创建校长室
|
|
syncUserBaseService.addGroup("校长室", teaGroup.getId(),1,NodeType.NORMAL);
|
|
|
|
//获取学制
|
|
List<SchBinSchsystem> schsystemBinList=getSchBinSchsystemList(schId);
|
|
Map<Integer, String> schsystemMap=getSchBinSchsystemMap(schId);
|
|
if(schsystemBinList.size()==1)
|
|
{
|
|
|
|
}else if(schsystemBinList.size()>1)
|
|
{
|
|
Integer scort=1;
|
|
for (SchBinSchsystem schsystem:schsystemBinList) {
|
|
//添加学段组
|
|
BoaGroup sectionGroup= syncUserBaseService.addGroup(schsystemMap.get(schsystem.getId()),stuGroup.getId(),scort,NodeType.SECTION);
|
|
schsystem.setGroupId(sectionGroup.getId());
|
|
scort++;
|
|
}
|
|
}else {
|
|
throw new DException("请选择学制!");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|