public async Task <object> Parse(ColumnReadContext context) { var defaultValueString = context.ColumnInfo.DefaultValue; object result = ""; //如果没有默认值设置 if (!string.IsNullOrEmpty(defaultValueString)) { try { result = await ScriptRunner.EvaluateScript(context.ColumnInfo.DefaultValue); } catch (Exception ex) { Logger.Error(ex.Message, ex); //出错后直接显示字符串 result = defaultValueString; } } return(result); }
/// <summary> /// 从实体中读取对应列数据 /// </summary> /// <param name="context"></param> /// <returns></returns> public virtual Task Read(ColumnReadContext context) { return(Task.Run(() => { try { var columnInfo = context.ColumnInfo; var entity = context.Entity; var value = entity[columnInfo.ColumnKey]; //对于文件或图片类型字段,自动将文件信息加入 if (columnInfo.ColumnType == ColumnTypes.Images || columnInfo.ColumnType == ColumnTypes.Files) { var files = new List <File>(); var ids = value.ToString().Split(','); foreach (var id in ids) { files.Add(_fileManager.GetByIdFromCacheAsync(int.Parse(id)).Result); } entity.SetValue(columnInfo.ColumnKey + "_Files", files.Select(o => new { o.FileName, o.FilePath, o.FileSize, o.Id })); } //对于日期类型自动设置格式化字符串 if (columnInfo.ColumnType == ColumnTypes.DateTime && columnInfo.DisplayFormat.IsNullOrWhiteSpace()) { columnInfo.DisplayFormat = "yyyy-MM-dd"; } //select类型的自动把显示值加入 if (columnInfo.ColumnType == ColumnTypes.Select && !string.IsNullOrEmpty(columnInfo.DictionaryName)) { var allDictionary = DictionaryManager.GetAllDictionaries().Result; Dictionary <string, string> dic = new Dictionary <string, string>(); if (allDictionary.ContainsKey(columnInfo.DictionaryName)) { dic = allDictionary[columnInfo.DictionaryName]; } else { try { dic = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(columnInfo.DictionaryName); } catch { } } string displayValue = dic.ContainsKey(value.ToString()) ? dic[value.ToString()] : value.ToString(); entity[columnInfo.ColumnKey + "_display"] = displayValue; } //进行值格式化 if (!columnInfo.DisplayFormat.IsNullOrWhiteSpace()) { if (value != null) { //如果是日期类型 if (columnInfo.ColumnType == ColumnTypes.DateTime) { entity[columnInfo.ColumnKey] = Convert.ToDateTime(value).ToString(columnInfo.DisplayFormat); } //数值类型 if (columnInfo.ColumnType == ColumnTypes.Number) { entity[columnInfo.ColumnKey] = Convert.ToDecimal(value).ToString(columnInfo.DisplayFormat); } } } } catch { } })); }
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()); }