using CounsellorBL; using CounsellorBL.GROUP.ConstDefinition; using Newtonsoft.Json; using OT.COM.ArsenalDB; using OT.COM.ArsenalDB.SQL; using SoldierData.EnterprizeV4; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; namespace TransporterAP { public class ExportImport : DBService { List lsDeleteSeq = new List() { "Gen_tb_ord_purchase_detail", // BranchID "Gen_tb_ord_incoming_return_record", // OrderMstIDs => GROUP_UID "Gen_tb_ord_order_detail", // MemberID "Gen_tb_ord_order_master", // GROUP_UID "Gen_tb_meb_shopping_points_record", // MemberID "Gen_tb_grp_comment", // ArticleID "Gen_tb_prd_article2product", // ArticleID "Gen_tb_ord_purchase", // MemberID "Gen_tb_ord_checkout_list_detail", // MemberID "Gen_tb_prd_product", // GROUP_UID "Gen_tb_grp_article_media", // ArticleID "Gen_tb_ord_message_log_record", // MemberID "Gen_tb_ord_checkout_list_master", // GROUP_UID "Gen_tb_grp_article", // GROUP_UID "Gen_tb_hr_employee2branch", // BranchID "Gen_tb_ord_message_log", // BranchID "Gen_tb_meb_member", // FBID "Gen_tb_sys_uploadlog", // ArticleMediaMediaID => ArticleID "Gen_tb_sys_user2role", // ROLE_UID => GROUP_UID "Gen_tb_grp_group2user", // GROUP_UID "Gen_tb_hr_employee", // Employee2BranchID => BranchID "Gen_tb_grp_branch", // GROUP_UID "Gen_tb_sys_role2org", // RoleIDs => GROUP_UID "Gen_tb_sys_role", // GROUP_UID "Gen_tb_grp_group", // GROUP_NAME "Gen_tb_sys_user2entercode", // EmployeeID => Employee2BranchID => BranchID "Gen_tb_sys_user" // EmployeeIDs => Employee2BranchID => BranchID }; public override string MainTable => throw new NotImplementedException(); private SQLLib _GetSQLLib() => new SQLLib(new MSSQLDirectSQLHelper()); public string ImportFiles(string i_sExportFolderPath) { string sMsg = null; Logger.Debug($"ImportFiles Start {DateTime.Now}"); try { do { List lsInsert = new List(lsDeleteSeq); lsInsert.Reverse(); foreach (string sFunctionName in lsInsert) { string sTableName = sFunctionName.Substring(4); Type tCur = _GetEntityType(sTableName); int nBatchCount = 0; while(true) { string sFilePath = $"{Path.Combine(i_sExportFolderPath, sTableName)}.{nBatchCount}"; if(!File.Exists(sFilePath)) { break; } MethodInfo[] amiMethods = typeof(JsonConvert).GetMethods(); MethodInfo miFid = amiMethods.FirstOrDefault(f => f.Name == "DeserializeObject" && f.IsGenericMethod && f.GetParameters().Length == 1); // MethodInfo miFid = amiMethods.FirstOrDefault(f => f.Name == "DeserializeObject" ); // Type generic = typeof(List<>); Type[] typeArgs = { tCur }; Type constructed = generic.MakeGenericType(typeArgs); string sContent = File.ReadAllText(sFilePath, Encoding.UTF8); object oResult = miFid.MakeGenericMethod(constructed).Invoke(null, new object[] { sContent }); IEnumerable myList = oResult as IEnumerable; List lcInsert = new List(); foreach (object o in myList) { Command cInsert = Command.SetupInsertCmd(o as EntityBase); lcInsert.Add(cInsert); } if (lcInsert.Any()) { ArsenalInterface ai = ArsenalDBMgr.GetInst(lcInsert[0], GetDefaultSystemColumnInfo()); ai.RunEditCmds(lcInsert); } nBatchCount++; } } } while (false); } catch (Exception ex) { sMsg = ex.Message; } Logger.Debug($"ImportFiles End {DateTime.Now}"); return sMsg; } public string ExportFiles(string i_sExportFolderPath, List i_asGroupName) { string sMsg = null; System.Diagnostics.Debug.WriteLine(DateTime.Now); try { do { if(!Directory.Exists(i_sExportFolderPath)) { Directory.CreateDirectory(i_sExportFolderPath); } foreach (string sFunctionName in lsDeleteSeq) { sMsg = _GetSQLLib().GetUIDs(SQLLib.Action.SELECT_ALL, sFunctionName, i_asGroupName, out Command o_cCmd); if (sMsg != null) { break; } ArsenalInterface ai = ArsenalDBMgr.GetInst(o_cCmd, GetDefaultSystemColumnInfo()); string sTableName = sFunctionName.Substring(4); Type tCur = _GetEntityType(sTableName); MethodInfo[] mis = ai.GetType().GetMethods(); MethodInfo miFid = new List(mis).FirstOrDefault(f => f.Name == "RunQueryList" && f.GetParameters().Length == 4); object oResult = miFid.MakeGenericMethod(tCur).Invoke(ai, new object[] { o_cCmd, null, -1, -1 }); string sPath = Path.Combine(i_sExportFolderPath, sTableName); IEnumerable myList = oResult as IEnumerable; IEnumerator it = myList.GetEnumerator(); int nBatchCount = 0; List lTemp = new List(); while(it.MoveNext()) { lTemp.Add(it.Current); if(lTemp.Count == 10000) { makeFile(sPath, nBatchCount, lTemp); nBatchCount++; lTemp.Clear(); } } if(lTemp.Any()) { makeFile(sPath, nBatchCount, lTemp); } } } while (false); } catch (Exception ex) { sMsg = ex.Message; } System.Diagnostics.Debug.WriteLine(DateTime.Now); return sMsg; } protected string makeFile(string i_sFilePath, int i_nBatchID, List i_loData ) { string sMsg = null; string sPath = $"{i_sFilePath}.{i_nBatchID}"; if (File.Exists(sPath)) { File.Delete(sPath); } File.WriteAllText(sPath, JsonConvert.SerializeObject(i_loData), Encoding.UTF8); return sMsg; } public string Delete( List i_asGroupName) { string sMsg = null; Logger.Debug($"Delete start {DateTime.Now}"); try { do { Dictionary dicSelect = new Dictionary(); List lcDelete = new List(); foreach (string sFunctionName in lsDeleteSeq) { sMsg = _GetSQLLib().GetUIDs(SQLLib.Action.SELECT_IDONLY, sFunctionName, i_asGroupName, out Command o_cSelect); if (sMsg != null) { break; } dicSelect.Add(sFunctionName, o_cSelect); sMsg = _GetSQLLib().GetUIDs(SQLLib.Action.DELETE, sFunctionName, i_asGroupName, out Command o_cCmd); if (sMsg != null) { break; } lcDelete.Add(o_cCmd); } if(!lcDelete.Any()) { break; } // Store User _GetSQLLib().GetUIDs(SQLLib.Action.SELECT_ALL, "Gen_tb_sys_user", i_asGroupName, out Command o_cSelectUser); ArsenalInterface ai = ArsenalDBMgr.GetInst(o_cSelectUser, GetDefaultSystemColumnInfo()); List luUsers = ai.RunQueryList(o_cSelectUser); foreach (tb_sys_user u in luUsers) { if (u.account != "Admin") { lcDelete.Add(Command.SetupDeleteCmd(new tb_sys_user2entercode() { user_uid = u.uid })); lcDelete.Add(Command.SetupDeleteCmd(new tb_sys_user() { uid = u.uid })); } } Dictionary dicBefore = Count(ai, dicSelect); int nRunValue = ai.RunEditCmds(lcDelete); Dictionary dicAfter = Count(ai, dicSelect); long nCount = 0; foreach(KeyValuePair kvpBefore in dicBefore) { if(dicAfter.ContainsKey(kvpBefore.Key)) { Logger.Debug($"{kvpBefore.Key} {kvpBefore.Value} => {dicAfter[kvpBefore.Key]}"); nCount += kvpBefore.Value - dicAfter[kvpBefore.Key]; } else { Logger.Debug($"{kvpBefore.Key} {kvpBefore.Value} => Not found"); } } Logger.Debug($"RunValue={nRunValue}, nCount={nCount}"); } while (false); } catch (Exception ex) { sMsg = ex.Message; } Logger.Debug($"Delete end {DateTime.Now}"); return sMsg; } protected Dictionary Count(ArsenalInterface ai, Dictionary i_dicSelect) { Dictionary dicResult = new Dictionary(); foreach(KeyValuePair kvp in i_dicSelect) { QueryDataSet qds = ai.RunQueryDataSet(kvp.Value); if (qds.IsSuccess) { dicResult.Add(kvp.Key, qds.Total); } } return dicResult; } } }