private void LoadData() { string category = string.Empty; if (this.type == 2) { category = this.UserDep; } DynamicJson courseList = RestRC.BACARepository.Request(ApiList.courseList, string.Empty).AsDynamicJson(); dynamic categoryList = RestRC.BACARepository.Request(ApiList.categoryList, string.Empty).ToDynamicJson().list; if (categoryList != null) { foreach (DynamicJson item in categoryList) { IndexInfo catelogElement = new IndexInfo() { model = item }; catelogElement.Title.Content = item.GetStr("title"); foreach (var course in courseList.Get("list") as IEnumerable <object> ) { catelogElement._wpCourseItems.Children.Add(new CourseItem()); } _sp.Children.Add(catelogElement); } } }
/// <summary> /// 获取postData中操作的目标模型定义 /// </summary> /// <param name="dj"></param> /// <returns></returns> public JMI GetJMI(DynamicJson dj) { JMI jmi = new JMI(); if (dj.IsList && dj.GetStr(JModelNo.AccessTableKey).IsNullOrWhiteSpace()) { dj = (dj as dynamic)[0]; } if (dj.GetStr(JModelNo.AccessTableKey) != null && dj.GetStr(JModelNo.AccessTableKey).Length > 0) { jmi = JModelList.Models[dj.GetStr(JModelNo.AccessTableKey).ToUpper()]; } if (!dj.GetStr(JModelNo.AccessDbsKey).IsNullOrEmpty()) { jmi.DbsName = dj.GetStr(JModelNo.AccessDbsKey); } if (!dj.GetStr(JModelNo.AccessProcKey).IsNullOrEmpty()) { jmi.ProcName = dj.GetStr(JModelNo.AccessProcKey); } if (!dj.GetStr(JModelNo.AccessContextKey).IsNullOrEmpty()) { jmi.Context = dj.GetStr(JModelNo.AccessContextKey); } if (!dj.GetStr(JModelNo.AccessParaKey).IsNullOrEmpty()) { jmi.Paras = dj.Get(JModelNo.AccessParaKey).ToJson(); } if (!dj.GetStr(JModelNo.AccessBlockQueryKey).IsNullOrEmpty()) { jmi.BlockQueryCode = dj.GetStr(JModelNo.AccessBlockQueryKey); } return(jmi); }
public virtual string Save() { try { DynamicJson dj = GetParam(); JMI jmi = GetJMI(dj); if (!dj.IsList && !dj.GetStr(JModelNo.AccessSaveListKey).IsNullOrEmpty()) { dj = dj.Get(JModelNo.AccessSaveListKey).ToJson().ToDynamicJson(); if (!jmi.TBCode.IsNullOrWhiteSpace()) { foreach (DynamicJson djItem in dj) { djItem.Set(JModelNo.AccessTableKey, jmi.TBCode); } } } using (DBAccessor dba = DBAccessor.Instance(Dbs.Get(jmi.DbsName))) { return(OnSave(dj, dba)); } } catch (Exception ex) { return(Loger.Log(Assembly.GetExecutingAssembly().FullName, MethodBase.GetCurrentMethod().FullName(), ex)); } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dba"></param> /// <param name="dj"></param> /// <param name="primaryKey"></param> /// <param name="tableName"></param> /// <param name="seqName"></param> /// <returns></returns> protected string SaveOne(Type t, DBAccessor dba, DynamicJson dj, string primaryKey = "Id") { int rows = 0; string sql = string.Empty; string json = string.Empty; string tableName = TableName(t); IList <string> cols = new List <string>(); try { if (GetPrimaryKey(t).Count() > 0) { primaryKey = GetPrimaryKey(t).FirstOrDefault().Name; } if (dj.Get <int>(primaryKey) >= 0) { json = this.Update(t, dba, dj); return(json); } string seqName = SeqName(t); int id = dj.Get <int>(primaryKey); if (!seqName.IsNullOrWhiteSpace()) { id = dba.ExecuteScalar(string.Format(" select {0}.NEXTVAL from dual", seqName), false); dj.Set(primaryKey, id); } dj.Set("RowVersion", DateTime.Now); DbParameter[] paras = BuildParameter(t, dba, dj, ref cols); sql = string.Format(@" insert into {0} ( {1}) values ( {2} )", tableName, cols.ToString(" ,"), cols.Select(a => ":" + a).ToString(",")); rows = dba.ExecuteCommand(sql, paras, false); json = dba.GetReader(string.Format("select * from {0} where {1}=:{1}", tableName, primaryKey), dba.CreateDbParameter(primaryKey, dj.Get(primaryKey)), false) .ToList(t).FirstOrDefault().ToJson(); } catch (Exception ex) { return(Loger.Log(Assembly.GetExecutingAssembly().FullName, MethodBase.GetCurrentMethod().FullName(), ex)); } return(json); }
public static TEntity ParseTo <TEntity>(this DynamicJson json) where TEntity : class, new() { var entity = new TEntity(); foreach (var item in typeof(TEntity).GetProperties()) { item.FastSet(entity, json.Get(item.Name)); } return(entity); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dba"></param> /// <param name="dynamicJson"></param> /// <param name="columns"></param> /// <returns></returns> public DbParameter[] BuildParameter(Type t, DBAccessor dba, DynamicJson dynamicJson, ref IList <string> columns) { IList <DbParameter> paras = new List <DbParameter>(); PropertyInfo[] propertys = t.GetProperties().Where(a => a.GetAttribute <EdmScalarPropertyAttribute>() != null).ToArray(); if (propertys.Count() == 0) { propertys = t.GetProperties().Where(a => a.GetAttribute <ColumnAttribute>() != null).ToArray(); } foreach (var columnName in dynamicJson.Keys) { try { bool isDate = false; var p = propertys.FirstOrDefault(b => b.Name.ToUpper() == columnName.ToUpper()); if (p != null && p.PropertyType == typeof(DateTime)) { isDate = true; } if (columnName.StartsWith("_") || propertys.Count(b => b.Name.ToUpper() == columnName.ToUpper()) == 0) { continue; } if (dynamicJson.ContainsKey(columnName)) { object v = dynamicJson.Get(columnName); if (isDate) { v = v.Convert <DateTime>(); } else if (v.ToString().ToUpper() == "TRUE" || v.ToString().ToUpper() == "FALSE") { v = v.Convert <bool>() ? 1 : 0; } paras.Add(dba.CreateDbParameter(columnName, v)); } columns.Add(columnName); } catch (Exception ex) { Loger.Log(Assembly.GetExecutingAssembly().FullName, MethodBase.GetCurrentMethod().FullName() + "\r\n列名->\r\n" + columnName, ex); } } return(paras.ToArray()); }
/// <summary> /// 逻辑删除 post 数据格式类似为 { primaryKey: 1,2 } /// </summary> /// <returns></returns> public string Delete() { string json = GetPostData(); DynamicJson dj = GetParam(); JMI jmi = GetJMI(dj); if (!dj.IsList && !dj.GetStr(JModelNo.AccessSaveListKey).IsNullOrEmpty()) { dj = dj.Get(JModelNo.AccessSaveListKey).ToJson().ToDynamicJson(); if (!jmi.TBCode.IsNullOrWhiteSpace()) { foreach (DynamicJson djItem in dj) { djItem.Set(JModelNo.AccessTableKey, jmi.TBCode); } } } using (DBAccessor dba = DBAccessor.Instance(Dbs.Get(jmi.DbsName))) { return(OnSave(dj, dba, true)); } }
/// <summary> /// /// </summary> /// <param name="dba"></param> /// <param name="dynamicJson">以下划线开头的key不会构建参数</param> /// <returns></returns> public DbParameter[] BuildParameter(DBAccessor dba, DynamicJson dynamicJson) { IList <DbParameter> paras = new List <DbParameter>(); foreach (var columnName in dynamicJson.Keys) { try { bool isDate = false; if (dynamicJson.GetStr(columnName) != null && columnName.Contains("Date")) { isDate = true; } if (columnName.StartsWith("_")) { continue; } if (dynamicJson.ContainsKey(columnName)) { var dp = dba.CreateDbParameter(columnName, isDate ? dynamicJson.Get(columnName).Convert <DateTime>() : dynamicJson.Get(columnName)); if (columnName == "rs_1" && dp.GetType().FullName.Contains("Oracle")) { (dp as OracleParameter).OracleDbType = OracleDbType.RefCursor; dp.Direction = ParameterDirection.Output; } paras.Add(dp); } } catch (Exception ex) { Loger.Log(Assembly.GetExecutingAssembly().FullName, MethodBase.GetCurrentMethod().FullName() + "\r\n列名->\r\n" + columnName, ex); } } return(paras.ToArray()); }
public virtual string Remove() { DynamicJson dj = GetParam(); string resJson = string.Empty; string primaryKey = "Id"; try { JMI jmi = GetJMI(dj); using (DBAccessor dba = DBAccessor.Instance(Dbs.Get(jmi.DbsName))) { string condition = string.Empty; string sql = string.Empty; string tableName = TableName(jmi.DtoType); IList <string> cols = new List <string>(); if (GetPrimaryKey(jmi.DtoType).Count() > 0) { primaryKey = GetPrimaryKey(jmi.DtoType).FirstOrDefault().Name; } DbParameter[] paras = BuildParameter(jmi.DtoType, dba, dj, ref cols); int rows = dba.ExecuteCommand(string.Format("delete from {0} where {1}=:{1}", tableName, primaryKey), dba.CreateDbParameter(primaryKey, dj.Get(primaryKey)), false); if (rows > 0) { resJson = new { result = 1 } } .ToJson(); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dba"></param> /// <param name="dj"></param> /// <param name="primaryKey"></param> /// <param name="tableName"></param> /// <returns></returns> protected string Update(Type t, DBAccessor dba, DynamicJson dj, string primaryKey = "Id") { int rows = 0; string sql = string.Empty; string json = string.Empty; if (GetPrimaryKey(t).Count() > 0) { primaryKey = GetPrimaryKey(t).FirstOrDefault().Name; } string tableName = TableName(t); IList <string> cols = new List <string>(); dj.Set("RowVersion", DateTime.Now); DbParameter[] paras = BuildParameter(t, dba, dj, ref cols); sql = string.Format(" update {0} set {1} where {2}=:{2}", tableName, cols.Select(a => a + "= :" + a).ToString(","), primaryKey); rows = dba.ExecuteCommand(sql, paras, false); json = dba.GetReader(string.Format("select * from {0} where {1}=:{1}", tableName, primaryKey), dba.CreateDbParameter(primaryKey, dj.Get(primaryKey)), false) .ToList(t).FirstOrDefault().ToJson(); return(json); }