DBTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using DataService;
  2. using DB_Proxima;
  3. using Dm.util;
  4. using Mapster;
  5. using Newtonsoft.Json.Linq;
  6. using SqlSugar;
  7. using SqlSugar.Extensions;
  8. using SqlSugarORM;
  9. using System;
  10. using System.Diagnostics;
  11. using System.Dynamic;
  12. using System.Numerics;
  13. using System.Reflection;
  14. using System.Runtime.CompilerServices;
  15. using System.Text.Json;
  16. using Universal;
  17. namespace Test;
  18. internal class DBTest
  19. {
  20. private SqlSugarCustom _orm;
  21. public bool Initialize()
  22. {
  23. _orm = new SqlSugarCustom();
  24. _orm.CreateDataBase("DBTest");
  25. _orm.Initialize();
  26. string dbString = "Database=DBTest;Password=123456;Host=localhost;Username=postgres;Persist Security Info=True";
  27. return _orm.Open(dbString, DbType.PostgreSQL, true);
  28. }
  29. public void TestRaw()
  30. {
  31. SqlSugarCustom orm = new();
  32. orm.Initialize();
  33. string dbString = "Database=FROMTIN;Password=123456;Host=localhost;Username=postgres;Persist Security Info=True";
  34. //string dbString = "Database=Kepler;Password=123456;Host=localhost;Username=postgres;Persist Security Info=True";
  35. if (!orm.Open(dbString, SqlSugar.DbType.PostgreSQL, true))
  36. return;
  37. List<DbTableInfo> tables = orm.Client.DbMaintenance.GetTableInfoList();
  38. Stopwatch sw = new();
  39. sw.Start();
  40. int i = 0;
  41. Parallel.ForEach(tables, table =>
  42. {
  43. string localName = table.Name.Replace('.', '_');
  44. if (localName== "Recipe_History")
  45. return;
  46. if (localName == "event_data")
  47. return;
  48. var client = orm.Client.CopyNew();
  49. List<DbColumnInfo> dbColumns = client.DbMaintenance.GetColumnInfosByTableName(table.Name);
  50. RemoteToLocal remoteToLocal = new();
  51. //remoteToLocal.Initialize("DBTestKepler", "123456", "localhost", "postgres");
  52. remoteToLocal.Initialize("DBTestTIN", "123456", "localhost", "postgres");
  53. remoteToLocal.CreateTableRaw(localName, dbColumns);
  54. int totalCount = 0;
  55. int totalPage = 0;
  56. int currentPage = 1;
  57. int line = 0;
  58. lock (this)
  59. line = i++;
  60. do
  61. {
  62. dynamic[] dataCollection = client.Queryable<dynamic>().AS($"\"{table.Name}\"").ToPageList(currentPage, 100, ref totalCount, ref totalPage).ToArray();
  63. //Console.SetCursorPosition(0, Console.CursorTop - 1);
  64. lock (this)
  65. {
  66. Console.SetCursorPosition(0, line);
  67. Console.WriteLine($"{table.Name.PadRight(35)} Columns {dbColumns.Count.ToString().PadRight(15)} 100 Rows / page Page {currentPage}/{totalPage}");
  68. }
  69. foreach (dynamic data in dataCollection)
  70. {
  71. if (data is IDictionary<string, object> rawData)
  72. remoteToLocal.InsertDataRaw(localName, rawData);
  73. }
  74. } while (++currentPage <= totalPage);
  75. });
  76. //foreach (DbTableInfo table in tables)
  77. //{
  78. // string localName = table.Name.Replace('.', '_');
  79. // List<DbColumnInfo> dbColumns = orm.Client.DbMaintenance.GetColumnInfosByTableName(table.Name);
  80. // RemoteToLocal remoteToLocal = new();
  81. // remoteToLocal.Initialize("DBTestKepler", "123456", "localhost", "postgres");
  82. // remoteToLocal.CreateTableRaw(localName, dbColumns);
  83. // int totalCount = 0;
  84. // int totalPage = 0;
  85. // int currentPage = 1;
  86. // Console.WriteLine($"Move Table {localName} total column {dbColumns.Count}");
  87. // Console.WriteLine();
  88. // do
  89. // {
  90. // dynamic[] dataCollection = orm.Client!.Queryable<dynamic>().AS($"\"{table.Name}\"").ToPageList(currentPage, 100, ref totalCount, ref totalPage).ToArray();
  91. // Console.SetCursorPosition(0, Console.CursorTop - 1);
  92. // Console.WriteLine($"Page {currentPage}/{totalPage}");
  93. // foreach (dynamic data in dataCollection)
  94. // {
  95. // if (data is IDictionary<string, object> rawData)
  96. // remoteToLocal.InsertDataRaw(localName, rawData);
  97. // }
  98. // } while (++currentPage <= totalPage);
  99. //}
  100. sw.Stop();
  101. Console.SetCursorPosition(0, ++i);
  102. Console.WriteLine($"{sw.Elapsed.TotalSeconds} seconds");
  103. }
  104. public void TestOthers()
  105. {
  106. SqlSugarCustom orm = new();
  107. orm.Initialize();
  108. string dbString = "Database=tin01_db;Password=123456;Host=10.4.6.48;Username=postgres;Persist Security Info=True";
  109. if (!orm.Open(dbString, SqlSugar.DbType.PostgreSQL, true))
  110. return;
  111. dynamic system = orm.Client!.Queryable<dynamic>().AS("\"20250708.System\"").First();
  112. dynamic pm = orm.Client!.Queryable<dynamic>().AS("\"20250708.PM1\"").First();
  113. GeneralProcessData processData = new(0);
  114. if (!processData.ToDictionary(system, out Dictionary<string, object>? systemDic) || systemDic is null)
  115. return;
  116. if (!processData.ToDictionary(pm, out Dictionary<string, object>? pmDic) || pmDic is null)
  117. return;
  118. if (pmDic["PM1"] is not IDictionary<string, object> data)
  119. return;
  120. RemoteToLocal remoteToLocal = new();
  121. remoteToLocal.Initialize("DBTest", "123456", "localhost", "postgres");
  122. //remoteToLocal.CreateTableSystem(systemDic);
  123. remoteToLocal.CreateTablePM(data);
  124. Stopwatch sw = new();
  125. sw.Start();
  126. int totalCount = 0;
  127. int totalPage = 0;
  128. int currentPage = 1;
  129. do
  130. {
  131. Console.WriteLine($"page {currentPage}");
  132. foreach (var item in orm.Client!.Queryable<dynamic>().AS("\"20250708.PM1\"").ToPageList(currentPage, 1000, ref totalCount, ref totalPage).ToArray())
  133. {
  134. if (!processData.ToDictionary(item, out Dictionary<string, object>? pms) || pms is null)
  135. return;
  136. DateTime time = LongToDateTime(pms["time"]).Value;
  137. Guid guid = Guid.NewGuid();
  138. if (pms["PM1"] is not IDictionary<string, object> pmc)
  139. return;
  140. remoteToLocal.InsertDataPM(pmc, guid, time);
  141. }
  142. } while (++currentPage <= totalPage);
  143. //foreach (dynamic item in orm.Client!.Queryable<dynamic>().AS("\"20250708.PM1\"").ToArray())
  144. //{
  145. // if (!processData.ToDictionary(item, out Dictionary<string, object>? pms) || pms is null)
  146. // return;
  147. // DateTime time = LongToDateTime(pms["time"]).Value;
  148. // Guid guid = Guid.NewGuid();
  149. // if (pms["PM1"] is not IDictionary<string, object> pmc)
  150. // return;
  151. // remoteToLocal.InsertDataPM(pmc, guid, time);
  152. //}
  153. sw.Stop();
  154. Console.WriteLine(sw.Elapsed.TotalSeconds);
  155. }
  156. //public void Test(IDictionary<string, object> data, Guid guid, DateTime time)
  157. public void Test()
  158. {
  159. SqlSugarCustom orm = new();
  160. orm.Initialize();
  161. string dbString = "Database=tin01_db;Password=123456;Host=10.4.6.48;Username=postgres;Persist Security Info=True";
  162. if (!orm.Open(dbString, SqlSugar.DbType.PostgreSQL, true))
  163. return;
  164. dynamic t = orm.Client!.Queryable<dynamic>().AS("\"20250708.PM1\"").First();
  165. GeneralProcessData processData = new(0);
  166. if (!processData.ToDictionary(t, out Dictionary<string, object>? outputs) || outputs is null)
  167. return;
  168. DateTime time = LongToDateTime(outputs["time"]).Value;
  169. if (outputs["PM1"] is not IDictionary<string, object> data)
  170. return;
  171. Guid guid = Guid.NewGuid();
  172. _orm.CreateTable<FFU>("FFU");
  173. _orm.CreateTable<MFC>("MFC");
  174. _orm.CreateTable<GaslineHeater>("GaslineHeater");
  175. _orm.CreateTable<BufferFoup>("GaslineHeater");
  176. _orm.CreateTable<AvValue>("AvValue");
  177. Dictionary<string, object> ValueSensor = [];
  178. Dictionary<string, object> StatusSensor = [];
  179. Dictionary<string, object> leakCheck = [];
  180. Dictionary<string, object> recipe = [];
  181. Dictionary<string, object> aoValue = [];
  182. foreach (var item in data)
  183. {
  184. if (item.Value is not IDictionary<string, object> values)
  185. {
  186. switch (item.Key)
  187. {
  188. case string s when s.EndsWith("Enable"):
  189. continue;
  190. case string s when s.startsWith("LeakCheck"):
  191. leakCheck.Add(item.Key, item.Value);
  192. continue;
  193. default:
  194. recipe.Add(item.Key, item.Value);
  195. continue;
  196. }
  197. }
  198. switch (item.Key)
  199. {
  200. case "APC":
  201. case "APCVATGV":
  202. case "BoatElevatorServo":
  203. case "BoatRotationServo":
  204. case "BufferServo":
  205. case "Shutter":
  206. CreateTable(item.Key, values);
  207. InsertData(item.Key, time, guid, values);
  208. continue;
  209. case string s when s.startsWith("Trig"):
  210. if (item.Value is IDictionary<string, object> value)
  211. aoValue.Add(item.Key, value["AOValue"]);
  212. continue;
  213. case string s when s.startsWith("FS"):
  214. case string s1 when s1.startsWith("PG"):
  215. case string s2 when s2.startsWith("PS"):
  216. case string s3 when s3.startsWith("VG"):
  217. if (item.Value is IDictionary<string, object> vss)
  218. ValueSensor.Add(item.Key, vss["Value"]);
  219. continue;
  220. case string s when s.startsWith("ValveAV"):
  221. _orm.Insert<AvValue>("AvValue", CreateData<AvValue>(guid, time, item));
  222. continue;
  223. case string s when s.startsWith("Sensor"):
  224. if (item.Value is IDictionary<string, object> status)
  225. StatusSensor.Add(item.Key, status["Value"]);
  226. continue;
  227. case string s when s.startsWith("FFU"):
  228. _orm.Insert<FFU>("FFU", CreateData<FFU>(guid, time, item));
  229. continue;
  230. case string s when s.startsWith("GaslineHeater"):
  231. _orm.Insert<GaslineHeater>("GaslineHeater", CreateData<GaslineHeater>(guid, time, item));
  232. continue;
  233. case string s when s.startsWith("MFC"):
  234. _orm.Insert<MFC>("MFC", CreateData<MFC>(guid, time, item));
  235. continue;
  236. default:
  237. continue;
  238. }
  239. }
  240. CreateTable("ValueSensor", ValueSensor);
  241. CreateTable("StatusSensor", StatusSensor);
  242. CreateTable("Recipe", recipe);
  243. CreateTable("LeakCheck", leakCheck);
  244. CreateTable("AoValue", aoValue);
  245. InsertData("ValueSensor", time, guid, ValueSensor);
  246. InsertData("StatusSensor", time, guid, StatusSensor);
  247. InsertData("Recipe", time, guid, recipe);
  248. InsertData("LeakCheck", time, guid, leakCheck);
  249. InsertData("AoValue", time, guid, aoValue);
  250. }
  251. public void TestSystem()
  252. {
  253. SqlSugarCustom orm = new();
  254. orm.Initialize();
  255. string dbString = "Database=tin01_db;Password=123456;Host=10.4.6.48;Username=postgres;Persist Security Info=True";
  256. if (!orm.Open(dbString, SqlSugar.DbType.PostgreSQL, true))
  257. return;
  258. dynamic t = orm.Client!.Queryable<dynamic>().AS("\"20250708.System\"").First();
  259. GeneralProcessData processData = new(0);
  260. if (!processData.ToDictionary(t, out Dictionary<string, object>? outputs) || outputs is null)
  261. return;
  262. DateTime time = LongToDateTime(outputs["time"]).Value;
  263. Guid guid = Guid.NewGuid();
  264. Dictionary<string, object> systemCollection = [];
  265. Dictionary<string, object> alarmCollection = [];
  266. _orm.CreateTable<DB_Proxima.Heater>("Heater");
  267. _orm.CreateTable<Stocker>("Stocker");
  268. _orm.CreateTable<LoadPort>("LoadPort");
  269. _orm.CreateTable<FIMS>("FIMS");
  270. foreach (var item in outputs)
  271. {
  272. if (item.Value is not IDictionary<string, object> values)
  273. continue;
  274. switch (item.Key)
  275. {
  276. case "Boat":
  277. case "CarrierRobot":
  278. case "Scheduler":
  279. case "WaferRobot":
  280. CreateTable(item.Key, values);
  281. InsertData(item.Key, time, guid, values);
  282. continue;
  283. case "System":
  284. if (values is not IDictionary<string, object> systems)
  285. continue;
  286. foreach (var system in systems)
  287. {
  288. switch (system.Key)
  289. {
  290. case string s when s.startsWith("Heater"):
  291. _orm.Insert<DB_Proxima.Heater>("Heater", CreateData<DB_Proxima.Heater>(guid, time, item));
  292. continue;
  293. case string s when s.startsWith("AlarmSignalHeater"):
  294. alarmCollection.Add(system.Key, ((IDictionary<string, object>)system.Value)["Value"] ??= false);
  295. continue;
  296. default:
  297. systemCollection.Add(system.Key, system.Value);
  298. break;
  299. }
  300. }
  301. continue;
  302. case string s when s.startsWith("Stocker"):
  303. _orm.Insert<Stocker>("Stocker", CreateData<Stocker>(guid, time, item));
  304. continue;
  305. case string s when s.startsWith("LP"):
  306. continue;
  307. case string s when s.startsWith("FIMS"):
  308. continue;
  309. default:
  310. break;
  311. }
  312. }
  313. CreateTable("System", systemCollection);
  314. CreateTable("AlarmSignalHeater", alarmCollection);
  315. InsertData("System", time, guid, systemCollection);
  316. InsertData("AlarmSignalHeater", time, guid, alarmCollection);
  317. }
  318. public static T? CreateData<T>(Guid guid, DateTime time, KeyValuePair<string, object> input) where T : class, new()
  319. {
  320. if (input.Value is not IDictionary<string, object> values)
  321. return null;
  322. T source = new();
  323. if (source is not IBasicInfo basicInfo)
  324. return null;
  325. basicInfo.UID = guid;
  326. basicInfo.Time = time;
  327. basicInfo.Name = input.Key;
  328. DicToClass(ref source, values);
  329. return source;
  330. }
  331. public static DateTime? LongToDateTime(object o)
  332. {
  333. if (o is not long l)
  334. return null;
  335. return new(l);
  336. }
  337. public static void DicToClass<T>(ref T input, IDictionary<string, object> source)
  338. {
  339. foreach (PropertyInfo property in input.GetType().GetProperties())
  340. {
  341. if (!source.TryGetValue(property.Name, out object value) || value is null)
  342. continue;
  343. property.SetValue(input, value);
  344. }
  345. }
  346. public void CreateTable(string tableName, IDictionary<string, object> source)
  347. {
  348. DynamicProperyBuilder builder = _orm.Client.DynamicBuilder().CreateClass(tableName, new SugarTable());
  349. builder.CreateProperty("UID", typeof(Guid), new SugarColumn() { IsPrimaryKey = true });
  350. builder.CreateProperty("time", typeof(DateTime), new SugarColumn() { });
  351. foreach (var item in source)
  352. builder.CreateProperty(item.Key, item.Value.GetType(), new SugarColumn() { IsNullable = true });
  353. var type = builder.BuilderType();
  354. _orm.Client.CodeFirst.InitTables(type);
  355. }
  356. public int InsertData(string tableName, DateTime dateTime, Guid guid, IDictionary<string, object> source)
  357. {
  358. var dc = new Dictionary<string, object>();
  359. dc.Add("UID", guid);
  360. dc.Add("time", dateTime);
  361. foreach (var item in source)
  362. dc.Add(item.Key, item.Value);
  363. return _orm.Client.Insertable(dc).AS(tableName).ExecuteCommand();
  364. }
  365. }