boolean json对象存储_将json对象数据保存到数据库对应的表中

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingNewtonsoft.Json;usingStandard.Frame.BEntity;usingStandard.Frame.Utility;usingStandard.Frame.Config;usingStandard.Frame.DBAccess;usingSystem.Reflection;usingStandard.Frame.Utility;namespaceStandard.Frame.BLRuler

{public classJsonToTable

{public static void Save(objectinputJsonObj)

{try{

SaveJsonObj(inputJsonObj,"PlanProject", null);

}catch(Exception e)

{

LogUtility.Write(DateTime.Now.ToString()+ ":" +e.Message);

}

}///

///保存输入的json对象///

///

///

private static void SaveJsonObj(object inputJsonObj, string entityName, TupletupleFk)

{

Dictionary dicReq =CommonFacade.ParseJsonObjToDic(inputJsonObj);

Type type=CommonFacade.GetEntityType(entityName);string tableName =CommonFacade.GetTableNameByType(type);

System.Reflection.PropertyInfo[] properties=type.GetProperties();//先保存主表数据(string类型的节点)

object recordID =SaveStringNode(dicReq, tableName, properties, tupleFk);//保存从表数据(array类型的节点)

SaveArrayNode(dicReq, properties, entityName, recordID);

}///

///保存json对象中的字符串节点///

///

///

///

///

///

private static object SaveStringNode(Dictionary dicReq, string tableName, System.Reflection.PropertyInfo[] properties,TupletupleFk)

{

PreSqlAccess sqlAccess= newPreSqlAccess();

Dictionary dicTable = new Dictionary();

Tuple tuplePk =ParseStringNode(dicReq, properties, dicTable);if (tupleFk != null)

{

dicTable.Remove(tupleFk.Item1);

dicTable.Add(tupleFk.Item1, tupleFk.Item2);

}object recordID =sqlAccess.InsertOrUpdateDataSync(tableName, dicTable, tuplePk);//保存到数据库returnrecordID;

}///

///保存从表数据(数据类型为array的节点)///

///

///

///

private static void SaveArrayNode(Dictionary dicReq, System.Reflection.PropertyInfo[] properties, string mainEntityName, objectrecordID)

{

System.Attribute[] attrs= null;foreach (System.Reflection.PropertyInfo p inproperties)

{

attrs=System.Attribute.GetCustomAttributes(p);foreach (System.Attribute attr inattrs)

{if (attr isPropertyAttribute)

{

PropertyAttribute propertyAttr=(PropertyAttribute)attr;if (dicReq.ContainsKey(propertyAttr.FieldName) && propertyAttr.NodeType ==NodeType.Array)

{

Tuple tupleFk =GetTupleFKID(propertyAttr.MainEntity, mainEntityName, recordID);

List listObj =CommonFacade.ParseJsonObjToList(dicReq[propertyAttr.FieldName]);

listObj.ForEach((inputJsonObj)=>{

SaveJsonObj(inputJsonObj, propertyAttr.MainEntity, tupleFk);

});

}

}

}

}

}/// ///解析输入json对象中的字符串节点///

///

///

///

private static Tuple ParseStringNode(Dictionary dicReq, System.Reflection.PropertyInfo[] properties, DictionarydicTable)

{

Tuple tuplePk = null;

System.Attribute[] attrs= null;foreach (System.Reflection.PropertyInfo p inproperties)

{

attrs=System.Attribute.GetCustomAttributes(p);foreach (System.Attribute attr inattrs)

{if (attr isPropertyAttribute)

{

PropertyAttribute propertyAttr=(PropertyAttribute)attr;if(dicReq.ContainsKey(propertyAttr.FieldName))

{if (propertyAttr.IsPK ==BooleanType.True)

{//记录主键,备用

tuplePk = new Tuple(propertyAttr.ColumnName, dicReq[propertyAttr.FieldName]);break;

}if (propertyAttr.IsDeletedFlag ==BooleanType.True)

{//"是否删除" 这个标示列需要特殊处理

if (dicReq.ContainsKey(NodeOperation.Operation) && Convert.ToString(dicReq[propertyAttr.FieldName]) ==NodeOperationType.Deleted)

dicTable.Add(propertyAttr.ColumnName,1);break;

}if (propertyAttr.NodeType ==NodeType.String)

dicTable.Add(propertyAttr.ColumnName, dicReq[propertyAttr.FieldName]);if (propertyAttr.NodeType ==NodeType.Json)

dicTable.Add(propertyAttr.ColumnName,CommonFacade.GetPKID(dicReq[propertyAttr.FieldName], propertyAttr.MainEntity));

}

}

}

}returntuplePk;

}/// ///将主表ID匹配到从表对应的外键///

///

///

///

///

///

private static Tuple GetTupleFKID(string entityName, string mainEntityName, objectrecordID)

{

Tuple tupleFK = null;

Type type=CommonFacade.GetEntityType(entityName);

System.Reflection.PropertyInfo[] properties=type.GetProperties();

System.Attribute[] attrs= null;foreach (System.Reflection.PropertyInfo p inproperties)

{

attrs=System.Attribute.GetCustomAttributes(p);foreach (System.Attribute attr inattrs)

{if (attr isPropertyAttribute)

{

PropertyAttribute propertyAttr=(PropertyAttribute)attr;if (propertyAttr.NodeType == NodeType.Json && propertyAttr.MainEntity ==mainEntityName)

{

tupleFK= new Tuple(propertyAttr.ColumnName,recordID);gotoouter;

}

}

}

}

outer:returntupleFK;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值