feat: Add reading statistics to book creation message

This commit is contained in:
2025-02-16 19:09:40 +03:00
parent 0da26e98ee
commit cd81772e34
@@ -14,6 +14,8 @@ import ru.cathub.telegabot.utils.Constants;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import static ru.cathub.telegabot.model.BookEditStateCache.BookEditState.*;
import static ru.cathub.telegabot.utils.Constants.*;
@@ -104,7 +106,25 @@ public class BookCreationServiceImpl implements BookCreationService {
TextRole.DEFAULT
);
telegramClientService.sendMessageWithMarkdown(user, "🎉 *Поздравляю с прочтением!*\n\n" + congrat);
// Формируем статистику
LocalDate today = LocalDate.now();
LocalDate endOfYear = LocalDate.of(today.getYear(), 12, 31);
long weeksLeft = ChronoUnit.WEEKS.between(today, endOfYear);
int target = user.getBooksToRead() != null ? user.getBooksToRead() : 0;
int read = user.getBooks() != null ? user.getBooks().size() : 0;
int left = target - read;
String stats = String.format(
"\n\n📊 *Статистика:*\n" +
"⏳ До конца года: %d недель\n" +
"🎯 Цель: %d книг\n" +
"✅ Прочитано: %d\n" +
"📖 Осталось: %d",
weeksLeft, target, read, left > 0 ? left : 0
);
telegramClientService.sendMessageWithMarkdown(user, "🎉 *Поздравляю с прочтением!*\n\n" + congrat + stats);
} catch (Exception e) {
log.error("Error generating congratulation for user {}", user.getId(), e);
telegramClientService.sendMessage(user, "📚 Спасибо за добавление книги!");