diff --git a/go.mod b/go.mod index cde5a6f..038019a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module valera -go 1.18 +go 1.20 require ( github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible diff --git a/internal/db/db.go b/internal/db/db.go index 377bd21..7e12884 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -2,6 +2,7 @@ package db import ( "context" + "fmt" "log" "time" @@ -152,7 +153,7 @@ func (db *DB) SetPause(chatID int64, duration time.Duration) error { return err } -func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error) { +func (db *DB) GetStatBetween(chatID int64, startTime time.Time, endTime time.Time) (map[string]float64, error) { ctx := context.Background() if err := db.AddChat(chatID); err != nil { @@ -164,9 +165,17 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error cursor, err := db.workoutsColection.Find( ctx, bson.M{ - "chat_id": chatID, - "created_at": bson.M{"$gt": t}, + "chat_id": chatID, + "created_at": bson.M{ + "$gte": startTime, + "$lt": endTime, + }, }, + options.Find().SetSort( + bson.M{ + "created_at": 1, + }, + ), ) if err != nil { return nil, err @@ -176,6 +185,7 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error if err := cursor.Decode(&result); err != nil { log.Fatal(err) } + fmt.Println(result) res[result.Name] += float64(result.Count) } if err := cursor.Err(); err != nil { @@ -185,9 +195,17 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error caloriesCursor, err := db.caloriesColection.Find( ctx, bson.M{ - "chat_id": chatID, - "created_at": bson.M{"$gt": t}, + "chat_id": chatID, + "created_at": bson.M{ + "$gte": startTime, + "$lt": endTime, + }, }, + options.Find().SetSort( + bson.M{ + "created_at": 1, + }, + ), ) if err != nil { return nil, err @@ -206,9 +224,17 @@ func (db *DB) GetStatAfter(chatID int64, t time.Time) (map[string]float64, error weightCursor, err := db.weightColection.Find( ctx, bson.M{ - "chat_id": chatID, - "created_at": bson.M{"$gt": t}, + "chat_id": chatID, + "created_at": bson.M{ + "$gte": startTime, + "$lt": endTime, + }, }, + options.Find().SetSort( + bson.M{ + "created_at": 1, + }, + ), ) if err != nil { return nil, err diff --git a/internal/modules/times/times.go b/internal/modules/times/times.go index ac7f1ae..223c277 100644 --- a/internal/modules/times/times.go +++ b/internal/modules/times/times.go @@ -1,14 +1,36 @@ package times -import "time" +import ( + "time" +) + +var ( + loc *time.Location +) + +func init() { + loc, _ = time.LoadLocation("Asia/Novosibirsk") +} + +func GetNow() time.Time { + t := time.Now().In(loc) + return t +} func GetStartDay() time.Time { - loc, _ := time.LoadLocation("Asia/Novosibirsk") - t := time.Now().In(loc) + t := GetNow() t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc) return t } +func GetStartDayMinus(days int) time.Time { + t := GetStartDay() + for i := 0; i < days; i++ { + t = t.Add(-24 * time.Hour) + } + return t +} + func GetStartWeek() time.Time { t := GetStartDay() t = t.Add(-6 * 24 * time.Hour) diff --git a/internal/states/clear_bot_state/clear_bot_state.go b/internal/states/clear_bot_state/clear_bot_state.go index ae150c8..c251f77 100644 --- a/internal/states/clear_bot_state/clear_bot_state.go +++ b/internal/states/clear_bot_state/clear_bot_state.go @@ -2,11 +2,12 @@ package clear_bot_state import ( "net/http" - "valera/internal/db" - "valera/internal/states" tgbot "github.com/go-telegram-bot-api/telegram-bot-api" "golang.org/x/exp/slices" + + "valera/internal/db" + "valera/internal/states" ) var names = []string{ diff --git a/internal/states/eat_bot_state/eat_bot_state.go b/internal/states/eat_bot_state/eat_bot_state.go index 519ccc2..eb09862 100644 --- a/internal/states/eat_bot_state/eat_bot_state.go +++ b/internal/states/eat_bot_state/eat_bot_state.go @@ -4,12 +4,13 @@ import ( "fmt" "log" "net/http" - "valera/internal/db" - "valera/internal/modules/times" - "valera/internal/states" tgbot "github.com/go-telegram-bot-api/telegram-bot-api" "golang.org/x/exp/slices" + + "valera/internal/db" + "valera/internal/modules/times" + "valera/internal/states" ) var names = []string{ @@ -39,7 +40,7 @@ func (s *eatBotState) Do(text string, chatInfo *db.ChatInfo) error { log.Println(err) return nil } - stat, err := s.dataBase.GetStatAfter(chatInfo.ChatID, times.GetStartDay()) + stat, err := s.dataBase.GetStatBetween(chatInfo.ChatID, times.GetStartDay(), times.GetNow()) if err != nil { log.Println(err) return nil diff --git a/internal/states/stat_bot_state/stat_bot_state.go b/internal/states/stat_bot_state/stat_bot_state.go index 0160f8a..be948db 100644 --- a/internal/states/stat_bot_state/stat_bot_state.go +++ b/internal/states/stat_bot_state/stat_bot_state.go @@ -4,13 +4,13 @@ import ( "fmt" "net/http" "time" - "valera/internal/db" - "valera/internal/states" - - "valera/internal/modules/times" tgbot "github.com/go-telegram-bot-api/telegram-bot-api" "golang.org/x/exp/slices" + + "valera/internal/db" + "valera/internal/modules/times" + "valera/internal/states" ) var names = []string{ @@ -81,7 +81,7 @@ func (s *statBotState) GetHandler() (string, func(http.ResponseWriter, *http.Req func (s *statBotState) sendStatToChatAfter(chatID int64, prefix string, t time.Time) error { msgText := prefix - stat, err := s.dataBase.GetStatAfter(chatID, t) + stat, err := s.dataBase.GetStatBetween(chatID, t, times.GetNow()) if err != nil { return err } diff --git a/internal/states/weight_bot_state/weight_bot_state.go b/internal/states/weight_bot_state/weight_bot_state.go index bc2c630..0308189 100644 --- a/internal/states/weight_bot_state/weight_bot_state.go +++ b/internal/states/weight_bot_state/weight_bot_state.go @@ -6,11 +6,13 @@ import ( "net/http" "strconv" "strings" - "valera/internal/db" - "valera/internal/states" tgbot "github.com/go-telegram-bot-api/telegram-bot-api" "golang.org/x/exp/slices" + + "valera/internal/db" + "valera/internal/modules/times" + "valera/internal/states" ) var names = []string{ @@ -44,7 +46,16 @@ func (s *weightBotState) Do(text string, chatInfo *db.ChatInfo) error { log.Println(err) return nil } - _, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, fmt.Sprintf("%vкг, записал.", weight))) + stat, err := s.dataBase.GetStatBetween(chatInfo.ChatID, times.GetStartDayMinus(1), times.GetStartDay()) + if err != nil { + log.Println(err) + return nil + } + dWeight := 0.0 + if stat["Вес кг"] != 0 { + dWeight = weight - stat["Вес кг"] + } + _, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, fmt.Sprintf("%vкг, записал.\nИзменение веса: %vкг", weight, dWeight))) return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone) }