From c020f31a67fc5aa5644511bddff075f7ecc85234 Mon Sep 17 00:00:00 2001
From: qinchulong <qinchulong@hnkhzn.com>
Date: 星期二, 27 五月 2025 15:35:27 +0800
Subject: [PATCH] Merge branch 'master' of http://115.159.85.185:8098/r/HuaYiZhongHeng/ZhongHeLiTiKu

---
 代码管理/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs |  205 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 205 insertions(+), 0 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs"
new file mode 100644
index 0000000..3d50266
--- /dev/null
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs"
@@ -0,0 +1,205 @@
+锘縰sing Microsoft.AspNetCore.Mvc.ModelBinding;
+using Microsoft.Extensions.DependencyModel;
+using Microsoft.IdentityModel.Tokens;
+using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEA_Core.Attributes;
+using WIDESEA_Core.Helper;
+
+namespace WIDESEA_Core.Utilities
+{
+    public class ModelValidate
+    {
+        /// <summary>
+        /// 楠岃瘉瀹炰綋鍙傛暟
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="data"></param>
+        /// <returns></returns>
+        public static (bool, string, object?) ValidateModelData<T>(T data) where T : class, new()
+        {
+            Type modelType = typeof(T);
+            if (data == null) return (false, "浼犲叆鍙傛暟涓嶅彲涓簄ull", data);
+            ModelValidateAttribute? modelAttribute = modelType.GetCustomAttribute<ModelValidateAttribute>();
+            if (modelAttribute == null) return (false, $"{modelType.Name}鏈畾涔夈�怣odelValidateAttribute銆戠壒鎬�", data);
+            PropertyInfo[] propertyInfos = modelType.GetProperties();
+            return SimpleValidate(propertyInfos, data);
+        }
+
+        /// <summary>
+        /// 楠岃瘉瀹炰綋鍙傛暟
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="data"></param>
+        /// <returns></returns>
+        public static (bool, string, object?) ValidateModelData<T>(List<T> datas) where T : class, new()
+        {
+            Type modelType = typeof(T);
+            if (datas == null) return (false, "浼犲叆鍙傛暟涓嶅彲涓簄ull", datas);
+            if (datas.Count == 0) return (false, "闆嗗悎涓暟涓嶅彲绛変簬0", datas);
+            foreach (T data in datas)
+            {
+                if (data == null) return (false, "浼犲叆鍙傛暟涓嶅彲涓簄ull", data);
+                ModelValidateAttribute? modelAttribute = modelType.GetCustomAttribute<ModelValidateAttribute>();
+                if (modelAttribute == null) return (false, $"{modelType.Name}鏈畾涔夈�怣odelValidateAttribute銆戠壒鎬�", data);
+                PropertyInfo[] propertyInfos = modelType.GetProperties();
+                (bool, string, object?) result = SimpleValidate(propertyInfos, data);
+                if (!result.Item1) return result;
+            }
+
+            return (true, $"鎴愬姛", datas);
+        }
+
+        //private static (bool, string, object?) CustomMethodValidate<T>(ModelValidateAttribute modelAttribute, T data) where T : class, new()
+        //{
+        //    try
+        //    {
+        //        if (modelAttribute.CustomValidateMethodTypeName == null) return (false, $"鑷畾涔夐獙璇佹柟娉曢渶瑕佹彁渚涙柟娉曟墍鍦ㄧ被鐨勭被鍨嬪璞�", data);
+        //        if (modelAttribute.CustomValidateMethodName == null) return (false, $"鑷畾涔夐獙璇佹柟娉曢渶瑕佹彁渚涙柟娉曞悕", data);
+
+        //        string path = Path.Combine(AppContext.BaseDirectory, $"{modelAttribute.CustomValidateAssemblyName}.dll");
+        //        Assembly assembly = Assembly.LoadFrom(path);
+
+        //        Type t = assembly.GetType(modelAttribute.CustomValidateAssemblyName + "." + modelAttribute.CustomValidateMethodTypeName);
+        //        object bbb = App.GetService(t);
+        //        MethodInfo? methodInfo = t.GetMethod(modelAttribute.CustomValidateMethodName);
+        //        var result = methodInfo.Invoke(bbb, new object[] { data });
+        //        //MethodInfo? methodInfo = modelAttribute.CustomValidateMethodType.GetMethod(modelAttribute.CustomValidateMethodName);
+        //        //if (methodInfo == null) return (false, $"鏈湪璇ョ被鍨嬪璞°�恵modelAttribute.CustomValidateMethodType.Name}銆戜腑鎵惧埌璇ユ柟娉曘�恵modelAttribute.CustomValidateMethodName}銆�", data);
+        //        //methodInfo.GetGenericArguments()
+        //        return (true, "鎴愬姛", data);
+        //    }
+        //    catch(Exception ex)
+        //    {
+        //        throw new Exception();
+        //    }
+
+        //}
+
+        private static (bool, string, object?) SimpleValidate<T>(PropertyInfo[] propertyInfos, T data) where T : class, new()
+        {
+            try
+            {
+                foreach (PropertyInfo propertyInfo in propertyInfos)
+                {
+                    PropertyValidateAttribute? propertyAttribute = propertyInfo.GetCustomAttribute<PropertyValidateAttribute>();
+                    if (propertyAttribute == null) continue;
+
+                    object? value = propertyInfo.GetValue(data, null);
+                    if (propertyAttribute.NotNullAndEmpty)
+                    {
+                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}涓嶅彲涓簄ull", data);
+                        if (string.IsNullOrEmpty(value.ToString())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}涓嶅彲涓虹┖瀛楃涓�", data);
+                    }
+
+                    if (propertyAttribute.MinValue > int.MinValue)
+                    {
+                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}涓嶅彲涓簄ull", data);
+                        if (propertyAttribute.IsContainMinValue)
+                        {
+                            if (Convert.ToInt32(value.ToString()) < propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}鐨勫�笺�恵value}銆戜笉鍙皬浜庛�恵propertyAttribute.MinValue}銆�", data);
+                        }
+                        else
+                        {
+                            if (Convert.ToInt32(value.ToString()) <= propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}鐨勫�笺�恵value}銆戣澶т簬銆恵propertyAttribute.MinValue}銆�", data);
+                        }
+                    }
+
+                    if (propertyAttribute.MaxValue < int.MaxValue)
+                    {
+                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}涓嶅彲涓簄ull", data);
+                        if (propertyAttribute.IsContainMaxValue)
+                        {
+                            if (Convert.ToInt32(value.ToString()) >= propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}鐨勫�笺�恵value}銆戜笉鍙ぇ浜庛�恵propertyAttribute.MaxValue}銆�", data);
+                        }
+                        else
+                        {
+                            if (Convert.ToInt32(value.ToString()) > propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}鐨勫�笺�恵value}銆戣灏忎簬銆恵propertyAttribute.MaxValue}銆�", data);
+                        }
+                    }
+
+                    if (!string.IsNullOrEmpty(propertyAttribute.NotNullAndEmptyWithProperty))
+                    {
+                        PropertyInfo? property = propertyInfos.FirstOrDefault(x => x.Name == propertyAttribute.NotNullAndEmptyWithProperty);
+                        if (property != null)
+                        {
+                            object? tempValue = property.GetValue(data);
+                            if (tempValue != null && !string.IsNullOrEmpty(tempValue.ToString()))
+                            {
+                                Type[] types = propertyInfo.PropertyType.GenericTypeArguments;
+                                if (types.Length == 1)
+                                {
+                                    string str = value.Serialize();
+                                    if (str == "[]")
+                                    {
+                                        return (false, $"銆恵property.Name}銆戝睘鎬х殑鍊奸潪绌烘椂銆恵propertyInfo.Name}銆戝睘鎬х殑鍊间篃涓嶅彲涓虹┖", data);
+                                    }
+                                }
+                                else if (types.Length == 0)
+                                {
+                                    if (value == null)
+                                    {
+                                        return (false, $"銆恵property.Name}銆戝睘鎬х殑鍊奸潪绌烘椂銆恵propertyInfo.Name}銆戝睘鎬х殑鍊间篃涓嶅彲涓虹┖", data);
+                                    }
+                                }
+
+                            }
+                        }
+                        else
+                        {
+                            return (false, $"鏈壘鍒般�恵propertyInfo.Name}銆戠壒鎬у睘鎬с�恵propertyAttribute.NotNullAndEmptyWithProperty}銆�", data);
+                        }
+                    }
+
+                    if (propertyAttribute.NotNullAndEmptyWithPropertyAndValue != null && propertyAttribute.NotNullAndEmptyWithPropertyAndValue.Length == 2)
+                    {
+                        PropertyInfo? property = propertyInfos.FirstOrDefault(x => x.Name == propertyAttribute.NotNullAndEmptyWithPropertyAndValue[0]);
+                        if (property != null)
+                        {
+                            object? tempValue = property.GetValue(data);
+                            if (tempValue != null && !string.IsNullOrEmpty(tempValue.ToString()))
+                            {
+                                Type[] types = propertyInfo.PropertyType.GenericTypeArguments;
+                                if (types.Length == 1)
+                                {
+                                    if (tempValue.ChangeType(property.PropertyType).ToString() == propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1].ChangeType(property.PropertyType).ToString())
+                                    {
+                                        string str = value.Serialize();
+                                        if (str == "[]")
+                                        {
+                                            return (false, $"銆恵property.Name}銆戝睘鎬х殑鍊间负銆恵propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1]}銆戞椂銆恵propertyInfo.Name}銆戝睘鎬х殑鍊间笉鍙负绌�", data);
+                                        }
+                                    }
+                                       
+                                }
+                                else if (types.Length == 0)
+                                {
+                                    if (tempValue.ChangeType(property.PropertyType).ToString() == propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1].ChangeType(property.PropertyType).ToString() && value == null && string.IsNullOrEmpty(value.ToString()))
+                                    {
+                                        return (false, $"銆恵property.Name}銆戝睘鎬х殑鍊间负銆恵propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1]}銆戞椂銆恵propertyInfo.Name}銆戝睘鎬х殑鍊间笉鍙负绌�", data);
+                                    }
+                                }
+
+                            }
+                        }
+                        else
+                        {
+                            return (false, $"鏈壘鍒板睘鎬с�恵propertyAttribute.NotNullAndEmptyWithProperty}銆�", data);
+                        }
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                return (false, $"鏁版嵁楠岃瘉寮傚父,寮傚父淇℃伅:{ex.Message}", data);
+            }
+            return (true, "楠岃瘉鎴愬姛", data);
+        }
+    }
+}

--
Gitblit v1.9.3