private static FastDev.DevDB.Model.Config.Model GetModelByName(string modelName) { DbContext currentDb = SysContext.GetCurrentDb(); core_model core_model = currentDb.FirstOrDefault <core_model>("where ModelName = @0", new object[1] { modelName }); if (core_model == null) { return(null); } core_module core_module = null; if (!string.IsNullOrEmpty(core_model.ModuleID)) { core_module = currentDb.FirstOrDefault <core_module>("where ID = @0", new object[1] { core_model.ModuleID }); } string textField = "ID"; core_modelField core_modelField = currentDb.FirstOrDefault <core_modelField>("where IsTextField = 1 and FieldType = @0", new object[1] { "string" }); if (core_modelField != null) { textField = core_modelField.FieldName; } else { core_modelField = currentDb.FirstOrDefault <core_modelField>("where FieldType = @0", new object[1] { "string" }); if (core_modelField != null) { textField = core_modelField.FieldName; } } FastDev.DevDB.Model.Config.Model model = new FastDev.DevDB.Model.Config.Model(); model.name = core_model.ModelName; model.title = core_model.ModelTitle; model.textField = textField; if (core_module != null) { model.moduleName = core_module.ModuleName; model.moduleTitle = core_module.ModuleTitle; } return(model); }
private static List <core_modelField> GetModelFieldsByModelId(DbContext dbContext, string modelId) { List <core_modelField> list = dbContext.Fetch <core_modelField>("where modelid = @0", new object[1] { modelId }); List <core_modelField> list2 = new List <core_modelField>(); using (List <core_modelField> .Enumerator enumerator = list.GetEnumerator()) { while (enumerator.MoveNext()) { Func <core_modelField, bool> func = null; core_modelField field = enumerator.Current; List <core_modelField> source = list2; func = ((core_modelField a) => a.FieldName == field.FieldName); if (!source.Any(func)) { list2.Add(field); } } } return(list2); }
public static ServiceConfig GetServiceConfig(string model) { if (SysContext.IsDev) { #region 如果是开发过程 DbContext currentDb = SysContext.GetCurrentDb(); core_model core_model = currentDb.FirstOrDefault <core_model>("where ModelName = @0", new object[1] { model }); if (core_model == null) { return(null); } List <core_modelField> list = GetModelFieldsByModelId(currentDb, core_model.ID); core_module core_module = null; if (!string.IsNullOrEmpty(core_model.ModuleID)) { core_module = currentDb.FirstOrDefault <core_module>("where ID = @0", new object[1] { core_model.ModuleID }); } string textField = "ID"; core_modelField core_modelField = list.Where(f => f.IsTextField == 1).FirstOrDefault(); if (core_modelField != null) { textField = core_modelField.FieldName; } else { core_modelField = list.Where(f => f.FieldType == "string").FirstOrDefault(); if (core_modelField != null) { textField = core_modelField.FieldName; } } ServiceConfig serviceConfig = new ServiceConfig(); List <string> lstMany2One = new List <string>(); List <string> lstMany2Many = new List <string>(); List <string> lstOne2Many = new List <string>(); serviceConfig.model.name = core_model.ModelName; serviceConfig.model.title = core_model.ModelTitle; serviceConfig.model.textField = textField; if (((int?)core_model.NotIncludeSysFields).HasValue && core_model.NotIncludeSysFields.Value == 1) { serviceConfig.model.notIncludeSysFields = "Y"; } try { serviceConfig.model.dbName = currentDb.ExecuteScalar <string>("select DbName from core_model where ModelName = @0", new object[1] { model }); } catch { } if (core_module != null) { serviceConfig.model.moduleName = core_module.ModuleName; serviceConfig.model.moduleTitle = core_module.ModuleTitle; } foreach (core_modelField item in list) { Field field = new Field(); field.name = item.FieldName; field.title = item.FieldTitle; field.type = item.FieldType; field.dbName = item.DbName; try { if (currentDb.ExecuteScalar <string>("select IsPK from core_modelField where id = @0", new object[1] { item.ID }) == "1") { field.isPK = "Y"; } else { field.isPK = "N"; } } catch { } if (item.FieldLength.HasValue) { field.length = item.FieldLength.Value.ToStr(); } field.relationField = item.RelationField; field.relationModel = item.RelationModel; serviceConfig.fields.Add(field); if (item.IsFormField != 1) { if (item.FieldType == "many2one") { lstMany2One.Add(item.FieldName); } if (item.FieldType == "many2many") { lstMany2Many.Add(item.FieldName); } if (item.FieldType == "one2many") { lstOne2Many.Add(item.FieldName); } } if (item.IsSearchField == 1) { field.enabledSearch = "Y"; } } serviceConfig.getlist.many2one = string.Join(",", lstMany2One); serviceConfig.getlist.many2many = string.Join(",", lstMany2Many); serviceConfig.getlist.one2many = string.Join(",", lstOne2Many); serviceConfig.getpageddata.many2one = string.Join(",", lstMany2One); serviceConfig.getpageddata.many2many = string.Join(",", lstMany2Many); serviceConfig.getpageddata.one2many = string.Join(",", lstOne2Many); return(serviceConfig); #endregion } else { #region 如果是运行端 string serviceFileName = new HttpServerUtility(HttpContext.Current).MapPath(string.Format("~/Service/{0}/service.xml", model)); if (EnabledCache) { ServiceConfig serviceConfig = CacheHelper.GetCache("service." + model) as ServiceConfig; if (serviceConfig != null) { return(serviceConfig); } } if (!File.Exists(serviceFileName)) { ModelsConfig modelsConfig = GetModelsConfig(); string text2 = null; if (modelsConfig != null && modelsConfig.models != null) { text2 = (from a in modelsConfig.models where string.Compare(a.name, model, true) == 0 select a).Select(m => m.moduleName).FirstOrDefault(); } if (string.IsNullOrEmpty(text2)) { text2 = "others"; } serviceFileName = new HttpServerUtility(HttpContext.Current).MapPath(string.Format("~/Service/{0}/service.xml", model)); if (!File.Exists(serviceFileName)) { serviceFileName = new HttpServerUtility(HttpContext.Current).MapPath(string.Format("~/Service/{0}/{1}/service.xml", text2, model)); } if (!File.Exists(serviceFileName)) { return(null); } } string text3 = File.ReadAllText(serviceFileName); ServiceConfig rev = XmlHelper.XmlDeserialize <ServiceConfig>(text3, Encoding.UTF8); if (EnabledCache) { CacheHelper.SetCache("service." + model, (object)rev, new CacheDependency(serviceFileName)); } return(rev); #endregion } }