1
hutongqing
2024-10-29 9ca96199d92168fe221dda9aba56f55520a561d8
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)