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.

332 lines
12 KiB

  1. using CounsellorBL;
  2. using CounsellorBL.GROUP.ConstDefinition;
  3. using Newtonsoft.Json;
  4. using OT.COM.ArsenalDB;
  5. using OT.COM.ArsenalDB.SQL;
  6. using SoldierData.EnterprizeV4;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text;
  14. namespace TransporterAP
  15. {
  16. public class ExportImport : DBService
  17. {
  18. List<string> lsDeleteSeq = new List<string>() {
  19. "Gen_tb_ord_purchase_detail", // BranchID
  20. "Gen_tb_ord_incoming_return_record", // OrderMstIDs => GROUP_UID
  21. "Gen_tb_ord_order_detail", // MemberID
  22. "Gen_tb_ord_order_master", // GROUP_UID
  23. "Gen_tb_meb_shopping_points_record", // MemberID
  24. "Gen_tb_grp_comment", // ArticleID
  25. "Gen_tb_prd_article2product", // ArticleID
  26. "Gen_tb_ord_purchase", // MemberID
  27. "Gen_tb_ord_checkout_list_detail", // MemberID
  28. "Gen_tb_prd_product", // GROUP_UID
  29. "Gen_tb_grp_article_media", // ArticleID
  30. "Gen_tb_ord_message_log_record", // MemberID
  31. "Gen_tb_ord_checkout_list_master", // GROUP_UID
  32. "Gen_tb_grp_article", // GROUP_UID
  33. "Gen_tb_hr_employee2branch", // BranchID
  34. "Gen_tb_ord_message_log", // BranchID
  35. "Gen_tb_meb_member", // FBID
  36. "Gen_tb_sys_uploadlog", // ArticleMediaMediaID => ArticleID
  37. "Gen_tb_sys_user2role", // ROLE_UID => GROUP_UID
  38. "Gen_tb_grp_group2user", // GROUP_UID
  39. "Gen_tb_hr_employee", // Employee2BranchID => BranchID
  40. "Gen_tb_grp_branch", // GROUP_UID
  41. "Gen_tb_sys_role2org", // RoleIDs => GROUP_UID
  42. "Gen_tb_sys_role", // GROUP_UID
  43. "Gen_tb_grp_group", // GROUP_NAME
  44. "Gen_tb_sys_user2entercode", // EmployeeID => Employee2BranchID => BranchID
  45. "Gen_tb_sys_user" // EmployeeIDs => Employee2BranchID => BranchID
  46. };
  47. public override string MainTable => throw new NotImplementedException();
  48. private SQLLib _GetSQLLib() => new SQLLib(new MSSQLDirectSQLHelper());
  49. public string ImportFiles(string i_sExportFolderPath)
  50. {
  51. string sMsg = null;
  52. Logger.Debug($"ImportFiles Start {DateTime.Now}");
  53. try
  54. {
  55. do
  56. {
  57. List<string> lsInsert = new List<string>(lsDeleteSeq);
  58. lsInsert.Reverse();
  59. foreach (string sFunctionName in lsInsert)
  60. {
  61. string sTableName = sFunctionName.Substring(4);
  62. Type tCur = _GetEntityType(sTableName);
  63. int nBatchCount = 0;
  64. while(true)
  65. {
  66. string sFilePath = $"{Path.Combine(i_sExportFolderPath, sTableName)}.{nBatchCount}";
  67. if(!File.Exists(sFilePath))
  68. {
  69. break;
  70. }
  71. MethodInfo[] amiMethods = typeof(JsonConvert).GetMethods();
  72. MethodInfo miFid = amiMethods.FirstOrDefault(f => f.Name == "DeserializeObject" && f.IsGenericMethod && f.GetParameters().Length == 1);
  73. // MethodInfo miFid = amiMethods.FirstOrDefault(f => f.Name == "DeserializeObject" );
  74. //
  75. Type generic = typeof(List<>);
  76. Type[] typeArgs = { tCur };
  77. Type constructed = generic.MakeGenericType(typeArgs);
  78. string sContent = File.ReadAllText(sFilePath, Encoding.UTF8);
  79. object oResult = miFid.MakeGenericMethod(constructed).Invoke(null, new object[] { sContent });
  80. IEnumerable myList = oResult as IEnumerable;
  81. List<Command> lcInsert = new List<Command>();
  82. foreach (object o in myList)
  83. {
  84. Command cInsert = Command.SetupInsertCmd(o as EntityBase);
  85. lcInsert.Add(cInsert);
  86. }
  87. if (lcInsert.Any())
  88. {
  89. ArsenalInterface ai = ArsenalDBMgr.GetInst(lcInsert[0], GetDefaultSystemColumnInfo());
  90. ai.RunEditCmds(lcInsert);
  91. }
  92. nBatchCount++;
  93. }
  94. }
  95. }
  96. while (false);
  97. }
  98. catch (Exception ex)
  99. {
  100. sMsg = ex.Message;
  101. }
  102. Logger.Debug($"ImportFiles End {DateTime.Now}");
  103. return sMsg;
  104. }
  105. public string ExportFiles(string i_sExportFolderPath, List<object> i_asGroupName)
  106. {
  107. string sMsg = null;
  108. System.Diagnostics.Debug.WriteLine(DateTime.Now);
  109. try
  110. {
  111. do
  112. {
  113. if(!Directory.Exists(i_sExportFolderPath))
  114. {
  115. Directory.CreateDirectory(i_sExportFolderPath);
  116. }
  117. foreach (string sFunctionName in lsDeleteSeq)
  118. {
  119. sMsg = _GetSQLLib().GetUIDs(SQLLib.Action.SELECT_ALL, sFunctionName, i_asGroupName, out Command o_cCmd);
  120. if (sMsg != null)
  121. {
  122. break;
  123. }
  124. ArsenalInterface ai = ArsenalDBMgr.GetInst(o_cCmd, GetDefaultSystemColumnInfo());
  125. string sTableName = sFunctionName.Substring(4);
  126. Type tCur = _GetEntityType(sTableName);
  127. MethodInfo[] mis = ai.GetType().GetMethods();
  128. MethodInfo miFid = new List<MethodInfo>(mis).FirstOrDefault(f => f.Name == "RunQueryList" && f.GetParameters().Length == 4);
  129. object oResult = miFid.MakeGenericMethod(tCur).Invoke(ai, new object[] { o_cCmd, null, -1, -1 });
  130. string sPath = Path.Combine(i_sExportFolderPath, sTableName);
  131. IEnumerable myList = oResult as IEnumerable;
  132. IEnumerator it = myList.GetEnumerator();
  133. int nBatchCount = 0;
  134. List<object> lTemp = new List<object>();
  135. while(it.MoveNext())
  136. {
  137. lTemp.Add(it.Current);
  138. if(lTemp.Count == 10000)
  139. {
  140. makeFile(sPath, nBatchCount, lTemp);
  141. nBatchCount++;
  142. lTemp.Clear();
  143. }
  144. }
  145. if(lTemp.Any())
  146. {
  147. makeFile(sPath, nBatchCount, lTemp);
  148. }
  149. }
  150. }
  151. while (false);
  152. }
  153. catch (Exception ex)
  154. {
  155. sMsg = ex.Message;
  156. }
  157. System.Diagnostics.Debug.WriteLine(DateTime.Now);
  158. return sMsg;
  159. }
  160. protected string makeFile(string i_sFilePath, int i_nBatchID, List<object> i_loData )
  161. {
  162. string sMsg = null;
  163. string sPath = $"{i_sFilePath}.{i_nBatchID}";
  164. if (File.Exists(sPath))
  165. {
  166. File.Delete(sPath);
  167. }
  168. File.WriteAllText(sPath, JsonConvert.SerializeObject(i_loData), Encoding.UTF8);
  169. return sMsg;
  170. }
  171. public string Delete( List<object> i_asGroupName)
  172. {
  173. string sMsg = null;
  174. Logger.Debug($"Delete start {DateTime.Now}");
  175. try
  176. {
  177. do
  178. {
  179. Dictionary<string, Command> dicSelect = new Dictionary<string, Command>();
  180. List<Command> lcDelete = new List<Command>();
  181. foreach (string sFunctionName in lsDeleteSeq)
  182. {
  183. sMsg = _GetSQLLib().GetUIDs(SQLLib.Action.SELECT_IDONLY, sFunctionName, i_asGroupName, out Command o_cSelect);
  184. if (sMsg != null)
  185. {
  186. break;
  187. }
  188. dicSelect.Add(sFunctionName, o_cSelect);
  189. sMsg = _GetSQLLib().GetUIDs(SQLLib.Action.DELETE, sFunctionName, i_asGroupName, out Command o_cCmd);
  190. if (sMsg != null)
  191. {
  192. break;
  193. }
  194. lcDelete.Add(o_cCmd);
  195. }
  196. if(!lcDelete.Any())
  197. {
  198. break;
  199. }
  200. // Store User
  201. _GetSQLLib().GetUIDs(SQLLib.Action.SELECT_ALL, "Gen_tb_sys_user", i_asGroupName, out Command o_cSelectUser);
  202. ArsenalInterface ai = ArsenalDBMgr.GetInst(o_cSelectUser, GetDefaultSystemColumnInfo());
  203. List<tb_sys_user> luUsers = ai.RunQueryList<tb_sys_user>(o_cSelectUser);
  204. foreach (tb_sys_user u in luUsers)
  205. {
  206. if (u.account != "Admin")
  207. {
  208. lcDelete.Add(Command.SetupDeleteCmd(new tb_sys_user2entercode() { user_uid = u.uid }));
  209. lcDelete.Add(Command.SetupDeleteCmd(new tb_sys_user() { uid = u.uid }));
  210. }
  211. }
  212. Dictionary<string, long> dicBefore = Count(ai, dicSelect);
  213. int nRunValue = ai.RunEditCmds(lcDelete);
  214. Dictionary<string, long> dicAfter = Count(ai, dicSelect);
  215. long nCount = 0;
  216. foreach(KeyValuePair<string, long> kvpBefore in dicBefore)
  217. {
  218. if(dicAfter.ContainsKey(kvpBefore.Key))
  219. {
  220. Logger.Debug($"{kvpBefore.Key} {kvpBefore.Value} => {dicAfter[kvpBefore.Key]}");
  221. nCount += kvpBefore.Value - dicAfter[kvpBefore.Key];
  222. }
  223. else
  224. {
  225. Logger.Debug($"{kvpBefore.Key} {kvpBefore.Value} => Not found");
  226. }
  227. }
  228. Logger.Debug($"RunValue={nRunValue}, nCount={nCount}");
  229. }
  230. while (false);
  231. }
  232. catch (Exception ex)
  233. {
  234. sMsg = ex.Message;
  235. }
  236. Logger.Debug($"Delete end {DateTime.Now}");
  237. return sMsg;
  238. }
  239. protected Dictionary<string, long> Count(ArsenalInterface ai, Dictionary<string, Command> i_dicSelect)
  240. {
  241. Dictionary<string, long> dicResult = new Dictionary<string, long>();
  242. foreach(KeyValuePair<string, Command> kvp in i_dicSelect)
  243. {
  244. QueryDataSet qds = ai.RunQueryDataSet(kvp.Value);
  245. if (qds.IsSuccess)
  246. {
  247. dicResult.Add(kvp.Key, qds.Total);
  248. }
  249. }
  250. return dicResult;
  251. }
  252. }
  253. }