Bläddra i källkod

成绩均衡分班

dzx 1 år sedan
förälder
incheckning
c1f2d56e09

+ 85 - 51
src/main/java/com/xjrsoft/module/banding/service/impl/BandingTaskServiceImpl.java

@@ -31,8 +31,10 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -111,13 +113,7 @@ public class BandingTaskServiceImpl extends MPJBaseServiceImpl<BandingTaskMapper
                         .orderByDescStr(orderColumn)
         );
         //2、查询所有班级信息
-        List<BandingTaskClass> classList = taskClassMapper.selectList(
-                new QueryWrapper<BandingTaskClass>().lambda()
-                        .select(BandingTaskClass::getId)
-                        .select(BandingTaskClass.class, x -> VoToColumnUtil.fieldsToColumns(BandingTaskClass.class).contains(x.getProperty()))
-                        .eq(BandingTaskClass::getBandingTaskId, bandingTask.getId())
-                        .orderByAsc(BandingTaskClass::getSortCode)
-        );
+        List<BandingTaskClass> classList = taskClassMapper.getListOrderByAsc(bandingTask.getId());
 
         //3、查询所用到的规则
         List<BandingRule> ruleList = ruleMapper.selectJoinList(BandingRule.class,
@@ -132,12 +128,12 @@ public class BandingTaskServiceImpl extends MPJBaseServiceImpl<BandingTaskMapper
         //包含下面个条件,则需要计算每个班级应该分配的人数
         Map<Long, Integer> classLimitMap = new HashMap<>();
         if(ruleCodes.contains("BR0004")){
-            //查询每个专业下面的班级人数
-            Map<Long, Integer> majorClassStudentCount = taskClassMapper.getMajorClassStudentCount(bandingTask.getId())
-                    .stream().collect(Collectors.toMap(IdCountVo::getId, IdCountVo::getCount));
             //查询每个专业下面有多少个班级
             Map<Long, Integer> majorClassCount = taskClassMapper.getMajorClassCount(bandingTask.getId())
                     .stream().collect(Collectors.toMap(IdCountVo::getId, IdCountVo::getCount));
+            //查询每个专业下面的班级人数
+            Map<Long, Integer> majorClassStudentCount = taskClassMapper.getMajorClassStudentCount(bandingTask.getId())
+                    .stream().collect(Collectors.toMap(IdCountVo::getId, IdCountVo::getCount));
             //查询每个专业人数
             Map<Long, Integer> majorStudentCount = newStudentService.getMajorStudentCount()
                     .stream().collect(Collectors.toMap(IdCountVo::getId, IdCountVo::getCount));
@@ -170,46 +166,9 @@ public class BandingTaskServiceImpl extends MPJBaseServiceImpl<BandingTaskMapper
             classConditionMap.put(conditionDto.getMajorSetId(), conditionDto);
         }
 
-
-        Map<Long, List<BaseNewStudent>> majorStudentMap = new HashMap<>();
+        Map<Long, List<BaseNewStudent>> classStudentMap = new HashMap<>();
         if(ruleCodes.contains("BR0002")){
-            /**
-             * 1、先把每个专业匹配的学生分组存一起,并按照分数高低排序
-             *
-             */
-            for (Long majorSetId : classConditionMap.keySet()) {
-                List<BaseNewStudent> stuList = new ArrayList<>();
-                for (BaseNewStudent newStudent : baseNewStudents) {
-                    if(!Objects.equals(majorSetId, newStudent.getFirstAmbitionId()) && !Objects.equals(majorSetId, newStudent.getSecondAmbitionId())){
-                        continue;
-                    }
-
-                    List<Boolean> conditionList = new ArrayList<>();
-                    BandingTaskMajorCondition condition = classConditionMap.get(majorSetId);
-                    if(condition.getHeight() !=null && newStudent.getHeight() != null && newStudent.getHeight().compareTo(condition.getHeight()) >= 0 ){
-                        conditionList.add(true);
-                    }else if(condition.getHeight() !=null && newStudent.getHeight() != null && newStudent.getHeight().compareTo(condition.getHeight()) < 0){
-                        conditionList.add(false);
-                    }else if(condition.getHeight() !=null && newStudent.getHeight() == null){
-                        conditionList.add(false);
-                    }
-
-                    if(condition.getScore() !=null && newStudent.getScore() != null && newStudent.getScore().compareTo(condition.getScore()) >= 0 ){
-                        conditionList.add(true);
-                    }else if(condition.getScore() !=null && newStudent.getScore() != null && newStudent.getScore().compareTo(condition.getScore()) < 0){
-                        conditionList.add(false);
-                    }else if(condition.getScore() !=null && newStudent.getScore() == null){
-                        conditionList.add(false);
-                    }
-                    //如果包含false,则表明不符合条件,这个学生跳过
-                    if(conditionList.contains(false)){
-                        continue;
-                    }
-                    stuList.add(newStudent);
-                }
-                stuList.sort((x1, x2) -> x1.getScore().compareTo(x2.getScore()));
-                majorStudentMap.put(majorSetId, stuList);
-            }
+            classStudentMap.putAll(divideStudentByScore(classConditionMap, baseNewStudents, classList));
         }
         //存班级和学生的关系
         Map<Long, Long> studentClassMap = new HashMap<>();
@@ -241,8 +200,13 @@ public class BandingTaskServiceImpl extends MPJBaseServiceImpl<BandingTaskMapper
             List<String> nameList = new ArrayList<>();
             List<BaseNewStudent> maleList = new ArrayList();
             List<BaseNewStudent> femaleList = new ArrayList();
-
-            for (BaseNewStudent newStudent : baseNewStudents) {
+            List<BaseNewStudent> studentList = new ArrayList<>();
+            studentList.addAll(baseNewStudents);
+            if(ruleCodes.contains("BR0002") && !classStudentMap.get(taskClass.getMajorSetId()).isEmpty()){
+                studentList.clear();
+                studentList.addAll(classStudentMap.get(taskClass.getMajorSetId()));
+            }
+            for (BaseNewStudent newStudent : studentList) {
                 //人数已满,进行下一个班级的的循环
                 if(nameList.size() == number){
                     break;
@@ -319,6 +283,68 @@ public class BandingTaskServiceImpl extends MPJBaseServiceImpl<BandingTaskMapper
         return true;
     }
 
+    /**
+     * 按照成绩均衡划分学生
+     * @return 班级id和学生
+     */
+    Map<Long, List<BaseNewStudent>> divideStudentByScore(Map<Long, BandingTaskMajorCondition> classConditionMap, List<BaseNewStudent> baseNewStudents,
+                                                         List<BandingTaskClass> classList){
+        Map<Long, List<BaseNewStudent>> classStudentMap = new HashMap<>();
+        for (Long majorSetId : classConditionMap.keySet()) {
+            // 1、先把每个专业匹配的学生分组存一起,并按照分数高低排序
+            List<BaseNewStudent> stuList = new ArrayList<>();
+
+            for (BaseNewStudent newStudent : baseNewStudents) {
+                if(!Objects.equals(majorSetId, newStudent.getFirstAmbitionId()) && !Objects.equals(majorSetId, newStudent.getSecondAmbitionId())){
+                    continue;
+                }
+
+                List<Boolean> conditionList = new ArrayList<>();
+                BandingTaskMajorCondition condition = classConditionMap.get(majorSetId);
+                if(condition.getHeight() !=null && newStudent.getHeight() != null && newStudent.getHeight().compareTo(condition.getHeight()) >= 0 ){
+                    conditionList.add(true);
+                }else if(condition.getHeight() !=null && newStudent.getHeight() != null && newStudent.getHeight().compareTo(condition.getHeight()) < 0){
+                    conditionList.add(false);
+                }else if(condition.getHeight() !=null && newStudent.getHeight() == null){
+                    conditionList.add(false);
+                }
+
+                if(condition.getScore() !=null && newStudent.getScore() != null && newStudent.getScore().compareTo(condition.getScore()) >= 0 ){
+                    conditionList.add(true);
+                }else if(condition.getScore() !=null && newStudent.getScore() != null && newStudent.getScore().compareTo(condition.getScore()) < 0){
+                    conditionList.add(false);
+                }else if(condition.getScore() !=null && newStudent.getScore() == null){
+                    conditionList.add(false);
+                }
+                //如果包含false,则表明不符合条件,这个学生跳过
+                if(conditionList.contains(false)){
+                    continue;
+                }
+                stuList.add(newStudent);
+            }
+
+            Collections.sort(stuList, (s1, s2) -> (int) (s2.getScore().doubleValue() - s1.getScore().doubleValue())); //按照成绩降序排序
+            //查询该专业下面有几个班级,把这部分学生按照成绩均匀分组
+            List<List<BaseNewStudent>> result = new ArrayList<>();
+            for (int i = 0; i < classList.size(); i++) {
+                result.add(new ArrayList<>());
+            }
+
+            for (int i = 0; i < stuList.size(); i++) {
+                BaseNewStudent currentStudent = stuList.get(i);
+                int classIndex = i % stuList.size(); //分配班级
+                result.get(classIndex).add(currentStudent);
+            }
+
+
+            for (int i = 0; i < result.size(); i ++){
+                classStudentMap.put(classList.get(i).getId(), result.get(i));
+            }
+        }
+
+        return classStudentMap;
+    }
+
     @Override
     public Boolean sure(SureBandingTaskDto dto) {
         List<BandingTaskClassStudent> classStudents = classStudentService.selectJoinList(BandingTaskClassStudent.class,
@@ -344,10 +370,18 @@ public class BandingTaskServiceImpl extends MPJBaseServiceImpl<BandingTaskMapper
             newStudentService.updateBatchById(updateList);
         }
 
+        //形成学生数据
+        createStudentData(classStudents, updateList);
+
+
         BandingTask bandingTask = this.getById(dto.getId());
         bandingTask.setStatus(1);
         bandingTask.setModifyDate(new Date());
         Boolean isSuccess = this.update(bandingTask);
         return isSuccess;
     }
+
+    void createStudentData(List<BandingTaskClassStudent> classStudents, List<BaseNewStudent> updateList){
+
+    }
 }