2021-01-06 13:40:04 +01:00
|
|
|
package com.plannaplan.services;
|
|
|
|
|
2021-01-12 14:10:48 +01:00
|
|
|
import java.util.ArrayList;
|
2021-01-12 13:04:57 +01:00
|
|
|
import java.util.HashSet;
|
2021-01-07 16:51:35 +01:00
|
|
|
import java.util.List;
|
2021-01-06 16:50:08 +01:00
|
|
|
import java.util.Optional;
|
2021-01-12 13:04:57 +01:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.stream.Collectors;
|
2021-01-06 16:50:08 +01:00
|
|
|
|
|
|
|
import com.plannaplan.entities.Assignment;
|
2021-01-12 14:10:48 +01:00
|
|
|
import com.plannaplan.entities.Commision;
|
2021-01-06 13:40:04 +01:00
|
|
|
import com.plannaplan.entities.Exchange;
|
2021-01-06 16:50:08 +01:00
|
|
|
import com.plannaplan.entities.Groups;
|
2021-01-12 14:10:48 +01:00
|
|
|
import com.plannaplan.entities.User;
|
2021-01-13 16:52:18 +01:00
|
|
|
import com.plannaplan.models.EmailExchangesData;
|
2021-01-11 16:56:39 +01:00
|
|
|
import com.plannaplan.models.MatchData;
|
2021-01-06 13:40:04 +01:00
|
|
|
import com.plannaplan.repositories.ExchangeRepository;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
|
|
|
* Service to manage Exchanges
|
|
|
|
*/
|
2021-01-06 13:40:04 +01:00
|
|
|
@Service
|
|
|
|
public class ExchangeService {
|
2021-01-11 16:21:25 +01:00
|
|
|
|
2021-01-11 16:56:39 +01:00
|
|
|
@Autowired
|
|
|
|
private ExchangeRepository repo;
|
|
|
|
|
2021-01-12 14:10:48 +01:00
|
|
|
@Autowired
|
|
|
|
private AssignmentService assignmentService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private UserService userService;
|
|
|
|
|
2021-01-13 16:52:18 +01:00
|
|
|
@Autowired
|
|
|
|
private EmailService emailService;
|
|
|
|
|
2021-01-11 16:56:39 +01:00
|
|
|
/**
|
|
|
|
* @param exchange Instance to save in database
|
|
|
|
* @return Exchange Instance contains database id
|
|
|
|
*/
|
|
|
|
public Exchange save(Exchange exchange) {
|
2021-01-13 16:52:18 +01:00
|
|
|
return this.repo.save(exchange);
|
2021-01-11 16:56:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param id Id of exchange in database
|
|
|
|
* @return Optional Exchange if found
|
|
|
|
*/
|
|
|
|
public Optional<Exchange> getById(Long id) {
|
2021-01-13 16:52:18 +01:00
|
|
|
return this.repo.findById(id);
|
2021-01-11 16:56:39 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
|
|
|
* @return list of all exchanges in database
|
|
|
|
*/
|
2021-01-13 16:52:18 +01:00
|
|
|
public List<Exchange> getAllExchanges() {
|
|
|
|
return this.repo.findAll();
|
2021-01-12 18:39:22 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 16:56:39 +01:00
|
|
|
/**
|
|
|
|
* @param id Id of user
|
|
|
|
* @return List of exchanges that belong to user
|
|
|
|
*/
|
|
|
|
public List<Exchange> getByUserId(Long id) {
|
2021-01-13 16:52:18 +01:00
|
|
|
return this.repo.getByUserId(id);
|
2021-01-11 16:56:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param entity Exchange entity which we would like to delete
|
|
|
|
*/
|
|
|
|
public void deleteExchange(Exchange entity) {
|
2021-01-13 16:52:18 +01:00
|
|
|
this.repo.delete(entity);
|
2021-01-11 16:56:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param assignment Assignment to trade for
|
|
|
|
* @param group Desired group
|
|
|
|
* @return Optional with Exchange if exist
|
|
|
|
*/
|
|
|
|
public Optional<Exchange> checkForExchange(Assignment assignment, Groups group) {
|
2021-01-13 16:52:18 +01:00
|
|
|
return this.repo.checkForExchange(assignment, group);
|
2021-01-11 16:56:39 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
|
|
|
* method to perform Exchange algorythm. It search for matches and swap
|
|
|
|
* assignments between latests user commisions if it can be performed. After
|
|
|
|
* swap we block users matches that contains switched groups. After algorythm
|
|
|
|
* email is being sent to all users with information about performed exchanges
|
|
|
|
*/
|
2021-01-11 16:56:39 +01:00
|
|
|
public void performExchange() {
|
2021-01-13 16:52:18 +01:00
|
|
|
final List<MatchData> matchData = this.getMatches();
|
|
|
|
final List<Long> performedAssignmentExchanges = new ArrayList<>();
|
|
|
|
final EmailExchangesData emailData = new EmailExchangesData();
|
|
|
|
final List<Exchange> exchangesToDelete = new ArrayList<>();
|
|
|
|
|
|
|
|
matchData.forEach(m -> {
|
|
|
|
final Assignment assignmentOne = m.getAssignmentOne();
|
|
|
|
final Assignment assignmentTwo = m.getAssignmentTwo();
|
2021-01-12 14:10:48 +01:00
|
|
|
|
2021-01-13 16:52:18 +01:00
|
|
|
final Exchange exchange1 = m.getExchangeOne();
|
|
|
|
final Exchange exchange2 = m.getExchangeTwo();
|
2021-01-12 14:10:48 +01:00
|
|
|
|
2021-01-13 16:52:18 +01:00
|
|
|
if (!(performedAssignmentExchanges.contains(assignmentOne.getId())
|
|
|
|
|| performedAssignmentExchanges.contains(assignmentTwo.getId()))) {
|
|
|
|
final Commision commisionOne = assignmentOne.getCommision();
|
|
|
|
final User userOne = commisionOne.getCommisionOwner();
|
|
|
|
final Commision commisionTwo = assignmentTwo.getCommision();
|
|
|
|
final User userTwo = commisionTwo.getCommisionOwner();
|
2021-01-12 14:10:48 +01:00
|
|
|
|
2021-01-13 16:52:18 +01:00
|
|
|
assignmentOne.setCommision(commisionTwo);
|
|
|
|
assignmentTwo.setCommision(commisionOne);
|
2021-01-12 14:10:48 +01:00
|
|
|
|
2021-01-13 16:52:18 +01:00
|
|
|
userOne.removeGroup(assignmentOne.getGroup().getId());
|
|
|
|
userTwo.removeGroup(assignmentTwo.getGroup().getId());
|
2021-01-12 14:10:48 +01:00
|
|
|
|
2021-01-13 16:52:18 +01:00
|
|
|
userOne.claimGroup(assignmentTwo.getGroup());
|
|
|
|
userTwo.claimGroup(assignmentOne.getGroup());
|
2021-01-12 14:10:48 +01:00
|
|
|
|
2021-01-13 16:52:18 +01:00
|
|
|
this.assignmentService.save(assignmentOne);
|
|
|
|
this.assignmentService.save(assignmentTwo);
|
2021-01-12 14:10:48 +01:00
|
|
|
|
2021-01-13 16:52:18 +01:00
|
|
|
this.userService.save(userOne);
|
|
|
|
this.userService.save(userTwo);
|
2021-01-11 16:56:39 +01:00
|
|
|
|
2021-01-13 16:52:18 +01:00
|
|
|
performedAssignmentExchanges.add(assignmentOne.getId());
|
|
|
|
performedAssignmentExchanges.add(assignmentTwo.getId());
|
|
|
|
|
|
|
|
emailData.addExchange(exchange1.getOwnedAssignment().getCommision().getCommisionOwner(), exchange1);
|
|
|
|
emailData.addExchange(exchange2.getOwnedAssignment().getCommision().getCommisionOwner(), exchange2);
|
|
|
|
}
|
|
|
|
|
|
|
|
exchangesToDelete.add(exchange1);
|
|
|
|
exchangesToDelete.add(exchange2);
|
|
|
|
});
|
|
|
|
this.emailService.sendExchangesResults(emailData);
|
|
|
|
this.repo.deleteAll(exchangesToDelete);
|
2021-01-11 16:56:39 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
|
|
|
* @return list of matches found in database
|
|
|
|
*/
|
2021-01-13 16:52:18 +01:00
|
|
|
public List<MatchData> getMatches() {
|
|
|
|
final List<MatchData> matches = this.repo.getMatches().stream().map(m -> {
|
|
|
|
final Exchange exchangeOne = (Exchange) m[0];
|
|
|
|
final Exchange exchangeTwo = (Exchange) m[1];
|
|
|
|
return new MatchData(exchangeOne, exchangeTwo);
|
|
|
|
}).collect(Collectors.toList());
|
2021-01-12 13:04:57 +01:00
|
|
|
|
2021-01-13 16:52:18 +01:00
|
|
|
final Set<MatchData> uniqData = new HashSet<>(matches);
|
|
|
|
final List<MatchData> matchDataListSorted = uniqData.stream().sorted((m1, m2) -> -1 * m1.compare(m2))
|
|
|
|
.collect(Collectors.toList());
|
2021-01-13 14:13:26 +01:00
|
|
|
|
2021-01-13 16:52:18 +01:00
|
|
|
return matchDataListSorted;
|
|
|
|
}
|
2021-01-06 13:40:04 +01:00
|
|
|
}
|