first commit
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSMachine.WMS.Infrastructure.Extensions
|
||||
{
|
||||
public static class DateTimeExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当日零点
|
||||
/// </summary>
|
||||
/// <param name="dateTime"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetTodayBegin(this DateTime dateTime)
|
||||
{
|
||||
DateTime timeNow = DateTime.Now;
|
||||
|
||||
return new DateTime(timeNow.Year, timeNow.Month, timeNow.Day, 0, 0, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当日23点59分59秒
|
||||
/// </summary>
|
||||
/// <param name="dateTime"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetTodayEnd(this DateTime dateTime)
|
||||
{
|
||||
DateTime timeNow = DateTime.Now;
|
||||
|
||||
return new DateTime(timeNow.Year, timeNow.Month, timeNow.Day, 23, 59, 59);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将秒数转化为时分秒
|
||||
/// </summary>
|
||||
/// <param name="duration"></param>
|
||||
/// <returns></returns>
|
||||
public static string Sec_to_hms(int duration)
|
||||
{
|
||||
TimeSpan ts = new(0, 0, duration);
|
||||
string str = "";
|
||||
if (ts.Hours > 0)
|
||||
{
|
||||
str = string.Format("{0:00}", ts.Hours) + ":" + string.Format("{0:00}", ts.Minutes) + ":" + string.Format("{0:00}", ts.Seconds);
|
||||
}
|
||||
if (ts.Hours == 0 && ts.Minutes > 0)
|
||||
{
|
||||
str = "00:" + string.Format("{0:00}", ts.Minutes) + ":" + string.Format("{0:00}", ts.Seconds);
|
||||
}
|
||||
if (ts.Hours == 0 && ts.Minutes == 0)
|
||||
{
|
||||
str = "00:00:" + string.Format("{0:00}", ts.Seconds);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将秒数转换为分秒
|
||||
/// </summary>
|
||||
/// <param name="duration"></param>
|
||||
/// <returns></returns>
|
||||
public static string Sec_to_ms(int duration)
|
||||
{
|
||||
TimeSpan ts = new(0, 0, duration);
|
||||
string str = "";
|
||||
|
||||
if (ts.Hours == 0 && ts.Minutes > 0)
|
||||
{
|
||||
str = string.Format("{0:00}", ts.Minutes) + ":" + string.Format("{0:00}", ts.Seconds);
|
||||
}
|
||||
if (ts.Hours == 0 && ts.Minutes == 0)
|
||||
{
|
||||
str = "00:" + string.Format("{0:00}", ts.Seconds);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSMachine.WMS.Infrastructure
|
||||
{
|
||||
public static class EnumExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取枚举描述属性值
|
||||
/// </summary>
|
||||
/// <param name="enum"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetDescription(this Enum @enum)
|
||||
{
|
||||
Type type = @enum.GetType();
|
||||
string name = Enum.GetName(type, @enum);
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
return @enum.ToString();
|
||||
|
||||
FieldInfo field = type.GetField(name);
|
||||
DescriptionAttribute des = field?.GetCustomAttribute<DescriptionAttribute>();
|
||||
if (des == null)
|
||||
return @enum.ToString();
|
||||
|
||||
return des.Description;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Linq;
|
||||
using JSMachine.DCS.Infrastructure;
|
||||
using JSMachine.WMS.Infrastructure;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace System.Collections.Generic
|
||||
{
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
public static void ForEach<T>(this IEnumerable<T> @this, Action<T> callback, bool immutable = false)
|
||||
{
|
||||
Guards.ThrowIfNull(@this, callback);
|
||||
|
||||
var collection = immutable ? @this.ToArray() : @this;
|
||||
|
||||
foreach (T item in collection)
|
||||
{
|
||||
callback(item);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Do<T>(this IEnumerable<T> @this, Action<T> callback)
|
||||
{
|
||||
Guards.ThrowIfNull(@this, callback);
|
||||
|
||||
foreach (var item in @this)
|
||||
{
|
||||
callback?.Invoke(item);
|
||||
yield return item;
|
||||
}
|
||||
}
|
||||
|
||||
//public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
|
||||
//{
|
||||
// return source == null || source.Count() == 0;
|
||||
//}
|
||||
|
||||
public static bool IsNullOrEmpty(this IList source)
|
||||
{
|
||||
return source == null || source.Count == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSMachine.WMS.Infrastructure
|
||||
{
|
||||
public static class GlobalServericeProvidor
|
||||
{
|
||||
public static IServiceProvider IServiceProvider { get; private set; }
|
||||
|
||||
public static void UseGlobalServiceProvider(this IApplicationBuilder builder)
|
||||
{
|
||||
IServiceProvider = builder.ApplicationServices;
|
||||
}
|
||||
|
||||
public static T GetService<T>()
|
||||
{
|
||||
|
||||
return IServiceProvider.GetService<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using JSMachine.DCS.Infrastructure;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace System
|
||||
{
|
||||
public static class JsonExtensions
|
||||
{
|
||||
public static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };
|
||||
public static readonly JsonSerializerSettings JsonDeserializerSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };
|
||||
public static readonly FileLocatorConverter FileLocatorConverter = new FileLocatorConverter();
|
||||
|
||||
static JsonExtensions()
|
||||
{
|
||||
JsonSerializerSettings.Converters.Add(FileLocatorConverter);
|
||||
}
|
||||
|
||||
public static string ToJson<T>(this T @object, Formatting formatting = Formatting.None, bool specifyRootType = true)
|
||||
{
|
||||
var type = @object.GetType();
|
||||
|
||||
return typeof(T) != type && specifyRootType
|
||||
? JsonConvert.SerializeObject(@object, typeof(T), formatting, JsonSerializerSettings)
|
||||
: JsonConvert.SerializeObject(@object, formatting, JsonSerializerSettings);
|
||||
}
|
||||
|
||||
public static T ToObject<T>(this string json)
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(json)
|
||||
? JsonConvert.DeserializeObject<T>(json, JsonDeserializerSettings)
|
||||
: default;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class FileLocatorConverter : JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
var fileLocator = (FileLocator)value;
|
||||
writer.WriteValue(fileLocator.FullPath);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType) => objectType == typeof(FileLocator);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace Prism.Logging
|
||||
{
|
||||
public static class LoggerFacadeExtensions
|
||||
{
|
||||
public static void Error(this ILoggerFacade logger, string message, Exception e = null)
|
||||
{
|
||||
logger.Log(GetMessage(message, e), Category.Exception, Priority.High);
|
||||
}
|
||||
|
||||
public static void Warning(this ILoggerFacade logger, string message, Exception e = null)
|
||||
{
|
||||
logger.Log(GetMessage(message, e), Category.Warn, Priority.Medium);
|
||||
}
|
||||
|
||||
public static void Info(this ILoggerFacade logger, string message, Exception e = null)
|
||||
{
|
||||
logger.Log(GetMessage(message, e), Category.Info, Priority.Low);
|
||||
}
|
||||
|
||||
public static void Debug(this ILoggerFacade logger, string message, Exception e = null)
|
||||
{
|
||||
logger.Log(GetMessage(message, e), Category.Debug, Priority.Low);
|
||||
}
|
||||
|
||||
private static string GetMessage(string message, Exception e)
|
||||
{
|
||||
message = $"{message}{Environment.NewLine}";
|
||||
|
||||
if (e != null)
|
||||
{
|
||||
message = $"{message}{e.Message}{Environment.NewLine}" +
|
||||
$"{e.StackTrace}{Environment.NewLine}";
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace System
|
||||
{
|
||||
public static class ObjectExtensions
|
||||
{
|
||||
private const double Tolerance = 1e-6;
|
||||
|
||||
public static T CastTo<T>(this object value)
|
||||
{
|
||||
return typeof(T).IsValueType && value != null
|
||||
? (T)Convert.ChangeType(value, typeof(T))
|
||||
: value is T typeValue ? typeValue : default;
|
||||
}
|
||||
|
||||
public static bool EqualsWithinTolerance(this double @this, double other)
|
||||
{
|
||||
return Math.Abs(@this - other) < Tolerance;
|
||||
}
|
||||
|
||||
public static bool GreaterOrEqual(this double @this, double other)
|
||||
{
|
||||
return @this > other || @this.EqualsWithinTolerance(other);
|
||||
}
|
||||
|
||||
public static bool LessOrEqual(this double @this, double other)
|
||||
{
|
||||
return @this < other || @this.EqualsWithinTolerance(other);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
|
||||
namespace System
|
||||
{
|
||||
[SuppressUnmanagedCodeSecurity]
|
||||
public static class SecureStringExtensions
|
||||
{
|
||||
public static string AsString(this SecureString @this)
|
||||
{
|
||||
if (@this == null || @this.Length == 0)
|
||||
return string.Empty;
|
||||
|
||||
IntPtr bstr = IntPtr.Zero;
|
||||
try
|
||||
{
|
||||
bstr = Marshal.SecureStringToBSTR(@this);
|
||||
return Marshal.PtrToStringBSTR(bstr);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (bstr != IntPtr.Zero)
|
||||
Marshal.ZeroFreeBSTR(bstr);
|
||||
}
|
||||
}
|
||||
|
||||
public static SecureString AsSecureString(this string @this)
|
||||
{
|
||||
if (string.IsNullOrEmpty(@this))
|
||||
return new SecureString();
|
||||
|
||||
var secureString = new SecureString();
|
||||
Array.ForEach(@this.ToCharArray(), secureString.AppendChar);
|
||||
|
||||
return secureString;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using JSMachine.DCS.Infrastructure;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace System
|
||||
{
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static string RandomString(int length)
|
||||
{
|
||||
var b = new byte[4];
|
||||
new RNGCryptoServiceProvider().GetBytes(b);
|
||||
var r = new Random(BitConverter.ToInt32(b, 0));
|
||||
var ret = string.Empty;
|
||||
const string str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
for (var i = 0; i < length; i++)
|
||||
ret += str.Substring(r.Next(0, str.Length - 1), 1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static string LogId => RandomString(48);
|
||||
|
||||
public static string GetMatch(this string text, string p1, string p2)
|
||||
{
|
||||
var rg = new Regex("(?<=(" + p1 + "))[.\\s\\S]*?(?=(" + p2 + "))",
|
||||
RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
return rg.Match(text).Value;
|
||||
}
|
||||
|
||||
public static bool IsEmailAddress(this string @this)
|
||||
{
|
||||
return !string.IsNullOrEmpty(@this) && !string.IsNullOrWhiteSpace(@this); // TODO
|
||||
}
|
||||
|
||||
public static string TrimMiddle(this string @this, int limitLength, string omitPlaceholder = null)
|
||||
{
|
||||
const string defaultOmitPlaceholder = "...";
|
||||
|
||||
if (omitPlaceholder == null) omitPlaceholder = defaultOmitPlaceholder;
|
||||
Guards.ThrowIfNot(limitLength >= omitPlaceholder.Length);
|
||||
|
||||
if (string.IsNullOrEmpty(@this) || @this.Length <= limitLength) return @this;
|
||||
|
||||
if (limitLength == omitPlaceholder.Length) return omitPlaceholder;
|
||||
|
||||
var halfLength = (limitLength - omitPlaceholder.Length) / 2;
|
||||
var bias = (limitLength - omitPlaceholder.Length) % 2;
|
||||
|
||||
var firstHalfString = @this.Substring(0, halfLength + bias);
|
||||
var secondHalfString = @this.Substring(@this.Length - halfLength);
|
||||
|
||||
return $"{firstHalfString}{omitPlaceholder}{secondHalfString}";
|
||||
}
|
||||
|
||||
public static string UpperCamelCaseToDelimiterSeparated(this string upperCamelCase)
|
||||
{
|
||||
var stringBuilder = new StringBuilder(upperCamelCase.Length, upperCamelCase.Length * 2);
|
||||
|
||||
for (int i = 0; i < upperCamelCase.Length; i++)
|
||||
{
|
||||
var @char = upperCamelCase[i];
|
||||
if (char.IsUpper(@char))
|
||||
{
|
||||
if (i != 0) stringBuilder.Append('-');
|
||||
stringBuilder.Append(char.ToLower(@char));
|
||||
}
|
||||
else
|
||||
{
|
||||
stringBuilder.Append(@char);
|
||||
}
|
||||
}
|
||||
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user