From c0b8009ef36746c7a14606267662570e8c59c2c6 Mon Sep 17 00:00:00 2001
From: xcd <834800634@qq.com>
Date: 星期四, 09 七月 2020 19:12:37 +0800
Subject: [PATCH] 部分模型修改

---
 src/Bro.Common.Model/Model/IOItem.cs |  191 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 186 insertions(+), 5 deletions(-)

diff --git a/src/Bro.Common.Model/Model/IOItem.cs b/src/Bro.Common.Model/Model/IOItem.cs
index 64a2bca..14a5754 100644
--- a/src/Bro.Common.Model/Model/IOItem.cs
+++ b/src/Bro.Common.Model/Model/IOItem.cs
@@ -1,30 +1,211 @@
 锘縰sing Bro.Common.Helper;
+using Newtonsoft.Json;
 using System;
+using System.Collections;
 using System.Collections.Generic;
+using System.ComponentModel;
+using System.Globalization;
 using static Bro.Common.Helper.EnumHelper;
 
 namespace Bro.Common.Model
 {
-    public class IOItem : IComplexDisplay
+    public class IOItem : IComplexDisplay, IEqualityComparer
     {
         /// <summary>
         /// IO鐐圭紪鍙�
         /// </summary>
-        public int IONum { get; set; }
+        [Category("IO閰嶇疆")]
+        [Description("IO鐐圭紪鍙�")]
+        public virtual int IONum { get; set; }
 
         /// <summary>
         /// IO鐐圭殑鍊�
         /// </summary>
-        public int Value { get; set; }
+        [Category("IO閰嶇疆")]
+        [Description("IO鐘舵��")]
+        [TypeConverter(typeof(EnumDescriptionConverter<IOValue>))]
+        public virtual IOValue Value { get; set; }
 
         /// <summary>
         /// IO鐐规槸in杩樻槸out
         /// </summary>   
-        public IOModel Model { get; set; }
+        [Category("IO閰嶇疆")]
+        [Description("IO绫诲瀷")]
+        public virtual IOType IOType { get; set; }
+
+        public new bool Equals(object x, object y)
+        {
+            if (x is IOItem a && y is IOItem b)
+            {
+                return a.IOType == b.IOType && a.IONum == b.IONum;
+            }
+
+            return false;
+        }
+
+        public virtual string GetDisplayText()
+        {
+            return $"{IOType.GetEnumDescription()}-{IONum}-{Value.GetEnumDescription()}";
+        }
+
+        public int GetHashCode(object obj)
+        {
+            return obj.GetHashCode();
+        }
+    }
+
+    public class IODefinition : IOItem
+    {
+        [Category("IO閰嶇疆")]
+        [Description("IO棰勫畾涔�")]
+        [TypeConverter(typeof(EnumDescriptionConverter<IOPrestatement>))]
+        public IOPrestatement IOPreStatement { get; set; } = IOPrestatement.Customized;
+
+        [Category("IO閰嶇疆")]
+        [Description("IO鐢ㄩ�旀弿杩�")]
+        public string IODesc { get; set; }
+
+        [Category("IO閰嶇疆")]
+        [Description("澶囨敞璇存槑")]
+        public string Remark { get; set; }
+
+        [Browsable(false)]
+        [JsonIgnore]
+        public override IOValue Value { get; set; }
+
+        public override string GetDisplayText()
+        {
+            return $"{(IOPreStatement == IOPrestatement.Customized ? IODesc : IOPreStatement.GetEnumDescription())} {IOType.GetEnumDescription()} {IONum}";
+        }
+    }
+
+    public class IORefrenceItem : IComplexDisplay
+    {
+        [Category("IO鎿嶄綔閰嶇疆")]
+        [Description("闇�瑕佹搷浣滅殑IO")]
+        [TypeConverter(typeof(IORefrenceItemSourceConverter))]
+        public IODefinition IOItem { get; set; } = new IODefinition();
+
+        [Category("IO鎿嶄綔閰嶇疆")]
+        [Description("闇�瑕佹搷浣滅殑IO鐨勭姸鎬�")]
+        public IOValue CheckValue
+        {
+            get => IOItem.Value;
+            set => IOItem.Value = value;
+        }
+
+        [Browsable(false)]
+        [JsonIgnore]
+        public List<IODefinition> IOItemSource { get; set; } = new List<IODefinition>();
 
         public string GetDisplayText()
         {
-            return $"{Model.GetEnumDescription()},缂栧彿{IONum}锛孖O鐐圭殑鍊納Value}";
+            return IOItem.IODesc + "-" + CheckValue.GetEnumDescription();
+        }
+    }
+
+    public class IORefrenceItemSourceConverter : TypeConverter
+    {
+        Dictionary<IODefinition, string> itemDict = new Dictionary<IODefinition, string>();
+        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
+        {
+            if (context.Instance is IORefrenceItem item)
+            {
+                item.IOItemSource.ForEach(i =>
+                {
+                    IODefinition temp = new IODefinition();
+                    temp.DataFrom(i);
+                    itemDict[temp] = (string.IsNullOrWhiteSpace(i.IODesc) ? i.IOPreStatement.GetEnumDescription() : i.IODesc) + "|" + i.IOType.GetEnumDescription() + "|" + i.IONum;
+                });
+
+                return new StandardValuesCollection(itemDict.Keys);
+            }
+
+            return base.GetStandardValues(context);
+        }
+        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
+        {
+            return true;
+        }
+
+        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
+        {
+            return true;
+        }
+
+        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
+        {
+            if (sourceType == typeof(String))
+                return true;
+
+            return base.CanConvertFrom(context, sourceType);
+        }
+
+        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
+        {
+            return base.CanConvertTo(context, destinationType);
+        }
+
+        public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
+        {
+            return base.CreateInstance(context, propertyValues);
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="context"></param>
+        /// <param name="culture"></param>
+        /// <param name="value">string</param>
+        /// <returns></returns>
+        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
+        {
+            var s = value.ToString().Split(new char[] { '|' });
+
+            if (s.Length == 3)
+            {
+                foreach (KeyValuePair<IODefinition, string> pair in itemDict)
+                {
+                    if (pair.Value == value.ToString())
+                    {
+                        return pair.Key;
+                    }
+                }
+
+                IODefinition item = new IODefinition();
+                item.IODesc = s[0];
+                item.IOType = (IOType)Enum.Parse(typeof(IOType), s[1]);
+                item.IONum = int.Parse(s[2]);
+                return item;
+            }
+
+            return base.ConvertFrom(context, culture, value);
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="context"></param>
+        /// <param name="culture"></param>
+        /// <param name="value">IOItem</param>
+        /// <param name="destinationType"></param>
+        /// <returns></returns>
+        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
+        {
+            if (value is IODefinition item)
+            {
+                foreach (KeyValuePair<IODefinition, string> pair in itemDict)
+                {
+                    if (pair.Key.IOType == item.IOType && pair.Key.IONum == item.IONum)
+                    {
+                        return pair.Value;
+                    }
+                }
+
+                return item.IODesc + "-" + item.IOType.GetEnumDescription() + "-" + item.IONum;
+            }
+
+            return base.ConvertTo(context, culture, value, destinationType);
         }
     }
 }

--
Gitblit v1.8.0