DatabaseHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
  18. {
  19. //192.168.1.100
  20. ConnectionString = "Database=LingPaoProject; Data Source=192.168.1.26; Port=3306;User Id=root; Password=root;Charset=utf8;AllowPublicKeyRetrieval=True;SslMode=None;",//连接符字串
  21. DbType = SqlSugar.DbType.MySql,//数据库类型
  22. IsAutoCloseConnection = true //不设成true要手动close
  23. },
  24. db =>
  25. {
  26. db.Aop.OnLogExecuting = (sql, pars) =>
  27. {
  28. Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
  29. };
  30. });
  31. public static void CreataDataTable()
  32. {
  33. try
  34. {
  35. Db.CodeFirst.InitTables<SnInfo>();
  36. Db.CodeFirst.InitTables<BindRecord>();
  37. Db.CodeFirst.InitTables<ProcessRecord>();
  38. Db.CodeFirst.InitTables<ParkingLot>();
  39. Db.CodeFirst.InitTables<WorkOrderInfo>();
  40. // 您的数据库操作代码
  41. }
  42. catch (AggregateException ex)
  43. {
  44. // 遍历所有内部异常
  45. foreach (var inner in ex.InnerExceptions)
  46. {
  47. Console.WriteLine($"内部异常: {inner.Message}");
  48. Console.WriteLine(inner.StackTrace);
  49. }
  50. // 或者只取第一个
  51. // var actualException = ex.InnerException;
  52. }
  53. catch (SqlSugarException ex)
  54. {
  55. Console.WriteLine($"错误消息:{ex.Message}");
  56. Console.WriteLine($"内部异常:{ex.InnerException?.Message}");
  57. Console.WriteLine($"堆栈:{ex.StackTrace}");
  58. // 也可以记录日志
  59. }
  60. catch (Exception e)
  61. {
  62. }
  63. }
  64. public static int GetProductProductionRecords(string sn)
  65. {
  66. try
  67. {
  68. return Db.Queryable<ProductionToStation>().Where(it => it.SN == sn).ToList().Count;
  69. }
  70. catch ( Exception ex )
  71. {
  72. LogHelper.WriteLogError("获取一条产品的生产记录至数据库时出错!", ex);
  73. }
  74. return 0;
  75. }
  76. public static List<WorkOrderInfo> SelectWorkOrderInfo(string workOrderNo = null,bool islocalhost=false)
  77. {
  78. if ( workOrderNo != null )
  79. {
  80. return Db.Queryable<WorkOrderInfo>().Where(it => it.WorkOrderNo == workOrderNo).ToList();
  81. }
  82. else
  83. {
  84. return Db.Queryable<WorkOrderInfo>().
  85. Select(it=>new WorkOrderInfo
  86. {OrderNo=it.OrderNo,
  87. LineCode=it.LineCode,
  88. CreateTime=it.CreateTime,
  89. EndTime=it.EndTime,
  90. IsLocalhost=it.IsLocalhost,
  91. MaterialCode=it.MaterialCode,
  92. MaterialName=it.MaterialName,
  93. PlannedQuantity=it.PlannedQuantity,
  94. StartTime=it.StartTime,
  95. Status=it.Status,
  96. WorkOrderNo=it.WorkOrderNo,
  97. CompletedQuantity=SqlFunc.Subqueryable<SnInfo>().Where(s=>s.WorkOrderNo==it.WorkOrderNo).Count() }).ToList();
  98. }
  99. }
  100. public static bool InsertWorkOrderInfo(WorkOrderInfo info)
  101. {
  102. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  103. {
  104. return true;
  105. }
  106. return false;
  107. }
  108. public static bool UpdateWorkOrderInfo(WorkOrderInfo info)
  109. {
  110. if ( Db.Updateable(info).Where(it => it.WorkOrderNo == info.WorkOrderNo).ExecuteCommand() == 1 )
  111. {
  112. return true;
  113. }
  114. return false;
  115. }
  116. public static bool DeleteWorkOrderInfo(WorkOrderInfo info)
  117. {
  118. if ( Db.Deleteable(info).Where(it => it.WorkOrderNo == info.WorkOrderNo).ExecuteCommand() == 1 )
  119. {
  120. return true;
  121. }
  122. return false;
  123. }
  124. public static List<SnInfo> SelectSnInfo(string workOrderNo = null, bool needused = false, bool isused = false)
  125. {
  126. if ( workOrderNo != null )
  127. {
  128. if ( needused )
  129. {
  130. return Db.Queryable<SnInfo>().Where(it => it.WorkOrderNo == workOrderNo && it.IsUsed == isused).ToList();
  131. }
  132. else
  133. {
  134. return Db.Queryable<SnInfo>().Where(it => it.WorkOrderNo == workOrderNo).ToList();
  135. }
  136. }
  137. else
  138. {
  139. return Db.Queryable<SnInfo>().ToList();
  140. }
  141. }
  142. public static bool InsertSnInfo(SnInfo info)
  143. {
  144. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  145. {
  146. return true;
  147. }
  148. return false;
  149. }
  150. public static bool InsertSnInfos(List<SnInfo> info)
  151. {
  152. if (Db.Storageable(info).ExecuteCommand() == info.Count)
  153. {
  154. return true;
  155. }
  156. return false;
  157. }
  158. public static bool UpdateSnInfo(SnInfo info)
  159. {
  160. if ( Db.Updateable(info).Where(it => it.Sn == info.Sn).ExecuteCommand() == 1 )
  161. {
  162. return true;
  163. }
  164. return false;
  165. }
  166. public static bool DeleteSnInfo(SnInfo info)
  167. {
  168. if ( Db.Deleteable(info).Where(it => it.Sn == info.Sn).ExecuteCommand() == 1 )
  169. {
  170. return true;
  171. }
  172. return false;
  173. }
  174. public static List<BindRecord> SelectBindRecord(string Sn = null, string PartNum = null, string StationCode = null)
  175. {
  176. var query = Db.Queryable<BindRecord>();
  177. // 动态添加查询条件
  178. if ( !string.IsNullOrWhiteSpace(Sn) )
  179. {
  180. query = query.Where(it => it.Sn == Sn);
  181. }
  182. if ( !string.IsNullOrWhiteSpace(PartNum) )
  183. {
  184. query = query.Where(it => it.PartNum == PartNum);
  185. }
  186. if ( !string.IsNullOrWhiteSpace(StationCode) )
  187. {
  188. query = query.Where(it => it.StationCode == StationCode);
  189. }
  190. return query.ToList();
  191. }
  192. public static bool InsertBindRecord(BindRecord info)
  193. {
  194. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  195. {
  196. return true;
  197. }
  198. return false;
  199. }
  200. public static bool UpdateBindRecord(BindRecord info)
  201. {
  202. if ( Db.Updateable(info).Where(it => it.Sn == info.Sn).ExecuteCommand() == 1 )
  203. {
  204. return true;
  205. }
  206. return false;
  207. }
  208. public static bool DeleteBindRecord(string sn = null)
  209. {
  210. if ( sn != null )
  211. {
  212. if ( Db.Deleteable<BindRecord>().Where(it => it.Sn == sn).ExecuteCommand() == 1 )
  213. {
  214. return true;
  215. }
  216. }
  217. else
  218. {
  219. if ( Db.Deleteable<BindRecord>().ExecuteCommand() != 0 )
  220. {
  221. return true;
  222. }
  223. }
  224. return false;
  225. }
  226. public static List<ProcessRecord> SelectProcessRecord(string Sn = null, string StationCode = null)
  227. {
  228. var query = Db.Queryable<ProcessRecord>();
  229. // 动态添加查询条件
  230. if ( !string.IsNullOrWhiteSpace(Sn) )
  231. {
  232. query = query.Where(it => it.Sn == Sn);
  233. }
  234. if ( !string.IsNullOrWhiteSpace(StationCode) )
  235. {
  236. query = query.Where(it => it.Station == StationCode);
  237. }
  238. return query.ToList();
  239. }
  240. public static bool InsertProcessRecord(ProcessRecord info)
  241. {
  242. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  243. {
  244. return true;
  245. }
  246. return false;
  247. }
  248. public static bool UpdateProcessRecord(ProcessRecord info)
  249. {
  250. if ( Db.Updateable(info).Where(it => it.Sn == info.Sn).ExecuteCommand() == 1 )
  251. {
  252. return true;
  253. }
  254. return false;
  255. }
  256. public static bool DeleteProcessRecord(string sn = null)
  257. {
  258. if ( Db.Deleteable<ProcessRecord>().Where(it => it.Sn == sn).ExecuteCommand() == 1 )
  259. {
  260. return true;
  261. }
  262. return false;
  263. }
  264. public static List<LocalhostPartInfo> SelectPartInfo(StationType StationCode = StationType.OP10)
  265. {
  266. var query = Db.Queryable<LocalhostPartInfo>().Where(it => it.FormulaType == StationCode); ;
  267. return query.ToList();
  268. }
  269. public static bool InsertPartInfo(LocalhostPartInfo info)
  270. {
  271. if (Db.Storageable(info).ExecuteCommand() == 1)
  272. {
  273. return true;
  274. }
  275. return false;
  276. }
  277. public static bool DeletePartInfo(StationType StationCode, string MaterialCode)
  278. {
  279. if (Db.Deleteable<LocalhostPartInfo>().Where(it => it.FormulaType == StationCode&&it.MaterialCode==MaterialCode).ExecuteCommand() == 1)
  280. {
  281. return true;
  282. }
  283. return false;
  284. }
  285. public static List<ParkingLot> SelectParkingLot(StationType StationCode = StationType.OP10)
  286. {
  287. var query = Db.Queryable<ParkingLot>().Where(it => it.FormulaType == StationCode); ;
  288. return query.ToList();
  289. }
  290. public static bool InsertParkingLot(ParkingLot info)
  291. {
  292. if (Db.Storageable(info).ExecuteCommand() == 1)
  293. {
  294. return true;
  295. }
  296. return false;
  297. }
  298. public static bool DeleteParkingLot(StationType StationCode, int id)
  299. {
  300. if (Db.Deleteable<ParkingLot>().Where(it => it.FormulaType == StationCode && it.Id == id).ExecuteCommand() == 1)
  301. {
  302. return true;
  303. }
  304. return false;
  305. }
  306. }
  307. }