From 1b9fab466005893d03a8d3bb809c466e3fdae0f3 Mon Sep 17 00:00:00 2001 From: Sercan Yesildal <sercan.yesildal@gmail.com> Date: Mon, 10 Jul 2023 18:30:14 +0200 Subject: [PATCH] shopping balancer finished --- service/planner/service/shopping.go | 75 ++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/service/planner/service/shopping.go b/service/planner/service/shopping.go index 8763772..bdfd7e3 100644 --- a/service/planner/service/shopping.go +++ b/service/planner/service/shopping.go @@ -70,15 +70,78 @@ func GetShoppingList(id int) ([]*model.ShoppingLists, error) { } } } - meal, err := GetMeal(v.MealID) - if err != nil { - return nil, err + if lowestDuration != 255*time.Hour*24*365 { + meal, err := GetMeal(v.MealID) + if err != nil { + return nil, err + } + + key := dates[currentIdx].Format(time.DateOnly) + userList.Lists[key] = append(userList.Lists[key], meal.Ingredients...) } - key := dates[currentIdx].Format(time.DateOnly) - userList.Lists[key] = append(userList.Lists[key], meal.Ingredients...) } } - result = append(result, &userList) + for _, groupId := range user.GroupIds { + group, err := GetGroup(groupId) + if err != nil { + continue + } + var groupList model.ShoppingLists + groupList.Name = group.Name + groupList.Lists = make(map[string][]*model.Ingredient) + groupDates := make(map[int][]time.Time) + for _, userId := range group.UserIDs { + groupUser, err := GetUser(userId) + if err != nil { + continue + } + for _, v := range groupUser.ShoppingDates { + date, err := time.Parse(time.RFC3339, v.Date) + if err != nil { + return nil, err + } + timeNow := time.Now().Truncate(24 * time.Hour) + log.Infof("Compare time (%s, %s)", timeNow.Format(time.DateOnly), date.Format(time.DateOnly)) + if timeNow.Equal(date) || timeNow.Before(date) { + groupDates[userId] = append(groupDates[userId], date) + } + } + } + + if len(group.Meals) != 0 { + for _, v := range group.Meals { + date, err := time.Parse(time.RFC3339, v.Date) + if err != nil { + return nil, err + } + currentUserId := 0 + currentIdx := 0 + lowestDuration := 255 * time.Hour * 24 * 365 + for id, dates := range groupDates { + for idx, d := range dates { + if date.Equal(d) || date.After(d) { + if date.Sub(d) < lowestDuration { + currentUserId = id + currentIdx = idx + lowestDuration = date.Sub(d) + } + } + } + } + if lowestDuration != 255*time.Hour*24*365 { + if currentUserId == id { + meal, err := GetMeal(v.MealID) + if err != nil { + return nil, err + } + key := groupDates[currentUserId][currentIdx].Format(time.DateOnly) + groupList.Lists[key] = append(groupList.Lists[key], meal.Ingredients...) + } + } + } + } + result = append(result, &groupList) + } return result, nil } -- GitLab