DatabaseHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. using LocalhostMES.Core;
  2. using LocalhostMES.Enums;
  3. using LocalhostMES.Models;
  4. using SqlSugar;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Security.Claims;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using static Org.BouncyCastle.Pqc.Crypto.Utilities.PqcOtherInfoGenerator;
  13. namespace LocalhostMES.DataBase
  14. {
  15. public class DatabaseHelper
  16. {
  17. private static void NotifyChanged(MesDataScope scope)
  18. {
  19. MesDataChangedNotifier.Raise(scope);
  20. }
  21. public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
  22. {
  23. //192.168.1.100
  24. ConnectionString = "Database=LingPaoProject; Data Source=127.0.0.1; Port=3306;User Id=root; Password=root;Charset=utf8;AllowPublicKeyRetrieval=True;SslMode=None;",//连接符字串
  25. DbType = SqlSugar.DbType.MySql,//数据库类型
  26. IsAutoCloseConnection = true //不设成true要手动close
  27. },
  28. db =>
  29. {
  30. db.Aop.OnLogExecuting = (sql, pars) =>
  31. {
  32. Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
  33. };
  34. });
  35. public static void CreataDataTable()
  36. {
  37. try
  38. {
  39. Db.CodeFirst.InitTables<SnInfo>();
  40. Db.CodeFirst.InitTables<BindRecord>();
  41. Db.CodeFirst.InitTables<ProcessRecord>();
  42. Db.CodeFirst.InitTables<ParkingLot>();
  43. Db.CodeFirst.InitTables<WorkOrderInfo>();
  44. Db.CodeFirst.InitTables<LocalhostPartInfo>();
  45. // 您的数据库操作代码
  46. }
  47. catch (AggregateException ex)
  48. {
  49. // 遍历所有内部异常
  50. foreach (var inner in ex.InnerExceptions)
  51. {
  52. Console.WriteLine($"内部异常: {inner.Message}");
  53. Console.WriteLine(inner.StackTrace);
  54. }
  55. // 或者只取第一个
  56. // var actualException = ex.InnerException;
  57. }
  58. catch (SqlSugarException ex)
  59. {
  60. Console.WriteLine($"错误消息:{ex.Message}");
  61. Console.WriteLine($"内部异常:{ex.InnerException?.Message}");
  62. Console.WriteLine($"堆栈:{ex.StackTrace}");
  63. // 也可以记录日志
  64. }
  65. catch (Exception e)
  66. {
  67. }
  68. }
  69. public static int GetProductProductionRecords(string sn)
  70. {
  71. try
  72. {
  73. return Db.Queryable<ProductionToStation>().Where(it => it.SN == sn).ToList().Count;
  74. }
  75. catch ( Exception ex )
  76. {
  77. LogHelper.WriteLogError("获取一条产品的生产记录至数据库时出错!", ex);
  78. }
  79. return 0;
  80. }
  81. public static List<WorkOrderInfo> SelectWorkOrderInfo(string workOrderNo = null)
  82. {
  83. if ( workOrderNo != null )
  84. {
  85. var single = Db.Queryable<WorkOrderInfo>()
  86. .Where(it => it.WorkOrderNo == workOrderNo)
  87. .Select(it => new WorkOrderInfo
  88. {
  89. OrderNo = it.OrderNo,
  90. LineCode = it.LineCode,
  91. CreateTime = it.CreateTime,
  92. EndTime = it.EndTime,
  93. IsLocalhost = it.IsLocalhost,
  94. MaterialCode = it.MaterialCode,
  95. MaterialName = it.MaterialName,
  96. PlannedQuantity = it.PlannedQuantity,
  97. StartTime = it.StartTime,
  98. Status = it.Status,
  99. WorkOrderNo = it.WorkOrderNo,
  100. CompletedQuantity = SqlFunc.Subqueryable<SnInfo>().Where(s => s.WorkOrderNo == it.WorkOrderNo&&s.IsUsed).Count()
  101. }).ToList();
  102. NormalizeWorkOrderStatus(single);
  103. return single;
  104. }
  105. else
  106. {
  107. var list = Db.Queryable<WorkOrderInfo>().
  108. Select(it=>new WorkOrderInfo
  109. {OrderNo=it.OrderNo,
  110. LineCode=it.LineCode,
  111. CreateTime=it.CreateTime,
  112. EndTime=it.EndTime,
  113. IsLocalhost=it.IsLocalhost,
  114. MaterialCode=it.MaterialCode,
  115. MaterialName=it.MaterialName,
  116. PlannedQuantity=it.PlannedQuantity,
  117. StartTime=it.StartTime,
  118. Status=it.Status,
  119. WorkOrderNo=it.WorkOrderNo,
  120. CompletedQuantity=SqlFunc.Subqueryable<SnInfo>().Where(s=>s.WorkOrderNo==it.WorkOrderNo && s.IsUsed).Count() }).ToList();
  121. NormalizeWorkOrderStatus(list);
  122. return list;
  123. }
  124. }
  125. private static void NormalizeWorkOrderStatus(List<WorkOrderInfo> workOrders)
  126. {
  127. if (workOrders == null || workOrders.Count == 0)
  128. {
  129. return;
  130. }
  131. var changed = false;
  132. foreach (var workOrder in workOrders)
  133. {
  134. if (workOrder.PlannedQuantity > 0 &&
  135. workOrder.CompletedQuantity >= workOrder.PlannedQuantity &&
  136. workOrder.Status != "6")
  137. {
  138. workOrder.Status = "6";
  139. Db.Updateable<WorkOrderInfo>()
  140. .SetColumns(it => new WorkOrderInfo { Status = "6" })
  141. .Where(it => it.WorkOrderNo == workOrder.WorkOrderNo)
  142. .ExecuteCommand();
  143. changed = true;
  144. }
  145. }
  146. if (changed)
  147. {
  148. NotifyChanged(MesDataScope.WorkOrder);
  149. }
  150. }
  151. public static bool InsertWorkOrderInfo(WorkOrderInfo info)
  152. {
  153. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  154. {
  155. NotifyChanged(MesDataScope.WorkOrder);
  156. return true;
  157. }
  158. return false;
  159. }
  160. public static bool UpdateWorkOrderInfo(WorkOrderInfo info)
  161. {
  162. if ( Db.Updateable(info).Where(it => it.WorkOrderNo == info.WorkOrderNo).ExecuteCommand() == 1 )
  163. {
  164. NotifyChanged(MesDataScope.WorkOrder);
  165. return true;
  166. }
  167. return false;
  168. }
  169. public static bool DeleteWorkOrderInfo(WorkOrderInfo info)
  170. {
  171. if ( Db.Deleteable(info).Where(it => it.WorkOrderNo == info.WorkOrderNo).ExecuteCommand() == 1 )
  172. {
  173. NotifyChanged(MesDataScope.WorkOrder);
  174. return true;
  175. }
  176. return false;
  177. }
  178. public static List<SnInfo> SelectSnInfo(string workOrderNo = null, bool needused = false, bool isused = false)
  179. {
  180. if ( workOrderNo != null )
  181. {
  182. if ( needused )
  183. {
  184. return Db.Queryable<SnInfo>().Where(it => it.WorkOrderNo == workOrderNo && it.IsUsed == isused).ToList();
  185. }
  186. else
  187. {
  188. return Db.Queryable<SnInfo>().Where(it => it.WorkOrderNo == workOrderNo).ToList();
  189. }
  190. }
  191. else
  192. {
  193. return Db.Queryable<SnInfo>().ToList();
  194. }
  195. }
  196. public static bool InsertSnInfo(SnInfo info)
  197. {
  198. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  199. {
  200. NotifyChanged(MesDataScope.Sn);
  201. return true;
  202. }
  203. return false;
  204. }
  205. public static bool InsertSnInfos(List<SnInfo> info)
  206. {
  207. if (Db.Storageable(info).ExecuteCommand() == info.Count)
  208. {
  209. NotifyChanged(MesDataScope.Sn);
  210. return true;
  211. }
  212. return false;
  213. }
  214. public static bool UpdateSnInfo(SnInfo info)
  215. {
  216. if ( Db.Updateable(info).Where(it => it.Sn == info.Sn).ExecuteCommand() == 1 )
  217. {
  218. NotifyChanged(MesDataScope.Sn);
  219. return true;
  220. }
  221. return false;
  222. }
  223. public static bool DeleteSnInfo(SnInfo info)
  224. {
  225. if ( Db.Deleteable(info).Where(it => it.Sn == info.Sn).ExecuteCommand() == 1 )
  226. {
  227. NotifyChanged(MesDataScope.Sn);
  228. return true;
  229. }
  230. return false;
  231. }
  232. public static List<BindRecord> SelectBindRecord(string Sn = null, string PartNum = null, string StationCode = null)
  233. {
  234. var query = Db.Queryable<BindRecord>();
  235. // 动态添加查询条件
  236. if ( !string.IsNullOrWhiteSpace(Sn) )
  237. {
  238. query = query.Where(it => it.Sn == Sn);
  239. }
  240. if ( !string.IsNullOrWhiteSpace(PartNum) )
  241. {
  242. query = query.Where(it => it.PartNum == PartNum);
  243. }
  244. if ( !string.IsNullOrWhiteSpace(StationCode) )
  245. {
  246. query = query.Where(it => it.StationCode == StationCode);
  247. }
  248. return query.ToList();
  249. }
  250. public static bool InsertBindRecord(BindRecord info)
  251. {
  252. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  253. {
  254. NotifyChanged(MesDataScope.BindRecord);
  255. return true;
  256. }
  257. return false;
  258. }
  259. public static bool UpdateBindRecord(BindRecord info)
  260. {
  261. if ( Db.Updateable(info).Where(it => it.Sn == info.Sn).ExecuteCommand() == 1 )
  262. {
  263. NotifyChanged(MesDataScope.BindRecord);
  264. return true;
  265. }
  266. return false;
  267. }
  268. public static bool DeleteBindRecord(string sn = null)
  269. {
  270. if ( sn != null )
  271. {
  272. if ( Db.Deleteable<BindRecord>().Where(it => it.Sn == sn).ExecuteCommand() == 1 )
  273. {
  274. NotifyChanged(MesDataScope.BindRecord);
  275. return true;
  276. }
  277. }
  278. else
  279. {
  280. if ( Db.Deleteable<BindRecord>().ExecuteCommand() != 0 )
  281. {
  282. NotifyChanged(MesDataScope.BindRecord);
  283. return true;
  284. }
  285. }
  286. return false;
  287. }
  288. public static List<ProcessRecord> SelectProcessRecord(string Sn = null, string StationCode = null)
  289. {
  290. var query = Db.Queryable<ProcessRecord>();
  291. // 动态添加查询条件
  292. if ( !string.IsNullOrWhiteSpace(Sn) )
  293. {
  294. query = query.Where(it => it.Sn == Sn);
  295. }
  296. if ( !string.IsNullOrWhiteSpace(StationCode) )
  297. {
  298. query = query.Where(it => it.Station == StationCode);
  299. }
  300. return query.ToList();
  301. }
  302. public static bool InsertProcessRecord(ProcessRecord info)
  303. {
  304. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  305. {
  306. NotifyChanged(MesDataScope.ProcessRecord);
  307. return true;
  308. }
  309. return false;
  310. }
  311. public static bool UpdateProcessRecord(ProcessRecord info)
  312. {
  313. if ( Db.Updateable(info).Where(it => it.Sn == info.Sn).ExecuteCommand() == 1 )
  314. {
  315. NotifyChanged(MesDataScope.ProcessRecord);
  316. return true;
  317. }
  318. return false;
  319. }
  320. public static bool DeleteProcessRecord(string sn = null)
  321. {
  322. if ( Db.Deleteable<ProcessRecord>().Where(it => it.Sn == sn).ExecuteCommand() == 1 )
  323. {
  324. NotifyChanged(MesDataScope.ProcessRecord);
  325. return true;
  326. }
  327. return false;
  328. }
  329. public static List<LocalhostPartInfo> SelectPartInfo(KeyPartStationType stationType)
  330. {
  331. return Db.Queryable<LocalhostPartInfo>().Where(it => it.StationType == stationType).ToList();
  332. }
  333. public static bool InsertPartInfo(LocalhostPartInfo info)
  334. {
  335. var ok = Db.Storageable(info).ExecuteCommand() > 0;
  336. if (ok)
  337. {
  338. NotifyChanged(MesDataScope.KeyPart);
  339. }
  340. return ok;
  341. }
  342. public static bool DeletePartInfo(KeyPartStationType stationType, string partNum)
  343. {
  344. var ok = Db.Deleteable<LocalhostPartInfo>()
  345. .Where(it => it.StationType == stationType && it.PartNum == partNum)
  346. .ExecuteCommand() >= 1;
  347. if (ok)
  348. {
  349. NotifyChanged(MesDataScope.KeyPart);
  350. }
  351. return ok;
  352. }
  353. public static List<ParkingLot> SelectParkingLot()
  354. {
  355. return Db.Queryable<ParkingLot>().OrderBy(it => it.Id).ToList();
  356. }
  357. public static bool InsertParkingLot(ParkingLot info)
  358. {
  359. var ok = Db.Storageable(info).ExecuteCommand() > 0;
  360. if (ok)
  361. {
  362. NotifyChanged(MesDataScope.ParkingLot);
  363. }
  364. return ok;
  365. }
  366. public static bool UpdateParkingLot(ParkingLot info)
  367. {
  368. var ok = Db.Updateable(info).Where(it => it.Id == info.Id).ExecuteCommand() == 1;
  369. if (ok)
  370. {
  371. NotifyChanged(MesDataScope.ParkingLot);
  372. }
  373. return ok;
  374. }
  375. public static bool DeleteParkingLot(int id)
  376. {
  377. var ok = Db.Deleteable<ParkingLot>().Where(it => it.Id == id).ExecuteCommand() == 1;
  378. if (ok)
  379. {
  380. NotifyChanged(MesDataScope.ParkingLot);
  381. }
  382. return ok;
  383. }
  384. #region 当前左右工位识别码
  385. public static OrderToMesInfo SelectWorkOrderInfo()
  386. {
  387. List<OrderToMesInfo> lilsts = Db.Queryable<OrderToMesInfo>().ToList();
  388. if ( lilsts.Count != 0 )
  389. {
  390. return lilsts[ 0 ];
  391. }
  392. else
  393. {
  394. return new OrderToMesInfo() { CreateTime = DateTime.Now, Id = 0, LeftCheckCode = "", RightCheckCode = "" };
  395. }
  396. }
  397. public static bool InsertWorkOrderInfo(OrderToMesInfo info)
  398. {
  399. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  400. {
  401. return true;
  402. }
  403. return false;
  404. }
  405. public static bool UpdateWorkOrderInfo(OrderToMesInfo info, bool isleft = true)
  406. {
  407. if ( info.CurrMarkCode != string.Empty )
  408. {
  409. if ( Db.Updateable(info).UpdateColumns(it => new { it.CurrMarkCode }).Where(it => it.Id == info.Id).ExecuteCommand() == 1 )
  410. {
  411. return true;
  412. }
  413. return false;
  414. }
  415. if ( isleft )
  416. {
  417. if ( Db.Updateable(info).UpdateColumns(it => new { it.CreateTime, it.LeftCheckCode }).Where(it => it.Id == info.Id).ExecuteCommand() == 1 )
  418. {
  419. return true;
  420. }
  421. }
  422. else
  423. {
  424. if ( Db.Updateable(info).UpdateColumns(it => new { it.CreateTime, it.RightCheckCode }).Where(it => it.Id == info.Id).ExecuteCommand() == 1 )
  425. {
  426. return true;
  427. }
  428. }
  429. return false;
  430. }
  431. #endregion
  432. }
  433. }