From 76ca3e8406ea63124d17073632f9933338dc9c79 Mon Sep 17 00:00:00 2001 From: upagge Date: Fri, 2 Oct 2020 16:21:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D1=80=D0=B5=D0=B9=D1=82=D0=B8=D0=BD=D0=B3=D0=BE?= =?UTF-8?q?=D0=B2=D0=BE=D0=B9=20=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BC=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/scheduler/RatingScheduler.java | 25 ++++++++++- .../liquibase/v.2.0.0/2020-10-01-rating.xml | 4 ++ bitbucket-app/src/main/resources/logback.xml | 2 +- .../vcs/core/repository/PersonRepository.java | 2 - .../repository/impl/PersonRepositoryImpl.java | 5 --- .../bot/vcs/core/service/PersonService.java | 6 --- .../core/service/impl/PersonServiceImpl.java | 15 ------- .../core/service/impl/RatingServiceImpl.java | 44 +++++++++++++++---- 8 files changed, 64 insertions(+), 39 deletions(-) diff --git a/bitbucket-app/src/main/java/org/sadtech/bot/vcs/bitbucket/app/scheduler/RatingScheduler.java b/bitbucket-app/src/main/java/org/sadtech/bot/vcs/bitbucket/app/scheduler/RatingScheduler.java index 8855191..783f30c 100644 --- a/bitbucket-app/src/main/java/org/sadtech/bot/vcs/bitbucket/app/scheduler/RatingScheduler.java +++ b/bitbucket-app/src/main/java/org/sadtech/bot/vcs/bitbucket/app/scheduler/RatingScheduler.java @@ -7,19 +7,42 @@ package org.sadtech.bot.vcs.bitbucket.app.scheduler; */ import lombok.RequiredArgsConstructor; +import org.sadtech.bot.vcs.core.domain.entity.Person; +import org.sadtech.bot.vcs.core.domain.notify.SimpleTextNotify; +import org.sadtech.bot.vcs.core.service.NotifyService; +import org.sadtech.bot.vcs.core.service.PersonService; import org.sadtech.bot.vcs.core.service.RatingService; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import java.util.Collections; +import java.util.List; + @Component @RequiredArgsConstructor public class RatingScheduler { private final RatingService ratingService; + private final PersonService personService; + private final NotifyService notifyService; - @Scheduled(cron = "0 */1 * * * *") + @Scheduled(cron = "0 */50 * * * *") private void ratingRecalculation() { ratingService.ratingRecalculation(); } + @Scheduled(cron = "0 20 8 * * MON-FRI") + private void goodMorningRating() { + List allRegister = personService.getAllRegister(); + for (Person person : allRegister) { + final String message = ratingService.getRatingTop(person.getLogin()); + notifyService.send( + SimpleTextNotify.builder() + .message(message) + .logins(Collections.singleton(person.getLogin())) + .build() + ); + } + } + } diff --git a/bitbucket-app/src/main/resources/liquibase/v.2.0.0/2020-10-01-rating.xml b/bitbucket-app/src/main/resources/liquibase/v.2.0.0/2020-10-01-rating.xml index e41bb20..195b4ed 100644 --- a/bitbucket-app/src/main/resources/liquibase/v.2.0.0/2020-10-01-rating.xml +++ b/bitbucket-app/src/main/resources/liquibase/v.2.0.0/2020-10-01-rating.xml @@ -37,4 +37,8 @@ + + + + \ No newline at end of file diff --git a/bitbucket-app/src/main/resources/logback.xml b/bitbucket-app/src/main/resources/logback.xml index e106cf3..0335154 100644 --- a/bitbucket-app/src/main/resources/logback.xml +++ b/bitbucket-app/src/main/resources/logback.xml @@ -18,7 +18,7 @@ - + \ No newline at end of file diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/repository/PersonRepository.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/repository/PersonRepository.java index 9f12541..065f44d 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/repository/PersonRepository.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/repository/PersonRepository.java @@ -22,8 +22,6 @@ public interface PersonRepository { List findAllByTelegramIdNotNullAndTokenNotNull(); - Long findTelegramIdByLogin(String login); - Set findAllTelegramIdByLogin(Set logins); Optional findByLogin(@NonNull String login); diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/repository/impl/PersonRepositoryImpl.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/repository/impl/PersonRepositoryImpl.java index 6888703..d8a15bb 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/repository/impl/PersonRepositoryImpl.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/repository/impl/PersonRepositoryImpl.java @@ -56,11 +56,6 @@ public class PersonRepositoryImpl implements PersonRepository { return Collections.emptyList(); } - @Override - public Long findTelegramIdByLogin(String login) { - return jpaRepository.findTelegramIdByLogin(login); - } - @Override public Set findAllTelegramIdByLogin(Set logins) { return jpaRepository.findAllTelegramIdByLogin(logins); diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/PersonService.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/PersonService.java index 84d2172..4b8a14f 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/PersonService.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/PersonService.java @@ -10,18 +10,12 @@ import java.util.Set; public interface PersonService { - Optional getByLogin(String login); - Set existsByLogin(@NonNull Set logins); - boolean existsByLogin(@NonNull String login); - Person reg(@NonNull Person user); List getAllRegister(); - Optional getTelegramIdByLogin(@NonNull String login); - Set getAllTelegramIdByLogin(Set logins); Person create(@NonNull Person person); diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/PersonServiceImpl.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/PersonServiceImpl.java index 94d7a51..2f2b7c3 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/PersonServiceImpl.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/PersonServiceImpl.java @@ -40,11 +40,6 @@ public class PersonServiceImpl implements PersonService { this.notifyService = notifyService; } - @Override - public Optional getByLogin(String login) { - return personRepository.findByLogin(login); - } - @Override public Set existsByLogin(@NonNull Set logins) { return logins.stream() @@ -52,11 +47,6 @@ public class PersonServiceImpl implements PersonService { .collect(Collectors.toSet()); } - @Override - public boolean existsByLogin(@NonNull String login) { - return personRepository.existsByLogin(login); - } - @Override public Person reg(@NonNull Person user) { final Optional optUser = personRepository.findByLogin(user.getLogin()); @@ -92,11 +82,6 @@ public class PersonServiceImpl implements PersonService { return personRepository.findAllByTelegramIdNotNullAndTokenNotNull(); } - @Override - public Optional getTelegramIdByLogin(@NonNull String login) { - return Optional.ofNullable(personRepository.findTelegramIdByLogin(login)); - } - @Override public Set getAllTelegramIdByLogin(Set logins) { return personRepository.findAllTelegramIdByLogin(logins); diff --git a/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/RatingServiceImpl.java b/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/RatingServiceImpl.java index d9ddb3c..6ddf88c 100644 --- a/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/RatingServiceImpl.java +++ b/bot-core/src/main/java/org/sadtech/bot/vcs/core/service/impl/RatingServiceImpl.java @@ -3,11 +3,13 @@ package org.sadtech.bot.vcs.core.service.impl; import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.sadtech.bot.vcs.core.domain.PointType; +import org.sadtech.bot.vcs.core.domain.entity.Person; import org.sadtech.bot.vcs.core.domain.entity.RatingHistory; import org.sadtech.bot.vcs.core.domain.entity.RatingList; import org.sadtech.bot.vcs.core.exception.NotFoundException; import org.sadtech.bot.vcs.core.repository.RatingHistoryRepository; import org.sadtech.bot.vcs.core.repository.RatingListRepository; +import org.sadtech.bot.vcs.core.service.PersonService; import org.sadtech.bot.vcs.core.service.RatingService; import org.sadtech.bot.vcs.core.utils.Smile; import org.springframework.stereotype.Service; @@ -15,6 +17,7 @@ import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @@ -29,6 +32,7 @@ public class RatingServiceImpl implements RatingService { private final RatingHistoryRepository ratingHistoryRepository; private final RatingListRepository ratingListRepository; + private final PersonService personService; @Override public void addRating(@NonNull String login, @NonNull PointType type, @NonNull Integer points) { @@ -47,10 +51,32 @@ public class RatingServiceImpl implements RatingService { .collect(Collectors.groupingBy(RatingHistory::getLogin, Collectors.summingInt(RatingHistory::getPoints))) .entrySet().stream() .map(this::createRatingList) - .sorted() - .peek(ratingList -> ratingList.setNumber(i.getAndIncrement())) .collect(Collectors.toList()); - ratingListRepository.saveAll(newRatingList); + final Set ratingListLogins = newRatingList.stream() + .map(RatingList::getLogin) + .collect(Collectors.toSet()); + final Set regLogins = personService.getAllRegister().stream() + .map(Person::getLogin) + .collect(Collectors.toSet()); + newRatingList.addAll( + regLogins.stream() + .filter(s -> !ratingListLogins.contains(s)) + .map(this::createEmptyRatingList) + .collect(Collectors.toList()) + ); + ratingListRepository.saveAll( + newRatingList.stream() + .sorted() + .peek(ratingList -> ratingList.setNumber(i.getAndIncrement())) + .collect(Collectors.toList()) + ); + } + + private RatingList createEmptyRatingList(String s) { + final RatingList ratingList = new RatingList(); + ratingList.setLogin(s); + ratingList.setPoints(0); + return ratingList; } private RatingList createRatingList(Map.Entry entry) { @@ -70,10 +96,10 @@ public class RatingServiceImpl implements RatingService { .map(this::createString) .collect(Collectors.joining("\n")); final String lastMessage = ratingListRepository.findLastThree().stream() + .limit(countPerson - 3 < 0 ? 0 : countPerson - 3) .map(this::createString) - .limit(countPerson - 3) .collect(Collectors.joining("\n")); - String message = threeMessage; + String message = "Рейтинговая таблица | Всего участников: " + countPerson + "\n\n" + threeMessage; if (numberRatingList <= 2) { if (countPerson > 3) { @@ -92,13 +118,13 @@ public class RatingServiceImpl implements RatingService { String message = ""; final Integer number = ratingList.getNumber(); if (number == 0) { - message += Smile.TOP_ONE + " " + ratingList.getLogin() + " " + Smile.TOP_ONE; + message += Smile.TOP_ONE.getValue() + " " + ratingList.getLogin() + " " + Smile.TOP_ONE.getValue(); } else if (number == 1) { - message += Smile.TOP_TWO + " " + ratingList.getLogin() + " " + Smile.TOP_TWO; + message += Smile.TOP_TWO.getValue() + " " + ratingList.getLogin() + " " + Smile.TOP_TWO.getValue(); } else if (number == 2) { - message += Smile.TOP_THREE + " " + ratingList.getLogin() + " " + Smile.TOP_THREE; + message += Smile.TOP_THREE.getValue() + " " + ratingList.getLogin() + " " + Smile.TOP_THREE.getValue(); } else { - message += Smile.KAKASHKA + " " + ratingList.getLogin(); + message += Smile.KAKASHKA.getValue() + " " + ratingList.getLogin(); } return message; }