123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using SqlSugar;
- namespace ProximaAnalizer.Helpers;
- internal class DBDataHelper(SqlSugarCustom sqlSugarCustom)
- {
- public bool GetRecipeSteps(string? guid, out string reason, out List<RecipeStepData>? recipeSteps)
- {
- recipeSteps = default;
- reason = string.Empty;
- if (string.IsNullOrEmpty(guid))
- return false;
- if (sqlSugarCustom.Client is null)
- return false;
- try
- {
- recipeSteps = sqlSugarCustom.Client.Queryable<RecipeStepData>().AS("recipe_step_data")
- .Where(t => t.Process_Data_Guid == guid)
- .OrderBy(t => t.Step_Begin_Time).ToList();
- }
- catch
- {
- reason = "recipe_step_data 记录不存在";
- return false;
- }
- if (recipeSteps.Count == 0)
- {
- reason = "recipe_step 步骤数量为0";
- return false;
- }
- return true;
- }
- public bool GetFirstData(DateTime beginTime, out string reason, out Dictionary<string, object>? temp)
- {
- temp = default;
- reason = string.Empty;
- if (sqlSugarCustom.Client is null)
- return false;
- dynamic pm;
- dynamic system;
- try
- {
- pm = sqlSugarCustom.Client.Queryable<dynamic>().AS($"\"{beginTime:yyyyMMdd}.PM1\"").First();
- system = sqlSugarCustom.Client.Queryable<dynamic>().AS($"\"{beginTime:yyyyMMdd}.System\"").First();
- }
- catch
- {
- reason = $"{beginTime:yyyyMMdd} 记录不存在";
- return false;
- }
- if (pm is not IDictionary<string, object> input)
- return false;
- GeneralProcessData processData = new();
- if (!processData.ToDictionary(input, out Dictionary<string, object>? output) || output is null)
- return false;
- if (output["PM1"] is not IDictionary<string, object> pmData)
- return false;
- if (!processData.ToDictionary(system, out Dictionary<string, object>? systemDic) || systemDic is null)
- return false;
- temp = [];
- Dictionary<string, object> PM1 = [];
- Dictionary<string, object> System = [];
- temp.Add("PM1", PM1);
- temp.Add("System", System);
- GeneralProcessData.CreateTablePM(pmData, PM1);
- GeneralProcessData.CreateTableSystem(systemDic!, System);
- return true;
- }
- public DateTime? StartTime { get; internal set; }
- public DateTime? EndTime { get; internal set; }
- private ObjectFuncModel? _whereFunc;
- public void SetTimeRange(DateTime startTime, DateTime endTime)
- {
- this.StartTime = startTime;
- this.EndTime = endTime;
- _whereFunc = ObjectFuncModel.Create("Format", "time", ">", "{long}:" + $"{startTime.Ticks}", "&&", "time", "<", "{long}:" + $"{endTime.Ticks}");
- }
- public bool GetPMData(out List<dynamic>? pm1)
- {
- pm1 = null;
- if (this._whereFunc is null)
- return false;
- if (sqlSugarCustom.Client is null)
- return false;
- try
- {
- pm1 = sqlSugarCustom.Client
- .Queryable<dynamic>()
- .AS($"\"{StartTime:yyyyMMdd}.PM1\"")
- .Where(_whereFunc).ToList();
- }
- catch
- {
- return false;
- }
- return true;
- }
- public bool GetPMData(IEnumerable<string> fields, out List<dynamic>? pm1)
- {
- pm1 = null;
- if (this._whereFunc is null)
- return false;
- if (sqlSugarCustom.Client is null)
- return false;
- List<SelectModel> selectModels = [];
- foreach (var field in fields)
- selectModels.Add(new() { FieldName = $"\"{field}\"" });
- try
- {
- pm1 = sqlSugarCustom.Client
- .Queryable<dynamic>()
- .AS($"\"{StartTime:yyyyMMdd}.PM1\"")
- .Select(selectModels)
- .Where(_whereFunc).ToList();
- }
- catch
- {
- return false;
- }
- return true;
- }
- public bool GetSystemData(out List<dynamic>? systemData)
- {
- systemData = default;
- if (this._whereFunc is null)
- return false;
- if (sqlSugarCustom.Client is null)
- return false;
- try
- {
- systemData = sqlSugarCustom.Client
- .Queryable<dynamic>()
- .AS($"\"{StartTime:yyyyMMdd}.System\"")
- .Where(_whereFunc).ToList();
- }
- catch
- {
- return false;
- }
- return true;
- }
- public bool GetSystemData(IEnumerable<string> fields, out List<dynamic>? systemData)
- {
- systemData = default;
- if (this._whereFunc is null)
- return false;
- if (sqlSugarCustom.Client is null)
- return false;
- List<SelectModel> selectModels = [];
- foreach (var field in fields)
- selectModels.Add(new() { FieldName = $"\"{field}\"" });
- try
- {
- systemData = sqlSugarCustom.Client
- .Queryable<dynamic>()
- .AS($"\"{StartTime:yyyyMMdd}.System\"")
- .Select(selectModels)
- .Where(_whereFunc).ToList();
- }
- catch
- {
- return false;
- }
- return true;
- }
- public bool GetAlarmData(out List<EventData>? alarm)
- {
- alarm = default;
- if (this._whereFunc is null)
- return false;
- if (sqlSugarCustom.Client is null)
- return false;
- alarm = sqlSugarCustom.Client
- .Queryable<EventData>()
- .AS($"event_data")
- .Where(t => t.Occur_Time >= this.StartTime && t.Occur_Time <= this.EndTime && (t.Level == "Alarm" || t.Level == "Warning")).ToList();
- return alarm is not null;
- }
- }
|