You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

136 lines
6.3 KiB

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 jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Component
@Transactional("RoTransactionManager")
public class DonateService {
EntityManager entityManager;
Stats stats;
@Autowired
public DonateService(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager) {
this.entityManager = entityManager;
this.stats = stats;
}
//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)")
.setParameter(1, VipGiveMethod.STEAM.ordinal())
.setParameter(2, StatisticRange.cast(statisticRange))
.getResultList();
int metal = 0, key = 0;
if(!steam.isEmpty()) {
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)")
.setParameter(1, VipGiveMethod.QIWI.ordinal())
.setParameter(2, StatisticRange.cast(statisticRange))
.getResultList();
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();
return count.intValue();
}
public HashMap getGivedVipStatistic(StatisticRange statisticRange) {
List<Object[]> raw_donateStatistics = entityManager.createNativeQuery("SELECT `givemethod`,COUNT(*) as count,`reserved`, `amount` FROM `gived_vip` WHERE `givemethod` IN (0,1,2) AND `timestamp` > CAST(DATE_FORMAT(NOW() ,?1) as DATE) GROUP BY `givemethod`, `amount`")
.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.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());
}
}
}
});
return map;
}
public List<DonateStat> getDonateStatistic(SteamID steamID) {
return entityManager.createNativeQuery("SELECT *, UNIX_TIMESTAMP(`timestamp`) as utime FROM `gived_vip` WHERE `steam2` LIKE ?1")
.setParameter(1, steamID.steam2)
.getResultStream().map(obj -> new DonateStat((Object[]) obj)).toList();
}
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));
stats.getDonate().put("month", getDonateStatistic(StatisticRange.MONTH));
stats.getDonate().put("year", getDonateStatistic(StatisticRange.YEAR));
}
//надо добавить шейхов GROUP BY `steam2` ORDER BY `r` DESC
}