Exemple #1
0
 /// <summary>
 /// 查找日期数据,如果不存在则创建
 /// </summary>
 /// <param name="date"></param>
 /// <returns></returns>
 public Models.Statistic.TomatoModel FindCreateData(DateTime date)
 {
     if (date.Date == DateTime.Now.Date &&
         tomatoDataToday != null &&
         tomatoDataToday.Date == date.Date)
     {
         //当日
         return(tomatoDataToday);
     }
     else
     {
         //非当日从数据库中查找
         using (var db = new StatisticContext())
         {
             var res = db.Tomatos.Where(m => m.Date == date.Date);
             if (res.Count() == 0)
             {
                 //数据库中没有时则创建
                 var dateData = new Models.Statistic.TomatoModel()
                 {
                     Date         = date.Date,
                     RestartCount = 0,
                     TomatoCount  = 0
                 };
                 db.Tomatos.Add(dateData);
                 db.SaveChanges();
                 return(dateData);
             }
             else
             {
                 return(res.ToList().FirstOrDefault());
             }
         }
     }
 }
 /// <summary>
 /// 查找日期数据,如果不存在则创建
 /// </summary>
 /// <param name="date"></param>
 /// <returns></returns>
 public StatisticModel FindCreate(DateTime date)
 {
     if (date.Date == DateTime.Now.Date &&
         todayStatistic != null &&
         todayStatistic.Date == date.Date)
     {
         //当日
         return todayStatistic;
     }
     else
     {
         //非当日从数据库中查找
         using (var db = new StatisticContext())
         {
             var res = db.Statistics.Where(m => m.Date == date.Date);
             if (res.Count() == 0)
             {
                 //数据库中没有时则创建
                 var dateData = GetNewdayData(date);
                 db.Statistics.Add(dateData);
                 db.SaveChanges();
                 return dateData;
             }
             else
             {
                 return res.ToList().FirstOrDefault();
             }
         }
     }
 }
Exemple #3
0
        public Task Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Starting statistics persistence in database");
            var calculation = (INewCalculationTask)context.MergedJobDataMap.Get("calculation");

            try
            {
                using (StatisticContext dbContext = new StatisticContext())
                {
                    dbContext.Database.EnsureCreated();

                    foreach (var operation in calculation.GetOperations())
                    {
                        foreach (KeyValuePair <String, Statistic> kvp in operation.GetComputeDictionary())
                        {
                            StatisticType statisticType = dbContext.StatisticTypes.Single(st => st.Name == operation.GetName());
                            kvp.Value.StatisticType = statisticType;
                            dbContext.Statistics.Add(kvp.Value);
                        }
                        operation.GetComputeDictionary().Clear();
                    }

                    dbContext.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.WriteLine("Persistence finished");
            return(Task.FromResult(0));
        }
        /// <summary>
        /// 创建本月的数据
        /// </summary>
        private void CreateMonthlyItems(DateTime dateTime)
        {
            int days  = DateTime.DaysInMonth(dateTime.Year, dateTime.Month);
            int today = dateTime.Day;

            using (var db = new StatisticContext())
            {
                for (int i = 0; i < days; i++)
                {
                    var date = dateTime.AddDays(-today + (i + 1)).Date;
                    if (db.Statistics.Where(m => m.Date == date).Count() == 0)
                    {
                        //补上缺少的日期
                        db.Statistics.Add(new StatisticModel()
                        {
                            Date        = date,
                            ResetTime   = 0,
                            SkipCount   = 0,
                            WorkingTime = 0
                        });
                    }
                }
                db.SaveChanges();
            }
        }
 public void WriteToDB(string path, EntityType type)
 {
     using (var ctx = new StatisticContext())
     {
         var saga = new SagaAction()
         {
             SagaName = path, AddedDate = DateTime.Now, Type = type
         };
         ctx.Sagas.Add(saga);
         ctx.SaveChanges();
     }
 }
 public void WriteToDB(string path, EntityType type)
 {
     using (var ctx = new StatisticContext())
     {
         var selector = new Selector()
         {
             SelectorName = path, AddedDate = DateTime.Now, Type = type
         };
         ctx.Selectors.Add(selector);
         ctx.SaveChanges();
     }
 }
 public void WriteToDB(string path, EntityType type)
 {
     using (var ctx = new StatisticContext())
     {
         var test = new ClientTest()
         {
             Name = path, CreationDate = DateTime.Now, Type = type
         };
         ctx.ClientTests.Add(test);
         ctx.SaveChanges();
     }
 }
Exemple #8
0
 /// <summary>
 /// 数据持久化
 /// </summary>
 public void SaveData()
 {
     backgroundWorker.AddAction(() =>
     {
         if (tomatoDataToday == null)
         {
             tomatoDataToday = FindCreateTodayData();
         }
         using (var db = new StatisticContext())
         {
             var item          = (from c in db.Tomatos where c.Date == tomatoDataToday.Date select c).FirstOrDefault();
             item.TomatoCount  = tomatoDataToday.TomatoCount;
             item.RestartCount = tomatoDataToday.RestartCount;
             db.SaveChanges();
         }
     });
     backgroundWorker.Run();
 }
 /// <summary>
 /// 数据持久化
 /// </summary>
 public void Save()
 {
     backgroundWorker.AddAction(() =>
     {
         if (todayStatistic == null)
         {
             todayStatistic = FindCreate();
         }
         using (var db = new StatisticContext())
         {
             //var item = db.Statistics.Where(m => m.Date == todayStatistic.Date).Single();
             var item = (from c in db.Statistics where c.Date == todayStatistic.Date select c).FirstOrDefault();
             item.ResetTime = todayStatistic.ResetTime;
             item.SkipCount = todayStatistic.SkipCount;
             item.WorkingTime = todayStatistic.WorkingTime;
             db.SaveChanges();
         }
     });
     backgroundWorker.Run();
 }
        //new code
        #region 迁移数据,从xml到sqlite。下一个版本将弃用 :)
        /// <summary>
        /// 迁移数据,从xml到sqlite。下一个版本将弃用 :)
        /// </summary>
        private void MigrateXMLDataToDb()
        {

            if (File.Exists(xmlPath))
            {
                //需要迁移
                xml = new XmlExtensions(xmlPath);

                var data = xml.ToModel(typeof(StatisticListModel));
                if (data != null)
                {
                    statisticList = data as StatisticListModel;

                    if (statisticList != null && statisticList.Data != null && statisticList.Data.Count > 0)
                    {
                        using (var db = new StatisticContext())
                        {
                            foreach (var item in statisticList.Data)
                            {
                                db.Statistics.Add(item);
                            }
                            db.SaveChanges();
                            //备份数据文件
                            File.Copy(xmlPath, xmlPath + ".migrate.backup");
                            //删除原数据文件
                            File.Delete(xmlPath);
                            //迁移标记文件,在用户收到迁移提示后将删除
                            File.WriteAllText(xmlPath + ".migrate.mark", "");
                        }
                    }

                }
            }


            //迁移判断
            IsMigrated = File.Exists(xmlPath + ".migrate.mark");

        }
Exemple #11
0
        /// <summary>
        /// 创建本月的数据
        /// </summary>
        private void CreateMonthlyData(DateTime dateTime)
        {
            int days  = DateTime.DaysInMonth(dateTime.Year, dateTime.Month);
            int today = dateTime.Day;

            using (var db = new StatisticContext())
            {
                for (int i = 0; i < days; i++)
                {
                    var date = dateTime.AddDays(-today + (i + 1)).Date;
                    if (db.Tomatos.Where(m => m.Date == date).Count() == 0)
                    {
                        //补上缺少的日期
                        db.Tomatos.Add(new Models.Statistic.TomatoModel()
                        {
                            Date         = date,
                            RestartCount = 0,
                            TomatoCount  = 0
                        });
                    }
                }
                db.SaveChanges();
            }
        }
Exemple #12
0
        public static void WriteFirstSelector()
        {
            List <string> result = new List <string>();
            StreamReader  reader = File.OpenText(@"D:\selector.txt");
            string        line;

            while ((line = reader.ReadLine()) != null)
            {
                result.Add(line);
            }

            using (var ctx = new StatisticContext())
            {
                foreach (var s in result)
                {
                    var selector = new Selector()
                    {
                        SelectorName = s, AddedDate = DateTime.Now
                    };
                    ctx.Selectors.Add(selector);
                    ctx.SaveChanges();
                }
            }
        }
 public bool SaveAll()
 {
     return(_statisticContext.SaveChanges() > 0);
 }