1 | package mk.ukim.finki.eglas.repository;
|
---|
2 |
|
---|
3 | import jakarta.transaction.Transactional;
|
---|
4 | import mk.ukim.finki.eglas.model.CandidatesElectionRealization;
|
---|
5 | import mk.ukim.finki.eglas.model.ElectionRealization;
|
---|
6 | import mk.ukim.finki.eglas.model.PollingStation;
|
---|
7 | import mk.ukim.finki.eglas.model.Turnout;
|
---|
8 | import mk.ukim.finki.eglas.model.views.TotalTurnoutByMunicipality;
|
---|
9 | import mk.ukim.finki.eglas.records.TotalCandidacyResults;
|
---|
10 | import mk.ukim.finki.eglas.records.TotalListResultsPerPollingStation;
|
---|
11 | import org.springframework.data.jpa.repository.JpaRepository;
|
---|
12 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
---|
13 | import org.springframework.data.jpa.repository.Modifying;
|
---|
14 | import org.springframework.data.jpa.repository.Query;
|
---|
15 | import org.springframework.data.repository.query.Param;
|
---|
16 | import org.springframework.security.core.parameters.P;
|
---|
17 |
|
---|
18 | import java.time.LocalDate;
|
---|
19 | import java.util.List;
|
---|
20 | import java.util.Map;
|
---|
21 |
|
---|
22 | public interface TurnoutRepository extends JpaRepository<Turnout, Long>, JpaSpecificationExecutor<Turnout> {
|
---|
23 | @Query(value = "SELECT (SELECT COUNT(v) FROM Turnout v WHERE v.electionRealization = :ri) * 100.0 / (SELECT COUNT(c) FROM Citizen c WHERE c.dateOfBirth <= :dateThreshold) FROM ElectionRealization ri WHERE ri = :ri")
|
---|
24 | Double turnOutByRealization(@Param("ri") ElectionRealization electionRealization, @Param("dateThreshold") LocalDate dateThreshold);
|
---|
25 |
|
---|
26 | @Query("SELECT NEW mk.ukim.finki.eglas.records.TotalCandidacyResults(kan, COUNT(distinct cv), null) " +
|
---|
27 | "FROM Candidate k " +
|
---|
28 | "LEFT JOIN Candidacy kan ON kan.candidate = k AND kan.candidatesElectionRealization = :realization " +
|
---|
29 | "LEFT JOIN CandidacyVote cv ON cv.candidacy = kan " +
|
---|
30 | "WHERE kan is not null " +
|
---|
31 | "GROUP BY kan")
|
---|
32 | List<TotalCandidacyResults> countVotesByCitizenAndRealization(@Param("realization") CandidatesElectionRealization realization);
|
---|
33 |
|
---|
34 | @Query("select tr from TotalTurnoutByMunicipality tr where tr.map_id = :mapId and tr.realizationId = :realizationId")
|
---|
35 | List<TotalTurnoutByMunicipality> turnOutByMunicipality(@Param("mapId") String mapId, @Param("realizationId") Long realizationId);
|
---|
36 |
|
---|
37 |
|
---|
38 | @Query(value = "SELECT (SELECT COUNT(v) FROM Turnout v WHERE v.electionRealization.id = :realizationId and v.pollingStation.id = :pollingStationId) * 100.0 / (SELECT COUNT(c) FROM Citizen c join Address a on c.address = a WHERE c.dateOfBirth <= :dateThreshold and a.pollingStation.id = :pollingStationId) FROM ElectionRealization ri WHERE ri.id = :realizationId")
|
---|
39 | Double turnOutByRealizationAndMunicipalityAndPollingStation(@Param("realizationId") Long realizationId, @Param("pollingStationId") Long pollingStationId, @Param("dateThreshold") LocalDate dateThreshold);
|
---|
40 |
|
---|
41 | // Turnout getTurnoutByPollingStationAndAndElectionRealization(PollingStation pollingStation, ElectionRealization electionRealization);
|
---|
42 |
|
---|
43 | @Modifying
|
---|
44 | @Transactional
|
---|
45 | @Query(value = "refresh materialized view turnout_by_municipality", nativeQuery = true)
|
---|
46 | void refreshTurnoutByMunicipalityView();
|
---|
47 |
|
---|
48 | @Query("select count(distinct t.id) > 0 from Turnout t where t.citizen.id = :citizenId and t.electionRealization.id = :realizationId")
|
---|
49 | Boolean hasCitizenVotedOnRealization(@Param("citizenId") Long citizenId, @Param("realizationId") Long realizationId);
|
---|
50 |
|
---|
51 | @Query(value = "select kl.kl_id as list_name, p.p_ime as participant, im_id as polling_station_id, res.vote_count as vote_count " +
|
---|
52 | "from kandidatski_listi kl " +
|
---|
53 | " join public.partii p on p.p_id = kl.p_id " +
|
---|
54 | " left join get_view_for_lists_realizations(:realizationId) res on res.kl_id = kl.kl_id and res.im_id = :pollingStationId " +
|
---|
55 | "where kl.ri_id = :realizationId", nativeQuery = true)
|
---|
56 | List<Map<String, Object>> totalListResultsPerPollingStation(
|
---|
57 | @Param("realizationId") Long realizationId,
|
---|
58 | @Param("pollingStationId") Long pollingStationId);
|
---|
59 |
|
---|
60 | @Query(value = "select kl.kl_id as list_name, p.p_ime as participant, res.o_id as municipality_id, res.vote_count as vote_count " +
|
---|
61 | "from kandidatski_listi kl " +
|
---|
62 | " join public.partii p on p.p_id = kl.p_id " +
|
---|
63 | " left join get_view_for_lists_realizations_by_municipalities(:realizationId) res on res.gl_id = kl.kl_id and res.o_id = :municipalityId " +
|
---|
64 | "where kl.ri_id = :realizationId", nativeQuery = true)
|
---|
65 | List<Map<String, Object>> totalListResultsPerMunicipality(
|
---|
66 | @Param("realizationId") Long realizationId,
|
---|
67 | @Param("municipalityId") Long municipalityId);
|
---|
68 |
|
---|
69 | @Query(value = "select kan.kan_id as candidacy_id, (g.g_ime || ' ' || g.g_prezime) participant, res.im_id as polling_station_id, res.vote_count as vote_count " +
|
---|
70 | "from kandidaturi kan " +
|
---|
71 | " join gragjani g on g.g_id = kan.g_id " +
|
---|
72 | "left join get_view_for_candidates_realizations(1) res on res.kan_id = kan.kan_id and res.im_id = :pollingStationId " +
|
---|
73 | "where kan.ri_id = :realizationId", nativeQuery = true)
|
---|
74 | List<Map<String, Object>> totalCandidacyResultsPerPollingStation(
|
---|
75 | @Param("realizationId") Long realizationId,
|
---|
76 | @Param("pollingStationId") Long pollingStationId);
|
---|
77 |
|
---|
78 | @Query(value = "select kan.kan_id as candidacy_id, (g.g_ime || ' ' || g.g_prezime) as participant, res.o_id as municipality_id, res.vote_count as vote_count " +
|
---|
79 | "from kandidaturi kan " +
|
---|
80 | " join gragjani g on g.g_id = kan.g_id " +
|
---|
81 | "left join get_view_for_candidates_realizations_by_municipalities(:realizationId) res on res.kan_id = kan.kan_id and res.o_id = :municipalityId " +
|
---|
82 | "where kan.ri_id = :realizationId", nativeQuery = true)
|
---|
83 | List<Map<String, Object>> totalCandidacyResultsPerMunicipality(
|
---|
84 | @Param("realizationId") Long realizationId,
|
---|
85 | @Param("municipalityId") Long municipalityId);
|
---|
86 | // List<TotalListResultsPerPollingStation> totalListResultsPerPollingStation(@Param("realizationId") Long realizationId,
|
---|
87 | // @Param("pollingStationId") Long pollingStationId);
|
---|
88 |
|
---|
89 | @Query(value = "select refresh_view_for_candidacy_realizations(:realizationId)", nativeQuery = true)
|
---|
90 | public void refreshCandidatesResults(@Param("realizationId") Long realizationId);
|
---|
91 | } |
---|