Browse Source

хайп покупки, стата для интереса

master
gsd 2 years ago
parent
commit
f8b13780f0
  1. 22
      src/main/java/app/entities/DonateStatistic.java
  2. 84
      src/main/java/app/services/db/DonateService.java

22
src/main/java/app/entities/DonateStatistic.java

@ -0,0 +1,22 @@
package app.entities;
import lombok.Data;
@Data
public class DonateStatistic {
VipGiveMethod giveMethod;
Integer count;
String reserved;
//sub
public DonateStatistic(Object[] raw) {
giveMethod = VipGiveMethod.values()[(int) raw[0]];
count = ((Long) raw[1]).intValue();
reserved = raw[2]==null?"": (String) raw[2];
}
public DonateStatistic(Object o) {
this((Object[]) o);
}
public DonateStatistic() {}
}

84
src/main/java/app/services/db/DonateService.java

@ -1,11 +1,13 @@
package app.services.db;
import app.entities.DonateStatistic;
import app.entities.StatisticRange;
import app.entities.Stats;
import app.entities.VipGiveMethod;
import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@ -13,6 +15,7 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Component
@ -28,44 +31,89 @@ public class DonateService {
this.stats = stats;
}
public HashMap<String, Integer> getDonateStatistic(StatisticRange statisticRange) {
HashMap<String, Integer> map = new HashMap<>();
//KEY, METAL
public Pair<Integer, Integer> getSummarySteamDonateValue(StatisticRange statisticRange) {
List<Object[]> steam = entityManager.createNativeQuery(
"SELECT SUM(CAST(REPLACE(REPLACE(substring_index(`reserved`,';',-2), 'metal=',''), ';','') AS INT)) as r," +
" SUM(CAST(REPLACE(REPLACE(substring_index(`reserved`,';',1), 'keys=',''), ';','') as INT)) as k " +
"FROM `gived_vip` WHERE `givemethod` = ?1 AND `timestamp` > CAST(DATE_FORMAT(NOW() ,?2) as DATE)")
"SELECT SUM(CAST(REPLACE(REPLACE(substring_index(`reserved`,';',-2), 'metal=',''), ';','') AS INT)) as r," +
" SUM(CAST(REPLACE(REPLACE(substring_index(`reserved`,';',1), 'keys=',''), ';','') as INT)) as k " +
"FROM `gived_vip` WHERE `givemethod` = ?1 AND `timestamp` > CAST(DATE_FORMAT(NOW() ,?2) as DATE)")
.setParameter(1, VipGiveMethod.STEAM.ordinal())
.setParameter(2, StatisticRange.cast(statisticRange))
.getResultList();
int metal = 0, key = 0;
if(!steam.isEmpty()) {
System.out.println(steam.get(0));
//За то что ниже мне стыдно
map.put("metal", steam.get(0)==null||steam.get(0)[0]==null?0: Integer.parseInt(steam.get(0)[0].toString()));
map.put("key", steam.get(0)==null||steam.get(0)[1]==null?0: Integer.parseInt(steam.get(0)[1].toString()));
} else {
map.put("metal", 0);
map.put("key", 0);
metal = steam.get(0)==null||steam.get(0)[0]==null?0: Integer.parseInt(steam.get(0)[0].toString());
key = steam.get(0)==null||steam.get(0)[1]==null?0: Integer.parseInt(steam.get(0)[1].toString());
}
return Pair.of(key, metal);
}
public Integer getSummaryQiwiDonateValue(StatisticRange statisticRange) {
List<Object> qiwi = entityManager.createNativeQuery(
"SELECT SUM(CAST(REPLACE(REPLACE(substring_index(`reserved`,';',1), 'rub=',''), ';','') as INT)) as k " +
"FROM `gived_vip` WHERE `givemethod` = ?1 AND `timestamp` > CAST(DATE_FORMAT(NOW() ,?2) as DATE)")
"SELECT SUM(CAST(REPLACE(REPLACE(substring_index(`reserved`,';',1), 'rub=',''), ';','') as INT)) as k " +
"FROM `gived_vip` WHERE `givemethod` = ?1 AND `timestamp` > CAST(DATE_FORMAT(NOW() ,?2) as DATE)")
.setParameter(1, VipGiveMethod.QIWI.ordinal())
.setParameter(2, StatisticRange.cast(statisticRange))
.getResultList();
if(!qiwi.isEmpty()) map.put("rub", qiwi.get(0)==null?0: Integer.parseInt(qiwi.get(0).toString()));
else map.put("rub", 0);
return qiwi.get(0)==null?0: Integer.parseInt(qiwi.get(0).toString());
}
//no need if use getGivedVipStatistic
public Integer getSummaryGivedFreeVip(StatisticRange statisticRange) {
Long count = (Long) entityManager.createNativeQuery("SELECT COUNT(*) as count FROM `gived_vip` WHERE `givemethod` = ?1 AND `timestamp` > CAST(DATE_FORMAT(NOW() ,?2) as DATE)")
.setParameter(1, VipGiveMethod.FREE.ordinal())
.setParameter(2, StatisticRange.cast(statisticRange))
.getSingleResult();
map.put("free", count.intValue());
return count.intValue();
}
public HashMap getGivedVipStatistic(StatisticRange statisticRange) {
List<Object[]> raw_donateStatistics = entityManager.createNativeQuery("SELECT `givemethod`,COUNT(*) as count,`reserved` FROM `gived_vip` WHERE `givemethod` IN (0,1,2) AND `timestamp` > CAST(DATE_FORMAT(NOW() ,?1) as DATE) GROUP BY `givemethod`, `reserved`")
.setParameter(1, StatisticRange.cast(statisticRange))
.getResultList();
List<DonateStatistic> donateStatistics = raw_donateStatistics.stream().map(DonateStatistic::new).toList();
HashMap<String, HashMap> map = new HashMap<>();
map.put("steam", new HashMap<>());
map.put("qiwi", new HashMap<>());
map.put("free", new HashMap<>());
donateStatistics.forEach(ds -> {
switch (ds.getGiveMethod()) {
case FREE -> map.get("free").put("day", ds.getCount());
case STEAM -> {
switch (ds.getReserved()) {
case "keys=0;metal=20;" -> map.get("steam").put("week", ds.getCount());
case "keys=0;metal=5;" -> map.get("steam").put("day", ds.getCount());
case "keys=1;metal=0;" -> map.get("steam").put("month", ds.getCount());
}
}
case QIWI -> {
switch (ds.getReserved()) {
case "rub=150;" -> map.get("qiwi").put("month", ds.getCount());
case "rub=75;" -> map.get("qiwi").put("week", ds.getCount());
case "rub=20;" -> map.get("qiwi").put("day", ds.getCount());
}
}
}
});
return map;
}
public HashMap getDonateStatistic(StatisticRange statisticRange) {
Pair<Integer, Integer> summary_steam = getSummarySteamDonateValue(statisticRange);
Integer summary_qiwi = getSummaryQiwiDonateValue(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),
"statistic", statistic
)
);
}
@PostConstruct
public void UpdateStatistic() {
stats.getDonate().put("day", getDonateStatistic(StatisticRange.DAY));

Loading…
Cancel
Save