From 9ca96199d92168fe221dda9aba56f55520a561d8 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期二, 29 十月 2024 17:30:59 +0800
Subject: [PATCH] 1

---
 WIDESEAWCS_Server/WIDESEAWCS_Core/Utilities/ModelValidate.cs |   77 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 77 insertions(+), 0 deletions(-)

diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Core/Utilities/ModelValidate.cs b/WIDESEAWCS_Server/WIDESEAWCS_Core/Utilities/ModelValidate.cs
index 005239e..f60f92f 100644
--- a/WIDESEAWCS_Server/WIDESEAWCS_Core/Utilities/ModelValidate.cs
+++ b/WIDESEAWCS_Server/WIDESEAWCS_Core/Utilities/ModelValidate.cs
@@ -5,6 +5,7 @@
 using System.Text;
 using System.Threading.Tasks;
 using WIDESEAWCS_Core.Attributes;
+using WIDESEAWCS_Core.Helper;
 
 namespace WIDESEAWCS_Core.Utilities
 {
@@ -26,6 +27,12 @@
             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);
@@ -111,6 +118,76 @@
                             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)

--
Gitblit v1.9.3