예제 #1
0
    public string ExportToCsvFile(string userId, string menuTitle, Foods.Totals totals, Foods.Recommendations recommendations, string separator, string lang)
    {
        try {
            if (string.IsNullOrWhiteSpace(userId))
            {
                return(null);
            }
            string fileName       = Guid.NewGuid().ToString();
            string tempFolderPath = string.Format("~/upload/users/{0}/temp/", userId);
            if (Directory.Exists(Server.MapPath(tempFolderPath)))
            {
                Directory.Delete(Server.MapPath(tempFolderPath), true);
            }
            CreateFolder(tempFolderPath);
            string       path = Server.MapPath(string.Format("{0}{1}.csv", tempFolderPath, fileName));
            StreamWriter sw   = new StreamWriter(path, false);

            if (!string.IsNullOrWhiteSpace(separator))
            {
                delimiter = separator;
            }

            sw.Write(string.Format("{0}:", SpecTran("menu", lang).ToUpper()));
            sw.Write(delimiter);
            sw.Write(!string.IsNullOrWhiteSpace(menuTitle) ? G.ReplaceSpecChr(menuTitle) : "");
            sw.Write(sw.NewLine);
            sw.Write(sw.NewLine);

            sw.Write(SpecTran("energy value", lang).ToUpper());
            sw.Write(sw.NewLine);

            sw.Write(SpecTran("meals", lang));
            sw.Write(delimiter);
            sw.Write(SpecTran("choosen", lang));
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write(SpecTran("recommended", lang));
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write(SpecTran("carbohydrates", lang));
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write(SpecTran("proteins", lang));
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write(SpecTran("fats", lang));
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(sw.NewLine);

            sw.Write("");
            sw.Write(delimiter);
            sw.Write(SpecTran("kcal", lang));
            sw.Write(delimiter);
            sw.Write("%");
            sw.Write(delimiter);
            sw.Write(SpecTran("kcal", lang));
            sw.Write(delimiter);
            sw.Write("%");
            sw.Write(delimiter);
            sw.Write(SpecTran("g", lang));
            sw.Write(delimiter);
            sw.Write("%");
            sw.Write(delimiter);
            sw.Write(SpecTran("g", lang));
            sw.Write(delimiter);
            sw.Write("%");
            sw.Write(delimiter);
            sw.Write(SpecTran("g", lang));
            sw.Write(delimiter);
            sw.Write("%");
            sw.Write(sw.NewLine);

            foreach (var meal in totals.mealsTotal)
            {
                CsvWriteMealLine(sw, meal, recommendations.mealsRecommendationEnergy.Find(a => a.meal.code == meal.code).meal, lang);
            }
            sw.Write(SpecTran("total", lang));
            sw.Write(delimiter);
            sw.Write(totals.energy);
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write(recommendations.energy);
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write(totals.carbohydrates);
            sw.Write(delimiter);
            sw.Write(totals.carbohydratesPercentage);
            sw.Write(delimiter);
            sw.Write(totals.proteins);
            sw.Write(delimiter);
            sw.Write(totals.proteinsPercentage);
            sw.Write(delimiter);
            sw.Write(totals.fats);
            sw.Write(delimiter);
            sw.Write(totals.fatsPercentage);

            sw.Write(sw.NewLine);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write(string.Format("{0}:", SpecTran("recommended", lang)));
            sw.Write(delimiter);
            sw.Write(string.Format("({0}-{1})", recommendations.carbohydratesPercentageMin, recommendations.carbohydratesPercentageMax));
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write(string.Format("({0}-{1})", recommendations.proteinsPercentageMin, recommendations.proteinsPercentageMax));
            sw.Write(delimiter);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write(string.Format("({0}-{1})", recommendations.fatsPercentageMin, recommendations.fatsPercentageMax));
            sw.Write(sw.NewLine);

            sw.Write(SpecTran("unit servings", lang).ToUpper());
            sw.Write(sw.NewLine);
            sw.Write("");
            sw.Write(delimiter);
            sw.Write(SpecTran("choosen", lang));
            sw.Write(delimiter);
            sw.Write(SpecTran("recommended", lang));
            sw.Write(sw.NewLine);

            CsvWriteServingLine(sw, "cereals and cereal products", totals.servings.cerealsServ, recommendations.servings.cerealsServ, lang);
            CsvWriteServingLine(sw, "vegetables", totals.servings.vegetablesServ, recommendations.servings.vegetablesServ, lang);
            CsvWriteServingLine(sw, "fruitServ", totals.servings.fruitServ, recommendations.servings.fruitServ, lang);
            CsvWriteServingLine(sw, "meat and substitutes", totals.servings.meatServ, recommendations.servings.meatServ, lang);
            CsvWriteServingLine(sw, "milk and dairy products", totals.servings.milkServ, recommendations.servings.milkServ, lang);

            sw.Write("");
            sw.Write(delimiter);
            sw.Write(SpecTran("choosen", lang));
            sw.Write(delimiter);
            sw.Write(SpecTran("acceptable", lang));
            sw.Write(sw.NewLine);
            CsvWriteServingLine(sw, "other foods", totals.servings.otherFoodsEnergy, recommendations.servings.otherFoodsEnergy, lang);



            sw.Write(sw.NewLine);
            sw.Write(sw.NewLine);

            sw.Write(T.Tran("parameters", lang).ToUpper());
            sw.Write(sw.NewLine);

            sw.Write("", lang);
            sw.Write(delimiter);
            sw.Write(T.Tran("choosen", lang));
            sw.Write(delimiter);
            sw.Write(T.Tran("mda", lang));
            sw.Write(delimiter);
            sw.Write(T.Tran("ul", lang));
            sw.Write(delimiter);
            sw.Write(T.Tran("rda", lang));
            sw.Write(sw.NewLine);
            CsvWriteParameterLine(sw, "carbohydrates", "g", totals.starch, recommendations.starch.mda, recommendations.starch.ui, recommendations.starch.rda, lang);
            CsvWriteParameterLine(sw, "total sugar", "g", totals.totalSugar, recommendations.totalSugar.mda, recommendations.totalSugar.ui, recommendations.totalSugar.rda, lang);
            CsvWriteParameterLine(sw, "glucose", "g", totals.glucose, recommendations.glucose.mda, recommendations.glucose.ui, recommendations.glucose.rda, lang);
            CsvWriteParameterLine(sw, "fructose", "g", totals.fructose, recommendations.fructose.mda, recommendations.fructose.ui, recommendations.fructose.rda, lang);
            CsvWriteParameterLine(sw, "saccharose", "g", totals.saccharose, recommendations.saccharose.mda, recommendations.saccharose.ui, recommendations.saccharose.rda, lang);
            CsvWriteParameterLine(sw, "maltose", "g", totals.maltose, recommendations.maltose.mda, recommendations.maltose.ui, recommendations.maltose.rda, lang);
            CsvWriteParameterLine(sw, "lactose", "g", totals.lactose, recommendations.lactose.mda, recommendations.lactose.ui, recommendations.lactose.rda, lang);
            CsvWriteParameterLine(sw, "fibers", "g", totals.fibers, recommendations.fibers.mda, recommendations.fibers.ui, recommendations.fibers.rda, lang);
            CsvWriteParameterLine(sw, "saturated fats", "g", totals.saturatedFats, recommendations.saturatedFats.mda, recommendations.saturatedFats.ui, recommendations.saturatedFats.rda, lang);
            CsvWriteParameterLine(sw, "monounsaturated fats", "g", totals.monounsaturatedFats, recommendations.monounsaturatedFats.mda, recommendations.monounsaturatedFats.ui, recommendations.monounsaturatedFats.rda, lang);
            CsvWriteParameterLine(sw, "polyunsaturated fats", "g", totals.polyunsaturatedFats, recommendations.polyunsaturatedFats.mda, recommendations.polyunsaturatedFats.ui, recommendations.polyunsaturatedFats.rda, lang);
            CsvWriteParameterLine(sw, "trifluoroacetic acid", "g", totals.trifluoroaceticAcid, recommendations.trifluoroaceticAcid.mda, recommendations.trifluoroaceticAcid.ui, recommendations.trifluoroaceticAcid.rda, lang);
            CsvWriteParameterLine(sw, "cholesterol", "mg", totals.cholesterol, recommendations.cholesterol.mda, recommendations.cholesterol.ui, recommendations.cholesterol.rda, lang);
            CsvWriteParameterLine(sw, "sodium", "mg", totals.sodium, recommendations.sodium.mda, recommendations.sodium.ui, recommendations.sodium.rda, lang);
            CsvWriteParameterLine(sw, "potassium", "mg", totals.potassium, recommendations.potassium.mda, recommendations.potassium.ui, recommendations.potassium.rda, lang);
            CsvWriteParameterLine(sw, "calcium", "mg", totals.calcium, recommendations.calcium.mda, recommendations.calcium.ui, recommendations.calcium.rda, lang);
            CsvWriteParameterLine(sw, "magnesium", "mg", totals.magnesium, recommendations.magnesium.mda, recommendations.magnesium.ui, recommendations.magnesium.rda, lang);
            CsvWriteParameterLine(sw, "phosphorus", "mg", totals.phosphorus, recommendations.phosphorus.mda, recommendations.phosphorus.ui, recommendations.phosphorus.rda, lang);
            CsvWriteParameterLine(sw, "iron", "mg", totals.iron, recommendations.iron.mda, recommendations.iron.ui, recommendations.iron.rda, lang);
            CsvWriteParameterLine(sw, "copper", "mg", totals.copper, recommendations.copper.mda, recommendations.copper.ui, recommendations.copper.rda, lang);
            CsvWriteParameterLine(sw, "zinc", "mg", totals.zinc, recommendations.zinc.mda, recommendations.zinc.ui, recommendations.zinc.rda, lang);
            CsvWriteParameterLine(sw, "chlorine", "mg", totals.chlorine, recommendations.chlorine.mda, recommendations.chlorine.ui, recommendations.chlorine.rda, lang);
            CsvWriteParameterLine(sw, "manganese", "mg", totals.manganese, recommendations.manganese.mda, recommendations.manganese.ui, recommendations.manganese.rda, lang);
            CsvWriteParameterLine(sw, "selenium", "g", totals.selenium, recommendations.selenium.mda, recommendations.selenium.ui, recommendations.selenium.rda, lang);
            CsvWriteParameterLine(sw, "iodine", "g", totals.iodine, recommendations.iodine.mda, recommendations.iodine.ui, recommendations.iodine.rda, lang);
            CsvWriteParameterLine(sw, "retinol", "g", totals.retinol, recommendations.retinol.mda, recommendations.retinol.ui, recommendations.retinol.rda, lang);
            CsvWriteParameterLine(sw, "carotene", "μg", totals.carotene, recommendations.carotene.mda, recommendations.carotene.ui, recommendations.carotene.rda, lang);
            CsvWriteParameterLine(sw, "vitamin D", "μg", totals.vitaminD, recommendations.vitaminD.mda, recommendations.vitaminD.ui, recommendations.vitaminD.rda, lang);
            CsvWriteParameterLine(sw, "vitamin E", "g", totals.vitaminE, recommendations.vitaminE.mda, recommendations.vitaminE.ui, recommendations.vitaminE.rda, lang);
            CsvWriteParameterLine(sw, "vitamin E", "mg", totals.vitaminE, recommendations.vitaminE.mda, recommendations.vitaminE.ui, recommendations.vitaminE.rda, lang);
            CsvWriteParameterLine(sw, "vitamin B1", "mg", totals.vitaminB1, recommendations.vitaminB1.mda, recommendations.vitaminB1.ui, recommendations.vitaminB1.rda, lang);
            CsvWriteParameterLine(sw, "vitamin B2", "mg", totals.vitaminB2, recommendations.vitaminB2.mda, recommendations.vitaminB2.ui, recommendations.vitaminB2.rda, lang);
            CsvWriteParameterLine(sw, "vitamin B3", "mg", totals.vitaminB3, recommendations.vitaminB3.mda, recommendations.vitaminB3.ui, recommendations.vitaminB3.rda, lang);
            CsvWriteParameterLine(sw, "vitamin B6", "g", totals.vitaminB6, recommendations.vitaminB6.mda, recommendations.vitaminB6.ui, recommendations.vitaminB6.rda, lang);
            CsvWriteParameterLine(sw, "vitamin B12", "μg", totals.vitaminB12, recommendations.vitaminB12.mda, recommendations.vitaminB12.ui, recommendations.vitaminB12.rda, lang);
            CsvWriteParameterLine(sw, "folate", "μg", totals.folate, recommendations.folate.mda, recommendations.folate.ui, recommendations.folate.rda, lang);
            CsvWriteParameterLine(sw, "pantothenic acid", "mg", totals.pantothenicAcid, recommendations.pantothenicAcid.mda, recommendations.pantothenicAcid.ui, recommendations.pantothenicAcid.rda, lang);
            CsvWriteParameterLine(sw, "biotin", "μg", totals.biotin, recommendations.biotin.mda, recommendations.biotin.ui, recommendations.biotin.rda, lang);
            CsvWriteParameterLine(sw, "vitamin C", "mg", totals.vitaminC, recommendations.vitaminC.mda, recommendations.vitaminC.ui, recommendations.vitaminC.rda, lang);
            CsvWriteParameterLine(sw, "vitamin K", "μg", totals.vitaminK, recommendations.vitaminK.mda, recommendations.vitaminK.ui, recommendations.vitaminK.rda, lang);

            sw.Close();
            return(JsonConvert.SerializeObject(fileName, Formatting.None));
        } catch (Exception e) {
            return(JsonConvert.SerializeObject(e.Message, Formatting.None));
        }
    }
예제 #2
0
    public string GetWeeklyMenusTotals(string userId, List <string> menuList)
    {
        try {
            Foods f = new Foods();
            List <Foods.Totals>             xx  = new List <Foods.Totals>();
            List <List <Foods.MealsTotal> > lmt = new List <List <Foods.MealsTotal> >();
            List <Foods.MealsTotal>         mt_ = new List <Foods.MealsTotal>();
            foreach (string menu in menuList)
            {
                if (!string.IsNullOrEmpty(menu))
                {
                    Menues         m  = new Menues();
                    Menues.NewMenu nm = new Menues.NewMenu();
                    nm = m.WeeklyMenu(userId, menu);
                    Foods.Totals t = new Foods.Totals();
                    t = f.GetTotals_(nm.data.selectedFoods, nm.data.meals);
                    xx.Add(t);
                    List <Foods.MealsTotal> mt = new List <Foods.MealsTotal>();
                    mt = f.GetMealsTotal(nm.data.selectedFoods, nm.data.meals);
                    lmt.Add(mt);
                }
            }

            foreach (List <Foods.MealsTotal> l in lmt)
            {
                foreach (Foods.MealsTotal o in l)
                {
                    mt_.Add(o);
                }
            }

            List <Foods.MealsTotal> distMeal = mt_.GroupBy(a => a.code).Select(b => b.First()).ToList();
            List <Foods.MealsTotal> zz       = new List <Foods.MealsTotal>();
            foreach (var ii in distMeal)
            {
                Foods.MealsTotal z = new Foods.MealsTotal();
                z.code  = ii.code;
                z.title = ii.title;

                List <Foods.MealsTotal> fmt_energy = mt_.Where(a => a.code == ii.code).ToList();
                z.energy.val = fmt_energy.Count() > 0 ? Math.Round(fmt_energy.Average(a => a.energy.val), 1) : 0;

                List <Foods.MealsTotal> fmt_energyPerc = mt_.Where(a => a.code == ii.code).ToList();
                z.energy.perc = fmt_energyPerc.Count() > 0 ? Math.Round(fmt_energyPerc.Average(a => a.energy.perc), 1) : 0;

                List <Foods.MealsTotal> fmt_carbohydrates = mt_.Where(a => a.code == ii.code).ToList();
                z.carbohydrates.val = fmt_carbohydrates.Count() > 0 ? Math.Round(fmt_carbohydrates.Average(a => a.carbohydrates.val), 1) : 0;

                List <Foods.MealsTotal> fmt_carbohydratesPerc = mt_.Where(a => a.code == ii.code).ToList();
                z.carbohydrates.perc = fmt_carbohydratesPerc.Count() > 0 ? Math.Round(fmt_carbohydratesPerc.Average(a => a.carbohydrates.perc), 1) : 0;

                List <Foods.MealsTotal> fmt_proteins = mt_.Where(a => a.code == ii.code).ToList();
                z.proteins.val = fmt_proteins.Count() > 0 ? Math.Round(fmt_proteins.Average(a => a.proteins.val), 1) : 0;

                List <Foods.MealsTotal> fmt_proteinsPerc = mt_.Where(a => a.code == ii.code).ToList();
                z.proteins.perc = fmt_proteinsPerc.Count() > 0 ? Math.Round(fmt_proteinsPerc.Average(a => a.proteins.perc), 1) : 0;

                List <Foods.MealsTotal> fmt_fats = mt_.Where(a => a.code == ii.code).ToList();
                z.fats.val = fmt_fats.Count() > 0 ? Math.Round(fmt_fats.Average(a => a.fats.val), 1) : 0;

                List <Foods.MealsTotal> fmt_fatsPerc = mt_.Where(a => a.code == ii.code).ToList();
                z.fats.perc = fmt_fatsPerc.Count() > 0 ? Math.Round(fmt_fatsPerc.Average(a => a.fats.perc), 1) : 0;

                zz.Add(z);
            }

            Foods.Totals x = new Foods.Totals();
            x.mass                      = f.SmartRound(xx.Average(a => a.mass));
            x.energy                    = f.SmartRound(xx.Average(a => a.energy));
            x.carbohydrates             = f.SmartRound(xx.Average(a => a.carbohydrates));
            x.carbohydratesPercentage   = f.SmartRound(xx.Average(a => a.carbohydratesPercentage));
            x.proteins                  = f.SmartRound(xx.Average(a => a.proteins));
            x.proteinsPercentage        = f.SmartRound(xx.Average(a => a.proteinsPercentage));
            x.fats                      = f.SmartRound(xx.Average(a => a.fats));
            x.fatsPercentage            = f.SmartRound(xx.Average(a => a.fatsPercentage));
            x.servings.cerealsServ      = f.SmartRound(xx.Average(a => a.servings.cerealsServ));
            x.servings.vegetablesServ   = f.SmartRound(xx.Average(a => a.servings.vegetablesServ));
            x.servings.fruitServ        = f.SmartRound(xx.Average(a => a.servings.fruitServ));
            x.servings.meatServ         = f.SmartRound(xx.Average(a => a.servings.meatServ));
            x.servings.milkServ         = f.SmartRound(xx.Average(a => a.servings.milkServ));
            x.servings.fatsServ         = f.SmartRound(xx.Average(a => a.servings.fatsServ));
            x.servings.otherFoodsServ   = f.SmartRound(xx.Average(a => a.servings.otherFoodsServ));
            x.servings.otherFoodsEnergy = f.SmartRound(xx.Average(a => a.servings.otherFoodsEnergy));
            x.mealsTotal                = zz;
            x.starch                    = f.SmartRound(xx.Average(a => a.starch));
            x.totalSugar                = f.SmartRound(xx.Average(a => a.totalSugar));
            x.glucose                   = f.SmartRound(xx.Average(a => a.glucose));
            x.fructose                  = f.SmartRound(xx.Average(a => a.fructose));
            x.saccharose                = f.SmartRound(xx.Average(a => a.saccharose));
            x.maltose                   = f.SmartRound(xx.Average(a => a.maltose));
            x.lactose                   = f.SmartRound(xx.Average(a => a.lactose));
            x.fibers                    = f.SmartRound(xx.Average(a => a.fibers));
            x.saturatedFats             = f.SmartRound(xx.Average(a => a.saturatedFats));
            x.monounsaturatedFats       = f.SmartRound(xx.Average(a => a.monounsaturatedFats));
            x.polyunsaturatedFats       = f.SmartRound(xx.Average(a => a.polyunsaturatedFats));
            x.trifluoroaceticAcid       = f.SmartRound(xx.Average(a => a.trifluoroaceticAcid));
            x.cholesterol               = f.SmartRound(xx.Average(a => a.cholesterol));
            x.sodium                    = f.SmartRound(xx.Average(a => a.sodium));
            x.potassium                 = f.SmartRound(xx.Average(a => a.potassium));
            x.calcium                   = f.SmartRound(xx.Average(a => a.calcium));
            x.magnesium                 = f.SmartRound(xx.Average(a => a.magnesium));
            x.phosphorus                = f.SmartRound(xx.Average(a => a.phosphorus));
            x.iron                      = f.SmartRound(xx.Average(a => a.iron));
            x.copper                    = f.SmartRound(xx.Average(a => a.copper));
            x.zinc                      = f.SmartRound(xx.Average(a => a.zinc));
            x.chlorine                  = f.SmartRound(xx.Average(a => a.chlorine));
            x.manganese                 = f.SmartRound(xx.Average(a => a.manganese));
            x.selenium                  = f.SmartRound(xx.Average(a => a.selenium));
            x.iodine                    = f.SmartRound(xx.Average(a => a.iodine));
            x.retinol                   = f.SmartRound(xx.Average(a => a.retinol));
            x.carotene                  = f.SmartRound(xx.Average(a => a.carotene));
            x.vitaminD                  = f.SmartRound(xx.Average(a => a.vitaminD));
            x.vitaminE                  = f.SmartRound(xx.Average(a => a.vitaminE));
            x.vitaminB1                 = f.SmartRound(xx.Average(a => a.vitaminB1));
            x.vitaminB2                 = f.SmartRound(xx.Average(a => a.vitaminB2));
            x.vitaminB3                 = f.SmartRound(xx.Average(a => a.vitaminB3));
            x.vitaminB6                 = f.SmartRound(xx.Average(a => a.vitaminB6));
            x.vitaminB12                = f.SmartRound(xx.Average(a => a.vitaminB12));
            x.folate                    = f.SmartRound(xx.Average(a => a.folate));
            x.pantothenicAcid           = f.SmartRound(xx.Average(a => a.pantothenicAcid));
            x.biotin                    = f.SmartRound(xx.Average(a => a.biotin));
            x.vitaminC                  = f.SmartRound(xx.Average(a => a.vitaminC));
            x.vitaminK                  = f.SmartRound(xx.Average(a => a.vitaminK));
            x.price.value               = Math.Round(xx.Average(a => a.price.value), 2);
            return(JsonConvert.SerializeObject(x, Formatting.None));
        } catch (Exception e) { return(JsonConvert.SerializeObject(e.Message, Formatting.None)); }
    }