using Dapper; using Hydro.CommonBase; using MetroFramework; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; namespace Hydro.ConfigModel { public class LogicPoint { public dict History = null; public static dict Compare(LogicPoint host1, LogicPoint host2,out bool Sameflag,out bool Sameflag_Calc) { dict di=new dict(); Sameflag = true; Sameflag_Calc = true; //比较host1和host2的所有属性,并记录不同的属性,按照以下格式记录:"属性名(old)":host1的属性值(old) "属性名(new)":host2的属性值(new),如果属性值相同,则不记录,例如:di.Add("Name (old)", host1.Name.ToString()); di.Add("Name (new)", host2.Name.ToString()); if (host1.Name != host2.Name) { Sameflag_Calc=Sameflag = false; di.Add("Name (old)", host1.Name); di.Add("Name (new)", host2.Name); } if (host1.LogicType != host2.LogicType) { Sameflag_Calc = Sameflag = false; di.Add("LogicType (old)", host1.LogicType); di.Add("LogicType (new)", host1.LogicType); } if (host1.ExpressionType != host2.ExpressionType) { Sameflag_Calc = Sameflag = false; di.Add("ExpressionType (old)", host1.ExpressionType); di.Add("ExpressionType (new)", host2.ExpressionType); } if (host1.IndicatorType != host2.IndicatorType) { Sameflag_Calc = Sameflag = false; di.Add("IndicatorType (old)", host1.IndicatorType); di.Add("IndicatorType (new)", host2.IndicatorType); } if (host1.ModelObjectID != host2.ModelObjectID) { Sameflag_Calc = Sameflag = false; di.Add("ModelObjectID (old)", host1.ModelObjectID); di.Add("ModelObjectID (new)", host2.ModelObjectID); } //if (host1.Sort != host2.Sort) { flag = false; di.Add("Sort", host1.Sort); } if (host1.ParentId != host2.ParentId) { Sameflag_Calc = Sameflag = false; di.Add("ParentId (old)", host1.ParentId); di.Add("ParentId (new)", host2.ParentId); } if (host1.Expression != host2.Expression) { Sameflag_Calc = Sameflag = false; di.Add("Expression (old)", host1.Expression); di.Add("Expression (new)", host2.Expression); } //if (!isShowVisual) //{ //} //else //{ // host1.ExtJson=JsonConvert.SerializeObject(host1.Ext); // host2.ExtJson=JsonConvert.SerializeObject(host2.Ext); // if (host1.ExtJson != host2.ExtJson) { Sameflag = false; di.Add("ExtJson (old)", host1.ExtJson); di.Add("ExtJson (new)", host2.ExtJson); } //} if (host1.ObjectType != host2.ObjectType) { Sameflag_Calc = Sameflag = false; di.Add("ObjectType (old)", host1.ObjectType); di.Add("ObjectType (new)", host2.ObjectType); } if (host1.accurary != host2.accurary) { Sameflag_Calc = Sameflag = false; di.Add("accurary (old)", host1.accurary); di.Add("accurary (new)", host2.accurary); } if (host1.childSolution != host2.childSolution) { Sameflag_Calc = Sameflag = false; di.Add("childSolution (old)", host1.childSolution); di.Add("childSolution (new)", host2.childSolution); } if (host1.childTag != host2.childTag) { Sameflag_Calc = Sameflag = false; di.Add("childTag (old)", host1.childTag); di.Add("childTag (new)", host2.childTag); } if (host1.childType != host2.childType) { Sameflag_Calc = Sameflag = false; di.Add("childType (old)", host1.childType); di.Add("childType (new)", host2.childType); } if (host1.SaveAcc != host2.SaveAcc) { Sameflag_Calc = Sameflag = false; di.Add("SaveAcc (old)", host1.SaveAcc); di.Add("SaveAcc (new)", host2.SaveAcc); } if (host1.isSave != host2.isSave) { Sameflag_Calc = Sameflag = false; di.Add("isSave (old)", host1.isSave); di.Add("isSave (new)", host2.isSave); } if (host1.SaveRange != host2.SaveRange) { Sameflag_Calc = Sameflag = false; di.Add("SaveRange (old)", host1.SaveRange); di.Add("SaveRange (new)", host2.SaveRange); } if (host1.isMemSave != host2.isMemSave) { Sameflag_Calc = Sameflag = false; di.Add("isMemSave (old)", host1.isMemSave); di.Add("isMemSave (new)", host2.isMemSave); } if (host1.MemSaveRange != host2.MemSaveRange) { Sameflag_Calc = Sameflag = false; di.Add("MemSaveRange (old)", host1.MemSaveRange); di.Add("MemSaveRange (new)", host2.MemSaveRange); } if (host1.MemSaveAcc != host2.MemSaveAcc) { Sameflag_Calc = Sameflag = false; di.Add("MemSaveAcc (old)", host1.MemSaveAcc); di.Add("MemSaveAcc (new)", host2.MemSaveAcc); } if (host1.MemSaveIndex != host2.MemSaveIndex) { Sameflag_Calc = Sameflag = false; di.Add("MemSaveIndex (old)", host1.MemSaveIndex); di.Add("MemSaveIndex (new)", host2.MemSaveIndex); } if (host1.Enable != host2.Enable) { Sameflag_Calc = Sameflag = false; di.Add("Enable (old)", host1.Enable); di.Add("Enable (new)", host2.Enable); } if (host1.Tag != host2.Tag) { Sameflag_Calc = Sameflag = false; di.Add("Tag (old)", host1.Tag); di.Add("Tag (new)", host2.Tag); } if (host1.Prefer != host2.Prefer) { Sameflag = false; di.Add("Prefer (old)", host1.Prefer); di.Add("Prefer (new)", host2.Prefer); } if (host1.Visible != host2.Visible) { Sameflag = false; di.Add("Visible (old)", host1.Visible); di.Add("Visible (new)", host2.Visible); } if (host1.isUseCustomColor != host2.isUseCustomColor) { Sameflag = false; di.Add("isUseCustomColor (old)", host1.isUseCustomColor); di.Add("isUseCustomColor (new)", host2.isUseCustomColor); } if (host1.ContentColor != host2.ContentColor) { Sameflag = false; di.Add("ContentColor (old)", host1.ContentColor); di.Add("ContentColor (new)", host2.ContentColor); } return di; } public static string selectColumn = "Id, Name, LogicType, ExpressionType, IndicatorType, ModelObjectID, Sort, ParentId, Expression, ExtJson"; public void updateByhost(LogicPoint host,bool OnlyUpdateShowProperty) { if (!OnlyUpdateShowProperty) { //更新host的所有属性到当前对象 this.Name = host.Name; this.LogicType = host.LogicType; this.ExpressionType = host.ExpressionType; this.IndicatorType = host.IndicatorType; this.ModelObjectID = host.ModelObjectID; this.Sort = host.Sort; this.ParentId = host.ParentId; this.Expression = host.Expression; //host.ExtJson = JsonConvert.SerializeObject(host.Ext); //this.ExtJson = host.ExtJson; //this._ext = null; this.accurary = host.accurary; this.childSolution = host.childSolution; this.childTag = host.childTag; this.childType = host.childType; this.SaveAcc = host.SaveAcc; this.isSave = host.isSave; this.SaveRange = host.SaveRange; this.isMemSave = host.isMemSave; this.MemSaveRange = host.MemSaveRange; this.MemSaveAcc = host.MemSaveAcc; this.MemSaveIndex = host.MemSaveIndex; this.Enable = host.Enable; this.Tag = host.Tag; this.ObjectType = host.ObjectType; this.VPN = host.VPN; } this.Visible = host.Visible; this.Prefer = host.Prefer; this.isUseCustomColor = host.isUseCustomColor; this.ContentColor = host.ContentColor; this.ExtJson = JsonConvert.SerializeObject(this.Ext); //this.ExtJson = host.ExtJson; } public int saveToDb(Db Db, bool delete = false, bool isSetID = true, bool isEdit = true, bool onlyUpdateShowProperty = false) { var host = this; host.ExtJson = JsonConvert.SerializeObject(host.Ext); int Exec_Num = 0; if (!delete) { if (isEdit) { if (onlyUpdateShowProperty) { //只更新显示属性,将原LogicPoint查询出来,然后更新显示属性 var host0 = ((List)Db.Connection.Query( $"SELECT {LogicPoint.selectColumn} FROM RemoteHost WHERE Id={host.Id}"))[0]; host0.updateByhost(host, true); host = host0; } Exec_Num = Db.Connection.Execute( @"UPDATE RemoteHost SET Name=@Name,LogicType=@LogicType,ExpressionType=@ExpressionType,IndicatorType=@IndicatorType,ModelObjectID=@ModelObjectID,Sort=@Sort,ParentId=@ParentId, Expression=@Expression,ExtJson=@ExtJson,VPN=@VPN WHERE Id=" + host.Id, host); } if (Exec_Num == 0)//没有成功更新,则插入 { if (isSetID) { Exec_Num = Db.Connection.Execute( @"INSERT INTO RemoteHost(Id,Name,LogicType,ExpressionType,IndicatorType,ModelObjectID,Sort,ParentId,Expression,ExtJson,VPN) VALUES(@Id,@Name,@LogicType,@ExpressionType,@IndicatorType,@ModelObjectID,@Sort,@ParentId,@Expression,@ExtJson,@VPN)", host); return host.Id; } else { Exec_Num = Db.Connection.Execute( @"INSERT INTO RemoteHost(Name,LogicType,ExpressionType,IndicatorType,ModelObjectID,Sort,ParentId,Expression,ExtJson,VPN) VALUES(@Name,@LogicType,@ExpressionType,@IndicatorType,@ModelObjectID,@Sort,@ParentId,@Expression,@ExtJson,@VPN)", host); var ids = (List)Db.Connection.Query( $"SELECT Max(Id) FROM RemoteHost"); host.Id = ids[0]; return host.Id; } } return host.Id; } else { Db.Connection.Execute("DELETE FROM RemoteHost WHERE Id=" + host.Id, " VACUUM"); return 0; } return host.Id; } public void UpdateByOldVersion() { var host = this; if (host.LogicType == "集合") { List list = new List() { "最大值", "最小值", "平均值", "累计值", "计数" }; if (list.IndexOf(host.ExpressionType) >= 0) { host.accurary = host.ExpressionType; host.ExpressionType = "集合计算"; } } } public static List Columns { get { return selectColumn.Split(',').Select(item => item.Trim()).ToList(); } set { selectColumn = string.Concat(value, ','); } } [DisplayName("编号")] [Category("1、基本信息")] public int Id { get; set; } [DisplayName("名称")] [Category("1、基本信息")] public string Name { get; set; } /// /// 逻辑类型 /// [DisplayName("类型")] [Category("1、基本信息")] public string LogicType { get; set; } /// /// 数据来源类型(接口值、基准值、计算值、变化值、初始计算值、最小、最大、∈、∉) /// [DisplayName("数据来源")] [Category("2、计算参数")] public string ExpressionType { get; set; } /// /// 数值表达式(接口值、基准值) /// [DisplayName("值")] [Category("2、计算参数")] public string Expression { get; set; } private string _fullAddress; [Browsable(false)] public string FullAddress => _fullAddress ?? (_fullAddress = ExpressionType == "3389" ? LogicType : $"{LogicType}:{ExpressionType}"); [Browsable(false)] public string ExpressionToShow { get { if (ExpressionType == null) return null; if (ExpressionType=="集合计算") { return ExpressionType + ":" + accurary; } else if (ExpressionType.IndexOf("接口") >= 0 ) { return ExpressionType + " " + Expression; } else if (ExpressionType.IndexOf("基准值") >= 0 || ExpressionType.IndexOf("计算值") >= 0 || ExpressionType.IndexOf("变化值") >= 0 || ExpressionType.IndexOf("初始计算值") >= 0) { return ExpressionType + " " + Expression; } else if (ExpressionType.IndexOf("值") >= 0 || ExpressionType.IndexOf("系数") >= 0) { return ExpressionType; } else { return $"{ExpressionType} " + (ExpressionType == "∈" ? $"[{Expression}] ▲{accurary}" : (ExpressionType == "最小" || ExpressionType == "最大" ? $"| 容许范围[{Expression}]" : Expression)); } } } [DisplayName("监测类型")] [Category("1、基本信息")] public string IndicatorType { get; set; } [DisplayName("模型编号或内容")] [Category("2、计算参数")] public string ModelObjectID { get; set; } [DisplayName("排序")] [Category("4、其他")] public int Sort { get; set; } [DisplayName("父节点编号")] [Category("4、其他")] public int ParentId { get; set; } [Browsable(false)] public string ExtJson { get; set; } = ""; private HostExt _ext; [Browsable(false)] public HostExt Ext { get { if (_ext == null) { try { _ext = JsonConvert.DeserializeObject(ExtJson); if (_ext == null) _ext = new HostExt(); return _ext; } catch { return (_ext=new HostExt()); } } else return _ext;// ?? } } [Browsable(false)] public byte[] VPN { get; set; } = null; [DisplayName("结果")] [Category("2、计算参数")] public string Result { get { string result = null; if (Ext==null || Ext.Result_Arr == null) { return "-"; } int 小数位数 = Ext.小数位数; if (Ext.ResultShowType==0) { if (LogicType=="试算参数" ) { try { var arr = Expression.Split(',').Select(v => Convert.ToDouble(v)).ToArray(); var range = new DRange(arr[0], arr[1]); var accuracy = double.Parse(accurary); result = getFormat(getRoundByAccuracy(Ext.Result_Arr[Ext.Period], range,accuracy)); } catch { result = "-"; } } else if ( Ext.Result_Arr.Length> Ext.Period && !double.IsNaN( Ext.Result_Arr[Ext.Period])) { result= getFormat(Math.Round(Ext.Result_Arr[Ext.Period],小数位数)); } else { result= "-"; } } else if (Ext.ResultShowType == 1) { int LengthLimit = 16; //GlobalConfig.LengthLimit??16; string txt = Ext.Result_Arr.ToList().Select(v0 => getFormat(Math.Round( v0, 小数位数))).Aggregate((a, b) => a + "," + b); //最长保留16个字符 if (txt.Length > LengthLimit) txt = txt.Substring(0, LengthLimit) + "..."; result= txt; } if (Ext.bestObj!=null) { result += Ext.bestObj; } return result; } } private double getRoundByAccuracy(double v, DRange range, double accuracy) { //精度为accuracy的倍数,且在range范围内,最小值为range.Min,取最接近v的值 if (accuracy == 0) return v; if (v < range.Min) return range.Min; if (v > range.Max) v=range.Max; var delta = v - range.Min; //比较v - delta % accuracy和v+accuracy-delta % accuracy哪个接近v,且在range范围内 double value = 0; if (delta % accuracy>accuracy/2) { value = v + accuracy - delta % accuracy; } else { value = v - delta % accuracy; } return Math.Round(value,5); //return v - delta % accuracy; } private string getFormat(double value) { if (Ext.ResultFormat == null) return value.ToString(); if (Ext.ResultFormat.ContainsKey(value)) { return Ext.ResultFormat[value]; } else { return value.ToString(); } } public string objListString = null; public string Text_change = null; [DisplayName("对象名称")] [Category("1、基本信息")] public string ObjectType { get { return this.Ext.ObjectType; } set { this.Ext.ObjectType = value; } } [DisplayName("精度")] [Category("2、计算参数")] public string accurary { get { return this.Ext.accurary; } set { this.Ext.accurary = value; } } [DisplayName("子方案名称")] [Category("3、子方案")] public string childSolution { get { return this.Ext.childSolution; } set { this.Ext.childSolution = value; } } [DisplayName("子方案键")] [Category("3、子方案")] public string childTag { get { return this.Ext.childTag; } set { this.Ext.childTag = value; } } [DisplayName("子方案传输")] [Category("3、子方案")] public string childType { get { return this.Ext.childType; } set { this.Ext.childType = value; } } [DisplayName("子搜索精度")] [Category("3、子方案")] public string SaveAcc { get { return this.Ext.SaveAcc; } set { this.Ext.SaveAcc = value; } } [DisplayName("是否存储")] [Category("3、子方案")] public string isSave { get { return this.Ext.isSave; } set { this.Ext.isSave = value; } } [DisplayName("存储键值")] [Category("3、子方案")] public string SaveRange { get { return this.Ext.SaveRange; } set { this.Ext.SaveRange = value; } } [DisplayName("是否内存存储")] [Category("3、子方案")] public string isMemSave { get { return this.Ext.isMemSave; } set { this.Ext.isMemSave = value; } } [DisplayName("内存存储范围")] [Category("3、子方案")] public string MemSaveRange { get { return this.Ext.MemSaveRange; } set { this.Ext.MemSaveRange = value; } } [DisplayName("内存存储精度")] [Category("3、子方案")] public string MemSaveAcc { get { return this.Ext.MemSaveAcc; } set { this.Ext.MemSaveAcc = value; } } [DisplayName("内存存储次序")] [Category("3、子方案")] public string MemSaveIndex { get { return this.Ext.MemSaveIndex; } set { this.Ext.MemSaveIndex = value; } } [DisplayName("启用")] [Category("1、基本信息")] public bool Enable { get { return this.Ext.Enable; } set { this.Ext.Enable = value; } } [DisplayName("可见性")] [Category("1、基本信息")] public bool Visible { get { return this.Ext.Visible; } set { this.Ext.Visible = value; } } [DisplayName("标签")] [Category("1、基本信息")] public string Tag { get { return this.Ext.Tag; } set { this.Ext.Tag = value; } } [DisplayName("收藏")] [Category("1、基本信息")] public bool Prefer { get { return this.Ext.Prefer; } set { this.Ext.Prefer = value; } } [DisplayName("使用自定义色")] [Category("4、自定义")] public bool isUseCustomColor { get { return this.Ext.isUseCustomColor; } set { this.Ext.isUseCustomColor = value; } } [DisplayName("内容色")] [Category("4、自定义")] public MetroColorStyle_Inner ContentColor { get { return this.Ext.ContentColor; } set { this.Ext.ContentColor = value; } } } }