Merge pull request 'Фейн и формирование отчёта в пдф готово' (#3) from feature/egor/feign into dev
Reviewed-on: #3
This commit is contained in:
commit
ebf269d45b
@ -2,10 +2,12 @@ package edu.unive.schedule;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@PropertySource("classpath:application.yml")
|
@PropertySource("classpath:application.yml")
|
||||||
|
@EnableFeignClients
|
||||||
public class ScheduleApplication {
|
public class ScheduleApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package edu.unive.schedule.client;
|
||||||
|
|
||||||
|
import edu.unive.schedule.domain.Group;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@FeignClient(name = "group-service", url = "http://localhost:8281/api/groups")
|
||||||
|
public interface GroupServiceClient {
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
ResponseEntity<Void> createGroup(@RequestBody Group group);
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
ResponseEntity<Group> getGroupById(@PathVariable Long id);
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
List<Group> getAllGroups();
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
ResponseEntity<Void> updateGroup(@PathVariable Long id, @RequestBody Group group);
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
ResponseEntity<Void> deleteGroup(@PathVariable Long id);
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package edu.unive.schedule.client;
|
||||||
|
|
||||||
|
import edu.unive.schedule.domain.Teacher;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@FeignClient(name = "teacher-service", url = "http://localhost:8281/api/teachers")
|
||||||
|
public interface TeacherServiceClient {
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
ResponseEntity<Void> createTeacher(@RequestBody Teacher teacher);
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
ResponseEntity<Teacher> getTeacherById(@PathVariable Long id);
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
List<Teacher> getAllTeachers();
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
ResponseEntity<Void> updateTeacher(@PathVariable Long id, @RequestBody Teacher teacher);
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
ResponseEntity<Void> deleteTeacher(@PathVariable Long id);
|
||||||
|
}
|
10
schedule/src/main/java/edu/unive/schedule/domain/Group.java
Normal file
10
schedule/src/main/java/edu/unive/schedule/domain/Group.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package edu.unive.schedule.domain;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Group {
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
private Long directionId;
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package edu.unive.schedule.domain;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Teacher {
|
||||||
|
private Long id;
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private Long departmentId;
|
||||||
|
}
|
@ -1,5 +1,9 @@
|
|||||||
package edu.unive.schedule.service;
|
package edu.unive.schedule.service;
|
||||||
|
|
||||||
|
import edu.unive.schedule.client.GroupServiceClient;
|
||||||
|
import edu.unive.schedule.client.TeacherServiceClient;
|
||||||
|
import edu.unive.schedule.domain.Group;
|
||||||
|
import edu.unive.schedule.domain.Teacher;
|
||||||
import edu.unive.schedule.domain.entity.ScheduleEntity;
|
import edu.unive.schedule.domain.entity.ScheduleEntity;
|
||||||
import edu.unive.schedule.repository.ScheduleRepository;
|
import edu.unive.schedule.repository.ScheduleRepository;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@ -7,23 +11,25 @@ import org.apache.pdfbox.pdmodel.PDDocument;
|
|||||||
import org.apache.pdfbox.pdmodel.PDPage;
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class PdfGenerationService {
|
public class PdfGenerationService {
|
||||||
|
|
||||||
private final ScheduleRepository scheduleRepository;
|
private final ScheduleRepository scheduleRepository;
|
||||||
|
private final GroupServiceClient groupServiceClient;
|
||||||
//private final GroupServiceClient groupServiceClient;
|
private final TeacherServiceClient teacherServiceClient;
|
||||||
|
|
||||||
//private final TeacherServiceClient teacherServiceClient;
|
|
||||||
|
|
||||||
public byte[] generatePdf(Long groupId) throws IOException {
|
public byte[] generatePdf(Long groupId) throws IOException {
|
||||||
|
// Получаем расписания для указанной группы
|
||||||
List<ScheduleEntity> schedules = scheduleRepository.findByGroupIdsContaining(groupId);
|
List<ScheduleEntity> schedules = scheduleRepository.findByGroupIdsContaining(groupId);
|
||||||
|
|
||||||
try (PDDocument document = new PDDocument()) {
|
try (PDDocument document = new PDDocument()) {
|
||||||
@ -31,44 +37,87 @@ public class PdfGenerationService {
|
|||||||
document.addPage(page);
|
document.addPage(page);
|
||||||
|
|
||||||
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
|
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
|
||||||
|
// Настройка шрифта и заголовка
|
||||||
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
|
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
|
||||||
contentStream.beginText();
|
contentStream.beginText();
|
||||||
contentStream.newLineAtOffset(100, 700);
|
contentStream.newLineAtOffset(100, 700);
|
||||||
contentStream.showText("Group Schedule");
|
contentStream.showText("Group Schedule");
|
||||||
contentStream.endText();
|
contentStream.endText();
|
||||||
|
|
||||||
int yOffset = 650;
|
int yOffset = 650; // Начальная позиция текста по вертикали
|
||||||
|
|
||||||
for (ScheduleEntity schedule : schedules) {
|
for (ScheduleEntity schedule : schedules) {
|
||||||
// Получаем название группы из внешнего микросервиса
|
// Получаем название групп из внешнего микросервиса
|
||||||
//String groupNames = schedule.getGroupIds().stream()
|
String groupNames = fetchGroupNames(schedule.getGroupIds());
|
||||||
// .map(groupServiceClient::getGroupById)
|
|
||||||
// .map(Group::getName)
|
|
||||||
// .collect(Collectors.joining(", "));
|
|
||||||
|
|
||||||
// Получаем данные о преподавателе из внешнего микросервиса
|
// Получаем данные о преподавателе из внешнего микросервиса
|
||||||
//Teacher teacher = teacherServiceClient.getTeacherById(schedule.getTeacherId());
|
String teacherName = fetchTeacherName(schedule.getTeacherId());
|
||||||
|
|
||||||
String text = String.format("Week %d, %s, Pair %d: %s - %s (Classroom: %s, Teacher: %s, Groups: %s)",
|
// Формируем текст для PDF
|
||||||
|
String classroomName = (schedule.getClassroom() != null) ? schedule.getClassroom().getName() : "N/A";
|
||||||
|
|
||||||
|
String text = String.format(
|
||||||
|
"Week %d, %s, Pair %d: Course %s - Classroom %s (Teacher: %s, Groups: %s)",
|
||||||
schedule.getWeekNumber(),
|
schedule.getWeekNumber(),
|
||||||
schedule.getDayOfWeek(),
|
schedule.getDayOfWeek(),
|
||||||
schedule.getPairNumber(),
|
schedule.getPairNumber(),
|
||||||
schedule.getCourseId(),
|
schedule.getCourseId(),
|
||||||
schedule.getClassroom().getName());
|
classroomName,
|
||||||
//teacher.getName(),
|
teacherName,
|
||||||
//groupNames);
|
groupNames
|
||||||
|
);
|
||||||
|
|
||||||
|
// Выводим текст на страницу PDF
|
||||||
contentStream.beginText();
|
contentStream.beginText();
|
||||||
contentStream.newLineAtOffset(100, yOffset);
|
contentStream.newLineAtOffset(100, yOffset);
|
||||||
contentStream.showText(text);
|
contentStream.showText(text);
|
||||||
contentStream.endText();
|
contentStream.endText();
|
||||||
|
|
||||||
yOffset -= 20;
|
yOffset -= 20; // Смещение следующей строки вниз
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Сохраняем документ PDF в ByteArrayOutputStream
|
||||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
document.save(byteArrayOutputStream);
|
document.save(byteArrayOutputStream);
|
||||||
return byteArrayOutputStream.toByteArray();
|
return byteArrayOutputStream.toByteArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Метод для получения названий групп.
|
||||||
|
*
|
||||||
|
* @param groupIds Список ID групп
|
||||||
|
* @return Строка с названиями групп через запятую или "No Groups"
|
||||||
|
*/
|
||||||
|
private String fetchGroupNames(List<Long> groupIds) {
|
||||||
|
if (groupIds == null || groupIds.isEmpty()) {
|
||||||
|
return "No Groups";
|
||||||
|
}
|
||||||
|
|
||||||
|
return groupIds.stream()
|
||||||
|
.map(groupId -> {
|
||||||
|
ResponseEntity<Group> groupResponse = groupServiceClient.getGroupById(groupId);
|
||||||
|
Group group = groupResponse.getBody();
|
||||||
|
return (group != null) ? group.getName() : "Unknown Group";
|
||||||
|
})
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.collect(Collectors.joining(", "));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Метод для получения имени преподавателя.
|
||||||
|
*
|
||||||
|
* @param teacherId ID преподавателя
|
||||||
|
* @return Имя преподавателя или "Unknown Teacher"
|
||||||
|
*/
|
||||||
|
private String fetchTeacherName(Long teacherId) {
|
||||||
|
if (teacherId == null) {
|
||||||
|
return "Unknown Teacher";
|
||||||
|
}
|
||||||
|
|
||||||
|
ResponseEntity<Teacher> teacherResponse = teacherServiceClient.getTeacherById(teacherId);
|
||||||
|
Teacher teacher = teacherResponse.getBody();
|
||||||
|
return (teacher != null && teacher.getLastName() != null) ? teacher.getLastName() : "Unknown Teacher";
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user