Доработка рейтинговой системы

This commit is contained in:
upagge 2020-10-02 16:21:03 +03:00
parent 892cb7bed1
commit 76ca3e8406
No known key found for this signature in database
GPG Key ID: 15CD012E46F6BA34
8 changed files with 64 additions and 39 deletions

View File

@ -7,19 +7,42 @@ package org.sadtech.bot.vcs.bitbucket.app.scheduler;
*/ */
import lombok.RequiredArgsConstructor; 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.sadtech.bot.vcs.core.service.RatingService;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.List;
@Component @Component
@RequiredArgsConstructor @RequiredArgsConstructor
public class RatingScheduler { public class RatingScheduler {
private final RatingService ratingService; private final RatingService ratingService;
private final PersonService personService;
private final NotifyService notifyService;
@Scheduled(cron = "0 */1 * * * *") @Scheduled(cron = "0 */50 * * * *")
private void ratingRecalculation() { private void ratingRecalculation() {
ratingService.ratingRecalculation(); ratingService.ratingRecalculation();
} }
@Scheduled(cron = "0 20 8 * * MON-FRI")
private void goodMorningRating() {
List<Person> 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()
);
}
}
} }

View File

@ -37,4 +37,8 @@
</createTable> </createTable>
</changeSet> </changeSet>
<changeSet id="2020-10-01-delete-unique" author="upagge">
<dropUniqueConstraint tableName="rating_list" constraintName="rating_list_number_key"/>
</changeSet>
</databaseChangeLog> </databaseChangeLog>

View File

@ -18,7 +18,7 @@
</appender> </appender>
<root level="info"> <root level="info">
<appender-ref ref="STDOUT"/> <appender-ref ref="FILE"/>
</root> </root>
</configuration> </configuration>

View File

@ -22,8 +22,6 @@ public interface PersonRepository {
List<Person> findAllByTelegramIdNotNullAndTokenNotNull(); List<Person> findAllByTelegramIdNotNullAndTokenNotNull();
Long findTelegramIdByLogin(String login);
Set<Long> findAllTelegramIdByLogin(Set<String> logins); Set<Long> findAllTelegramIdByLogin(Set<String> logins);
Optional<Person> findByLogin(@NonNull String login); Optional<Person> findByLogin(@NonNull String login);

View File

@ -56,11 +56,6 @@ public class PersonRepositoryImpl implements PersonRepository {
return Collections.emptyList(); return Collections.emptyList();
} }
@Override
public Long findTelegramIdByLogin(String login) {
return jpaRepository.findTelegramIdByLogin(login);
}
@Override @Override
public Set<Long> findAllTelegramIdByLogin(Set<String> logins) { public Set<Long> findAllTelegramIdByLogin(Set<String> logins) {
return jpaRepository.findAllTelegramIdByLogin(logins); return jpaRepository.findAllTelegramIdByLogin(logins);

View File

@ -10,18 +10,12 @@ import java.util.Set;
public interface PersonService { public interface PersonService {
Optional<Person> getByLogin(String login);
Set<String> existsByLogin(@NonNull Set<String> logins); Set<String> existsByLogin(@NonNull Set<String> logins);
boolean existsByLogin(@NonNull String login);
Person reg(@NonNull Person user); Person reg(@NonNull Person user);
List<Person> getAllRegister(); List<Person> getAllRegister();
Optional<Long> getTelegramIdByLogin(@NonNull String login);
Set<Long> getAllTelegramIdByLogin(Set<String> logins); Set<Long> getAllTelegramIdByLogin(Set<String> logins);
Person create(@NonNull Person person); Person create(@NonNull Person person);

View File

@ -40,11 +40,6 @@ public class PersonServiceImpl implements PersonService {
this.notifyService = notifyService; this.notifyService = notifyService;
} }
@Override
public Optional<Person> getByLogin(String login) {
return personRepository.findByLogin(login);
}
@Override @Override
public Set<String> existsByLogin(@NonNull Set<String> logins) { public Set<String> existsByLogin(@NonNull Set<String> logins) {
return logins.stream() return logins.stream()
@ -52,11 +47,6 @@ public class PersonServiceImpl implements PersonService {
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
@Override
public boolean existsByLogin(@NonNull String login) {
return personRepository.existsByLogin(login);
}
@Override @Override
public Person reg(@NonNull Person user) { public Person reg(@NonNull Person user) {
final Optional<Person> optUser = personRepository.findByLogin(user.getLogin()); final Optional<Person> optUser = personRepository.findByLogin(user.getLogin());
@ -92,11 +82,6 @@ public class PersonServiceImpl implements PersonService {
return personRepository.findAllByTelegramIdNotNullAndTokenNotNull(); return personRepository.findAllByTelegramIdNotNullAndTokenNotNull();
} }
@Override
public Optional<Long> getTelegramIdByLogin(@NonNull String login) {
return Optional.ofNullable(personRepository.findTelegramIdByLogin(login));
}
@Override @Override
public Set<Long> getAllTelegramIdByLogin(Set<String> logins) { public Set<Long> getAllTelegramIdByLogin(Set<String> logins) {
return personRepository.findAllTelegramIdByLogin(logins); return personRepository.findAllTelegramIdByLogin(logins);

View File

@ -3,11 +3,13 @@ package org.sadtech.bot.vcs.core.service.impl;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.sadtech.bot.vcs.core.domain.PointType; 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.RatingHistory;
import org.sadtech.bot.vcs.core.domain.entity.RatingList; import org.sadtech.bot.vcs.core.domain.entity.RatingList;
import org.sadtech.bot.vcs.core.exception.NotFoundException; import org.sadtech.bot.vcs.core.exception.NotFoundException;
import org.sadtech.bot.vcs.core.repository.RatingHistoryRepository; import org.sadtech.bot.vcs.core.repository.RatingHistoryRepository;
import org.sadtech.bot.vcs.core.repository.RatingListRepository; 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.service.RatingService;
import org.sadtech.bot.vcs.core.utils.Smile; import org.sadtech.bot.vcs.core.utils.Smile;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -15,6 +17,7 @@ import org.springframework.stereotype.Service;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -29,6 +32,7 @@ public class RatingServiceImpl implements RatingService {
private final RatingHistoryRepository ratingHistoryRepository; private final RatingHistoryRepository ratingHistoryRepository;
private final RatingListRepository ratingListRepository; private final RatingListRepository ratingListRepository;
private final PersonService personService;
@Override @Override
public void addRating(@NonNull String login, @NonNull PointType type, @NonNull Integer points) { 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))) .collect(Collectors.groupingBy(RatingHistory::getLogin, Collectors.summingInt(RatingHistory::getPoints)))
.entrySet().stream() .entrySet().stream()
.map(this::createRatingList) .map(this::createRatingList)
.collect(Collectors.toList());
final Set<String> ratingListLogins = newRatingList.stream()
.map(RatingList::getLogin)
.collect(Collectors.toSet());
final Set<String> 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() .sorted()
.peek(ratingList -> ratingList.setNumber(i.getAndIncrement())) .peek(ratingList -> ratingList.setNumber(i.getAndIncrement()))
.collect(Collectors.toList()); .collect(Collectors.toList())
ratingListRepository.saveAll(newRatingList); );
}
private RatingList createEmptyRatingList(String s) {
final RatingList ratingList = new RatingList();
ratingList.setLogin(s);
ratingList.setPoints(0);
return ratingList;
} }
private RatingList createRatingList(Map.Entry<String, Integer> entry) { private RatingList createRatingList(Map.Entry<String, Integer> entry) {
@ -70,10 +96,10 @@ public class RatingServiceImpl implements RatingService {
.map(this::createString) .map(this::createString)
.collect(Collectors.joining("\n")); .collect(Collectors.joining("\n"));
final String lastMessage = ratingListRepository.findLastThree().stream() final String lastMessage = ratingListRepository.findLastThree().stream()
.limit(countPerson - 3 < 0 ? 0 : countPerson - 3)
.map(this::createString) .map(this::createString)
.limit(countPerson - 3)
.collect(Collectors.joining("\n")); .collect(Collectors.joining("\n"));
String message = threeMessage; String message = "Рейтинговая таблица | Всего участников: " + countPerson + "\n\n" + threeMessage;
if (numberRatingList <= 2) { if (numberRatingList <= 2) {
if (countPerson > 3) { if (countPerson > 3) {
@ -92,13 +118,13 @@ public class RatingServiceImpl implements RatingService {
String message = ""; String message = "";
final Integer number = ratingList.getNumber(); final Integer number = ratingList.getNumber();
if (number == 0) { 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) { } 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) { } 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 { } else {
message += Smile.KAKASHKA + " " + ratingList.getLogin(); message += Smile.KAKASHKA.getValue() + " " + ratingList.getLogin();
} }
return message; return message;
} }