From 5e0bf40b68effee25aa3ebd78606896d8ebcd3f1 Mon Sep 17 00:00:00 2001 From: pb8qzmito <13100778657@163.com> Date: Mon, 7 Oct 2024 12:23:39 +0800 Subject: [PATCH] ADD file via upload --- AttendanceService.java | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 AttendanceService.java diff --git a/AttendanceService.java b/AttendanceService.java new file mode 100644 index 0000000..b5f3196 --- /dev/null +++ b/AttendanceService.java @@ -0,0 +1,53 @@ +package com.example.classroomattendance.service; + +import com.example.classroomattendance.model.Student; +import com.example.classroomattendance.repository.StudentRepository; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.io.InputStream; +import java.util.*; + +@Service +public class AttendanceService { + @Autowired + private StudentRepository studentRepository; + + public List getAllStudents() { + return studentRepository.findAll(); + } + + public void addPoints(Long studentId, double points) { + Optional student = studentRepository.findById(studentId); + student.ifPresent(s -> { + s.setPoints(s.getPoints() + points); + studentRepository.save(s); + }); + } + + public Student pickRandomStudent() { + List students = getAllStudents(); + Collections.shuffle(students, new Random(System.nanoTime())); + return students.get(0); + } + + public void loadStudentsFromExcel(InputStream inputStream) throws IOException { + List students = new ArrayList<>(); + try (Workbook workbook = new XSSFWorkbook(inputStream)) { + Sheet sheet = workbook.getSheetAt(0); + for (Row row : sheet) { + if (row.getRowNum() == 0) { + continue; + } + String name = row.getCell(0).getStringCellValue(); + String studentId = row.getCell(1).getStringCellValue(); + double points = row.getCell(2).getNumericCellValue(); + students.add(new Student(name, studentId, points)); + } + } + studentRepository.saveAll(students); + } +} \ No newline at end of file