|
|
|
@ -127,6 +127,43 @@ public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
|
|
|
|
|
baseMapper.insert(student);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Student rollCall() {
|
|
|
|
|
List<Student> studentList = this.list();
|
|
|
|
|
|
|
|
|
|
if (studentList.isEmpty()) {
|
|
|
|
|
throw new RuntimeException("学生列表为空");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 找出最高积分,用于计算反向权重
|
|
|
|
|
final double maxPoints = studentList.stream()
|
|
|
|
|
.mapToDouble(Student::getPoints)
|
|
|
|
|
.max()
|
|
|
|
|
.orElse(0.0) + 1.0;
|
|
|
|
|
|
|
|
|
|
// 计算总的反向权重
|
|
|
|
|
double totalInverseWeights = studentList.stream()
|
|
|
|
|
.mapToDouble(student -> maxPoints - student.getPoints())
|
|
|
|
|
.sum();
|
|
|
|
|
|
|
|
|
|
// 生成一个0到总反向权重之间的随机数
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
double randomValue = random.nextDouble() * totalInverseWeights;
|
|
|
|
|
|
|
|
|
|
// 使用累加器方法选择学生
|
|
|
|
|
double accumulatedWeight = 0;
|
|
|
|
|
for (Student student : studentList) {
|
|
|
|
|
// 计算该学生的反向权重
|
|
|
|
|
double inverseWeight = maxPoints - student.getPoints();
|
|
|
|
|
accumulatedWeight += inverseWeight;
|
|
|
|
|
if (accumulatedWeight >= randomValue) {
|
|
|
|
|
return student;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 以防万一,返回最后一个学生
|
|
|
|
|
return studentList.get(studentList.size() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void importStudentsByExcel(MultipartFile file) {
|
|
|
|
@ -232,4 +269,10 @@ public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void updatePoints(Integer id, Double points) {
|
|
|
|
|
Student student = baseMapper.selectById(id);
|
|
|
|
|
student.setPoints(student.getPoints() + points);
|
|
|
|
|
baseMapper.updateById(student);
|
|
|
|
|
}
|
|
|
|
|
}
|