1 | package mk.ukim.finki.eglas.services.Impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.eglas.model.ElectionRealization;
|
---|
4 | import mk.ukim.finki.eglas.model.dto.ResultsDto;
|
---|
5 | import mk.ukim.finki.eglas.records.TotalCandidacyResultsPerMunicipality;
|
---|
6 | import mk.ukim.finki.eglas.records.TotalCandidacyResultsPerPollingStation;
|
---|
7 | import mk.ukim.finki.eglas.records.TotalListResultsPerMunicipality;
|
---|
8 | import mk.ukim.finki.eglas.records.TotalListResultsPerPollingStation;
|
---|
9 | import mk.ukim.finki.eglas.repository.CandidatesListElectionRealizationRepository;
|
---|
10 | import mk.ukim.finki.eglas.repository.TurnoutRepository;
|
---|
11 | import mk.ukim.finki.eglas.services.CandidatesListElectionRealizationService;
|
---|
12 | import mk.ukim.finki.eglas.services.ElectionRealizationService;
|
---|
13 | import mk.ukim.finki.eglas.services.MunicipalityService;
|
---|
14 | import org.springframework.scheduling.annotation.Scheduled;
|
---|
15 | import org.springframework.stereotype.Service;
|
---|
16 |
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Map;
|
---|
19 |
|
---|
20 | @Service
|
---|
21 | public class ResultsServiceImpl {
|
---|
22 |
|
---|
23 | private final ElectionRealizationService electionRealizationService;
|
---|
24 | private final CandidatesListElectionRealizationService candidatesListElectionRealizationService;
|
---|
25 | private final MunicipalityService municipalityService;
|
---|
26 | private final TurnoutRepository turnoutRepository;
|
---|
27 | private final CandidatesListElectionRealizationRepository candidatesListElectionRealizationRepository;
|
---|
28 |
|
---|
29 | public ResultsServiceImpl(ElectionRealizationService electionRealizationService, CandidatesListElectionRealizationService candidatesListElectionRealizationService, MunicipalityService municipalityService, TurnoutRepository turnoutRepository, CandidatesListElectionRealizationRepository candidatesListElectionRealizationRepository) {
|
---|
30 | this.electionRealizationService = electionRealizationService;
|
---|
31 | this.candidatesListElectionRealizationService = candidatesListElectionRealizationService;
|
---|
32 | this.municipalityService = municipalityService;
|
---|
33 | this.turnoutRepository = turnoutRepository;
|
---|
34 | this.candidatesListElectionRealizationRepository = candidatesListElectionRealizationRepository;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public List<ResultsDto> getResultsForRealizationBy(Long realizationId, Long municipalityId, Long pollingStationId) {
|
---|
38 | ElectionRealization electionRealization = electionRealizationService.findById(realizationId);
|
---|
39 | if(pollingStationId != null) {
|
---|
40 | if(isListRealization(realizationId)) {
|
---|
41 | List<TotalListResultsPerPollingStation> totalListResultsPerPollingStation = listResultsPerPollingStation(2L, 2L);
|
---|
42 | // List<TotalListResultsPerPollingStation> totalListResultsPerPollingStation = listResultsPerPollingStation(2L, 2L);
|
---|
43 | Long totalVotes = totalListResultsPerPollingStation.stream().mapToLong(x -> Long.parseLong(x.getVoteCount())).sum();
|
---|
44 | return totalListResultsPerPollingStation.stream().map(x -> {
|
---|
45 | ResultsDto resultsDto = new ResultsDto();
|
---|
46 | resultsDto.setId(Long.parseLong(x.getPollingStationId()));
|
---|
47 | resultsDto.setVotesCount(Long.parseLong(x.getVoteCount()));
|
---|
48 | resultsDto.setParticipantName(x.getPartyName());
|
---|
49 | resultsDto.setVotesPercentage((Long.parseLong(x.getVoteCount()) * 100.0) / totalVotes);
|
---|
50 | return resultsDto;
|
---|
51 | }).toList();
|
---|
52 | }
|
---|
53 | else {
|
---|
54 | List<TotalCandidacyResultsPerPollingStation> totalCandidacyResultsPerPollingStations = candidacyResultsPerPollingStations(realizationId, municipalityId);
|
---|
55 | Long totalVotes = totalCandidacyResultsPerPollingStations.stream().mapToLong(x -> Long.parseLong(x.getVoteCount())).sum();
|
---|
56 | return totalCandidacyResultsPerPollingStations.stream().map(x -> {
|
---|
57 | ResultsDto resultsDto = new ResultsDto();
|
---|
58 | resultsDto.setId(Long.parseLong(x.getCandidacyId()));
|
---|
59 | resultsDto.setVotesCount(Long.parseLong(x.getVoteCount() != null ? x.getVoteCount() : "0"));
|
---|
60 | resultsDto.setParticipantName(x.getCitizenName());
|
---|
61 | resultsDto.setVotesPercentage((Long.parseLong(x.getVoteCount()) * 100.0) / totalVotes);
|
---|
62 | return resultsDto;
|
---|
63 | }).toList();
|
---|
64 | }
|
---|
65 |
|
---|
66 | }
|
---|
67 | else {
|
---|
68 | if(isListRealization(realizationId)) {
|
---|
69 | List<TotalListResultsPerMunicipality> totalListResultsPerMunicipalities = listResultsPerMuncipality(realizationId, municipalityId);
|
---|
70 | Long totalVotes = totalListResultsPerMunicipalities.stream().mapToLong(x -> Long.parseLong(x.getVoteCount())).sum();
|
---|
71 | return totalListResultsPerMunicipalities.stream().map(x -> {
|
---|
72 | ResultsDto resultsDto = new ResultsDto();
|
---|
73 | resultsDto.setId(Long.parseLong(x.getMunicipalityId()));
|
---|
74 | resultsDto.setVotesCount(Long.parseLong(x.getVoteCount() != null ? x.getVoteCount() : "0"));
|
---|
75 | resultsDto.setParticipantName(x.getPartyName());
|
---|
76 | resultsDto.setVotesPercentage((Long.parseLong(x.getVoteCount()) * 100.0) / totalVotes);
|
---|
77 | return resultsDto;
|
---|
78 | }).toList();
|
---|
79 | }
|
---|
80 | else {
|
---|
81 | List<TotalCandidacyResultsPerMunicipality> totalCandidacyResultsPerMunicipalities = candidacyResultsPerMunicipalities(realizationId, municipalityId);
|
---|
82 | Long totalVotes = totalCandidacyResultsPerMunicipalities.stream().mapToLong(x -> Long.parseLong(x.getVoteCount())).sum();
|
---|
83 | return totalCandidacyResultsPerMunicipalities.stream().map(x -> {
|
---|
84 | ResultsDto resultsDto = new ResultsDto();
|
---|
85 | resultsDto.setId(Long.parseLong(x.getCandidacyId()));
|
---|
86 | resultsDto.setVotesCount(Long.parseLong(x.getVoteCount() != null ? x.getVoteCount() : "0"));
|
---|
87 | resultsDto.setParticipantName(x.getCitizenName());
|
---|
88 | resultsDto.setVotesPercentage((Long.parseLong(x.getVoteCount()) * 100.0) / totalVotes);
|
---|
89 | return resultsDto;
|
---|
90 | }).toList();
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | private boolean isListRealization(Long realizationId) {
|
---|
96 | return candidatesListElectionRealizationRepository.existsCandidateListElections(realizationId);
|
---|
97 | }
|
---|
98 |
|
---|
99 | List<TotalListResultsPerPollingStation> listResultsPerPollingStation(Long realizationId, Long pollingStationId) {
|
---|
100 | List<Map<String, Object>> results = turnoutRepository.totalListResultsPerPollingStation(realizationId, pollingStationId);
|
---|
101 | return results.stream().map(x -> {
|
---|
102 | TotalListResultsPerPollingStation totalListResultsPerPollingStation = new TotalListResultsPerPollingStation();
|
---|
103 | totalListResultsPerPollingStation.setListName((x.get("list_name").toString()));
|
---|
104 | totalListResultsPerPollingStation.setPartyName((String) x.get("participant").toString());
|
---|
105 | totalListResultsPerPollingStation.setVoteCount((String) x.get("vote_count").toString());
|
---|
106 | totalListResultsPerPollingStation.setPollingStationId((String) x.get("polling_station_id").toString());
|
---|
107 | return totalListResultsPerPollingStation;
|
---|
108 | }).toList();
|
---|
109 | }
|
---|
110 |
|
---|
111 | List<TotalListResultsPerMunicipality> listResultsPerMuncipality(Long realizationId, Long municipalityId) {
|
---|
112 | List<Map<String, Object>> results = turnoutRepository.totalListResultsPerMunicipality(realizationId, municipalityId);
|
---|
113 | return results.stream().map(x -> {
|
---|
114 | TotalListResultsPerMunicipality totalListResultsPerMunicipality = new TotalListResultsPerMunicipality();
|
---|
115 | totalListResultsPerMunicipality.setListName((x.get("list_name").toString()));
|
---|
116 | totalListResultsPerMunicipality.setPartyName(x.get("participant") != null ? x.get("participant").toString() : null);
|
---|
117 | totalListResultsPerMunicipality.setVoteCount(x.get("vote_count") != null ? x.get("vote_count").toString() : "0");
|
---|
118 | totalListResultsPerMunicipality.setMunicipalityId(x.get("municipality_id") != null ? x.get("vote_count").toString() : "0");
|
---|
119 | return totalListResultsPerMunicipality;
|
---|
120 | }).toList();
|
---|
121 | }
|
---|
122 |
|
---|
123 | List<TotalCandidacyResultsPerPollingStation> candidacyResultsPerPollingStations (Long realizationId, Long pollingStationId) {
|
---|
124 | List<Map<String, Object>> results = turnoutRepository.totalCandidacyResultsPerPollingStation(realizationId, pollingStationId);
|
---|
125 | return results.stream().map(x -> {
|
---|
126 | TotalCandidacyResultsPerPollingStation totalCandidacyResultsPerPollingStationa = new TotalCandidacyResultsPerPollingStation();
|
---|
127 | totalCandidacyResultsPerPollingStationa.setCandidacyId((x.get("candidacy_id").toString()));
|
---|
128 | totalCandidacyResultsPerPollingStationa.setCitizenName((String) x.get("participant").toString());
|
---|
129 | totalCandidacyResultsPerPollingStationa.setVoteCount((String) x.get("vote_count").toString());
|
---|
130 | totalCandidacyResultsPerPollingStationa.setPollingStationId((String) x.get("polling_station_id").toString());
|
---|
131 | return totalCandidacyResultsPerPollingStationa;
|
---|
132 | }).toList();
|
---|
133 | }
|
---|
134 |
|
---|
135 | List<TotalCandidacyResultsPerMunicipality> candidacyResultsPerMunicipalities (Long realizationId, Long municipalityId) {
|
---|
136 | List<Map<String, Object>> results = turnoutRepository.totalCandidacyResultsPerMunicipality(realizationId, municipalityId);
|
---|
137 | return results.stream().map(x -> {
|
---|
138 | TotalCandidacyResultsPerMunicipality totalCandidacyResultsPerMunicipality = new TotalCandidacyResultsPerMunicipality();
|
---|
139 | totalCandidacyResultsPerMunicipality.setCandidacyId((x.get("candidacy_id").toString()));
|
---|
140 | totalCandidacyResultsPerMunicipality.setCitizenName((String) x.get("participant").toString());
|
---|
141 | totalCandidacyResultsPerMunicipality.setVoteCount(x.get("vote_count") != null ? x.get("vote_count").toString() : "0");
|
---|
142 | totalCandidacyResultsPerMunicipality.setMunicipalityId(x.get("municipality_id") != null ? x.get("vote_count").toString() : "0");
|
---|
143 | return totalCandidacyResultsPerMunicipality;
|
---|
144 | }).toList();
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | @Scheduled(cron = "*/30 * * * *")
|
---|
149 | private void refreshMaterializedViews() {
|
---|
150 | //ElectionRealization er = electionRealizationService.findTodaysRealization()
|
---|
151 | //turnoutRepository.refreshCandidatesResults(er.id);
|
---|
152 | System.out.println("REFRESHED VIEW");
|
---|
153 | turnoutRepository.refreshCandidatesResults(7L);
|
---|
154 | }
|
---|
155 |
|
---|
156 | }
|
---|