private void Exporter_OnExported <T>(object sender, ExportEntityEventArgs <T> e) where T : EntityBase, new() { Console.WriteLine("------------------------------------------------------------"); if (e.Succeed) { //处理上次没有导入的剩余数据============================ //尝试加载本地数据,与导出的数据合并,以本次导出的数据优先 List <T> lastData = this.MemDB.LoadEntity <T>(); if (lastData != null && lastData.Count > 0) { string pkName = lastData[0].PrimaryKeys[0]; if (e.ExportedDataList.Count > 0) { object[] ids = e.ExportedDataList.Select(p => p[pkName]).ToArray(); var except = lastData.Where(p => !ids.Contains(p[pkName])).ToList(); e.ExportedDataList.AddRange(except); } else { e.ExportedDataList.AddRange(lastData); } } if (e.ExportedDataList.Count > 0) { if (e.EntityType == typeof(DeletedPKIDEntity)) { //ID记录表,导出后删除当前批次数据库记录 DeletedPKIDEntity entity = new DeletedPKIDEntity(); OQL q = OQL.From(entity) .Delete() .Where(cmp => cmp.Comparer(entity.BatchNumber, "=", e.BatchNumber)) .END; int count = EntityQuery <DeletedPKIDEntity> .ExecuteOql(q, CurrDbContext.CurrentDataBase); Console.WriteLine("当前导出批次{0} 已经清除当前的ID删除表信息记录,条数:{1}", e.BatchNumber, count); } else { //已经提前更新了导出批次号,参见 FilterQuery 方法 } this.HaveDataTableCount++; } Console.WriteLine("导出数据成功!\t 导出批次号:{0}\t 导出表名称:{1}\t 导出记录数:{2}", e.BatchNumber, e.ExportTable, e.ExportedDataList.Count); } else { Console.WriteLine("导出数据失败,\r\n 导出批次号:{0}\r\n 导出表名称:{1}\r\n 出错原因:{2}", e.BatchNumber, e.ExportTable, e.OperationExcepiton.Message); this.AllSucceed = false; } }
/// <summary> /// 导出实体数据到内存数据库。如果当前实体操作失败,请检查导出事件的异常参数对象。 /// </summary> /// <param name="funQ">获取导出数据的查询表达式委托方法,委托方法的参数为导出批次号;如果结果为空,导出实体全部数据</param> /// <param name="initBatchNumber">要初始化导出批次号的函数</param> public void Export(Func <int, T, OQL> funQ, Func <T, int> initBatchNumber) { Type entityType = typeof(T); try { //导出批次管理 string exportTableName = EntityFieldsCache.Item(entityType).TableName; List <ExportBatchInfo> batchList = MemDB.Get <ExportBatchInfo>(); ExportBatchInfo currBatch = batchList.FirstOrDefault(p => p.ExportTableName == exportTableName); if (currBatch == null) { currBatch = new ExportBatchInfo(); currBatch.BatchNumber = initBatchNumber == null?1: initBatchNumber(new T()); currBatch.ExportTableName = exportTableName; currBatch.LastExportDate = DateTime.Now; // batchList.Add(currBatch); MemDB.Add(currBatch); } else { currBatch.BatchNumber += 1; currBatch.LastExportDate = DateTime.Now; } MemDB.Save <ExportBatchInfo>(); //导出数据 OQL q = funQ(currBatch.BatchNumber, new T()); List <T> entityList = q != null?CurrDbContext.QueryList <T>(q) : CurrDbContext.QueryAllList <T>(); ExportEntityEventArgs <T> args = new ExportEntityEventArgs <T>(entityList, entityType, exportTableName); args.Succeed = true; args.OperationExcepiton = null; args.BatchNumber = currBatch.BatchNumber; if (OnExported != null) { OnExported(this, args); } if (!args.Cancel) { SaveEntity(entityList.ToArray(), args); } } catch (Exception ex) { ExportEntityEventArgs <T> args = new ExportEntityEventArgs <T>(null, entityType, EntityFieldsCache.Item(entityType).TableName); args.Succeed = false; args.OperationExcepiton = ex; if (OnExported != null) { OnExported(this, args); } } }
private void Exporter_OnSaved <T>(object sender, ExportEntityEventArgs <T> e) where T : EntityBase, new() { if (e.Succeed) { Console.WriteLine("保存数据成功!"); } else { Console.WriteLine("保存数据失败。"); } }
private void SaveEntity(T[] entitys, ExportEntityEventArgs <T> args) { if (entitys.Length > 0) { args.Succeed = MemDB.SaveEntity <T>(entitys); } else { MemDB.DropEntity <T>(); args.Succeed = true; } if (OnSaved != null) { OnSaved(this, args); } }