林豪 左 2 tahun lalu
induk
melakukan
4aaf420eb9

+ 45 - 0
ServiceCenter/Extensions/AssemblyExtension.cs

@@ -0,0 +1,45 @@
+using System.Reflection;
+
+namespace ServiceCenter.Extensions
+{
+    /// <summary>
+    /// 针对程序集操作的扩展
+    /// </summary>
+    public static class AssemblyExtension
+    {
+        /// <summary>
+        /// 删除重复的程序集
+        /// </summary>
+        /// <param name="assemblies"></param>
+        public static IEnumerable<Assembly> RemoveDuplicateAssemblies(this IEnumerable<Assembly> assemblies)
+        {
+            return assemblies.DistinctBy(p => p.FullName);
+        }
+
+        /// <summary>
+        /// 删除指定名称的程序集
+        /// </summary>
+        /// <param name="assemblyNames"></param>
+        /// <returns></returns>
+        public static IEnumerable<AssemblyName> RemoveTheAssemblyNamespecified(this IEnumerable<AssemblyName> assemblyNames)
+        {
+            return assemblyNames.Where(p => p.Name != null && !p.Name.StartsWith("System."))
+                .Where(p => !p.Name.StartsWith("mscorlib."))
+                .Where(p => !p.Name.StartsWith("netstandard."))
+                .Where(p => !p.Name.StartsWith("Senparc."))
+                .Where(p => !p.Name.StartsWith("Newtonsoft."))
+                .Where(p => !p.Name.StartsWith("UnityEngine."))
+                .Where(p => !p.Name.StartsWith("ZKWeb."))
+                .Where(p => !p.Name.StartsWith("NPOI."))
+                .Where(p => !p.Name.StartsWith("ICSharpCode."))
+                .Where(p => !p.Name.StartsWith("NLog"))
+                .Where(p => !p.Name.StartsWith("e_sqlite"))
+                .Where(p => !p.Name.StartsWith("MsgPack."))
+                .Where(p => !p.Name.StartsWith("netstandard"))
+                .Where(p => !p.Name.StartsWith("Jiguang"))
+                .Where(p => !p.Name.StartsWith("StackExchange"))
+                .Where(p => !p.Name.StartsWith("Microsoft."))
+                .Where(p => !p.Name.StartsWith("TIBCO."));
+        }
+    }
+}

+ 27 - 0
ServiceCenter/Extensions/LinqExtension.cs

@@ -0,0 +1,27 @@
+namespace ServiceCenter.Extensions
+{
+    /// <summary>
+    /// Linq 扩展
+    /// </summary>
+    public static class LinqExtension
+    {
+        /// <summary>
+        /// 根据指定的属性返回集合中的非重复元素
+        /// </summary>
+        /// <remarks>不明白本方法是如何实现去重的</remarks>
+        /// <typeparam name="TSource">数据源的类型</typeparam>
+        /// <typeparam name="TKey">数据源需要进行筛选的属性</typeparam>
+        /// <param name="source">数据源</param>
+        /// <param name="keySelector">指定属性的函数表达式</param>
+        /// <returns></returns>
+        public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
+        {
+            var seenKeys = new HashSet<TKey>();
+            foreach (var source1 in source)
+            {
+                if (seenKeys.Add(keySelector(source1)))
+                    yield return source1;
+            }
+        }
+    }
+}

+ 460 - 0
ServiceCenter/Extensions/TypeExtension.cs

@@ -0,0 +1,460 @@
+using SqlSugar;
+using System.Collections.Concurrent;
+using System.ComponentModel;
+using System.Data;
+using System.Globalization;
+using System.Numerics;
+using System.Reflection;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace ServiceCenter.Extensions
+{
+    /// <summary>
+    /// 类型转换扩展
+    /// </summary>
+    public static class TypeExtension
+    {
+        /// <summary>
+        /// 将字符串转换为short
+        /// </summary>
+        /// <param name="value">需要转换的字符串</param>
+        /// <returns></returns>
+        public static short ToShort(this string value)
+        {
+            return Convert.ToInt16(value);
+        }
+
+        /// <summary>
+        /// 将int转换为short
+        /// </summary>
+        /// <param name="value">需要转换的字符串</param>
+        /// <returns></returns>
+        public static short ToShort(this int value)
+        {
+            return Convert.ToInt16(value);
+        }
+
+        /// <summary>
+        /// 将decimal转换为short
+        /// </summary>
+        /// <param name="value">需要转换的字符串</param>
+        /// <returns></returns>
+        public static short ToShort(this decimal value)
+        {
+            return Convert.ToInt16(value);
+        }
+
+        /// <summary>
+        /// 将字符串转换为int
+        /// </summary>
+        /// <param name="value">需要转换的字符串</param>
+        /// <returns></returns>
+        public static int ToInt(this string value)
+        {
+            return Convert.ToInt32(value);
+        }
+
+        /// <summary>
+        /// 判断值为奇数/偶数
+        /// </summary>
+        /// <param name="value">需要判断的值</param>
+        /// <returns> true:是奇数   false:是偶数</returns>
+        public static bool OddNumberOrEven(this short value)
+        {
+            return value % 2 != 0;
+        }
+
+        /// <summary>
+        /// 获取short类型Code,只限设备组
+        /// </summary>
+        /// <param name="value"></param>
+        /// <returns></returns>
+        public static short GetShortCode(this string value)
+        {
+            return value.Replace("G", "").ToShort();
+        }
+
+        /// <summary>
+        /// 数据映射
+        /// </summary>
+        /// <typeparam name="D"></typeparam>
+        /// <typeparam name="S"></typeparam>
+        /// <param name="s"></param>
+        /// <returns></returns>
+        public static D Mapper<D, S>(S s)
+        {
+            var d = Activator.CreateInstance<D>();
+
+            var sType = s?.GetType();
+            var dType = typeof(D);
+
+            foreach (var sP in sType.GetProperties())
+            {
+                foreach (var dP in dType.GetProperties())
+                {
+                    if (dP.Name == sP.Name)
+                    {
+                        dP.SetValue(d, sP.GetValue(s));
+                        break;
+                    }
+                }
+            }
+
+            return d;
+        }
+
+        /// <summary>
+        /// 获取字典
+        /// </summary>
+        /// <typeparam name="T1"></typeparam>
+        /// <typeparam name="T2"></typeparam>
+        /// <typeparam name="T3"></typeparam>
+        /// <param name="t3"></param>
+        /// <returns></returns>
+        public static Dictionary<string, object> EntityClassToDictionary<T>(T t)
+        {
+            var type = typeof(SugarColumn);
+            Dictionary<string, object> d = new Dictionary<string, object>();
+
+            var sType = t.GetType();
+            foreach (var sP in sType.GetProperties())
+            {
+                if (sP.CustomAttributes.Any(v => v.AttributeType == type) && sP.Name != "VER" && sP.Name != "ID")
+                {
+                    d.Add(sP.Name, sP.GetValue(t));
+                }
+            }
+
+            return d;
+        }
+
+        /// <summary>
+        /// 获取MD5字符串
+        /// </summary>
+        /// <param name="myString"></param>
+        /// <returns></returns>
+        public static string GetMD5(this string myString)
+        {
+            var md5 = MD5.Create();
+            var fromData = Encoding.Unicode.GetBytes(myString);
+            var targetData = md5.ComputeHash(fromData);
+            string byte2String = null;
+
+            for (var i = 0; i < targetData.Length; i++)
+            {
+                byte2String += targetData[i].ToString("x");
+            }
+
+            return byte2String;
+        }
+
+        /// <summary>
+        /// DataTable转换成实体类
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="dt"></param>
+        /// <returns></returns>
+        public static List<object> TableToEntity(this DataTable dt, string typeName)
+        {
+            var list = new List<object>();
+            try
+            {
+                foreach (DataRow row in dt.Rows)
+                {
+                    var entity = Type.GetType(typeName);
+                    PropertyInfo[] pArray = entity.GetType().GetProperties();
+
+                    foreach (var p in pArray)
+                    {
+                        if (dt.Columns.Contains(p.Name))
+                        {
+                            if (!p.CanWrite) continue;
+                            var value = row[p.Name];
+                            if (value != DBNull.Value)
+                            {
+                                var targetType = p.PropertyType;
+                                var convertType = targetType;
+                                if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
+                                {
+                                    //可空类型
+                                    var nullableConverter = new NullableConverter(targetType);
+                                    convertType = nullableConverter.UnderlyingType;
+                                }
+                                if (!string.IsNullOrEmpty(convertType.FullName) && !string.IsNullOrEmpty(value.ToString()))
+                                {
+                                    value = Convert.ChangeType(value, convertType);
+                                }
+                                switch (convertType.FullName)
+                                {
+                                    case "System.Decimal":
+                                        p.SetValue(entity, Convert.ToDecimal(value), null);
+                                        break;
+
+                                    case "System.String":
+                                        p.SetValue(entity, Convert.ToString(value), null);
+                                        break;
+
+                                    case "System.Int32":
+                                        p.SetValue(entity, Convert.ToInt32(value), null);
+                                        break;
+
+                                    case "System.Int64":
+                                        p.SetValue(entity, Convert.ToInt64(value), null);
+                                        break;
+
+                                    case "System.Int16":
+                                        p.SetValue(entity, Convert.ToInt16(value), null);
+                                        break;
+
+                                    case "System.Double":
+                                        p.SetValue(entity, Convert.ToDouble(value), null);
+                                        break;
+
+                                    case "System.Single":
+                                        p.SetValue(entity, Convert.ToSingle(value), null);
+                                        break;
+
+                                    case "System.DateTime":
+                                        p.SetValue(entity, Convert.ToDateTime(value), null);
+                                        break;
+
+                                    default:
+                                        p.SetValue(entity, value, null);
+                                        break;
+                                }
+                            }
+                        }
+                    }
+                    list.Add(entity);
+                }
+            }
+            catch (Exception ex)
+            {
+            }
+            return list;
+        }
+
+        /// <summary>
+        /// 转换到目标类型
+        /// </summary>
+        /// <param name="value"></param>
+        /// <typeparam name="T"></typeparam>
+        /// <returns></returns>
+        internal static T ConvertTo<T>(this object value) => (T)typeof(T).FromObject(value);
+
+        private static ConcurrentDictionary<Type, Func<string, object>> _dicFromObject = new ConcurrentDictionary<Type, Func<string, object>>();
+
+        public static object FromObject(this Type targetType, object value, Encoding encoding = null)
+        {
+            if (targetType == typeof(object)) return value;
+            if (encoding == null) encoding = Encoding.UTF8;
+            var valueIsNull = value == null;
+            var valueType = valueIsNull ? typeof(string) : value.GetType();
+            if (valueType == targetType) return value;
+            if (valueType == typeof(byte[])) //byte[] -> guid
+            {
+                if (targetType == typeof(Guid))
+                {
+                    var bytes = value as byte[];
+                    return Guid.TryParse(BitConverter.ToString(bytes, 0, Math.Min(bytes.Length, 36)).Replace("-", ""), out var tryguid) ? tryguid : Guid.Empty;
+                }
+                if (targetType == typeof(Guid?))
+                {
+                    var bytes = value as byte[];
+                    return Guid.TryParse(BitConverter.ToString(bytes, 0, Math.Min(bytes.Length, 36)).Replace("-", ""), out var tryguid) ? (Guid?)tryguid : null;
+                }
+            }
+            if (targetType == typeof(byte[])) //guid -> byte[]
+            {
+                if (valueIsNull) return null;
+                if (valueType == typeof(Guid) || valueType == typeof(Guid?))
+                {
+                    var bytes = new byte[16];
+                    var guidN = ((Guid)value).ToString("N");
+                    for (var a = 0; a < guidN.Length; a += 2)
+                        bytes[a / 2] = byte.Parse($"{guidN[a]}{guidN[a + 1]}", NumberStyles.HexNumber);
+                    return bytes;
+                }
+                return encoding.GetBytes(value.ToInvariantCultureToString());
+            }
+            else if (targetType.IsArray)
+            {
+                if (value is Array valueArr)
+                {
+                    var targetElementType = targetType.GetElementType();
+                    var sourceArrLen = valueArr.Length;
+                    var target = Array.CreateInstance(targetElementType, sourceArrLen);
+                    for (var a = 0; a < sourceArrLen; a++) target.SetValue(targetElementType.FromObject(valueArr.GetValue(a), encoding), a);
+                    return target;
+                }
+                //if (value is IList valueList)
+                //{
+                //    var targetElementType = targetType.GetElementType();
+                //    var sourceArrLen = valueList.Count;
+                //    var target = Array.CreateInstance(targetElementType, sourceArrLen);
+                //    for (var a = 0; a < sourceArrLen; a++) target.SetValue(targetElementType.FromObject(valueList[a], encoding), a);
+                //    return target;
+                //}
+            }
+            var func = _dicFromObject.GetOrAdd(targetType, tt =>
+            {
+                if (tt == typeof(object)) return vs => vs;
+                if (tt == typeof(string)) return vs => vs;
+                if (tt == typeof(char[])) return vs => vs == null ? null : vs.ToCharArray();
+                if (tt == typeof(char)) return vs => vs == null ? default(char) : vs.ToCharArray(0, 1).FirstOrDefault();
+                if (tt == typeof(bool)) return vs =>
+                {
+                    if (vs == null) return false;
+                    switch (vs.ToLower())
+                    {
+                        case "true":
+                        case "1":
+                            return true;
+                    }
+                    return false;
+                };
+                if (tt == typeof(bool?)) return vs =>
+                {
+                    if (vs == null) return false;
+                    switch (vs.ToLower())
+                    {
+                        case "true":
+                        case "1":
+                            return true;
+
+                        case "false":
+                        case "0":
+                            return false;
+                    }
+                    return null;
+                };
+                if (tt == typeof(byte)) return vs => vs == null ? 0 : byte.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
+                if (tt == typeof(byte?)) return vs => vs == null ? null : byte.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (byte?)tryval : null;
+                if (tt == typeof(decimal)) return vs => vs == null ? 0 : decimal.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
+                if (tt == typeof(decimal?)) return vs => vs == null ? null : decimal.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (decimal?)tryval : null;
+                if (tt == typeof(double)) return vs => vs == null ? 0 : double.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
+                if (tt == typeof(double?)) return vs => vs == null ? null : double.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (double?)tryval : null;
+                if (tt == typeof(float)) return vs => vs == null ? 0 : float.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
+                if (tt == typeof(float?)) return vs => vs == null ? null : float.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (float?)tryval : null;
+                if (tt == typeof(int)) return vs => vs == null ? 0 : int.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
+                if (tt == typeof(int?)) return vs => vs == null ? null : int.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (int?)tryval : null;
+                if (tt == typeof(long)) return vs => vs == null ? 0 : long.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
+                if (tt == typeof(long?)) return vs => vs == null ? null : long.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (long?)tryval : null;
+                if (tt == typeof(sbyte)) return vs => vs == null ? 0 : sbyte.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
+                if (tt == typeof(sbyte?)) return vs => vs == null ? null : sbyte.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (sbyte?)tryval : null;
+                if (tt == typeof(short)) return vs => vs == null ? 0 : short.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
+                if (tt == typeof(short?)) return vs => vs == null ? null : short.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (short?)tryval : null;
+                if (tt == typeof(uint)) return vs => vs == null ? 0 : uint.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
+                if (tt == typeof(uint?)) return vs => vs == null ? null : uint.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (uint?)tryval : null;
+                if (tt == typeof(ulong)) return vs => vs == null ? 0 : ulong.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
+                if (tt == typeof(ulong?)) return vs => vs == null ? null : ulong.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (ulong?)tryval : null;
+                if (tt == typeof(ushort)) return vs => vs == null ? 0 : ushort.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
+                if (tt == typeof(ushort?)) return vs => vs == null ? null : ushort.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (ushort?)tryval : null;
+                if (tt == typeof(DateTime)) return vs => vs == null ? DateTime.MinValue : DateTime.TryParse(vs, out var tryval) ? tryval : DateTime.MinValue;
+                if (tt == typeof(DateTime?)) return vs => vs == null ? null : DateTime.TryParse(vs, out var tryval) ? (DateTime?)tryval : null;
+                if (tt == typeof(DateTimeOffset)) return vs => vs == null ? DateTimeOffset.MinValue : DateTimeOffset.TryParse(vs, out var tryval) ? tryval : DateTimeOffset.MinValue;
+                if (tt == typeof(DateTimeOffset?)) return vs => vs == null ? null : DateTimeOffset.TryParse(vs, out var tryval) ? (DateTimeOffset?)tryval : null;
+                if (tt == typeof(TimeSpan)) return vs => vs == null ? TimeSpan.Zero : TimeSpan.TryParse(vs, out var tryval) ? tryval : TimeSpan.Zero;
+                if (tt == typeof(TimeSpan?)) return vs => vs == null ? null : TimeSpan.TryParse(vs, out var tryval) ? (TimeSpan?)tryval : null;
+                if (tt == typeof(Guid)) return vs => vs == null ? Guid.Empty : Guid.TryParse(vs, out var tryval) ? tryval : Guid.Empty;
+                if (tt == typeof(Guid?)) return vs => vs == null ? null : Guid.TryParse(vs, out var tryval) ? (Guid?)tryval : null;
+                if (tt == typeof(BigInteger)) return vs => vs == null ? 0 : BigInteger.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
+                if (tt == typeof(BigInteger?)) return vs => vs == null ? null : BigInteger.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (BigInteger?)tryval : null;
+                if (tt.NullableTypeOrThis().IsEnum)
+                {
+                    var tttype = tt.NullableTypeOrThis();
+                    var ttdefval = tt.CreateInstanceGetDefaultValue();
+                    return vs =>
+                    {
+                        if (string.IsNullOrWhiteSpace(vs)) return ttdefval;
+                        return Enum.Parse(tttype, vs, true);
+                    };
+                }
+                var localTargetType = targetType;
+                var localValueType = valueType;
+                return vs =>
+                {
+                    if (vs == null) return null;
+                    throw new NotSupportedException($"convert failed {localValueType.DisplayCsharp()} -> {localTargetType.DisplayCsharp()}");
+                };
+            });
+            var valueStr = valueIsNull ? null : valueType == typeof(byte[]) ? encoding.GetString(value as byte[]) : value.ToInvariantCultureToString();
+            return func(valueStr);
+        }
+
+        internal static string ToInvariantCultureToString(this object obj) => obj is string objstr ? objstr : string.Format(CultureInfo.InvariantCulture, @"{0}", obj);
+
+        private static bool IsNullableType(this Type that) => that.IsArray == false && that?.FullName.StartsWith("System.Nullable`1[") == true;
+
+        private static Type NullableTypeOrThis(this Type that) => that?.IsNullableType() == true ? that.GetGenericArguments().First() : that;
+
+        internal static object CreateInstanceGetDefaultValue(this Type that)
+        {
+            if (that == null) return null;
+            if (that == typeof(string)) return default(string);
+            if (that == typeof(Guid)) return default(Guid);
+            if (that == typeof(byte[])) return default(byte[]);
+            if (that.IsArray) return Array.CreateInstance(that.GetElementType(), 0);
+            if (that.IsInterface || that.IsAbstract) return null;
+            var ctorParms = that.InternalGetTypeConstructor0OrFirst(false)?.GetParameters();
+            if (ctorParms == null || ctorParms.Any() == false) return Activator.CreateInstance(that, true);
+            return Activator.CreateInstance(that, ctorParms.Select(a => a.ParameterType.IsInterface ||
+                                                                        a.ParameterType.IsAbstract ||
+                                                                        a.ParameterType == typeof(string) ||
+                                                                        a.ParameterType.IsArray ? null : Activator.CreateInstance(a.ParameterType, null)).ToArray());
+        }
+
+        private static ConcurrentDictionary<Type, ConstructorInfo> _dicInternalGetTypeConstructor0OrFirst = new ConcurrentDictionary<Type, ConstructorInfo>();
+
+        private static ConstructorInfo InternalGetTypeConstructor0OrFirst(this Type that, bool isThrow = true)
+        {
+            var ret = _dicInternalGetTypeConstructor0OrFirst.GetOrAdd(that, tp =>
+                tp.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[0], null) ??
+                tp.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FirstOrDefault());
+            if (ret == null && isThrow) throw new ArgumentException($"{that.FullName} has no method to access constructor");
+            return ret;
+        }
+
+        private static string DisplayCsharp(this Type type, bool isNameSpace = true)
+        {
+            if (type == null) return null;
+            if (type == typeof(void)) return "void";
+            if (type.IsGenericParameter) return type.Name;
+            if (type.IsArray) return $"{DisplayCsharp(type.GetElementType())}[]";
+            var sb = new StringBuilder();
+            var nestedType = type;
+            while (nestedType.IsNested)
+            {
+                sb.Insert(0, ".").Insert(0, DisplayCsharp(nestedType.DeclaringType, false));
+                nestedType = nestedType.DeclaringType;
+            }
+            if (isNameSpace && string.IsNullOrWhiteSpace(nestedType.Namespace) == false)
+                sb.Insert(0, ".").Insert(0, nestedType.Namespace);
+
+            if (type.IsGenericType == false)
+                return sb.Append(type.Name).ToString();
+
+            var genericParameters = type.GetGenericArguments();
+            if (type.IsNested && type.DeclaringType.IsGenericType)
+            {
+                var dic = genericParameters.ToDictionary(a => a.Name);
+                foreach (var nestedGenericParameter in type.DeclaringType.GetGenericArguments())
+                    if (dic.ContainsKey(nestedGenericParameter.Name))
+                        dic.Remove(nestedGenericParameter.Name);
+                genericParameters = dic.Values.ToArray();
+            }
+            if (genericParameters.Any() == false)
+                return sb.Append(type.Name).ToString();
+
+            sb.Append(type.Name.Remove(type.Name.IndexOf('`'))).Append("<");
+            var genericTypeIndex = 0;
+            foreach (var genericType in genericParameters)
+            {
+                if (genericTypeIndex++ > 0) sb.Append(", ");
+                sb.Append(DisplayCsharp(genericType, true));
+            }
+            return sb.Append(">").ToString();
+        }
+    }
+}

+ 155 - 0
ServiceCenter/Helpers/TypeConversionHelper.cs

@@ -0,0 +1,155 @@
+using ServiceCenter.Extensions;
+using System.Text;
+
+namespace ServiceCenter.Helpers
+{
+    public class TypeConversionHelper
+    {
+        #region 序列化写入,反序列化
+
+        public static Func<object, object> Serialize;
+        public static Func<string, Type, object> Deserialize;
+        public static Func<byte[], Type, object> DeserializeRaw;
+
+        public static object SerializeRedisValue(object value)
+        {
+            switch (value)
+            {
+                case null: return null;
+                case string _:
+                case byte[] _: return value;
+
+                case bool b: return b ? "1" : "0";
+                case char c: return value;
+                case decimal _:
+                case double _:
+                case float _:
+                case int _:
+                case long _:
+                case sbyte _:
+                case short _:
+                case uint _:
+                case ulong _:
+                case ushort _: return value.ToInvariantCultureToString();
+
+                case DateTime time: return time.ToString("yyyy-MM-ddTHH:mm:sszzzz", System.Globalization.DateTimeFormatInfo.InvariantInfo);
+                case DateTimeOffset _: return value.ToString();
+                case TimeSpan span: return span.Ticks;
+                case Guid _: return value.ToString();
+                default:
+                    if (Serialize != null) return Serialize(value);
+                    return value.ConvertTo<string>();
+            }
+        }
+
+        public static T DeserializeRedisValue<T>(byte[] valueRaw, Encoding encoding)
+        {
+            if (valueRaw == null) return default(T);
+            var type = typeof(T);
+            var typename = type.ToString().TrimEnd(']');
+            if (typename == "System.Byte[") return (T)Convert.ChangeType(valueRaw, type);
+            if (typename == "System.String") return (T)Convert.ChangeType(encoding.GetString(valueRaw), type);
+            if (typename == "System.Boolean[") return (T)Convert.ChangeType(valueRaw.Select(a => a == 49).ToArray(), type);
+            if (valueRaw.Length == 0) return default(T);
+
+            string valueStr = null;
+            if (type.IsValueType)
+            {
+                valueStr = encoding.GetString(valueRaw);
+                bool isNullable = typename.StartsWith("System.Nullable`1[");
+                var basename = isNullable ? typename.Substring(18) : typename;
+
+                bool isElse = false;
+                object obj = null;
+                switch (basename)
+                {
+                    case "System.Boolean":
+                        if (valueStr == "1") obj = true;
+                        else if (valueStr == "0") obj = false;
+                        break;
+
+                    case "System.Byte":
+                        if (byte.TryParse(valueStr, out var trybyte)) obj = trybyte;
+                        break;
+
+                    case "System.Char":
+                        if (valueStr.Length > 0) obj = valueStr[0];
+                        break;
+
+                    case "System.Decimal":
+                        if (Decimal.TryParse(valueStr, out var trydec)) obj = trydec;
+                        break;
+
+                    case "System.Double":
+                        if (Double.TryParse(valueStr, out var trydb)) obj = trydb;
+                        break;
+
+                    case "System.Single":
+                        if (Single.TryParse(valueStr, out var trysg)) obj = trysg;
+                        break;
+
+                    case "System.Int32":
+                        if (Int32.TryParse(valueStr, out var tryint32)) obj = tryint32;
+                        break;
+
+                    case "System.Int64":
+                        if (Int64.TryParse(valueStr, out var tryint64)) obj = tryint64;
+                        break;
+
+                    case "System.SByte":
+                        if (SByte.TryParse(valueStr, out var trysb)) obj = trysb;
+                        break;
+
+                    case "System.Int16":
+                        if (Int16.TryParse(valueStr, out var tryint16)) obj = tryint16;
+                        break;
+
+                    case "System.UInt32":
+                        if (UInt32.TryParse(valueStr, out var tryuint32)) obj = tryuint32;
+                        break;
+
+                    case "System.UInt64":
+                        if (UInt64.TryParse(valueStr, out var tryuint64)) obj = tryuint64;
+                        break;
+
+                    case "System.UInt16":
+                        if (UInt16.TryParse(valueStr, out var tryuint16)) obj = tryuint16;
+                        break;
+
+                    case "System.DateTime":
+                        if (DateTime.TryParse(valueStr, out var trydt)) obj = trydt;
+                        break;
+
+                    case "System.DateTimeOffset":
+                        if (DateTimeOffset.TryParse(valueStr, out var trydtos)) obj = trydtos;
+                        break;
+
+                    case "System.TimeSpan":
+                        if (Int64.TryParse(valueStr, out tryint64)) obj = new TimeSpan(tryint64);
+                        break;
+
+                    case "System.Guid":
+                        if (Guid.TryParse(valueStr, out var tryguid)) obj = tryguid;
+                        break;
+
+                    default:
+                        isElse = true;
+                        break;
+                }
+
+                if (isElse == false)
+                {
+                    if (obj == null) return default(T);
+                    return (T)obj;
+                }
+            }
+            if (DeserializeRaw != null) return (T)DeserializeRaw(valueRaw, typeof(T));
+
+            if (valueStr == null) valueStr = encoding.GetString(valueRaw);
+            if (Deserialize != null) return (T)Deserialize(valueStr, typeof(T));
+            return valueStr.ConvertTo<T>();
+        }
+
+        #endregion 序列化写入,反序列化
+    }
+}

+ 1 - 1
ServiceCenter/ServiceCenter.csproj

@@ -11,7 +11,7 @@
     <PackageReference Include="DBHelper" Version="1.0.0.2" />
     <PackageReference Include="PlcSiemens" Version="1.0.0.2" />
     <PackageReference Include="WCS.Core" Version="1.0.0.4" />
-    <PackageReference Include="WCS.Entity" Version="1.0.0.5" />
+    <PackageReference Include="WCS.Entity" Version="1.0.0.9" />
   </ItemGroup>
 
 </Project>

+ 1 - 1
WCS.Entity.Protocol/WCS.Entity.Protocol.csproj

@@ -7,7 +7,7 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="WCS.Entity" Version="1.0.0.4" />
+    <PackageReference Include="WCS.Entity" Version="1.0.0.9" />
   </ItemGroup>
 
 </Project>

+ 1 - 1
WCS.Entity/WCS_PlcData.cs

@@ -31,7 +31,7 @@ namespace WCS.Entity
         /// <summary>
         /// 内容
         /// </summary>
-        [SugarColumn(ColumnDescription = "内容")]
+        [SugarColumn(ColumnDescription = "内容", ColumnDataType = "text")]
         public string CONTENT { get; set; }
     }
 }

+ 3 - 6
WCS.Service/ProtocolProxy.cs

@@ -1,9 +1,5 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using WCS.Core;
+using WCS.Core;
+using WCS.Service.Systems;
 
 namespace WCS.Service
 {
@@ -11,6 +7,7 @@ namespace WCS.Service
     {
         public ProtocolProxy(Device dev, ProtocolInfo info, Type protocolType) : base(dev, info, protocolType)
         {
+           
         }
 
         protected override void DataChanged()

+ 12 - 2
WCS.Service/Systems/DataCollectionSysyem.cs

@@ -1,5 +1,7 @@
 using DBHelper;
+using MessagePack;
 using Newtonsoft.Json;
+using ServiceCenter.Helpers;
 using System.ComponentModel;
 using WCS.Core;
 using WCS.Entity;
@@ -27,13 +29,21 @@ namespace WCS.Service.Systems
         {
             Db.Do(db =>
             {
+                //byte[] bytes = System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(ConvList, null, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }));
+
                 var plcData = new WCS_PlcData()
                 {
                     UPDATETIME = DateTime.Now,
                     WAREHOUSE = "111",
-                    CONTENT = JsonConvert.SerializeObject(ConvList),
+                    CONTENT = JsonConvert.SerializeObject(ConvList, null, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }),
                 };
-                db.Default.Insertable<WCS_PlcData>(plcData).ExecuteCommand(); ;
+
+                var a = TypeConversionHelper.SerializeRedisValue(ConvList);
+
+                //对bytes进行数据压缩
+                //plcData.CONTENT = bytes.Compress();
+
+                db.Default.Insertable(plcData).ExecuteCommand(); ;
             });
 
             return true;

+ 1 - 0
WCS.Service/WCS.Service.csproj

@@ -8,6 +8,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="MessagePack" Version="2.5.108" />
     <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
     <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
     <PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="7.0.0" />

+ 10 - 0
WCS.Service/Worker.cs

@@ -11,6 +11,7 @@ using WCS.Entity;
 using WCS.Entity.Protocol;
 using WCS.Entity.Protocol.BCR;
 using WCS.Entity.Protocol.Station;
+using WCS.Service.Systems;
 
 namespace WCS.Service
 {
@@ -228,6 +229,15 @@ namespace WCS.Service
                 #endregion 唤醒所有的世界
 
                 _logger.LogInformation("WCS启动成功");
+
+                #region 启用数据采集器
+
+                while (true)
+                {
+                    Ltc.GetSystem<DataCollectionSysyem>().Invoke(true);
+                }
+
+                #endregion 启用数据采集器
             }
             catch (Exception ex)
             {

+ 7 - 12
WCS.Service/Worlds/DataCollectionWorld.cs

@@ -1,8 +1,5 @@
-using DBHelper;
-using Newtonsoft.Json;
-using System.ComponentModel;
+using System.ComponentModel;
 using WCS.Core;
-using WCS.Entity;
 using WCS.Service.Systems;
 
 namespace WCS.Service.Worlds
@@ -13,20 +10,18 @@ namespace WCS.Service.Worlds
     /// </summary>
     [Description("数据采集世界")]
     public class DataCollectionWorld : World
-    { /// <summary>
-      /// 构造函数
-      /// </summary>
+    {
+        /// <summary>
+        /// 构造函数
+        /// </summary>
         public DataCollectionWorld()
         {
-            Interval = 50;
+            Interval = 300;
         }
 
         protected override void AfterUpdate()
         {
-           
-            //var a = Ltc.GetSystem<DataCollectionSysyem>();
-            //var v = a.Invoke(true);
-            // Ltc.GetSystem<DataCollectionSysyem>().Invoke(true);
+            //Ltc.GetSystem<DataCollectionSysyem>().Invoke(true);
         }
     }
 }

+ 1 - 1
WCS.WorkEngineering/WCS.WorkEngineering.csproj

@@ -8,7 +8,7 @@
 
   <ItemGroup>
     <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
-    <PackageReference Include="ServiceCenter" Version="1.0.0.7" />
+    <PackageReference Include="ServiceCenter" Version="1.0.0.12" />
   </ItemGroup>
 
   <ItemGroup>