From fb4f781e56382ce006d00be602d52d0849030eff Mon Sep 17 00:00:00 2001 From: Spark <2666652@gmail.com> Date: Sat, 12 Oct 2024 00:51:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E7=82=B9=E5=90=8D?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aspark/controller/StudentController.java | 13 ++++++ .../cc/aspark/service/StudentService.java | 3 ++ .../service/impl/StudentServiceImpl.java | 43 +++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/RollCallServer/src/main/java/cc/aspark/controller/StudentController.java b/RollCallServer/src/main/java/cc/aspark/controller/StudentController.java index dc5d436..1928f49 100644 --- a/RollCallServer/src/main/java/cc/aspark/controller/StudentController.java +++ b/RollCallServer/src/main/java/cc/aspark/controller/StudentController.java @@ -84,5 +84,18 @@ public class StudentController { return Result.success(); } + @Operation(summary = "随机抽取一名学生") + @GetMapping("/rollcall") + public Result rollCall() { + Student stu = studentService.rollCall(); + return Result.success(stu); + } + + @Operation(summary = "更新积分") + @PutMapping("/points/{id}/{points}") + public Result updatePoints(@PathVariable Integer id, @PathVariable Double points) { + studentService.updatePoints(id, points); + return Result.success(); + } } diff --git a/RollCallServer/src/main/java/cc/aspark/service/StudentService.java b/RollCallServer/src/main/java/cc/aspark/service/StudentService.java index 81e3a40..4b1086a 100644 --- a/RollCallServer/src/main/java/cc/aspark/service/StudentService.java +++ b/RollCallServer/src/main/java/cc/aspark/service/StudentService.java @@ -21,8 +21,11 @@ public interface StudentService extends IService { void save(StudentDTO studentDTO); + Student rollCall(); + void importStudentsByExcel(MultipartFile file); void exportStudentsToExcel(HttpServletResponse response); + void updatePoints(Integer id, Double points); } diff --git a/RollCallServer/src/main/java/cc/aspark/service/impl/StudentServiceImpl.java b/RollCallServer/src/main/java/cc/aspark/service/impl/StudentServiceImpl.java index 5cf98c4..4bc4207 100644 --- a/RollCallServer/src/main/java/cc/aspark/service/impl/StudentServiceImpl.java +++ b/RollCallServer/src/main/java/cc/aspark/service/impl/StudentServiceImpl.java @@ -127,6 +127,43 @@ public class StudentServiceImpl extends ServiceImpl baseMapper.insert(student); } + @Override + public Student rollCall() { + List 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 } } + @Override + public void updatePoints(Integer id, Double points) { + Student student = baseMapper.selectById(id); + student.setPoints(student.getPoints() + points); + baseMapper.updateById(student); + } } \ No newline at end of file