diff --git a/ProductionMgr/Tnb.ProductionMgr/WorklineCommService.cs b/ProductionMgr/Tnb.ProductionMgr/WorklineCommService.cs index ac7ba7c1..63e4c92e 100644 --- a/ProductionMgr/Tnb.ProductionMgr/WorklineCommService.cs +++ b/ProductionMgr/Tnb.ProductionMgr/WorklineCommService.cs @@ -1,29 +1,11 @@ -using System.Dynamic; -using System.Reflection.Emit; -using Aop.Api.Domain; -using JNPF.Common.Core.Manager; -using JNPF.Common.Enums; -using JNPF.Common.Extension; -using JNPF.Common.Security; +using JNPF.Common.Core.Manager; using JNPF.DependencyInjection; using JNPF.DynamicApiController; -using JNPF.Extensitions.EventBus; -using JNPF.FriendlyException; -using JNPF.Logging; using JNPF.Systems.Interfaces.System; -using JNPF.VisualDev; -using JNPF.VisualDev.Entitys.Dto.VisualDevModelData; -using Mapster; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Senparc.Weixin.Work.AdvancedAPIs.MailList; using SqlSugar; -using Tnb.BasicData; -using Tnb.BasicData.Entitys.Entity; -using Tnb.EquipMgr.Entities; using Tnb.ProductionMgr.Entities; -using Tnb.ProductionMgr.Entities.Dto; -using Tnb.ProductionMgr.Entities.Enums; using Tnb.ProductionMgr.Interfaces; namespace Tnb.ProductionMgr; diff --git a/common/Tnb.Common/DataValidation/Check.cs b/common/Tnb.Common/DataValidation/Check.cs new file mode 100644 index 00000000..cc102ead --- /dev/null +++ b/common/Tnb.Common/DataValidation/Check.cs @@ -0,0 +1,350 @@ +using JetBrains.Annotations; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace Tnb.Common; + +[DebuggerStepThrough] +public static class Check +{ + public static T NotNull(T value, string parameterName) + { + if (value == null) + { + throw new ArgumentNullException(parameterName); + } + + return value; + } + + public static T NotNull( + T value, + string parameterName, + string message) + { + if (value == null) + { + throw new ArgumentNullException(parameterName, message); + } + + return value; + } + + public static string NotNull( + string value, + string parameterName, + int maxLength = int.MaxValue, + int minLength = 0) + { + if (value == null) + { + throw new ArgumentException($"{parameterName} can not be null!", parameterName); + } + + if (value.Length > maxLength) + { + throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName); + } + + if (minLength > 0 && value.Length < minLength) + { + throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName); + } + + return value; + } + + public static string NotNullOrWhiteSpace( + string value, + string parameterName, + int maxLength = int.MaxValue, + int minLength = 0) + { + if (string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentException($"{parameterName} can not be null, empty or white space!", parameterName); + } + + if (value.Length > maxLength) + { + throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName); + } + + if (minLength > 0 && value.Length < minLength) + { + throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName); + } + + return value; + } + + public static string NotNullOrEmpty( + string value, + string parameterName, + int maxLength = int.MaxValue, + int minLength = 0) + { + if (string.IsNullOrEmpty(value)) + { + throw new ArgumentException($"{parameterName} can not be null or empty!", parameterName); + } + + if (value.Length > maxLength) + { + throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName); + } + + if (minLength > 0 && value.Length < minLength) + { + throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName); + } + + return value; + } + + public static ICollection NotNullOrEmpty(ICollection value, string parameterName) + { + if (value == null || value.Count <= 0) + { + throw new ArgumentException(parameterName + " can not be null or empty!", parameterName); + } + + return value; + } + + public static Type AssignableTo( + Type type, + string parameterName) + { + NotNull(type, parameterName); + + if (!type.IsAssignableTo(typeof(TBaseType))) + { + throw new ArgumentException($"{parameterName} (type of {type.AssemblyQualifiedName}) should be assignable to the {typeof(TBaseType).FullName}!"); + } + + return type; + } + + public static string Length( + [AllowNull] string value, + string parameterName, + int maxLength, + int minLength = 0) + { + if (minLength > 0) + { + if (string.IsNullOrEmpty(value)) + { + throw new ArgumentException(parameterName + " can not be null or empty!", parameterName); + } + + if (value.Length < minLength) + { + throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName); + } + } + + if (value != null && value.Length > maxLength) + { + throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName); + } + + return value!; + } + + public static short Positive( + short value, + string parameterName) + { + if(value == 0) + { + throw new ArgumentException($"{parameterName} is equal to zero"); + } + else if(value < 0) + { + throw new ArgumentException($"{parameterName} is less than zero"); + } + return value; + } + + public static Int32 Positive( + Int32 value, + string parameterName) + { + if (value == 0) + { + throw new ArgumentException($"{parameterName} is equal to zero"); + } + else if (value < 0) + { + throw new ArgumentException($"{parameterName} is less than zero"); + } + return value; + } + + public static Int64 Positive( + Int64 value, + string parameterName) + { + if (value == 0) + { + throw new ArgumentException($"{parameterName} is equal to zero"); + } + else if (value < 0) + { + throw new ArgumentException($"{parameterName} is less than zero"); + } + return value; + } + + public static float Positive( + float value, + string parameterName) + { + if (value == 0) + { + throw new ArgumentException($"{parameterName} is equal to zero"); + } + else if (value < 0) + { + throw new ArgumentException($"{parameterName} is less than zero"); + } + return value; + } + + public static double Positive( + double value, + string parameterName) + { + if (value == 0) + { + throw new ArgumentException($"{parameterName} is equal to zero"); + } + else if (value < 0) + { + throw new ArgumentException($"{parameterName} is less than zero"); + } + return value; + } + + public static decimal Positive( + decimal value, + string parameterName) + { + if (value == 0) + { + throw new ArgumentException($"{parameterName} is equal to zero"); + } + else if (value < 0) + { + throw new ArgumentException($"{parameterName} is less than zero"); + } + return value; + } + + public static Int16 Range( + Int16 value, + string parameterName, + Int16 minimumValue, + Int16 maximumValue = Int16.MaxValue) + { + if(value < minimumValue || value > maximumValue) + { + throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}"); + } + + return value; + } + public static Int32 Range( + Int32 value, + string parameterName, + Int32 minimumValue, + Int32 maximumValue = Int32.MaxValue) + { + if (value < minimumValue || value > maximumValue) + { + throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}"); + } + + return value; + } + + public static Int64 Range( + Int64 value, + string parameterName, + Int64 minimumValue, + Int64 maximumValue = Int64.MaxValue) + { + if (value < minimumValue || value > maximumValue) + { + throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}"); + } + + return value; + } + + + public static float Range( + float value, + string parameterName, + float minimumValue, + float maximumValue = float.MaxValue) + { + if (value < minimumValue || value > maximumValue) + { + throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}"); + } + return value; + } + + + public static double Range( + double value, + string parameterName, + double minimumValue, + double maximumValue = double.MaxValue) + { + if (value < minimumValue || value > maximumValue) + { + throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}"); + } + + return value; + } + + + public static decimal Range( + decimal value, + string parameterName, + decimal minimumValue, + decimal maximumValue = decimal.MaxValue) + { + if (value < minimumValue || value > maximumValue) + { + throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}"); + } + + return value; + } + + public static T NotDefaultOrNull( + T? value, + string parameterName) + where T : struct + { + if (value == null) + { + throw new ArgumentException($"{parameterName} is null!", parameterName); + } + + if (value.Value.Equals(default(T))) + { + throw new ArgumentException($"{parameterName} has a default value!", parameterName); + } + + return value.Value; + } +} diff --git a/common/Tnb.Common/Extension/BooleanExtensions.cs b/common/Tnb.Common/Extension/BooleanExtensions.cs index c163c4f9..5feffa27 100644 --- a/common/Tnb.Common/Extension/BooleanExtensions.cs +++ b/common/Tnb.Common/Extension/BooleanExtensions.cs @@ -31,7 +31,7 @@ public static class BooleanExtensions /// private static bool? GetBool(this object data) { - switch (data.ToString().Trim().ToLower()) + switch (data.ToString()?.Trim().ToLower()) { case "0": return false; diff --git a/common/Tnb.Common/Extension/DictionaryExtensions.cs b/common/Tnb.Common/Extension/DictionaryExtensions.cs index b9b052cd..962810bf 100644 --- a/common/Tnb.Common/Extension/DictionaryExtensions.cs +++ b/common/Tnb.Common/Extension/DictionaryExtensions.cs @@ -16,7 +16,7 @@ public static class DictionaryExtensions /// 要操作的字典. /// 指定键名. /// 获取到的值. - public static TValue GetOrDefault(this IDictionary dictionary, TKey key) + public static TValue? GetOrDefault(this IDictionary dictionary, TKey key) { return dictionary.TryGetValue(key, out TValue value) ? value : default(TValue); } diff --git a/common/Tnb.Common/Extension/EnumExtensions.cs b/common/Tnb.Common/Extension/EnumExtensions.cs index 8e438b11..15206eee 100644 --- a/common/Tnb.Common/Extension/EnumExtensions.cs +++ b/common/Tnb.Common/Extension/EnumExtensions.cs @@ -152,7 +152,7 @@ public static class EnumExtensions /// public static string GetDescription(this System.Enum value) { - return value.GetType().GetMember(value.ToString()).FirstOrDefault()?.GetCustomAttribute()?.Description; + return value.GetType().GetMember(value.ToString()).FirstOrDefault()?.GetCustomAttribute()?.Description ?? string.Empty; } /// @@ -162,7 +162,7 @@ public static class EnumExtensions /// public static string GetDescription(this object value) { - return value.GetType().GetMember(value.ToString() ?? string.Empty).FirstOrDefault()?.GetCustomAttribute()?.Description; + return value.GetType().GetMember(value.ToString() ?? string.Empty).FirstOrDefault()?.GetCustomAttribute()?.Description ?? string.Empty; } /// diff --git a/common/Tnb.Common/Extension/Extensions.cs b/common/Tnb.Common/Extension/Extensions.cs index 0815e1d6..05718f61 100644 --- a/common/Tnb.Common/Extension/Extensions.cs +++ b/common/Tnb.Common/Extension/Extensions.cs @@ -219,7 +219,7 @@ public static partial class Extensions /// private static bool? GetBool(this object data) { - switch (data.ToString().Trim().ToLower()) + switch (data.ToString()?.Trim().ToLower()) { case "0": return false; @@ -466,7 +466,7 @@ public static partial class Extensions { try { - return obj == null ? string.Empty : obj.ToString(); + return obj == null ? string.Empty : obj.ToString()!; } catch { @@ -489,7 +489,7 @@ public static partial class Extensions return string.Join(",", list); } - return obj.ToString(); + return obj.ToString()!; } catch { @@ -678,7 +678,7 @@ public static partial class Extensions /// public static string ObjToString(this object thisValue) { - if (thisValue != null) return thisValue.ToString().Trim(); + if (thisValue != null) return thisValue.ToString()!.Trim(); return string.Empty; } diff --git a/common/Tnb.Common/Extension/RandomExtensions.cs b/common/Tnb.Common/Extension/RandomExtensions.cs index 2e1a62f5..b8466442 100644 --- a/common/Tnb.Common/Extension/RandomExtensions.cs +++ b/common/Tnb.Common/Extension/RandomExtensions.cs @@ -38,7 +38,7 @@ public static class RandomExtensions Array array = System.Enum.GetValues(type); int index = random.Next(array.GetLowerBound(0), array.GetUpperBound(0) + 1); - return (T)array.GetValue(index); + return (T)array.GetValue(index)!; } /// @@ -67,7 +67,7 @@ public static class RandomExtensions public static T NextItem(this Random random, T[] items) { if (items == null || items.Length == 0) - return default(T); + return default(T)!; return items[random.Next(items.Length)]; } diff --git a/common/Tnb.Common/Net/UserAgent.cs b/common/Tnb.Common/Net/UserAgent.cs index cd578d03..3407035a 100644 --- a/common/Tnb.Common/Net/UserAgent.cs +++ b/common/Tnb.Common/Net/UserAgent.cs @@ -111,10 +111,10 @@ public class UserAgent : IUserAgent private DeviceInfo _device; private OSInfo _os; - private bool? _isBot; + //private bool? _isBot; private bool? _isMobileDevice; private bool? _isTablet; - private bool? _isPdfConverter; + //private bool? _isPdfConverter; static UserAgent() { @@ -142,7 +142,7 @@ public class UserAgent : IUserAgent } } - return _rawValue; + return _rawValue ?? ""; } set @@ -151,10 +151,10 @@ public class UserAgent : IUserAgent _userAgent = null; _device = null; _os = null; - _isBot = null; + //_isBot = null; _isMobileDevice = null; _isTablet = null; - _isPdfConverter = null; + //_isPdfConverter = null; } } diff --git a/common/Tnb.Common/Security/CodeGenHelper.cs b/common/Tnb.Common/Security/CodeGenHelper.cs index e06dad23..5bf8591d 100644 --- a/common/Tnb.Common/Security/CodeGenHelper.cs +++ b/common/Tnb.Common/Security/CodeGenHelper.cs @@ -190,7 +190,7 @@ public static class CodeGenHelper field = entityInfo.Columns.Find(it => it.PropertyName.Equals(sort.ToUpperCase()))?.DbColumnName; break; } - return string.IsNullOrEmpty(field) ? null : field; + return string.IsNullOrEmpty(field) ? "" : field; } /// diff --git a/common/Tnb.Common/Security/ComparisonHelper.cs b/common/Tnb.Common/Security/ComparisonHelper.cs index 8248657c..0b540ac0 100644 --- a/common/Tnb.Common/Security/ComparisonHelper.cs +++ b/common/Tnb.Common/Security/ComparisonHelper.cs @@ -63,7 +63,7 @@ public static class ComparisonHelper /// /// /// - public int Compare(T x, T y) + public int Compare(T? x, T? y) { return _comparer.Compare(_keySelector(x), _keySelector(y)); } diff --git a/common/Tnb.Common/Security/EqualityHelper.cs b/common/Tnb.Common/Security/EqualityHelper.cs index 4d33b45b..2dc3b6b4 100644 --- a/common/Tnb.Common/Security/EqualityHelper.cs +++ b/common/Tnb.Common/Security/EqualityHelper.cs @@ -1,4 +1,5 @@ -using JNPF.DependencyInjection; +using System.Diagnostics.CodeAnalysis; +using JNPF.DependencyInjection; namespace JNPF.Common.Security; @@ -49,14 +50,14 @@ public static class EqualityHelper : this(keySelector, EqualityComparer.Default) { } - public bool Equals(T x, T y) + public bool Equals(T? x, T? y) { return _comparer.Equals(_keySelector(x), _keySelector(y)); } - public int GetHashCode(T obj) + public int GetHashCode([DisallowNull] T obj) { - return _comparer.GetHashCode(_keySelector(obj)); + return _comparer.GetHashCode(_keySelector(obj)!); } } } diff --git a/common/Tnb.Common/Security/ExcelExportHelper.cs b/common/Tnb.Common/Security/ExcelExportHelper.cs index 3b975fde..6ffcab01 100644 --- a/common/Tnb.Common/Security/ExcelExportHelper.cs +++ b/common/Tnb.Common/Security/ExcelExportHelper.cs @@ -11,6 +11,7 @@ using NPOI.XSSF.UserModel; namespace JNPF.Common.Security; +#pragma warning disable CS8602, CS0618, CA2200 /// /// Excel导出操作类 /// 版 本:V3.2.0 @@ -740,7 +741,7 @@ public class ExcelExportHelper } catch (Exception ex) { - throw; + throw ex; } } @@ -1039,7 +1040,7 @@ public class ExcelExportHelper } catch (Exception ex) { - throw; + throw ex; } } @@ -1168,4 +1169,5 @@ public class ExcelExportHelper } #endregion -} \ No newline at end of file +} +#pragma warning restore CS8602, CS0618, CA2200 diff --git a/common/Tnb.Common/Security/ExcelImportHelper.cs b/common/Tnb.Common/Security/ExcelImportHelper.cs index bd7e2883..1d1deff8 100644 --- a/common/Tnb.Common/Security/ExcelImportHelper.cs +++ b/common/Tnb.Common/Security/ExcelImportHelper.cs @@ -108,7 +108,7 @@ namespace JNPF.Common.Helper //} //else //{ - dataRow[j] = cell.ToString().Trim(); + dataRow[j] = cell.ToString()?.Trim(); //} } } diff --git a/common/Tnb.Common/Security/FileHelper.cs b/common/Tnb.Common/Security/FileHelper.cs index 35b377ef..519fa06c 100644 --- a/common/Tnb.Common/Security/FileHelper.cs +++ b/common/Tnb.Common/Security/FileHelper.cs @@ -505,6 +505,7 @@ public class FileHelper #endregion +#pragma warning disable CA1416 // 验证平台兼容性 #region 生成高清晰缩略图 /// @@ -669,6 +670,7 @@ public class FileHelper } #endregion +#pragma warning restore CA1416 // 验证平台兼容性 #region 将文件路径转为内存流 @@ -712,7 +714,8 @@ public class FileHelper if (selectFiles.Count > 0) { - return selectFiles.FirstOrDefault().FullName; + //modified by PhilPan + return selectFiles.First().FullName; } return Path.Combine(folderPath, $@"{prefix}_{DateTime.Now.ParseToUnixTime()}.log"); @@ -737,7 +740,7 @@ public class FileHelper Microsoft.AspNetCore.Http.HttpContext? httpContext = App.HttpContext; httpContext.Response.ContentType = "application/octet-stream"; httpContext.Response.Headers.Add("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8)); - httpContext.Response.Headers.Add("Content-Length", buff.Length.ToString()); + httpContext.Response.Headers.Add("Content-Length", buff?.Length.ToString()); httpContext.Response.Body.WriteAsync(buff); httpContext.Response.Body.Flush(); httpContext.Response.Body.Close(); diff --git a/common/Tnb.Common/Security/JsonHelper.cs b/common/Tnb.Common/Security/JsonHelper.cs index cbb2b901..0128eecc 100644 --- a/common/Tnb.Common/Security/JsonHelper.cs +++ b/common/Tnb.Common/Security/JsonHelper.cs @@ -46,7 +46,7 @@ public static class JsonHelper /// public static T ToObject(this string json) { - return _ = _jsonSerializer.Deserialize(json) ?? default(T); + return _jsonSerializer.Deserialize(json); } /// @@ -58,7 +58,7 @@ public static class JsonHelper /// public static T ToObject(this string json, object jsonSerializerOptions = default) { - return _ = _jsonSerializer.Deserialize(json, jsonSerializerOptions) ?? default(T); + return _jsonSerializer.Deserialize(json, jsonSerializerOptions); } /// @@ -69,7 +69,7 @@ public static class JsonHelper /// public static T ToObject(this object json) { - return _ = ToJsonString(json).ToObject() ?? default(T); + return ToJsonString(json).ToObject(); } /// @@ -81,7 +81,7 @@ public static class JsonHelper /// public static T ToObject(this object json, object jsonSerializerOptions = default) { - return _ = ToJsonString(json, jsonSerializerOptions).ToObject(jsonSerializerOptions) ?? default(T); + return ToJsonString(json, jsonSerializerOptions).ToObject(jsonSerializerOptions); } /// @@ -92,7 +92,7 @@ public static class JsonHelper /// public static List ToList(this string json) { - return _ = _jsonSerializer.Deserialize>(json) ?? null; + return _jsonSerializer.Deserialize>(json); } /// @@ -104,7 +104,7 @@ public static class JsonHelper /// public static List ToList(this string json, object jsonSerializerOptions = default) { - return _ = _jsonSerializer.Deserialize>(json, jsonSerializerOptions) ?? null; + return _jsonSerializer.Deserialize>(json, jsonSerializerOptions); } /// diff --git a/common/Tnb.Common/Security/MachineHelper.cs b/common/Tnb.Common/Security/MachineHelper.cs index 43597cb2..e435e49b 100644 --- a/common/Tnb.Common/Security/MachineHelper.cs +++ b/common/Tnb.Common/Security/MachineHelper.cs @@ -217,6 +217,7 @@ public static class MachineHelper } #endregion +#pragma warning disable CA1416 // 验证平台兼容性 #region Windows /// @@ -364,4 +365,6 @@ public static class MachineHelper } #endregion +#pragma warning restore CA1416 // 验证平台兼容性 + } \ No newline at end of file diff --git a/common/Tnb.Common/Security/SuperQueryHelper.cs b/common/Tnb.Common/Security/SuperQueryHelper.cs index eaa6f636..e42973c6 100644 --- a/common/Tnb.Common/Security/SuperQueryHelper.cs +++ b/common/Tnb.Common/Security/SuperQueryHelper.cs @@ -60,7 +60,7 @@ public class SuperQueryHelper { case JnpfKeyConst.COMINPUT: case JnpfKeyConst.TEXTAREA: - item.fieldValue = item.fieldValue?.ToString().Replace("\r\n", string.Empty); + item.fieldValue = item.fieldValue?.ToString()?.Replace("\r\n", string.Empty); switch (item.symbol) { case "==": // 等于 @@ -549,8 +549,9 @@ public class SuperQueryHelper queryOr.mainWhere = true; queryList.Add(queryOr); } + //modified by PhilPan + continue; } - continue; switch (item.symbol) { case ">=": // 大于等于