cloudflight
2024-06-08 970022ca59c5c22cf572c458c01e0328be85a253
Hydraulic/Hydro.ConfigModel/LogicPoint.cs
@@ -1,5 +1,9 @@
using Hydro.CommonBase;
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;
@@ -9,40 +13,184 @@
    public class LogicPoint
    {
        public dict<string, dynamic> History = null;
        public static dict<string, dynamic> Compare(LogicPoint host1, LogicPoint host2)
        public static dict<string, dynamic> Compare(LogicPoint host1, LogicPoint host2,out bool Sameflag,out bool Sameflag_Calc)
        {
            dict<string,dynamic> di=new dict<string, dynamic>();
            bool flag = true;
            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) { flag = false; di.Add("Name (old)", host1.Name); di.Add("Name (new)", host2.Name); }
            if (host1.LogicType != host2.LogicType) { flag = false; di.Add("LogicType (old)", host1.LogicType); di.Add("LogicType (new)", host1.LogicType); }
            if (host1.ExpressionType != host2.ExpressionType) { flag = false; di.Add("ExpressionType (old)", host1.ExpressionType); di.Add("ExpressionType (new)", host2.ExpressionType); }
            if (host1.IndicatorType != host2.IndicatorType) { flag = false; di.Add("IndicatorType (old)", host1.IndicatorType); di.Add("IndicatorType (new)", host2.IndicatorType); }
            if (host1.ModelObjectID != host2.ModelObjectID) { flag = false; di.Add("ModelObjectID (old)", host1.ModelObjectID); di.Add("ModelObjectID (new)", host2.ModelObjectID); }
            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) { flag = false; di.Add("ParentId (old)", host1.ParentId); di.Add("ParentId (new)", host2.ParentId); }
            if (host1.Expression != host2.Expression) { flag = false; di.Add("Expression (old)", host1.Expression); di.Add("Expression (new)", host2.Expression); }
            if (host1.ObjectType != host2.ObjectType) { flag = false; di.Add("ObjectType (old)", host1.ObjectType); di.Add("ObjectType (new)", host2.ObjectType); }
            if (host1.accurary != host2.accurary) { flag = false; di.Add("accurary (old)", host1.accurary); di.Add("accurary (new)", host2.accurary); }
            if (host1.childSolution != host2.childSolution) { flag = false; di.Add("childSolution (old)", host1.childSolution); di.Add("childSolution (new)", host2.childSolution); }
            if (host1.childTag != host2.childTag) { flag = false; di.Add("childTag (old)", host1.childTag); di.Add("childTag (new)", host2.childTag); }
            if (host1.childType != host2.childType) { flag = false; di.Add("childType (old)", host1.childType); di.Add("childType (new)", host2.childType); }
            if (host1.SaveAcc != host2.SaveAcc) { flag = false; di.Add("SaveAcc (old)", host1.SaveAcc); di.Add("SaveAcc (new)", host2.SaveAcc); }
            if (host1.isSave != host2.isSave) { flag = false; di.Add("isSave (old)", host1.isSave); di.Add("isSave (new)", host2.isSave); }
            if (host1.SaveRange != host2.SaveRange) { flag = false; di.Add("SaveRange (old)", host1.SaveRange); di.Add("SaveRange (new)", host2.SaveRange); }
            if (host1.isMemSave != host2.isMemSave) { flag = false; di.Add("isMemSave (old)", host1.isMemSave); di.Add("isMemSave (new)", host2.isMemSave); }
            if (host1.MemSaveRange != host2.MemSaveRange) { flag = false; di.Add("MemSaveRange (old)", host1.MemSaveRange); di.Add("MemSaveRange (new)", host2.MemSaveRange); }
            if (host1.MemSaveAcc != host2.MemSaveAcc) { flag = false; di.Add("MemSaveAcc (old)", host1.MemSaveAcc); di.Add("MemSaveAcc (new)", host2.MemSaveAcc); }
            if (host1.MemSaveIndex != host2.MemSaveIndex) { flag = false; di.Add("MemSaveIndex (old)", host1.MemSaveIndex); di.Add("MemSaveIndex (new)", host2.MemSaveIndex); }
            if (host1.Enable != host2.Enable) { flag = false; di.Add("Enable (old)", host1.Enable); di.Add("Enable (new)", host2.Enable); }
            if (host1.Visible != host2.Visible) { flag = false; di.Add("Visible (old)", host1.Visible); di.Add("Visible (new)", host2.Visible); }
            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<LogicPoint>)Db.Connection.Query<LogicPoint>(
                            $"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<int>)Db.Connection.Query<int>(
                $"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<string> list = new List<string>() { "最大值", "最小值", "平均值", "累计值", "计数" };
                if (list.IndexOf(host.ExpressionType) >= 0)
                {
                    host.accurary = host.ExpressionType;
                    host.ExpressionType = "集合计算";
                }
            }
        }
        public static List<string> Columns
        {
            get
@@ -54,17 +202,29 @@
            }
        }
        [DisplayName("编号")]
        [Category("1、基本信息")]
        [Category("基本信息")]
        public int Id { get; set; }
        [DisplayName("名称")]
        [Category("1、基本信息")]
        [Category("基本信息")]
        public string Name { get; set; }
        /// <summary>
        /// 逻辑类型
        /// </summary>
        [DisplayName("类型")]
        [Category("1、基本信息")]
        [Category("基本信息")]
        public string LogicType { get; set; }
        [DisplayName("值类型")]
        [Category("2、计算参数")]
        /// <summary>
        /// 数据来源类型(接口值、基准值、计算值、变化值、初始计算值、最小、最大、∈、∉)
        /// </summary>
        [DisplayName("数据来源")]
        [Category("计算参数")]
        public string ExpressionType { get; set; }
        /// <summary>
        /// 数值表达式(接口值、基准值)
        /// </summary>
        [DisplayName("值")]
        [Category("计算参数")]
        public string Expression { get; set; }
        private string _fullAddress;
        [Browsable(false)]
@@ -94,20 +254,18 @@
            }
        }
        [DisplayName("监测类型")]
        [Category("1、基本信息")]
        [Category("基本信息")]
        public string IndicatorType { get; set; }
        [DisplayName("模型编号或内容")]
        [Category("2、计算参数")]
        [Category("计算参数")]
        public string ModelObjectID { get; set; }
        [DisplayName("排序")]
        [Category("4、其他")]
        [Category("其他")]
        public int Sort { get; set; }
        [DisplayName("父节点编号")]
        [Category("4、其他")]
        [Category("其他")]
        public int ParentId { get; set; }
        [DisplayName("值")]
        [Category("2、计算参数")]
        public string Expression { get; set; }
        [Browsable(false)]
        public string ExtJson { get; set; } = "";
@@ -140,66 +298,154 @@
        [Browsable(false)]
        public byte[] VPN { get; set; } = null;
        [DisplayName("结果")]
        [Category("2、计算参数")]
        public string Result { get; set; } = null;
        [Category("计算参数")]
        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、基本信息")]
        [Category("基本信息")]
        public string ObjectType
        {
            get { return this.Ext.ObjectType; }
            set { this.Ext.ObjectType = value; }
        }
        [DisplayName("精度")]
        [Category("2、计算参数")]
        [Category("计算参数")]
        public string accurary
        {
            get { return this.Ext.accurary; }
            set { this.Ext.accurary = value; }
        }
        [DisplayName("子方案名称")]
        [Category("3、子方案")]
        [Category("子方案")]
        public string childSolution
        {
            get { return this.Ext.childSolution; }
            set { this.Ext.childSolution = value; }
        }
        [DisplayName("子方案键")]
        [Category("3、子方案")]
        [Category("子方案")]
        public string childTag
        {
            get { return this.Ext.childTag; }
            set { this.Ext.childTag = value; }
        }
        [DisplayName("子方案传输")]
        [Category("3、子方案")]
        [Category("子方案")]
        public string childType
        {
            get { return this.Ext.childType; }
            set { this.Ext.childType = value; }
        }
        [DisplayName("子搜索精度")]
        [Category("3、子方案")]
        [Category("子方案")]
        public string SaveAcc
        {
            get { return this.Ext.SaveAcc; }
            set { this.Ext.SaveAcc = value; }
        }
        [DisplayName("是否存储")]
        [Category("3、子方案")]
        [Category("子方案")]
        public string isSave
        {
            get { return this.Ext.isSave; }
            set { this.Ext.isSave = value; }
        }
        [DisplayName("存储键值")]
        [Category("3、子方案")]
        [Category("子方案")]
        public string SaveRange
        {
            get { return this.Ext.SaveRange; }
@@ -207,35 +453,35 @@
        }
        [DisplayName("是否内存存储")]
        [Category("3、子方案")]
        [Category("子方案")]
        public string isMemSave
        {
            get { return this.Ext.isMemSave; }
            set { this.Ext.isMemSave = value; }
        }
        [DisplayName("内存存储范围")]
        [Category("3、子方案")]
        [Category("子方案")]
        public string MemSaveRange
        {
            get { return this.Ext.MemSaveRange; }
            set { this.Ext.MemSaveRange = value; }
        }
        [DisplayName("内存存储精度")]
        [Category("3、子方案")]
        [Category("子方案")]
        public string MemSaveAcc
        {
            get { return this.Ext.MemSaveAcc; }
            set { this.Ext.MemSaveAcc = value; }
        }
        [DisplayName("内存存储次序")]
        [Category("3、子方案")]
        [Category("子方案")]
        public string MemSaveIndex
        {
            get { return this.Ext.MemSaveIndex; }
            set { this.Ext.MemSaveIndex = value; }
        }
        [DisplayName("启用")]
        [Category("1、基本信息")]
        [Category("基本信息")]
        public bool Enable
        {
            get { return this.Ext.Enable; }
@@ -243,12 +489,43 @@
        }
        [DisplayName("可见性")]
        [Category("1、基本信息")]
        [Category("基本信息")]
        public bool Visible
        {
            get { return this.Ext.Visible; }
            set { this.Ext.Visible = value; }
        }
        [DisplayName("标签")]
        [Category("基本信息")]
        public string Tag
        {
            get { return this.Ext.Tag; }
            set { this.Ext.Tag = value; }
        }
        [DisplayName("收藏")]
        [Category("基本信息")]
        public bool Prefer
        {
            get { return this.Ext.Prefer; }
            set { this.Ext.Prefer = value; }
        }
        [DisplayName("使用自定义色")]
        [Category("自定义")]
        public bool isUseCustomColor
        {
            get { return this.Ext.isUseCustomColor; }
            set { this.Ext.isUseCustomColor = value; }
        }
        [DisplayName("内容色")]
        [Category("自定义")]
        public MetroColorStyle_Inner ContentColor
        {
            get { return this.Ext.ContentColor; }
            set { this.Ext.ContentColor = value; }
        }
    }
}