public ControllerBase() { if (OrmUtil.CheckCacheFlag <T>()) { DbSession.RegisterCacheTable(TableName, true); } EntityProxy <T> entityProxy = EntityProxyManager.Instance.GetEntityProxy <T>(); foreach (PropertySchema item in entityProxy.GetPropertyList()) { if (item.PropertyType.IsEnum) { Dictionary <string, string> dict = new Dictionary <string, string>(); foreach (object enumItem in Enum.GetValues(item.PropertyType)) { long l = Convert.ToInt64(enumItem); dict.Add(l.ToString(), enumItem.ToString()); } DataMapping.Instance.Add(item.PropertyType.ToString(), dict); } } }
/// <summary> /// 读取类的标示列 /// </summary> /// <typeparam name="T">类型</typeparam> /// <returns>主键列名</returns> public static string GetIdentityColumnName <T>() where T : class { string result = Constants.DefaultIdentityColumnName; EntityProxy <T> entityProxy = EntityProxyManager.Instance.GetEntityProxy <T>(); foreach (PropertySchema schema in entityProxy.GetPropertyList()) { OrmColumnAttribute ormColumnAttribute = schema.GetCustomerAttribute <OrmColumnAttribute>(); if (ormColumnAttribute != null && ormColumnAttribute.PrimaryKeyFlag) { result = schema.Name; } } return(result); }
public static DataTable ForImport <T>(DataTable dataTable) where T : class { DataTable result = new DataTable(); Dictionary <string, string> mapping = new Dictionary <string, string>(); Dictionary <string, string> mappingMapping = new Dictionary <string, string>(); EntityProxy <T> entityProxy = EntityProxyManager.Instance.GetEntityProxy <T>(); foreach (PropertySchema propertySchema in entityProxy.GetPropertyList()) { string columnName = propertySchema.Name; string newColumnName = columnName; ModelPropertyAttribute modelPropertyAttribute = propertySchema.GetCustomerAttribute <ModelPropertyAttribute>(); if (modelPropertyAttribute != null) { if (!modelPropertyAttribute.Visible) { continue; } if (!string.IsNullOrEmpty(modelPropertyAttribute.Description)) { newColumnName = modelPropertyAttribute.Description; } if (!string.IsNullOrEmpty(modelPropertyAttribute.MappingName)) { mappingMapping.Add(newColumnName, modelPropertyAttribute.MappingName); } } mapping.Add(newColumnName, columnName); result.Columns.Add(columnName); } dataTable.Columns.Add("ImportResult"); foreach (DataRow row in dataTable.Rows) { DataRow newRow = result.NewRow(); foreach (string columnName in mapping.Keys) { string itemResult = string.Empty; if (!dataTable.Columns.Contains(columnName)) { continue; } if (mappingMapping.ContainsKey(columnName)) { DataTable data = DataMapping.Instance.GetMapping(mappingMapping[columnName]) as DataTable; DataRow[] rows = data.Select("name='{0}'".FormatWith(row[columnName])); if (rows.Length == 1) { itemResult = rows[0]["id"].ToString(); } else { row["ImportResult"] = "无法匹配{0}:{1}".FormatWith(mappingMapping[columnName], row[columnName]); } } else { itemResult = row[columnName].ToString().Trim(); } newRow[mapping[columnName]] = itemResult; } result.Rows.Add(newRow); } return(result); }
/// <summary> /// Get a new DataTable contains the new column name and the value in the datarow /// according to the OrmColumnAttribute and ModelPropertyAttribute. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dataTable"></param> /// <returns></returns> public static DataTable ForExport <T>(DataTable dataTable) where T : class { DataTable result = new DataTable(); Dictionary <string, string> mapping = new Dictionary <string, string>(); Dictionary <string, string> mappingMapping = new Dictionary <string, string>(); EntityProxy <T> entityProxy = EntityProxyManager.Instance.GetEntityProxy <T>(); foreach (PropertySchema propertySchema in entityProxy.GetPropertyList()) { string columnName = propertySchema.Name; OrmColumnAttribute ormColumnAttribute = propertySchema.GetCustomerAttribute <OrmColumnAttribute>(); if (ormColumnAttribute != null && !string.IsNullOrEmpty(ormColumnAttribute.DbColumnName)) { columnName = ormColumnAttribute.DbColumnName; } string newColumnName = columnName; ModelPropertyAttribute modelPropertyAttribute = propertySchema.GetCustomerAttribute <ModelPropertyAttribute>(); if (modelPropertyAttribute != null) { if (!modelPropertyAttribute.Visible) { continue; } if (!string.IsNullOrEmpty(modelPropertyAttribute.Description)) { newColumnName = modelPropertyAttribute.Description; } if (!string.IsNullOrEmpty(modelPropertyAttribute.MappingName)) { mappingMapping.Add(columnName, modelPropertyAttribute.MappingName); } } mapping.Add(columnName, newColumnName); result.Columns.Add(newColumnName); } foreach (DataRow row in dataTable.Rows) { DataRow newRow = result.NewRow(); foreach (string columnName in mapping.Keys) { string itemResult = string.Empty; if (mappingMapping.ContainsKey(columnName)) { itemResult = DataMapping.Instance.Mapping(mappingMapping[columnName], row[columnName].ToString()); } else { itemResult = row[columnName].ToString(); } newRow[mapping[columnName]] = itemResult; } result.Rows.Add(newRow); } return(result); }
/* * * private List<BeeDataAdapter> GetSearchInfo(BeeDataAdapter dataAdapter) * { * List<BeeDataAdapter> result = new List<BeeDataAdapter>(); * * EntityProxy<T> entityProxy = EntityProxyManager.Instance.GetEntityProxy<T>(); * foreach (PropertySchema propertySchema in entityProxy.GetPropertyList()) * { * ModelPropertyAttribute modelPropertyAttribute * = propertySchema.GetCustomerAttribute<ModelPropertyAttribute>(); * if (modelPropertyAttribute != null) * { * if (!modelPropertyAttribute.Visible) * { * continue; * } * * if (!modelPropertyAttribute.Queryable) * { * continue; * } * * BeeDataAdapter dataItem = new BeeDataAdapter(); * * string descriptionInfo = modelPropertyAttribute.Description; * if (string.IsNullOrEmpty(descriptionInfo)) * { * descriptionInfo = propertySchema.Name; * } * * dataItem.Add("name", propertySchema.Name); * dataItem.Add("Type", propertySchema.PropertyType); * dataItem.Add("QueryType", modelPropertyAttribute.QueryType); * dataItem.Add("Description", descriptionInfo); * dataItem.Add("MappingName", modelPropertyAttribute.MappingName); * * result.Add(dataItem); * } * } * * return result; * } * * private List<BeeDataAdapter> GetHeaderInfo() * { * List<BeeDataAdapter> result = new List<BeeDataAdapter>(); * EntityProxy<T> entityProxy = EntityProxyManager.Instance.GetEntityProxy<T>(); * * ModelAttribute modelAttribute = entityProxy.GetCustomerAttribute<ModelAttribute>(); * * foreach (PropertySchema propertySchema in entityProxy.GetPropertyList()) * { * ModelPropertyAttribute modelPropertyAttribute * = propertySchema.GetCustomerAttribute<ModelPropertyAttribute>(); * * BeeDataAdapter dataAdapter = new BeeDataAdapter(); * string descriptionInfo; * if (modelPropertyAttribute != null) * { * if (!modelPropertyAttribute.Visible) * { * continue; * } * * descriptionInfo = modelPropertyAttribute.Description; * if (string.IsNullOrEmpty(descriptionInfo)) * { * descriptionInfo = propertySchema.Name; * } * * dataAdapter.Add("description", descriptionInfo); * dataAdapter.Add("name", propertySchema.Name); * * if (modelPropertyAttribute.ColumnWidth != 0) * { * dataAdapter.Add("width", modelPropertyAttribute.ColumnWidth.ToString()); * } * * if (!string.IsNullOrEmpty(modelPropertyAttribute.Align)) * { * dataAdapter.Add("align", modelPropertyAttribute.Align); * } * * if (modelPropertyAttribute.OrderableFlag) * { * dataAdapter.Add("orderField", propertySchema.Name); * } * * } * else * { * dataAdapter.Add("description", propertySchema.Name); * dataAdapter.Add("Name", propertySchema.Name); * } * * result.Add(dataAdapter); * } * * * return result; * } * * private List<BeeDataAdapter> GetDetailInfo() * { * List<BeeDataAdapter> result = new List<BeeDataAdapter>(); * * EntityProxy<T> entityProxy = EntityProxyManager.Instance.GetEntityProxy<T>(); * * ModelAttribute modelAttribute = entityProxy.GetCustomerAttribute<ModelAttribute>(); * string identityColumn = OrmUtil.GetIdentityColumnName<T>(); * * foreach (PropertySchema propertySchema in entityProxy.GetPropertyList()) * { * ModelPropertyAttribute modelPropertyAttribute * = propertySchema.GetCustomerAttribute<ModelPropertyAttribute>(); * BeeDataAdapter dataAdapter = new BeeDataAdapter(); * string descriptionInfo; * if (modelPropertyAttribute != null) * { * descriptionInfo = modelPropertyAttribute.Description; * if (string.IsNullOrEmpty(descriptionInfo)) * { * descriptionInfo = propertySchema.Name; * } * } * else * { * descriptionInfo = propertySchema.Name; * } * * dataAdapter.Add("description", descriptionInfo); * dataAdapter.Add("name", propertySchema.Name); * bool readOnly = false; * * if (string.Compare(identityColumn, propertySchema.Name, true) == 0) * { * readOnly = true; * } * * dataAdapter.Add("readonly", readOnly); * dataAdapter.Add("mappingname", modelPropertyAttribute.MappingName); * * result.Add(dataAdapter); * } * * return result; * } * */ private BeeAutoModelInfo InitBeeAutoModelInfo() { BeeAutoModelInfo result = new BeeAutoModelInfo(); List <BeeDataAdapter> headerInfo = new List <BeeDataAdapter>(); List <BeeDataAdapter> searchInfo = new List <BeeDataAdapter>(); List <BeeDataAdapter> detailInfo = new List <BeeDataAdapter>(); Dictionary <string, string> dataMappingInfo = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase); EntityProxy <T> entityProxy = EntityProxyManager.Instance.GetEntityProxy <T>(); ModelAttribute modelAttribute = entityProxy.GetCustomerAttribute <ModelAttribute>(); string identityColumn = OrmUtil.GetIdentityColumnName <T>(); TableSchema tableSchema = null; using (DbSession dbSession = GetDbSession()) { tableSchema = dbSession.GetTableSchema(OrmUtil.GetTableName <T>()); } foreach (PropertySchema propertySchema in entityProxy.GetPropertyList()) { string columnName = propertySchema.Name; OrmColumnAttribute ormColumnAttribute = propertySchema.GetCustomerAttribute <OrmColumnAttribute>(); if (ormColumnAttribute != null && !string.IsNullOrEmpty(ormColumnAttribute.DbColumnName)) { columnName = ormColumnAttribute.DbColumnName; } if (tableSchema != null && !tableSchema.ContainsColumn(columnName)) { continue; } BeeDataAdapter headerItem = GetHeaderItem(propertySchema); if (headerItem != null) { headerInfo.Add(headerItem); } BeeDataAdapter searchItem = GetSearchItem(propertySchema); if (searchItem != null) { searchInfo.Add(searchItem); } BeeDataAdapter detailItem = GetDetailItem(propertySchema, identityColumn); if (detailItem != null) { detailInfo.Add(detailItem); } ModelPropertyAttribute modelPropertyAttribute = propertySchema.GetCustomerAttribute <ModelPropertyAttribute>(); if (modelPropertyAttribute != null && !string.IsNullOrEmpty(modelPropertyAttribute.MappingName)) { dataMappingInfo.Add(propertySchema.Name, modelPropertyAttribute.MappingName); } if (propertySchema.PropertyType.IsEnum && !dataMappingInfo.ContainsKey("mappingname")) { dataMappingInfo.Add(propertySchema.Name, propertySchema.PropertyType.ToString()); } } result.DetailInfo = detailInfo; result.HeaderInfo = headerInfo; result.SearchInfo = searchInfo; result.DataMappingInfo = dataMappingInfo; return(result); }