终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

138 lines
4.2 KiB

  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace BPASmartClient.Academy.Model
  9. {
  10. public class Sqlite
  11. {
  12. private static Sqlite instance;
  13. public static Sqlite GetInstance { get; set; } = instance ??= new Sqlite();
  14. private Sqlite() { }
  15. public List<SaveData> saveDatas { get; set; } = new List<SaveData>();
  16. public ObservableCollection<RecipeChart> recipeCharts { get; set; } = new ObservableCollection<RecipeChart>();
  17. static string directoryPath = $"AccessFile\\DB\\{Json<DevicePar>.Data.ProjectTypeName.ToString()}";
  18. static string path
  19. {
  20. get
  21. {
  22. string tempPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\DB\\{Json<DevicePar>.Data.ProjectTypeName.ToString()}");
  23. Directory.CreateDirectory(tempPath);
  24. return Path.Combine(tempPath, $"data.db");
  25. }
  26. }
  27. public SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
  28. {
  29. ConnectionString = $"Data Source = {path}",
  30. DbType = DbType.Sqlite,
  31. IsAutoCloseConnection = true,
  32. });
  33. public void FindDateList(DateTime date)
  34. {
  35. int i = 1;
  36. recipeCharts.Clear();
  37. Db.Queryable<SaveNameData>().Where(o => o.Createtime.Date == date.Date).ToList().ForEach(t =>
  38. {
  39. recipeCharts.Add(new RecipeChart()
  40. {
  41. Num = i++,
  42. CreateTime = t.Createtime,
  43. Name = t.ProductNumber,
  44. Id = t.Id
  45. });
  46. });
  47. if (recipeCharts.Count<=0)
  48. {
  49. MessageNotify.GetInstance.OpenMsg("未查询到有效记录");
  50. }
  51. }
  52. public void FindName(string Name)
  53. {
  54. int i = 1;
  55. recipeCharts.Clear();
  56. Db.Queryable<SaveNameData>().Where(o => o.ProductNumber.Contains(Name)).ToList().ForEach(t =>
  57. {
  58. recipeCharts.Add(new RecipeChart()
  59. {
  60. Num = i++,
  61. CreateTime = t.Createtime,
  62. Name = t.ProductNumber,
  63. Id = t.Id
  64. });
  65. });
  66. }
  67. public List<SaveData> FindList(RecipeChart chart)
  68. {
  69. return Db.Queryable<SaveData>().SplitTable().Where(o => o.ProductNumberId == chart.Id).ToList();
  70. }
  71. public void Init()
  72. {
  73. try
  74. {
  75. if (!File.Exists(path))
  76. {
  77. Db.DbMaintenance.CreateDatabase();
  78. Db.CodeFirst.SetStringDefaultLength(100).InitTables<SaveData>();
  79. Db.CodeFirst.InitTables<SaveNameData>();
  80. //Db.CodeFirst.SetStringDefaultLength(100).InitTables<SaveNameData>();
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. MessageNotify.GetInstance.OpenMsg("创建数据库失败");
  86. MessageNotify.GetInstance.ShowRunLog(ex.Message);
  87. }
  88. }
  89. public bool AddData(SaveData dfb)
  90. {
  91. try
  92. {
  93. return Db.Insertable(dfb).SplitTable().ExecuteCommand() > 0;
  94. }
  95. catch (Exception)
  96. {
  97. return false;
  98. }
  99. }
  100. public bool AddData(SaveNameData snd)
  101. {
  102. try
  103. {
  104. return Db.Insertable(snd).ExecuteCommand() > 0;
  105. }
  106. catch (Exception)
  107. {
  108. return false;
  109. }
  110. }
  111. public List<SaveData> SelectId(string id)
  112. {
  113. try
  114. {
  115. return Db.Queryable<SaveData>().SplitTable().Where(o => o.ProductNumberId == id).ToList();
  116. }
  117. catch (Exception)
  118. {
  119. return null;
  120. }
  121. }
  122. public List<String> SelectAllName()
  123. {
  124. return Db.Queryable<SaveData>()
  125. .Select(x => x.Name)
  126. .Distinct()
  127. .ToList();
  128. }
  129. }
  130. }