private EveryDayProfileStatisticsModel createModelEveryday(IEnumerable <MyDBModels.EveryDayProfileStatistics> periodStatistics, string nameDate, int count)
        {
            var statisticsModel = new EveryDayProfileStatisticsModel();

            statisticsModel.CountDistance = (periodStatistics.Count() > 0 ? periodStatistics.Sum(e => e.CountDistance) / count : 0);
            statisticsModel.MiddleSpeed   = (periodStatistics.Count() > 0 ? periodStatistics.Sum(e => e.MiddleSpeed) / count : 0);
            statisticsModel.TimeInTrip    = (int)(periodStatistics.Count() > 0 ? new TimeSpan((long)periodStatistics.Sum(e => e.TimeInTrip.Ticks) / count) : new TimeSpan()).TotalSeconds;
            statisticsModel.Calories      = (periodStatistics.Count() > 0 ? (int)periodStatistics.Sum(e => e.Calories) / count : 0);
            statisticsModel.NameDate      = nameDate;
            return(statisticsModel);
        }
        public int everyDayStatistics(int userId, EveryDayProfileStatisticsModel model)
        {
            int statisticsId = -1;

            if (model.EveryDayProfileStatisticsId > 0)
            {
                statisticsId = everyDayStatisticsUpdate(userId, model);
                updateTotalStatistics(userId, model);
            }
            else
            {
                statisticsId = everyDayStatisticsCreate(userId, model);
            }
            return(statisticsId);
        }
        private int everyDayStatisticsUpdate(int userId, EveryDayProfileStatisticsModel model)
        {
            var db           = new MyDBModels.DB();
            var profileModel = getProfileDbByUserId(db, userId);
            var everyDayProfileStatisticsUpdate = db.everyDayProfileStatistics.Where(ps => ps.EveryDayProfileStatisticsId == model.EveryDayProfileStatisticsId).First();

            everyDayProfileStatisticsUpdate.CountDistance = model.CountDistance;
            everyDayProfileStatisticsUpdate.MiddleSpeed   = model.MiddleSpeed;
            everyDayProfileStatisticsUpdate.TimeInTrip    = new TimeSpan(model.TimeInTrip);
            everyDayProfileStatisticsUpdate.Calories      = model.Calories;

            db.SaveChanges();

            return(everyDayProfileStatisticsUpdate.EveryDayProfileStatisticsId);
        }
        private int everyDayStatisticsCreate(int userId, EveryDayProfileStatisticsModel model)
        {
            var db        = new MyDBModels.DB();
            var dateUtils = new DateUtils();
            var profileStatisticsModel = getStatisticsByUserId(db, userId);

            DateTime inputDate = model.TimeCreate;

            foreach (int everyDayId in profileStatisticsModel.EveryDayProfileStatisticsIdArray)
            {
                var modelEveryDay = db.everyDayProfileStatistics.Where(e => e.EveryDayProfileStatisticsId == everyDayId).First();
                if (modelEveryDay.TimeCreate.Year == inputDate.Year && modelEveryDay.TimeCreate.Month == inputDate.Month && modelEveryDay.TimeCreate.Day == inputDate.Day)
                {
                    return(modelEveryDay.EveryDayProfileStatisticsId);
                }
            }

            MyDBModels.EveryDayProfileStatistics everyDayStatistics = new MyDBModels.EveryDayProfileStatistics();
            everyDayStatistics.CountDistance = 0;
            everyDayStatistics.MiddleSpeed   = 0;
            everyDayStatistics.TimeInTrip    = new TimeSpan(0 * 10000000);
            everyDayStatistics.Calories      = 0;
            everyDayStatistics.TimeCreate    = DateTime.Now;

            db.everyDayProfileStatistics.Add(everyDayStatistics);
            db.SaveChanges();

            var lastStatisticsId = db.everyDayProfileStatistics.OrderByDescending(i => i.EveryDayProfileStatisticsId).FirstOrDefault().EveryDayProfileStatisticsId;

            var everyDayArrayStatisticsIdArray = profileStatisticsModel.EveryDayProfileStatisticsIdArray;
            int count = everyDayArrayStatisticsIdArray.Count();
            var everyDayArrayStatisticsIdArrayUpdate = new int[count + 1];

            Array.Copy(everyDayArrayStatisticsIdArray, everyDayArrayStatisticsIdArrayUpdate, count);
            everyDayArrayStatisticsIdArrayUpdate[count] = lastStatisticsId;

            profileStatisticsModel.EveryDayProfileStatisticsIdArray = everyDayArrayStatisticsIdArrayUpdate;

            /*правильнее
             * var everyDayArrayStatisticsIdArray = profileStatisticsModel.EveryDayProfileStatisticsIdArray.ToList();
             * everyDayArrayStatisticsIdArray.Add(db.everyDayProfileStatistics.OrderByDescending(i => i.EveryDayProfileStatisticsId).FirstOrDefault().EveryDayProfileStatisticsId);
             * profileStatisticsModel.EveryDayProfileStatisticsIdArra = everyDayArrayStatisticsIdArray.ToArray();
             */

            db.SaveChanges();

            return(lastStatisticsId);
        }
        private void updateTotalStatistics(int userId, EveryDayProfileStatisticsModel mode)
        {
            var db = new MyDBModels.DB();
            var profileStatisticsModel           = getStatisticsByUserId(db, userId);
            var everyDayProfileStatisticsIdArray = profileStatisticsModel.EveryDayProfileStatisticsIdArray;
            var everyDayProfileStatisticsMyList  = new List <MyDBModels.EveryDayProfileStatistics>();

            foreach (int itemId in everyDayProfileStatisticsIdArray)
            {
                everyDayProfileStatisticsMyList.Add(db.everyDayProfileStatistics.Where(e => e.EveryDayProfileStatisticsId == itemId).First());
            }

            profileStatisticsModel.CountDistanceTotal = everyDayProfileStatisticsMyList.Average(e => e.CountDistance);
            profileStatisticsModel.MiddleSpeedTotal   = everyDayProfileStatisticsMyList.Average(e => e.MiddleSpeed);;
            profileStatisticsModel.TimeInTripTotal    = new TimeSpan((long)everyDayProfileStatisticsMyList.Average(e => e.TimeInTrip.Ticks));
            profileStatisticsModel.CaloriesTotal      = (int)everyDayProfileStatisticsMyList.Average(e => e.Calories);

            db.SaveChanges();
        }
示例#6
0
        public int everyDayStatistics(EveryDayProfileStatisticsModel model)
        {
            AuthorizationUtils authorization = new AuthorizationUtils(Request);

            return(logic.everyDayStatisticsLogic(authorization.getUserId(), model));
        }
 public int everyDayStatisticsLogic(int userId, EveryDayProfileStatisticsModel model)
 {
     return(dataAccess.everyDayStatistics(userId, model));
 }