public virtual async Task Parse(ModuleDataContext context) { try { //内置关联数据 await ParseInnerData(context); //解析列自定义SQL数据 await ParseSqlData(context); //解析模块关联数据 await ParseModuleRelativeData(context); //遍历所有对应的关联数据列对实体进行数据赋值 foreach (var column in context.ModuleInfo.ColumnInfos.Where(o => o.IsRelativeColumn && !string.IsNullOrEmpty(o.ValuePath) && o.ValuePath.StartsWith($"@"))) { var propertyName = column.ValuePath.Replace($"@", ""); context.Entity.TryGetValue(propertyName, out var val); context.Entity.SetValue(column.ColumnKey, val); } } catch (Exception ex) { Logger.Error(ex.Message, ex); } }
/// <summary> /// 解析模块关联数据 /// </summary> /// <param name="context"></param> /// <returns></returns> private async Task ParseModuleRelativeData(ModuleDataContext context) { var moduleInfoManager = context.ModuleInfoManager; foreach (var relativeColumn in context.ModuleInfo.ColumnInfos.Where(o => o.RelativeDataType == RelativeDataType.Module)) { var moduleKey = relativeColumn.RelativeDataString; var moduleInfo = await moduleInfoManager.GetModuleInfo(moduleKey); if (moduleInfo != null) { var ids = context.Entity[relativeColumn.ColumnKey]?.ToString();//关联的模块ids if (!string.IsNullOrEmpty(ids)) { //Id=1 or Id=2 or Id=3 var whereCondition = string.Join(" or ", ids.Split(',').Select(o => $"Id={o}")); var relativeDatas = await moduleInfoManager.GetModuleDataListAsync(moduleInfo, whereCondition); context.Entity.SetValue($"{relativeColumn.ColumnKey}_data", relativeDatas); } else { //没有关联数据则设置默认空数组 context.Entity.SetValue($"{relativeColumn.ColumnKey}_data", new List <object>()); } } } }
/// <summary> /// 解析SQL关联数据 /// </summary> /// <param name="context"></param> /// <returns></returns> private async Task ParseSqlData(ModuleDataContext context) { await Task.Run(() => { foreach (var sqlColumn in context.ModuleInfo.ColumnInfos.Where(o => o.RelativeDataType == RelativeDataType.CustomSql)) { var dataResult = _dynamicQuery.Select(ParseDataString(sqlColumn.RelativeDataString, context.Entity)); //entity[relativeData.DataKey] = dataResult; if (dataResult.Count() > 0) { var firstDataRow = dataResult.First() as IDictionary <string, object>; foreach (var key in firstDataRow.Keys) { context.Entity.SetValue(key, firstDataRow[key]); } } } }); }
/// <summary> /// 解析内置关联数据 /// </summary> /// <param name="context"></param> /// <returns></returns> private async Task ParseInnerData(ModuleDataContext context) { var entityType = TypeFinder.Find(o => o.FullName == context.ModuleInfo.EntityFullName)[0]; //var entityType = Type.GetType(context.ModuleInfo.EntityFullName); if (_masterConfiguration.RelativeDataProviders.ContainsKey(entityType)) { foreach (var providerType in _masterConfiguration.RelativeDataProviders[entityType]) { using (var wrapper = IocManager.Instance.ResolveAsDisposable(providerType)) { var provider = wrapper.Object as IRelativeDataProvider; await provider.FillRelativeData(context); } } } //await Task.Run(() => { //}); }
/// <summary> /// 解析Url关联数据 /// </summary> /// <param name="context"></param> /// <returns></returns> private async Task ParseUrlRelativeData(ModuleDataContext context) { foreach (var relativeColumn in context.ModuleInfo.ColumnInfos.Where(o => o.RelativeDataType == RelativeDataType.Url)) { var ids = context.Entity[relativeColumn.ColumnKey]?.ToString();//关联的数据ids if (!string.IsNullOrEmpty(ids)) { //Id=1 or Id=2 or Id=3 var whereCondition = string.Join(" or ", ids.Split(',').Select(o => $"Id={o}")); var relativeDatas = await Senparc.CO2NET.HttpUtility.Get.GetJsonAsync <JObject>($"{relativeColumn.RelativeDataString}?where={whereCondition}"); //var relativeDatas = await moduleInfoManager.GetModuleDataListAsync(moduleInfo, whereCondition); context.Entity.SetValue($"{relativeColumn.ColumnKey}_data", relativeDatas); } else { //没有关联数据则设置默认空数组 context.Entity.SetValue($"{relativeColumn.ColumnKey}_data", new List <object>()); } } }
public virtual async Task <IEnumerable <IDictionary <string, object> > > FillModuleDataListAsync <TEntity>(IEnumerable <TEntity> entities, ModuleInfo moduleInfo, Expression <Func <ColumnInfo, bool> > columnFilterExpression = null) where TEntity : class, new() { var resourceManager = Resolve <ResourceManager>(); var manager = GetManager(typeof(TEntity)); //模块所有列 var columnInfos = columnFilterExpression != null?moduleInfo.ColumnInfos.Where(columnFilterExpression.Compile()) : moduleInfo.ColumnInfos; //var navigations = GetNavigationStrings(columnInfos); //await entities.EnsurePropertyLoadedAsync(navigations); var result = new List <IDictionary <string, object> >(); //加载原生数据及原生关联数据、自定义属性 var dynamicSelectString = BuildDynamicSelectString <TEntity>(columnInfos); var dynamicList = entities.AsQueryable().Select(dynamicSelectString).AsEnumerable(); //for(var i=0;i<entities.Count();i++) var index = 0; var _relativeDataParser = Resolve <IRelativeDataParser>(); var _columnReader = Resolve <IColumnReader>(); foreach (DynamicClass obj in dynamicList) { var oriEntity = entities.ElementAt(index); index++; var entity = obj.ToDictionary(); //数据前处理 await manager.FillEntityDataBefore(entity, moduleInfo, oriEntity); //读取实体标记 if (typeof(IHaveStatus).IsAssignableFrom(oriEntity.GetType())) { var flags = await resourceManager.GetCheckedFlagInfos(moduleInfo.EntityFullName, oriEntity as IHaveStatus); entity["Flags"] = flags; } //读取属性列 var propertyColumns = columnInfos.Where(o => o.IsPropertyColumn); if (propertyColumns.Count() > 0) { var properties = (obj.GetDynamicPropertyValue("Property") as JsonObject <IDictionary <string, object> >); if (properties?.Object != null) { foreach (var column in propertyColumns) { //entity[column.ColumnKey] = properties.FirstOrDefault(o => o.PropertyName == column.ColumnKey)?.PropertyValue; if (properties.Object.ContainsKey(column.ColumnKey)) { entity[column.ColumnKey] = properties.Object[column.ColumnKey]; } } } //移除属性信息 entity.Remove("Property"); } //加载自定义引用数据 var relativeDataContext = new ModuleDataContext() { Entity = entity, ModuleInfo = moduleInfo, ModuleInfoManager = this }; await _relativeDataParser.Parse(relativeDataContext); //列解析 foreach (var columnInfo in columnInfos) { columnInfo.Normalize(); var context = new ColumnReadContext() { Entity = entity, ColumnInfo = columnInfo }; if (columnInfo.ColumnType == ColumnTypes.Customize) { //自定义控件的列信息读取 await columnInfo.GetCustomizeControl().Read(context); } else { await _columnReader.Read(context); } } //数据后处理,交给具体的实体管理类 await manager.FillEntityDataAfter(entity, moduleInfo, oriEntity); result.Add(entity); } //} //catch(Exception ex) //{ // Logger.Error(ex.Message, ex); // throw new UserFriendlyException(L("数据解析错误:") + dynamicSelectString); //} return(result.AsEnumerable()); }