Esempio n. 1
0
 public void TestMethod1()
 {
     using (var dao = new TrendDbContext())
     {
         dao.Set <MarkSixRecord>().Add(new MarkSixRecord {
             Times = "2017004", FirstNum = 12, AwardingDate = DateTime.Now
         });
         dao.SaveChanges();
     }
 }
 public void RemoveSpecifiedLocation(params long[] ids)
 {
     using (var dao = new TrendDbContext())
     {
         foreach (var id in ids)
         {
             var record = dao.Set <MarkSixSpecifiedLocationPurchase>().FirstOrDefault(m => m.Id == id);
             if (record == null)
             {
                 throw new Exception(string.Format("错误,购买记录不存在!(Id:{0})", id));
             }
             dao.Entry(record).State = EntityState.Deleted;
         }
         dao.SaveChanges();
     }
 }
 public void SaveSpecifiedLocation(MarkSixSpecifiedLocationPurchase dto)
 {
     if (string.IsNullOrWhiteSpace(dto.Times))
     {
         throw new Exception("错误,期次不能为空");
     }
     if (string.IsNullOrWhiteSpace(dto.PurchaseList))
     {
         throw new Exception("错误,购买清单不能为空");
     }
     if (dto.Location > 7 || dto.Location < 1)
     {
         throw new Exception("错误,购买的指定位置必须为1-7!");
     }
     if (dto.Odds <= 0)
     {
         throw new Exception("错误,赔率不能小于等于0!");
     }
     using (var dao = new TrendDbContext())
     {
         if (dto.Id > 0)
         {
             var purchase = dao.Set <MarkSixSpecifiedLocationPurchase>().FirstOrDefault(m => m.Id == dto.Id);
             if (purchase == null)
             {
                 throw new Exception(string.Format("错误,购买记录不存在!(Id:{0})", dto.Id));
             }
             purchase.Times          = dto.Times;
             purchase.PurchaseList   = dto.PurchaseList;
             purchase.Odds           = dto.Odds;
             purchase.Location       = dto.Location;
             purchase.PurchaseAmount = dto.PurchaseAmount;
             purchase.OnModified     = DateTime.Now;
         }
         else
         {
             dto.OnCreated = dto.OnModified = DateTime.Now;
             dao.Set <MarkSixSpecifiedLocationPurchase>().Add(dto);
         }
         dao.SaveChanges();
     }
 }
        /// <summary>
        ///     导入
        /// </summary>
        public int Import(string fileName)
        {
            //数组列表
            var records = new List <MarkSixRecord>();

            using (var stream = new FileStream(fileName, FileMode.Open))
                using (var package = new ExcelPackage(stream))
                {
                    var workbook = package.Workbook;
                    for (int i = 1, len = workbook.Worksheets.Count; i <= len; i++)
                    {
                        //当前序号工作表
                        var sht         = workbook.Worksheets[i];
                        var startRow    = sht.Dimension.Start.Row;
                        var endRow      = sht.Dimension.End.Row;
                        var startColumn = sht.Dimension.Start.Column;
                        var endColumn   = sht.Dimension.End.Column;
                        var cell        = sht.Cells[startRow, startColumn, endRow, endColumn].FirstOrDefault(c => c.Text == "序号");

                        //查找最后一个"序号"单元格
                        if (cell == null)
                        {
                            throw new Exception("工作表" + sht.Name + ",没有第一个单元格为‘序号’的结束行!");
                        }

                        //记录日期
                        DateTime recordDate;
                        if (sht.Cells[2, 2].Value == null)
                        {
                            throw new Exception("工作表" + sht.Name + ",第二行记录日期为空!");
                        }
                        if (!DateTime.TryParse(sht.Cells[2, 2].Value.ToString(), out recordDate))
                        {
                            throw new Exception("工作表" + sht.Name + ",第二行记录日期格式错误!");
                        }

                        //遍历所有行
                        for (var r = 2; r < endRow; r++)
                        {
                            try
                            {
                                //声明记录对象
                                var record = new MarkSixRecord();

                                //开奖日期
                                record.AwardingDate = DateTime.Parse(sht.Cells[r, 2].Value.ToString());

                                //记录期次
                                var strTimes = sht.Cells[r, 3].Value.ToString();

                                //期次必须为4位年3位期次
                                if (strTimes != recordDate.Year + (r - 1).ToString("000"))
                                {
                                    throw new Exception("工作表" + sht.Name + ",第" + r + "行期次错误!" + "必须为4位年3位连续的期次。");
                                }

                                //写入数据到记录对象
                                record.Times      = strTimes;
                                record.TimesValue = int.Parse(strTimes);
                                record.FirstNum   = byte.Parse(sht.Cells[r, 4].Value.ToString());
                                record.SecondNum  = byte.Parse(sht.Cells[r, 5].Value.ToString());
                                record.ThirdNum   = byte.Parse(sht.Cells[r, 6].Value.ToString());
                                record.FourthNum  = byte.Parse(sht.Cells[r, 7].Value.ToString());
                                record.FifthNum   = byte.Parse(sht.Cells[r, 8].Value.ToString());
                                record.SixthNum   = byte.Parse(sht.Cells[r, 9].Value.ToString());
                                record.SeventhNum = byte.Parse(sht.Cells[r, 10].Value.ToString());

                                //添加到列表
                                records.Add(record);
                            }
                            catch (Exception ex)
                            {
                                throw new Exception("工作表" + sht.Name + ",第" + r + "行格式错误!" + ex.Message);
                            }
                        }
                    }
                }
            if (records.Count > 0)
            {
                RecordCount = records.Count;
                OnBeforeImportEvent(null);
                using (var dao = new TrendDbContext())
                {
                    //var timeses = records.Select(r => r.StartTimes).ToList();
                    //var originalRecords = dao.Set<MarkSixRecord>().Where(m => timeses.Any(times => times == m.StartTimes));
                    //dao.Set<MarkSixRecord>().RemoveRange(originalRecords);
                    //dao.Set<MarkSixRecord>().AddRange(records);
                    foreach (var record in records)
                    {
                        //记录已导入数量
                        ImportedCount++;

                        //处理正在导入事件
                        OnImportingEvent(null);
                        if (IsStopImporting)
                        {
                            return(0);
                        }
                        var originalRecord = dao.Set <MarkSixRecord>().FirstOrDefault(m => m.Times == record.Times);
                        if (originalRecord != null)
                        {
                            originalRecord.AwardingDate     = record.AwardingDate;
                            originalRecord.FirstNum         = record.FirstNum;
                            originalRecord.SecondNum        = record.SecondNum;
                            originalRecord.ThirdNum         = record.ThirdNum;
                            originalRecord.FourthNum        = record.FourthNum;
                            originalRecord.FifthNum         = record.FifthNum;
                            originalRecord.SixthNum         = record.SixthNum;
                            originalRecord.SeventhNum       = record.SeventhNum;
                            dao.Entry(originalRecord).State = EntityState.Modified;
                        }
                        else
                        {
                            dao.Set <MarkSixRecord>().Add(record);
                        }
                    }


                    dao.SaveChanges();
                }
            }
            return(records.Count());
        }