package app.services.db; import app.entities.DonateStatistic; import app.entities.StatisticRange; import app.entities.Stats; import app.entities.VipGiveMethod; import app.entities.db.DonateStat; import app.entities.other.SteamID; import jakarta.annotation.PostConstruct; import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.ResultSetExtractor; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Component; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @Component public class DonateService { @Autowired @Qualifier("jt_ro") private JdbcTemplate jdbcTemplate; private Stats stats; @Autowired public DonateService(Stats stats) { this.stats = stats; } //KEY, METAL public Pair getSummarySteamDonateValue(StatisticRange statisticRange) { return jdbcTemplate.query("SELECT SUM(CAST(REPLACE(REPLACE(split_part(reserved,';',-2), 'metal=',''), ';','') AS INT)) as r," + " SUM(CAST(REPLACE(REPLACE(split_part(reserved,';',1), 'keys=',''), ';','') as INT)) as k " + "FROM gived_vip WHERE givemethod = ? AND timestamp > date_trunc(?,now())::DATE", new Object[]{VipGiveMethod.STEAM.ordinal(), StatisticRange.cast(statisticRange)}, (rs, n) -> Pair.of( Optional.of(rs.getInt("k")).orElse(0), Optional.of(rs.getInt("r")).orElse(0) )) .stream().findFirst().orElse(Pair.of(0,0)); } public Integer getSummaryQiwiDonateValue(StatisticRange statisticRange) { return jdbcTemplate.query("SELECT SUM(CAST(REPLACE(REPLACE(split_part(reserved,';',1), 'rub=',''), ';','') as INT)) as k " + "FROM gived_vip WHERE givemethod = ? AND timestamp > date_trunc(?,now())::DATE", new Object[]{ VipGiveMethod.QIWI.ordinal(), StatisticRange.cast(statisticRange)}, (rs, n) -> rs.getInt("k")).stream().findFirst().orElse(0); } public Integer getSummaryDonationAlertsDonateValue(StatisticRange statisticRange) { return jdbcTemplate.query("SELECT SUM(CAST(REPLACE(REPLACE(split_part(reserved,';',1), 'rub=',''), ';','') as INT)) as k " + "FROM gived_vip WHERE givemethod = ? AND timestamp > date_trunc(?,now())::DATE", new Object[]{ VipGiveMethod.DONATIONALERTS.ordinal(), StatisticRange.cast(statisticRange)}, (rs, n) -> rs.getInt("k")).stream().findFirst().orElse(0); } //no need if use getGivedVipStatistic public Long getSummaryGivedFreeVip(StatisticRange statisticRange) { return jdbcTemplate.query("SELECT COUNT(*) as count FROM gived_vip WHERE givemethod = ? AND timestamp > date_trunc(?,now())::DATE", new Object[]{ VipGiveMethod.FREE.ordinal(), StatisticRange.cast(statisticRange) }, (rs, n) -> rs.getLong("count")) .stream().findFirst().orElse(0L); } public HashMap getGivedVipStatistic(StatisticRange statisticRange) { List donateStatistics = jdbcTemplate.query("SELECT givemethod,COUNT(*) as count, amount FROM gived_vip WHERE givemethod IN (0,1,2,6) AND timestamp > date_trunc(?,now())::DATE GROUP BY givemethod, amount", new Object[]{StatisticRange.cast(statisticRange)}, (rs, n) -> new DonateStatistic(rs)); HashMap map = new HashMap<>(); map.put("steam", new HashMap<>()); map.put("qiwi", new HashMap<>()); map.put("free", new HashMap<>()); map.put("donationalerts", new HashMap<>()); donateStatistics.forEach(ds -> { switch (ds.getGiveMethod()) { case FREE -> map.get("free").put("day", ds.getCount()); case STEAM -> { switch (ds.getAmount()) { case 86400 -> map.get("steam").put("day", ds.getCount()); case 604800 -> map.get("steam").put("week", ds.getCount()); case 2678400, 2592000 -> map.get("steam").put("month", ds.getCount()); } } case QIWI -> { switch (ds.getAmount()) { case 86400 -> map.get("qiwi").put("day", ds.getCount()); case 604800 -> map.get("qiwi").put("week", ds.getCount()); case 2678400, 2592000 -> map.get("qiwi").put("month", ds.getCount()); } } case DONATIONALERTS -> { switch (ds.getAmount()) { case 86400 -> map.get("donationalerts").put("day", ds.getCount()); case 604800 -> map.get("donationalerts").put("week", ds.getCount()); case 2678400, 2592000 -> map.get("donationalerts").put("month", ds.getCount()); } } } }); return map; } public List getDonateStatistic(SteamID steamID) { return jdbcTemplate.query("SELECT *, extract(epoch from timestamp) as utime FROM gived_vip WHERE steam2 LIKE ?", new Object[]{steamID.steam2}, (rs, n) -> new DonateStat(rs)); } public HashMap getDonateStatistic(StatisticRange statisticRange) { Pair summary_steam = getSummarySteamDonateValue(statisticRange); Integer summary_qiwi = getSummaryQiwiDonateValue(statisticRange); Integer summary_da = getSummaryDonationAlertsDonateValue(statisticRange); HashMap statistic = getGivedVipStatistic(statisticRange); return new HashMap( Map.of( "summary", Map.of( "steam", Map.of("key", summary_steam.getLeft(), "metal", summary_steam.getRight()), "qiwi", summary_qiwi, "donationalerts", summary_da), "statistic", statistic ) ); } @PostConstruct public void UpdateStatistic() { try { stats.getDonate().put("day", getDonateStatistic(StatisticRange.DAY)); stats.getDonate().put("month", getDonateStatistic(StatisticRange.MONTH)); stats.getDonate().put("year", getDonateStatistic(StatisticRange.YEAR)); } catch (Exception err) { err.printStackTrace(); } } //надо добавить шейхов GROUP BY steam2 ORDER BY r DESC }