DatabaseHelper.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. // ConnectionString= "Server = 192.168.1.26; Database =LingPaoProject; User Id = root; Password = root; ",
  20. ConnectionString = "Database=LingPaoProject; Data Source=192.168.1.26; Port=3306;User Id=root; Password=root;Charset=utf8;TreatTinyAsBoolean=false;",//连接符字串
  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. Db.CodeFirst.InitTables<WorkOrderInfo>();
  34. Db.CodeFirst.InitTables<SnInfo>();
  35. Db.CodeFirst.InitTables<BindRecord>();
  36. Db.CodeFirst.InitTables<ProcessRecord>();
  37. Db.CodeFirst.InitTables<ParkingLot>();
  38. }
  39. public static int GetProductProductionRecords(string sn)
  40. {
  41. try
  42. {
  43. return Db.Queryable<ProductionToStation>().Where(it => it.SN == sn).ToList().Count;
  44. }
  45. catch ( Exception ex )
  46. {
  47. LogHelper.WriteLogError("获取一条产品的生产记录至数据库时出错!", ex);
  48. }
  49. return 0;
  50. }
  51. public static List<WorkOrderInfo> SelectWorkOrderInfo(string workOrderNo = null,bool islocalhost=false)
  52. {
  53. if ( workOrderNo != null )
  54. {
  55. return Db.Queryable<WorkOrderInfo>().Where(it => it.WorkOrderNo == workOrderNo).ToList();
  56. }
  57. else
  58. {
  59. return Db.Queryable<WorkOrderInfo>().ToList();
  60. }
  61. }
  62. public static bool InsertWorkOrderInfo(WorkOrderInfo info)
  63. {
  64. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  65. {
  66. return true;
  67. }
  68. return false;
  69. }
  70. public static bool UpdateWorkOrderInfo(WorkOrderInfo info)
  71. {
  72. if ( Db.Updateable(info).Where(it => it.WorkOrderNo == info.WorkOrderNo).ExecuteCommand() == 1 )
  73. {
  74. return true;
  75. }
  76. return false;
  77. }
  78. public static bool DeleteWorkOrderInfo(WorkOrderInfo info)
  79. {
  80. if ( Db.Deleteable(info).Where(it => it.WorkOrderNo == info.WorkOrderNo).ExecuteCommand() == 1 )
  81. {
  82. return true;
  83. }
  84. return false;
  85. }
  86. public static List<SnInfo> SelectSnInfo(string workOrderNo = null, bool needused = false, bool isused = false)
  87. {
  88. if ( workOrderNo != null )
  89. {
  90. if ( needused )
  91. {
  92. return Db.Queryable<SnInfo>().Where(it => it.WorkOrderNo == workOrderNo && it.IsUsed == isused).ToList();
  93. }
  94. else
  95. {
  96. return Db.Queryable<SnInfo>().Where(it => it.WorkOrderNo == workOrderNo).ToList();
  97. }
  98. }
  99. else
  100. {
  101. return Db.Queryable<SnInfo>().ToList();
  102. }
  103. }
  104. public static bool InsertSnInfo(SnInfo info)
  105. {
  106. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  107. {
  108. return true;
  109. }
  110. return false;
  111. }
  112. public static bool InsertSnInfos(List<SnInfo> info)
  113. {
  114. if (Db.Storageable(info).ExecuteCommand() == info.Count)
  115. {
  116. return true;
  117. }
  118. return false;
  119. }
  120. public static bool UpdateSnInfo(SnInfo info)
  121. {
  122. if ( Db.Updateable(info).Where(it => it.Sn == info.Sn).ExecuteCommand() == 1 )
  123. {
  124. return true;
  125. }
  126. return false;
  127. }
  128. public static bool DeleteSnInfo(SnInfo info)
  129. {
  130. if ( Db.Deleteable(info).Where(it => it.Sn == info.Sn).ExecuteCommand() == 1 )
  131. {
  132. return true;
  133. }
  134. return false;
  135. }
  136. public static List<BindRecord> SelectBindRecord(string Sn = null, string PartNum = null, string StationCode = null)
  137. {
  138. var query = Db.Queryable<BindRecord>();
  139. // 动态添加查询条件
  140. if ( !string.IsNullOrWhiteSpace(Sn) )
  141. {
  142. query = query.Where(it => it.Sn == Sn);
  143. }
  144. if ( !string.IsNullOrWhiteSpace(PartNum) )
  145. {
  146. query = query.Where(it => it.PartNum == PartNum);
  147. }
  148. if ( !string.IsNullOrWhiteSpace(StationCode) )
  149. {
  150. query = query.Where(it => it.StationCode == StationCode);
  151. }
  152. return query.ToList();
  153. }
  154. public static bool InsertBindRecord(BindRecord info)
  155. {
  156. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  157. {
  158. return true;
  159. }
  160. return false;
  161. }
  162. public static bool UpdateBindRecord(BindRecord info)
  163. {
  164. if ( Db.Updateable(info).Where(it => it.Sn == info.Sn).ExecuteCommand() == 1 )
  165. {
  166. return true;
  167. }
  168. return false;
  169. }
  170. public static bool DeleteBindRecord(string sn = null)
  171. {
  172. if ( sn != null )
  173. {
  174. if ( Db.Deleteable<BindRecord>().Where(it => it.Sn == sn).ExecuteCommand() == 1 )
  175. {
  176. return true;
  177. }
  178. }
  179. else
  180. {
  181. if ( Db.Deleteable<BindRecord>().ExecuteCommand() != 0 )
  182. {
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. public static List<ProcessRecord> SelectProcessRecord(string Sn = null, string StationCode = null)
  189. {
  190. var query = Db.Queryable<ProcessRecord>();
  191. // 动态添加查询条件
  192. if ( !string.IsNullOrWhiteSpace(Sn) )
  193. {
  194. query = query.Where(it => it.Sn == Sn);
  195. }
  196. if ( !string.IsNullOrWhiteSpace(StationCode) )
  197. {
  198. query = query.Where(it => it.Station == StationCode);
  199. }
  200. return query.ToList();
  201. }
  202. public static bool InsertProcessRecord(ProcessRecord info)
  203. {
  204. if ( Db.Storageable(info).ExecuteCommand() == 1 )
  205. {
  206. return true;
  207. }
  208. return false;
  209. }
  210. public static bool UpdateProcessRecord(ProcessRecord info)
  211. {
  212. if ( Db.Updateable(info).Where(it => it.Sn == info.Sn).ExecuteCommand() == 1 )
  213. {
  214. return true;
  215. }
  216. return false;
  217. }
  218. public static bool DeleteProcessRecord(string sn = null)
  219. {
  220. if ( Db.Deleteable<ProcessRecord>().Where(it => it.Sn == sn).ExecuteCommand() == 1 )
  221. {
  222. return true;
  223. }
  224. return false;
  225. }
  226. public static List<LocalhostPartInfo> SelectPartInfo(StationType StationCode = StationType.OP10)
  227. {
  228. var query = Db.Queryable<LocalhostPartInfo>().Where(it => it.FormulaType == StationCode); ;
  229. return query.ToList();
  230. }
  231. public static bool InsertPartInfo(LocalhostPartInfo info)
  232. {
  233. if (Db.Storageable(info).ExecuteCommand() == 1)
  234. {
  235. return true;
  236. }
  237. return false;
  238. }
  239. public static bool DeletePartInfo(StationType StationCode, string MaterialCode)
  240. {
  241. if (Db.Deleteable<LocalhostPartInfo>().Where(it => it.FormulaType == StationCode&&it.MaterialCode==MaterialCode).ExecuteCommand() == 1)
  242. {
  243. return true;
  244. }
  245. return false;
  246. }
  247. public static List<ParkingLot> SelectParkingLot(StationType StationCode = StationType.OP10)
  248. {
  249. var query = Db.Queryable<ParkingLot>().Where(it => it.FormulaType == StationCode); ;
  250. return query.ToList();
  251. }
  252. public static bool InsertParkingLot(ParkingLot info)
  253. {
  254. if (Db.Storageable(info).ExecuteCommand() == 1)
  255. {
  256. return true;
  257. }
  258. return false;
  259. }
  260. public static bool DeleteParkingLot(StationType StationCode, int id)
  261. {
  262. if (Db.Deleteable<ParkingLot>().Where(it => it.FormulaType == StationCode && it.Id == id).ExecuteCommand() == 1)
  263. {
  264. return true;
  265. }
  266. return false;
  267. }
  268. }
  269. }