Add target books completion
This commit is contained in:
@@ -21,7 +21,7 @@ import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import static ru.cathub.telegabot.model.BookEditStateCache.BookEditState.*;
|
||||
import static ru.cathub.telegabot.utils.BookFormatUtils.escapeNonMarkdown;
|
||||
import static ru.cathub.telegabot.utils.BookFormatUtils.*;
|
||||
import static ru.cathub.telegabot.utils.Constants.*;
|
||||
|
||||
@Service
|
||||
@@ -101,15 +101,32 @@ public class BookCreationServiceImpl implements BookCreationService {
|
||||
// Формируем статистику
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate endOfYear = LocalDate.of(today.getYear(), 12, 31);
|
||||
long weeksLeft = ChronoUnit.WEEKS.between(today, endOfYear);
|
||||
Integer weeksLeft = Math.toIntExact(ChronoUnit.WEEKS.between(today, endOfYear));
|
||||
|
||||
BotUser freshUser = userRepository.findById(user.getId()).orElseThrow();
|
||||
int target = freshUser.getBooksToRead() != null ? freshUser.getBooksToRead() : 0;
|
||||
int read = freshUser.getBooks() != null ? freshUser.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);
|
||||
String stats;
|
||||
if (left < 0) {
|
||||
stats = formatBookMoreStats(weeksLeft, read);
|
||||
} else if (left == 0) {
|
||||
stats = "";
|
||||
try {
|
||||
String prompt = String.format("Придумай креативное поздравление с тем, что пользователь выполнил большое достижение по прочтению цель по проятению %d книг на год",freshUser.getBooksToRead());
|
||||
|
||||
String congrat = openRouterService.getChatResponse(prompt, TextRole.DEFAULT);
|
||||
|
||||
log.info("Congratulation target generated: {}", congrat);
|
||||
telegramClientService.sendMessageWithMarkdown(user, "🎉 *Поздравляю с прочтением\\!*\n\n" + escapeNonMarkdown(congrat));
|
||||
} catch (Exception e) {
|
||||
log.error("Error generating congratulation for user {}", user.getId(), e);
|
||||
telegramClientService.sendMessage(user, "📚 Спасибо за добавление книги!");
|
||||
}
|
||||
} else {
|
||||
stats = formatBookLeftStats(weeksLeft, target, read, left);
|
||||
}
|
||||
telegramClientService.sendMessageWithMarkdown(user, message + stats);
|
||||
|
||||
bookRepository.save(book);
|
||||
|
||||
@@ -51,4 +51,24 @@ public class BookFormatUtils {
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String formatBookMoreStats(int weeksLeft, int read) {
|
||||
String stats = String.format("\n\n📊 *Статистика:*\n"
|
||||
+ "⏳ До конца года: %d недель\n"
|
||||
+ "🎯 Цель уже выполнена!!\n"
|
||||
+ "✅ Прочитано: %d\n",
|
||||
weeksLeft, read);
|
||||
return stats;
|
||||
}
|
||||
|
||||
public static String formatBookLeftStats(int weeksLeft, int target, int read, int left) {
|
||||
String stats = String.format("\n\n📊 *Статистика:*\n"
|
||||
+ "⏳ До конца года: %d недель\n"
|
||||
+ "🎯 Цель: %d книг\n"
|
||||
+ "✅ Прочитано: %d\n"
|
||||
+ "📖 Осталось: %d",
|
||||
weeksLeft, target, read, Math.max(left, 0));
|
||||
return stats;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user