diff --git a/internal/db/db.go b/internal/db/db.go index 7663cc7..676a634 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -125,17 +125,13 @@ func (db *DB) SetPause(chatID int64, duration time.Duration) error { return err } -func (db *DB) GetStat(chatID int64) (map[string]int, error) { +func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]int, error) { ctx := context.Background() if err := db.AddChat(chatID); err != nil { return nil, err } - loc, _ := time.LoadLocation("Asia/Novosibirsk") - t := time.Now().In(loc) - t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc) - res := map[string]int{} cursor, err := db.workoutsColection.Find( diff --git a/internal/states/stat_bot_state/stat_bot_state.go b/internal/states/stat_bot_state/stat_bot_state.go index 1275edf..c119af6 100644 --- a/internal/states/stat_bot_state/stat_bot_state.go +++ b/internal/states/stat_bot_state/stat_bot_state.go @@ -3,6 +3,7 @@ package stat_bot_state import ( "fmt" "net/http" + "time" "valera/internal/db" "valera/internal/states" @@ -12,6 +13,7 @@ import ( var names = []string{ "/stat", + "/stat_week", } type statBotState struct { @@ -31,7 +33,14 @@ func (s *statBotState) Do(text string, chatInfo *db.ChatInfo, username string) e return nil } - s.sendStatToChat(chatInfo.ChatID, "") + if text == "/stat" { + s.sendStatToChatAfter(chatInfo.ChatID, "Результаты за сегодня:\n", s.getStartDay()) + } + + if text == "/stat_week" { + s.sendStatToChatAfter(chatInfo.ChatID, "Результаты за неделю:\n", s.getStartWeek()) + } + return nil } @@ -46,13 +55,21 @@ func (s *statBotState) GetHandler() (string, func(http.ResponseWriter, *http.Req return } for _, chatID := range chats { - if err := s.sendStatToChat(chatID, "Напоминаю:\n- Cегодня больше не жрем!\n\n"); err != nil { - fmt.Println(err) + t := s.getStartDay() + if err := s.sendStatToChatAfter(chatID, "Напоминаю:\n- Cегодня больше не жрем!\n\nРезультаты за сегодня:\n", s.getStartDay()); err != nil { w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusInternalServerError) _, _ = fmt.Fprintf(w, `{"result":"error"}`) return } + if t.Weekday() == time.Sunday { + if err := s.sendStatToChatAfter(chatID, "Результаты за неделю:\n", s.getStartWeek()); err != nil { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + _, _ = fmt.Fprintf(w, `{"result":"error"}`) + return + } + } } w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) @@ -60,15 +77,28 @@ func (s *statBotState) GetHandler() (string, func(http.ResponseWriter, *http.Req } } -func (s *statBotState) sendStatToChat(chatID int64, prefix string) error { - stat, err := s.dataBase.GetStat(chatID) +func (s *statBotState) sendStatToChatAfter(chatID int64, prefix string, t time.Time) error { + msgText := prefix + stat, err := s.dataBase.GetStatAfter(chatID, t) if err != nil { return err } - msgText := prefix + "Результаты за сегодня:\n" for k, v := range stat { msgText += fmt.Sprintf("- %s: %d\n", k, v) } _, _ = s.bot.Send(tgbot.NewMessage(chatID, msgText)) return nil } + +func (s *statBotState) getStartDay() time.Time { + loc, _ := time.LoadLocation("Asia/Novosibirsk") + t := time.Now().In(loc) + t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc) + return t +} + +func (s *statBotState) getStartWeek() time.Time { + t := s.getStartDay() + t = t.Add(-6 * 24 * time.Hour) + return t +}