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

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 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<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>
</changeSet>
<changeSet id="2020-10-01-delete-unique" author="upagge">
<dropUniqueConstraint tableName="rating_list" constraintName="rating_list_number_key"/>
</changeSet>
</databaseChangeLog>

View File

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

View File

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

View File

@ -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<Long> findAllTelegramIdByLogin(Set<String> logins) {
return jpaRepository.findAllTelegramIdByLogin(logins);

View File

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

View File

@ -40,11 +40,6 @@ public class PersonServiceImpl implements PersonService {
this.notifyService = notifyService;
}
@Override
public Optional<Person> getByLogin(String login) {
return personRepository.findByLogin(login);
}
@Override
public Set<String> existsByLogin(@NonNull Set<String> 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<Person> optUser = personRepository.findByLogin(user.getLogin());
@ -92,11 +82,6 @@ public class PersonServiceImpl implements PersonService {
return personRepository.findAllByTelegramIdNotNullAndTokenNotNull();
}
@Override
public Optional<Long> getTelegramIdByLogin(@NonNull String login) {
return Optional.ofNullable(personRepository.findTelegramIdByLogin(login));
}
@Override
public Set<Long> getAllTelegramIdByLogin(Set<String> logins) {
return personRepository.findAllTelegramIdByLogin(logins);

View File

@ -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<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()
.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<String, Integer> 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;
}