DBTest.cs 16 KB

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