2020-09-29 17:21:34 +02:00
|
|
|
package com.plannaplan.services;
|
|
|
|
|
2021-01-17 16:32:59 +01:00
|
|
|
import java.util.ArrayList;
|
2020-09-30 17:46:04 +02:00
|
|
|
import java.util.List;
|
2020-09-30 19:15:32 +02:00
|
|
|
import java.util.Optional;
|
2020-09-30 17:46:04 +02:00
|
|
|
|
2020-09-29 17:21:34 +02:00
|
|
|
import com.plannaplan.entities.Commision;
|
2020-09-30 17:46:04 +02:00
|
|
|
import com.plannaplan.entities.User;
|
2021-01-17 16:32:59 +01:00
|
|
|
import com.plannaplan.models.ExportData;
|
2020-11-14 16:52:22 +01:00
|
|
|
import com.plannaplan.repositories.AssignmentRepository;
|
2020-09-29 17:21:34 +02:00
|
|
|
import com.plannaplan.repositories.CommisionRepository;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2020-11-20 13:20:44 +01:00
|
|
|
/**
|
2021-01-05 11:44:54 +01:00
|
|
|
* Service of CommisionService which can save commision, get user's commisions,
|
|
|
|
* get newest user's commision, get ammount of commisions.
|
2020-11-20 13:20:44 +01:00
|
|
|
*/
|
|
|
|
|
2020-09-29 17:21:34 +02:00
|
|
|
@Service
|
|
|
|
public class CommisionService {
|
|
|
|
@Autowired
|
|
|
|
private CommisionRepository repo;
|
2020-11-14 16:52:22 +01:00
|
|
|
@Autowired
|
|
|
|
private AssignmentRepository aRepository;
|
2021-01-17 16:32:59 +01:00
|
|
|
@Autowired
|
|
|
|
private UserService userService;
|
2020-09-29 17:21:34 +02:00
|
|
|
|
|
|
|
public CommisionService() {
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
|
|
|
* save to database commision. It also checks for missing assignments from
|
|
|
|
* previous commision (you can not get rid of accepted assignment)
|
|
|
|
*
|
|
|
|
* @param commision to save to db
|
|
|
|
* @return Commision instance with id from database
|
|
|
|
*/
|
2020-09-29 17:21:34 +02:00
|
|
|
public Commision save(Commision commision) {
|
2020-11-14 16:52:22 +01:00
|
|
|
Optional<Commision> lastCommision = this.getNewestCommision(commision.getCommisionOwner());
|
|
|
|
if (lastCommision.isPresent()) {
|
|
|
|
final Commision lastCom = lastCommision.get();
|
|
|
|
lastCom.getAssignments().forEach(assignment -> {
|
|
|
|
assignment.setPastAssignment(true);
|
|
|
|
this.aRepository.save(assignment);
|
|
|
|
});
|
2021-01-14 16:13:09 +01:00
|
|
|
|
2020-11-14 16:52:22 +01:00
|
|
|
}
|
2020-09-29 17:21:34 +02:00
|
|
|
this.repo.save(commision);
|
|
|
|
return commision;
|
|
|
|
|
|
|
|
}
|
2020-09-30 17:46:04 +02:00
|
|
|
|
2021-01-05 11:44:54 +01:00
|
|
|
/**
|
|
|
|
* gets user commisions
|
|
|
|
*
|
|
|
|
* @param user owner of commisions
|
|
|
|
* @return list of user commisions
|
2020-12-02 10:22:50 +01:00
|
|
|
*/
|
2020-09-30 17:46:04 +02:00
|
|
|
public List<Commision> getUsersCommisions(User user) {
|
2020-10-05 17:56:35 +02:00
|
|
|
return this.repo.getUsers(user.getId());
|
2020-09-30 17:46:04 +02:00
|
|
|
}
|
2020-09-30 19:15:32 +02:00
|
|
|
|
2021-01-05 11:44:54 +01:00
|
|
|
/**
|
|
|
|
* get newest commision ov given user
|
|
|
|
*
|
|
|
|
* @param user owener of commision we attemp to get
|
|
|
|
* @return optional if commition was found
|
2020-12-02 10:22:50 +01:00
|
|
|
*/
|
2020-09-30 19:15:32 +02:00
|
|
|
public Optional<Commision> getNewestCommision(User user) {
|
2020-10-05 16:52:52 +02:00
|
|
|
return this.repo.getNewestCommision(user.getId()).stream().findFirst();
|
2020-09-30 19:15:32 +02:00
|
|
|
}
|
2020-10-02 17:55:42 +02:00
|
|
|
|
2021-01-05 11:44:54 +01:00
|
|
|
/**
|
|
|
|
* get ammpounts of commisions
|
|
|
|
*
|
|
|
|
* @return long - ammounts of commisions (all even from history, not only
|
|
|
|
* cutrrent one)
|
2020-12-02 10:22:50 +01:00
|
|
|
*/
|
2020-10-02 17:55:42 +02:00
|
|
|
public long getCommisionsAmmount() {
|
|
|
|
return this.repo.count();
|
|
|
|
}
|
2021-01-17 16:32:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return list of ExportData inmstancces keeping data to exprt to file
|
|
|
|
*/
|
|
|
|
public List<ExportData> getExportData() {
|
|
|
|
final List<ExportData> response = new ArrayList<>();
|
|
|
|
|
|
|
|
this.userService.getAllStudents().forEach(student -> {
|
|
|
|
student.getStudentRegisteredGrups().forEach(group -> {
|
|
|
|
response.add(new ExportData(student.getUsosId(), Integer.toString(group.getZajCykId()),
|
|
|
|
Integer.toString(group.getGrNr())));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2020-09-29 17:21:34 +02:00
|
|
|
}
|