You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

131 lines
3.9 KiB

using Newtonsoft.Json;
using OT.COM.ArsenalDB;
using System;
using System.Collections.Generic;
using System.IO;
namespace CounsellorBL
{
public class ExportAndImportUtility : DBService
{
string ExportPath = "";
string ImportPath = "";
public ExportAndImportUtility(string i_sExportPath, string i_sImportPath)
{
ExportPath = i_sExportPath;
ImportPath = i_sImportPath;
}
public override string MainTable => throw new NotImplementedException();
public List<T> GetTableEntity<T>(WhereNode i_wnWhereCondition_ = null) where T : EntityBase, new()
{
QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
QueryJsonElement queryJsonElement = lBlocks.GetInst();
var item = new T();
// QueryJsonElement required data settings
var attributeTableName = item.GetType().GetField("TABLENAME");
queryJsonElement.table = attributeTableName.GetValue(item) as string;
var FileFullName = Path.Combine(ExportPath, queryJsonElement.table) + ".json";
if (i_wnWhereCondition_ != null)
queryJsonElement.wherecols = i_wnWhereCondition_;
queryJsonElement.displaycols = new List<string>();
foreach (var param in item.GetType().GetProperties())
{
switch (param.Name)
{
case "PKNames":
case "Dirty":
case "SetNull":
break;
default:
queryJsonElement.displaycols.Add(param.Name);
break;
}
}
lBlocks.Add(queryJsonElement);
var sMsg = MakeSelectJoinByBlocks(lBlocks, out Command command);
if (sMsg != null)
throw new Exception(sMsg);
ArsenalInterface ai = ArsenalDBMgr.GetInst(command);
List<T> lRes = ai.RunQueryList<T>(command, null);
if (lRes.Count > 0)
{
if(File.Exists(FileFullName))
{
WriteExistFile(FileFullName, ReadFIle(FileFullName) + "," + lRes.SerializeObject<T>());
}
else
WriteFile(FileFullName, lRes.SerializeObject<T>());
}
return lRes;
}
public void SetTableEntity<T>(string source_) where T : EntityBase
{
T item = JsonConvert.DeserializeObject<T>(source_);
//foreach(var )
}
private string ReadFIle(string i_sPath)
{
string result = "";
using (var sr = new StreamReader(i_sPath, true))
{
result = sr.ReadToEnd();
sr.Close();
}
return result;
}
private void WriteFile(string i_sPath, string i_sSource)
{
using (var sw = new StreamWriter(i_sPath, true))
{
sw.WriteLine(i_sSource);
sw.Close();
}
}
private void WriteExistFile(string i_sExistContentPath, string i_sSource)
{
using (var sw = new StreamWriter(File.OpenWrite(i_sExistContentPath)))
{
sw.WriteLine(i_sSource);
sw.Close();
}
}
}
public static class WinstonExtension
{
public static bool IsNullEnpty(this string i_sSource)
{
return string.IsNullOrWhiteSpace(i_sSource);
}
public static string SerializeObject<T>(this List<T> i_lsSource)
{
return JsonConvert.SerializeObject(i_lsSource);
}
public static void PrintLine(this string i_sSource)
{
Console.WriteLine(i_sSource);
}
}
}