DBDataHelper.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using SqlSugar;
  2. namespace ProximaAnalizer.Helpers;
  3. internal class DBDataHelper(SqlSugarCustom sqlSugarCustom)
  4. {
  5. public bool GetRecipeSteps(string? guid, out string reason, out List<RecipeStepData>? recipeSteps)
  6. {
  7. recipeSteps = default;
  8. reason = string.Empty;
  9. if (string.IsNullOrEmpty(guid))
  10. return false;
  11. if (sqlSugarCustom.Client is null)
  12. return false;
  13. try
  14. {
  15. recipeSteps = sqlSugarCustom.Client.Queryable<RecipeStepData>().AS("recipe_step_data")
  16. .Where(t => t.Process_Data_Guid == guid)
  17. .OrderBy(t => t.Step_Begin_Time).ToList();
  18. }
  19. catch
  20. {
  21. reason = "recipe_step_data 记录不存在";
  22. return false;
  23. }
  24. if (recipeSteps.Count == 0)
  25. {
  26. reason = "recipe_step 步骤数量为0";
  27. return false;
  28. }
  29. return true;
  30. }
  31. public bool GetFirstData(DateTime beginTime, out string reason, out Dictionary<string, object>? temp)
  32. {
  33. temp = default;
  34. reason = string.Empty;
  35. if (sqlSugarCustom.Client is null)
  36. return false;
  37. dynamic pm;
  38. dynamic system;
  39. try
  40. {
  41. pm = sqlSugarCustom.Client.Queryable<dynamic>().AS($"\"{beginTime:yyyyMMdd}.PM1\"").First();
  42. system = sqlSugarCustom.Client.Queryable<dynamic>().AS($"\"{beginTime:yyyyMMdd}.System\"").First();
  43. }
  44. catch
  45. {
  46. reason = $"{beginTime:yyyyMMdd} 记录不存在";
  47. return false;
  48. }
  49. if (pm is not IDictionary<string, object> input)
  50. return false;
  51. GeneralProcessData processData = new();
  52. if (!processData.ToDictionary(input, out Dictionary<string, object>? output) || output is null)
  53. return false;
  54. if (output["PM1"] is not IDictionary<string, object> pmData)
  55. return false;
  56. if (!processData.ToDictionary(system, out Dictionary<string, object>? systemDic) || systemDic is null)
  57. return false;
  58. temp = [];
  59. Dictionary<string, object> PM1 = [];
  60. Dictionary<string, object> System = [];
  61. temp.Add("PM1", PM1);
  62. temp.Add("System", System);
  63. GeneralProcessData.CreateTablePM(pmData, PM1);
  64. GeneralProcessData.CreateTableSystem(systemDic!, System);
  65. return true;
  66. }
  67. public DateTime? StartTime { get; internal set; }
  68. public DateTime? EndTime { get; internal set; }
  69. private ObjectFuncModel? _whereFunc;
  70. public void SetTimeRange(DateTime startTime, DateTime endTime)
  71. {
  72. this.StartTime = startTime;
  73. this.EndTime = endTime;
  74. _whereFunc = ObjectFuncModel.Create("Format", "time", ">", "{long}:" + $"{startTime.Ticks}", "&&", "time", "<", "{long}:" + $"{endTime.Ticks}");
  75. }
  76. public bool GetPMData(out List<dynamic>? pm1)
  77. {
  78. pm1 = null;
  79. if (this._whereFunc is null)
  80. return false;
  81. if (sqlSugarCustom.Client is null)
  82. return false;
  83. try
  84. {
  85. pm1 = sqlSugarCustom.Client
  86. .Queryable<dynamic>()
  87. .AS($"\"{StartTime:yyyyMMdd}.PM1\"")
  88. .Where(_whereFunc).ToList();
  89. }
  90. catch
  91. {
  92. return false;
  93. }
  94. return true;
  95. }
  96. public bool GetPMData(IEnumerable<string> fields, out List<dynamic>? pm1)
  97. {
  98. pm1 = null;
  99. if (this._whereFunc is null)
  100. return false;
  101. if (sqlSugarCustom.Client is null)
  102. return false;
  103. List<SelectModel> selectModels = [];
  104. foreach (var field in fields)
  105. selectModels.Add(new() { FieldName = $"\"{field}\"" });
  106. try
  107. {
  108. pm1 = sqlSugarCustom.Client
  109. .Queryable<dynamic>()
  110. .AS($"\"{StartTime:yyyyMMdd}.PM1\"")
  111. .Select(selectModels)
  112. .Where(_whereFunc).ToList();
  113. }
  114. catch
  115. {
  116. return false;
  117. }
  118. return true;
  119. }
  120. public bool GetSystemData(out List<dynamic>? systemData)
  121. {
  122. systemData = default;
  123. if (this._whereFunc is null)
  124. return false;
  125. if (sqlSugarCustom.Client is null)
  126. return false;
  127. try
  128. {
  129. systemData = sqlSugarCustom.Client
  130. .Queryable<dynamic>()
  131. .AS($"\"{StartTime:yyyyMMdd}.System\"")
  132. .Where(_whereFunc).ToList();
  133. }
  134. catch
  135. {
  136. return false;
  137. }
  138. return true;
  139. }
  140. public bool GetSystemData(IEnumerable<string> fields, out List<dynamic>? systemData)
  141. {
  142. systemData = default;
  143. if (this._whereFunc is null)
  144. return false;
  145. if (sqlSugarCustom.Client is null)
  146. return false;
  147. List<SelectModel> selectModels = [];
  148. foreach (var field in fields)
  149. selectModels.Add(new() { FieldName = $"\"{field}\"" });
  150. try
  151. {
  152. systemData = sqlSugarCustom.Client
  153. .Queryable<dynamic>()
  154. .AS($"\"{StartTime:yyyyMMdd}.System\"")
  155. .Select(selectModels)
  156. .Where(_whereFunc).ToList();
  157. }
  158. catch
  159. {
  160. return false;
  161. }
  162. return true;
  163. }
  164. public bool GetAlarmData(out List<EventData>? alarm)
  165. {
  166. alarm = default;
  167. if (this._whereFunc is null)
  168. return false;
  169. if (sqlSugarCustom.Client is null)
  170. return false;
  171. alarm = sqlSugarCustom.Client
  172. .Queryable<EventData>()
  173. .AS($"event_data")
  174. .Where(t => t.Occur_Time >= this.StartTime && t.Occur_Time <= this.EndTime && (t.Level == "Alarm" || t.Level == "Warning")).ToList();
  175. return alarm is not null;
  176. }
  177. }