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;
}
}
}