parent
7babbae807
commit
5e0bf40b68
@ -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<Student> getAllStudents() {
|
||||||
|
return studentRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addPoints(Long studentId, double points) {
|
||||||
|
Optional<Student> student = studentRepository.findById(studentId);
|
||||||
|
student.ifPresent(s -> {
|
||||||
|
s.setPoints(s.getPoints() + points);
|
||||||
|
studentRepository.save(s);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student pickRandomStudent() {
|
||||||
|
List<Student> students = getAllStudents();
|
||||||
|
Collections.shuffle(students, new Random(System.nanoTime()));
|
||||||
|
return students.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadStudentsFromExcel(InputStream inputStream) throws IOException {
|
||||||
|
List<Student> 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue