From 1a7be7a6afecb3e239098ed46b44f85071057f71 Mon Sep 17 00:00:00 2001
From: cli <377476583@qq.com>
Date: Tue, 22 Oct 2024 09:28:58 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=85=AC=E7=94=A8=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
JiShe.CollectBus.Common/Consts/RegexConst.cs | 23 +
JiShe.CollectBus.Common/Enums/MaskTypeEnum.cs | 19 +
JiShe.CollectBus.Common/Enums/SortEnum.cs | 12 +
.../Extensions/CollectionExtensions.cs | 125 ++
.../Extensions/ComparableExtensions.cs | 23 +
.../Extensions/DataTableExtensions.cs | 291 +++++
.../Extensions/DateTimeExtensions.cs | 171 +++
.../Extensions/DayOfWeekExtensions.cs | 89 ++
.../Extensions/DecimalOrIntExtensions.cs | 128 ++
.../Extensions/DictionaryExtensions.cs | 83 ++
.../Extensions/EnumerableExtensions.cs | 68 +
.../Extensions/EventHandlerExtensions.cs | 56 +
.../Extensions/ExceptionExtensions.cs | 23 +
.../Extensions/HttpResponseExtensions.cs | 33 +
.../Extensions/ListExtensions.cs | 366 ++++++
.../Extensions/LockExtensions.cs | 68 +
.../Extensions/ObjectExtensions.cs | 154 +++
.../Extensions/StreamExtensions.cs | 59 +
.../Extensions/StringExtensions.cs | 1120 +++++++++++++++++
.../Extensions/XmlExtensions.cs | 64 +
JiShe.CollectBus.Common/Helpers/TypeHelper.cs | 66 +
.../JiShe.CollectBus.Common.csproj | 4 +
.../JiShe.CollectBus.Core.csproj | 8 +-
.../Plugins/TcpServiceReceivedPlugin.cs | 15 +-
.../Entities/EntityBase.cs | 13 +
.../Entities/FullAuditedEntity.cs | 19 +
.../Entities/ICreationAudited.cs | 22 +
.../Entities/IDeletionAudited.cs | 27 +
.../Entities/IEntity.cs | 13 +
.../Entities/IExtendableObject.cs | 20 +
.../Entities/IModificationAudited.cs | 22 +
.../Extensions/ExtendableObjectExtensions.cs | 153 +++
...iShe.CollectBus.EntityFrameworkCore.csproj | 18 +
.../Repositories/IRepository.cs | 62 +
.../Repositories/Repository.cs | 296 +++++
.../StandardProtocolPlugin.cs | 2 -
JiShe.CollectBus.sln | 9 +-
37 files changed, 3727 insertions(+), 17 deletions(-)
create mode 100644 JiShe.CollectBus.Common/Consts/RegexConst.cs
create mode 100644 JiShe.CollectBus.Common/Enums/MaskTypeEnum.cs
create mode 100644 JiShe.CollectBus.Common/Enums/SortEnum.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/CollectionExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/ComparableExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/DataTableExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/DateTimeExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/DayOfWeekExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/DecimalOrIntExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/DictionaryExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/EnumerableExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/EventHandlerExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/ExceptionExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/HttpResponseExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/ListExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/LockExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/ObjectExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/StreamExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/StringExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Extensions/XmlExtensions.cs
create mode 100644 JiShe.CollectBus.Common/Helpers/TypeHelper.cs
create mode 100644 JiShe.CollectBus.EntityFrameworkCore/Entities/EntityBase.cs
create mode 100644 JiShe.CollectBus.EntityFrameworkCore/Entities/FullAuditedEntity.cs
create mode 100644 JiShe.CollectBus.EntityFrameworkCore/Entities/ICreationAudited.cs
create mode 100644 JiShe.CollectBus.EntityFrameworkCore/Entities/IDeletionAudited.cs
create mode 100644 JiShe.CollectBus.EntityFrameworkCore/Entities/IEntity.cs
create mode 100644 JiShe.CollectBus.EntityFrameworkCore/Entities/IExtendableObject.cs
create mode 100644 JiShe.CollectBus.EntityFrameworkCore/Entities/IModificationAudited.cs
create mode 100644 JiShe.CollectBus.EntityFrameworkCore/Extensions/ExtendableObjectExtensions.cs
create mode 100644 JiShe.CollectBus.EntityFrameworkCore/JiShe.CollectBus.EntityFrameworkCore.csproj
create mode 100644 JiShe.CollectBus.EntityFrameworkCore/Repositories/IRepository.cs
create mode 100644 JiShe.CollectBus.EntityFrameworkCore/Repositories/Repository.cs
diff --git a/JiShe.CollectBus.Common/Consts/RegexConst.cs b/JiShe.CollectBus.Common/Consts/RegexConst.cs
new file mode 100644
index 0000000..a68e98b
--- /dev/null
+++ b/JiShe.CollectBus.Common/Consts/RegexConst.cs
@@ -0,0 +1,23 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace JiShe.CollectBus.Common.Consts
+{
+ public class RegexConst
+ {
+ public const string PhoneNumber = "1[3|4|5|7|8|9][0-9]{9}";
+
+ public const string IsNumeric = @"^[+-]?\d+[.]?\d*$";
+
+ public const string Ip = @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";
+
+ public const string Email = @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$";
+
+ public const string Url = @"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$";
+
+ public const string Date = @"(\d{4})-(\d{1,2})-(\d{1,2})";
+
+ public const string ZipCode = @"^\d{6}$";
+ }
+}
diff --git a/JiShe.CollectBus.Common/Enums/MaskTypeEnum.cs b/JiShe.CollectBus.Common/Enums/MaskTypeEnum.cs
new file mode 100644
index 0000000..44b1187
--- /dev/null
+++ b/JiShe.CollectBus.Common/Enums/MaskTypeEnum.cs
@@ -0,0 +1,19 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace JiShe.CollectBus.Common.Enums
+{
+ public enum MaskTypeEnum
+ {
+ ///
+ /// Masks all characters within the masking region, regardless of type.
+ ///
+ All,
+
+ ///
+ /// Masks only alphabetic and numeric characters within the masking region.
+ ///
+ AlphaNumericOnly,
+ }
+}
diff --git a/JiShe.CollectBus.Common/Enums/SortEnum.cs b/JiShe.CollectBus.Common/Enums/SortEnum.cs
new file mode 100644
index 0000000..b018c1c
--- /dev/null
+++ b/JiShe.CollectBus.Common/Enums/SortEnum.cs
@@ -0,0 +1,12 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace JiShe.CollectBus.Common.Enums
+{
+ public enum SortEnum
+ {
+ Asc,
+ Desc
+ }
+}
diff --git a/JiShe.CollectBus.Common/Extensions/CollectionExtensions.cs b/JiShe.CollectBus.Common/Extensions/CollectionExtensions.cs
new file mode 100644
index 0000000..6d493a4
--- /dev/null
+++ b/JiShe.CollectBus.Common/Extensions/CollectionExtensions.cs
@@ -0,0 +1,125 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using JetBrains.Annotations;
+
+namespace JiShe.CollectBus.Common.Extensions
+{
+ ///
+ /// Extension methods for Collections.
+ ///
+ public static class CollectionExtensions
+ {
+ ///
+ /// Checks whatever given collection object is null or has no item.
+ ///
+ [Description("检查给定的集合对象是否为null或没有项目")]
+ public static bool IsNullOrEmpty([CanBeNull] this ICollection source)
+ {
+ return source == null || source.Count <= 0;
+ }
+
+ ///
+ /// Adds an item to the collection if it's not already in the collection.
+ ///
+ /// Collection
+ /// Item to check and add
+ /// Type of the items in the collection
+ /// Returns True if added, returns False if not.
+ [Description("项不在集合中则将其添加到集合中")]
+ public static bool AddIfNotContains([NotNull] this ICollection source, T item)
+ {
+ if (source == null)
+ {
+ throw new ArgumentNullException("source");
+ }
+
+ if (source.Contains(item))
+ {
+ return false;
+ }
+
+ source.Add(item);
+ return true;
+ }
+
+ ///
+ /// Adds items to the collection which are not already in the collection.
+ ///
+ /// The collection
+ /// Item to check and add
+ /// Type of the items in the collection
+ /// Returns the added items.
+ public static IEnumerable AddIfNotContains([NotNull] this ICollection source, IEnumerable items)
+ {
+ var addedItems = new List();
+
+ foreach (var item in items)
+ {
+ if (source.Contains(item))
+ {
+ continue;
+ }
+
+ source.Add(item);
+ addedItems.Add(item);
+ }
+
+ return addedItems;
+ }
+
+ ///
+ /// Adds an item to the collection if it's not already in the collection based on the given .
+ ///
+ /// The collection
+ /// The condition to decide if the item is already in the collection
+ /// A factory that returns the item
+ /// Type of the items in the collection
+ /// Returns True if added, returns False if not.
+ public static bool AddIfNotContains([NotNull] this ICollection source, [NotNull] Func predicate, [NotNull] Func itemFactory)
+ {
+ if (source.Any(predicate))
+ {
+ return false;
+ }
+
+ source.Add(itemFactory());
+ return true;
+ }
+
+ ///
+ /// Removes all items from the collection those satisfy the given .
+ ///
+ /// Type of the items in the collection
+ /// The collection
+ /// The condition to remove the items
+ /// List of removed items
+ [Description("从集合中移除满足给定条件的所有项")]
+ public static IList RemoveAll([NotNull] this ICollection source, Func predicate)
+ {
+ var items = source.Where(predicate).ToList();
+
+ foreach (var item in items)
+ {
+ source.Remove(item);
+ }
+
+ return items;
+ }
+
+ ///
+ /// Removes all items from the collection those satisfy the given .
+ ///
+ /// Type of the items in the collection
+ /// The collection
+ /// Items to be removed from the list
+ public static void RemoveAll([NotNull] this ICollection source, IEnumerable items)
+ {
+ foreach (var item in items)
+ {
+ source.Remove(item);
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/JiShe.CollectBus.Common/Extensions/ComparableExtensions.cs b/JiShe.CollectBus.Common/Extensions/ComparableExtensions.cs
new file mode 100644
index 0000000..8516edc
--- /dev/null
+++ b/JiShe.CollectBus.Common/Extensions/ComparableExtensions.cs
@@ -0,0 +1,23 @@
+using System;
+using System.ComponentModel;
+
+namespace JiShe.CollectBus.Common.Extensions
+{
+ ///
+ /// Extension methods for .
+ ///
+ public static class ComparableExtensions
+ {
+ ///
+ /// Checks a value is between a minimum and maximum value.
+ ///
+ /// The value to be checked
+ /// Minimum (inclusive) value
+ /// Maximum (inclusive) value
+ [Description("检查值是否介于最小值和最大值之间")]
+ public static bool IsBetween(this T value, T minInclusiveValue, T maxInclusiveValue) where T : IComparable
+ {
+ return value.CompareTo(minInclusiveValue) >= 0 && value.CompareTo(maxInclusiveValue) <= 0;
+ }
+ }
+}
\ No newline at end of file
diff --git a/JiShe.CollectBus.Common/Extensions/DataTableExtensions.cs b/JiShe.CollectBus.Common/Extensions/DataTableExtensions.cs
new file mode 100644
index 0000000..6735fc7
--- /dev/null
+++ b/JiShe.CollectBus.Common/Extensions/DataTableExtensions.cs
@@ -0,0 +1,291 @@
+锘縰sing System;
+using System.Collections;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Linq;
+using System.Xml.Linq;
+
+namespace JiShe.CollectBus.Common.Extensions
+{
+ public static class DataTableExtensions
+ {
+
+ /// Converts to list.
+ ///
+ /// The dt.
+ ///
+ ///
+ ///
+ [Description("杞崲涓哄垪琛")]
+ public static IList ToList(this DataTable dt) where T : class
+ {
+ IList list = new List();
+ foreach (DataRow dr in dt.Rows)
+ {
+ T t = Activator.CreateInstance();
+ var props = typeof(T).GetProperties();
+ foreach (var pro in props)
+ {
+ var tempName = pro.Name;
+ if (!dt.Columns.Contains(tempName)) continue;
+ if (!pro.CanWrite) continue;
+ var value = dr[tempName];
+ if (value != DBNull.Value)
+ pro.SetValue(t, value, null);
+ }
+
+ list.Add(t);
+ }
+
+ return list;
+ }
+
+ /// Converts to data table.
+ ///
+ /// The source.
+ ///
+ ///
+ ///
+ [Description("杞崲涓篋ataTable")]
+ public static DataTable ToDataTable(this ICollection source)
+ {
+ var props = typeof(T).GetProperties();
+ var dt = new DataTable();
+ dt.Columns.AddRange(props.Select(p =>
+ new DataColumn(p.Name,
+ (p.PropertyType.IsGenericType) && (p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
+ ? p.PropertyType.GetGenericArguments()[0]
+ : p.PropertyType)).ToArray());
+
+ for (var i = 0; i < source.Count; i++)
+ {
+ var tempList = new ArrayList();
+ foreach (var obj in props.Select(pi => pi.GetValue(source.ElementAt(i), null))) tempList.Add(obj);
+ var array = tempList.ToArray();
+ dt.LoadDataRow(array, true);
+ }
+
+ return dt;
+ }
+
+ /// Converts to xml.
+ /// The dt.
+ /// Name of the root.
+ ///
+ ///
+ ///
+ [Description("杞崲涓篨ml")]
+ public static XDocument ToXml(this DataTable dt, string rootName)
+ {
+ var xdoc = new XDocument
+ {
+ Declaration = new XDeclaration("1.0", "utf-8", "")
+ };
+ xdoc.Add(new XElement(rootName));
+ foreach (DataRow row in dt.Rows)
+ {
+ var element = new XElement(dt.TableName);
+ foreach (DataColumn col in dt.Columns)
+ {
+ element.Add(new XElement(col.ColumnName, row[col].ToString().Trim(' ')));
+ }
+ xdoc.Root?.Add(element);
+ }
+
+ return xdoc;
+ }
+
+ ///
+ /// "SELECT DISTINCT" over a DataTable
+ ///
+ /// Input DataTable
+ /// Field to select (distinct)
+ ///
+ [Description("鍦―ataTable涓'SELECT DISTINCT'")]
+ public static DataTable SelectDistinct(this DataTable sourceTable, string fieldName)
+ {
+ return SelectDistinct(sourceTable, fieldName, string.Empty);
+ }
+
+ ///
+ /// "SELECT DISTINCT" over a DataTable
+ ///
+ /// Input DataTable
+ /// Fields to select (distinct) Split ','
+ /// Optional filter to be applied to the selection
+ ///
+ public static DataTable SelectDistinct(this DataTable sourceTable, string fieldNames, string filter)
+ {
+ var dt = new DataTable();
+ var arrFieldNames = fieldNames.Replace(" ", "").Split(',');
+ foreach (var s in arrFieldNames)
+ {
+ if (sourceTable.Columns.Contains(s))
+ dt.Columns.Add(s, sourceTable.Columns[s].DataType);
+ else
+ throw new Exception($"The column {s} does not exist.");
+ }
+
+ object[] lastValues = null;
+ foreach (DataRow dr in sourceTable.Select(filter, fieldNames))
+ {
+ var newValues = GetRowFields(dr, arrFieldNames);
+ if (lastValues == null || !(ObjectComparison(lastValues, newValues)))
+ {
+ lastValues = newValues;
+ dt.Rows.Add(lastValues);
+ }
+ }
+
+ return dt;
+ }
+
+ /// Selects the rows.
+ /// The dt.
+ /// The where expression.
+ /// The order by expression.
+ ///
+ ///
+ ///
+ [Description("鏌ヨ鍏ㄩ儴琛")]
+ public static DataTable SelectRows(this DataTable dt, string whereExpression, string orderByExpression)
+ {
+ dt.DefaultView.RowFilter = whereExpression;
+ dt.DefaultView.Sort = orderByExpression;
+ return dt.DefaultView.ToTable();
+ }
+
+ /// take any DataTable and remove duplicate rows based on any column.
+ /// The dt.
+ /// Name of the key col.
+ ///
+ ///
+ ///
+ [Description("鍒犻櫎閲嶅鐨勮")]
+ public static DataTable Duplicate(this DataTable dt, string keyColName)
+ {
+ var tblOut = dt.Clone();
+ foreach (DataRow row in dt.Rows)
+ {
+ var found = false;
+ var caseIdToTest = row[keyColName].ToString();
+ foreach (DataRow row2 in tblOut.Rows)
+ {
+ if (row2[keyColName].ToString() == caseIdToTest)
+ {
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ tblOut.ImportRow(row);
+ }
+ return tblOut;
+ }
+
+ /// Checks if two DataTable objects have the same content.
+ /// The this data table.
+ /// The other data table.
+ ///
+ ///
+ ///
+ [Description("妫鏌ヤ袱涓狣ataTable瀵硅薄鏄惁鍏锋湁鐩稿悓鐨勫唴瀹")]
+ public static bool EqualsByContent(this DataTable thisDataTable, DataTable otherDataTable)
+ {
+ // Compare row count.
+ if (thisDataTable.Rows.Count != otherDataTable.Rows.Count)
+ {
+ return false;
+ }
+
+ // Compare column count.
+ if (thisDataTable.Columns.Count != otherDataTable.Columns.Count)
+ {
+ return false;
+ }
+
+ // Compare data in each cell of each row.
+ for (int i = 0; i < thisDataTable.Rows.Count; i++)
+ {
+ for (int j = 0; j < thisDataTable.Columns.Count; j++)
+ {
+ if (!thisDataTable.Rows[i][j].Equals(otherDataTable.Rows[i][j]))
+ {
+ return false;
+ }
+ }
+ }
+
+ // The two DataTables contain the same data.
+ return true;
+ }
+
+ /// Renames the column.
+ /// The dt.
+ /// The old name.
+ /// The new name.
+ [Description("閲嶅懡鍚嶅垪")]
+ public static void RenameColumn(this DataTable dt, string oldName, string newName)
+ {
+ if (dt != null && !string.IsNullOrEmpty(oldName) && !string.IsNullOrEmpty(newName) && oldName != newName)
+ {
+ int idx = dt.Columns.IndexOf(oldName);
+ dt.Columns[idx].ColumnName = newName;
+ dt.AcceptChanges();
+ }
+ }
+
+ /// Removes the column.
+ /// The dt.
+ /// Name of the column.
+ [Description("鍒犻櫎鍒")]
+ public static void RemoveColumn(this DataTable dt, string columnName)
+ {
+ if (dt != null && !string.IsNullOrEmpty(columnName) && dt.Columns.IndexOf(columnName) >= 0)
+ {
+ int idx = dt.Columns.IndexOf(columnName);
+ dt.Columns.RemoveAt(idx);
+ dt.AcceptChanges();
+ }
+ }
+
+ private static object[] GetRowFields(DataRow dr, string[] arrFieldNames)
+ {
+ if (arrFieldNames.Length == 1)
+ return new object[] { dr[arrFieldNames[0]] };
+ var itemArray = new ArrayList();
+ foreach (var field in arrFieldNames)
+ itemArray.Add(dr[field]);
+
+ return itemArray.ToArray();
+ }
+
+ private static bool ObjectComparison(object a, object b)
+ {
+ if (a == DBNull.Value && b == DBNull.Value) // both are DBNull.Value
+ return true;
+ if (a == DBNull.Value || b == DBNull.Value) // only one is DBNull.Value
+ return false;
+ return a.Equals(b); // value type standard comparison
+ }
+
+ private static bool ObjectComparison(IReadOnlyList