1 | package mk.ukim.finki.eglas.services.Impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.eglas.model.*;
|
---|
4 | import mk.ukim.finki.eglas.model.views.TotalTurnoutByMunicipality;
|
---|
5 | import mk.ukim.finki.eglas.records.TotalCandidacyResults;
|
---|
6 | import mk.ukim.finki.eglas.repository.TurnoutRepository;
|
---|
7 | import mk.ukim.finki.eglas.services.*;
|
---|
8 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
9 | import org.springframework.data.repository.query.Param;
|
---|
10 | import org.springframework.stereotype.Service;
|
---|
11 |
|
---|
12 | import java.time.LocalDate;
|
---|
13 | import java.time.LocalDateTime;
|
---|
14 | import java.util.Comparator;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | @Service
|
---|
18 | public class TurnoutServiceImpl implements TurnoutService {
|
---|
19 |
|
---|
20 | private final ElectionRealizationService electionRealizationService;
|
---|
21 | private final CitizenService citizenService;
|
---|
22 | private final TurnoutRepository turnoutRepository;
|
---|
23 | private final AddressService addressService;
|
---|
24 |
|
---|
25 | @Autowired
|
---|
26 | private CandidatesElectionRealizationService candidatesElectionRealizationService;
|
---|
27 |
|
---|
28 | public TurnoutServiceImpl(ElectionRealizationService electionRealizationService, CitizenService citizenService, TurnoutRepository turnoutRepository, AddressService addressService) {
|
---|
29 | this.electionRealizationService = electionRealizationService;
|
---|
30 | this.citizenService = citizenService;
|
---|
31 | this.turnoutRepository = turnoutRepository;
|
---|
32 | this.addressService = addressService;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public List<Turnout> findAll() {
|
---|
37 | return turnoutRepository.findAll();
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public Turnout findById(Long id) {
|
---|
42 | return turnoutRepository.findById(id).orElseThrow(() -> new RuntimeException("Vote not found"));
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public Turnout update(Long id, LocalDateTime voteTimestamp, Long citizenId, Long electionRealizationId, PollingStation pollingStation) {
|
---|
47 | Turnout vote = new Turnout();
|
---|
48 | Citizen citizen = citizenService.findById(citizenId);
|
---|
49 | ElectionRealization electionRealization = electionRealizationService.findById(electionRealizationId);
|
---|
50 | if(id != null)
|
---|
51 | {
|
---|
52 | vote = findById(id);
|
---|
53 | }
|
---|
54 | vote.setVoteTimestamp(voteTimestamp);
|
---|
55 | vote.setCitizen(citizen);
|
---|
56 | vote.setElectionRealization(electionRealization);
|
---|
57 | vote.setPollingStation(pollingStation);
|
---|
58 | return turnoutRepository.save(vote);
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public Turnout delete(Long id) {
|
---|
63 | Turnout vote = findById(id);
|
---|
64 | turnoutRepository.delete(vote);
|
---|
65 | return vote;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public Double turnOutByElectionRealization(Long id)
|
---|
70 | {
|
---|
71 | ElectionRealization electionRealization = electionRealizationService.findById(id);
|
---|
72 | return turnoutRepository.turnOutByRealization(electionRealization, LocalDate.now().minusYears(18));
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public Double getTurnOutByRealizationAndMunicipality(Long realizationId, String map_id) {
|
---|
77 | if (map_id == null) {
|
---|
78 | return turnOutByElectionRealization(realizationId);
|
---|
79 | }
|
---|
80 | return turnoutRepository.turnOutByMunicipality(map_id, realizationId).get(0).getTotal();
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public Double getTurnOutByRealizationAndMunicipalityAndPollingStation(Long realizationId, String map_id, Long pollingStationId) {
|
---|
85 | if (map_id == null && pollingStationId == null) {
|
---|
86 | return turnOutByElectionRealization(realizationId);
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (pollingStationId == null) {
|
---|
90 | return getTurnOutByRealizationAndMunicipality(realizationId,map_id);
|
---|
91 | }
|
---|
92 | LocalDate dateThreshold = LocalDate.now().minusYears(18);
|
---|
93 | return turnoutRepository.turnOutByRealizationAndMunicipalityAndPollingStation(realizationId, pollingStationId, LocalDate.now().plusYears(180));
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public List<TotalCandidacyResults> resultsByCandidateElectionsRealization(Long realizationId)
|
---|
99 | {
|
---|
100 | CandidatesElectionRealization candidatesElectionRealization = candidatesElectionRealizationService.findById(realizationId);
|
---|
101 | List<TotalCandidacyResults> votes = turnoutRepository.countVotesByCitizenAndRealization(candidatesElectionRealization);
|
---|
102 | Long totalVotes = votes.stream().mapToLong(TotalCandidacyResults::voteCount).sum();
|
---|
103 | return votes.stream()
|
---|
104 | .map(x -> new TotalCandidacyResults(x.candidacy(), x.voteCount(), x.voteCount() * 100.0 / totalVotes))
|
---|
105 | .sorted(Comparator.comparing(TotalCandidacyResults::voteCount).reversed())
|
---|
106 | .toList();
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public Boolean hasCitizenVotedOnRealization (Long citizenId, Long realizationId)
|
---|
111 | {
|
---|
112 | return turnoutRepository.hasCitizenVotedOnRealization(citizenId, realizationId);
|
---|
113 | }
|
---|
114 | }
|
---|