647556386
2025-10-18 d01658c63cd541fe4ea5cec5c4bd7f23b9408cdb
WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs
@@ -31,7 +31,14 @@
            PropertyInfo[] propertyInfos = modelType.GetProperties();
            return SimpleValidate(propertyInfos, data);
        }
        public static (bool, string, object?) ValidateModelData(object data, Type type)
        {
            if (data == null) return (false, "传入参数不可为null", data);
            ModelValidateAttribute? modelAttribute = type.GetCustomAttribute<ModelValidateAttribute>();
            if (modelAttribute == null) return (false, $"{type.Name}未定义【ModelValidateAttribute】特性", data);
            PropertyInfo[] propertyInfos = type.GetProperties();
            return SimpleValidate(propertyInfos, data);
        }
        /// <summary>
        /// 验证实体参数
        /// </summary>
@@ -92,35 +99,63 @@
                    if (propertyAttribute == null) continue;
                    object? value = propertyInfo.GetValue(data, null);
                    // 提取描述(减少重复代码)
                    string propDesc = string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description;
                    // 非空验证(保持不变)
                    if (propertyAttribute.NotNullAndEmpty)
                    {
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data);
                        if (string.IsNullOrEmpty(value.ToString())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为空字符串", data);
                        if (value == null) return (false, $"{propDesc}不可为null", data);
                        if (string.IsNullOrEmpty(value.ToString())) return (false, $"{propDesc}不可为空字符串", data);
                    }
                    // 最小值验证(核心修改:兼容小数)
                    if (propertyAttribute.MinValue > int.MinValue)
                    {
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data);
                        if (value == null) return (false, $"{propDesc}不可为null", data);
                        // 关键:先将值转换为decimal(支持小数),避免直接转int报错
                        if (!decimal.TryParse(value.ToString(), out decimal valueDecimal))
                        {
                            return (false, $"{propDesc}的值【{value}】不是有效的数值格式(需为整数或小数)", data);
                        }
                        // 将int类型的MinValue转为decimal再比较(int可隐式转decimal)
                        decimal minValue = propertyAttribute.MinValue;
                        if (propertyAttribute.IsContainMinValue)
                        {
                            if (Convert.ToInt32(value.ToString()) < propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值【{value}】不可小于【{propertyAttribute.MinValue}】", data);
                            if (valueDecimal < minValue)
                                return (false, $"{propDesc}的值【{value}】不可小于【{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 (valueDecimal <= minValue)
                                return (false, $"{propDesc}的值【{value}】要大于【{minValue}】", data);
                        }
                    }
                    // 最大值验证(核心修改:兼容小数)
                    if (propertyAttribute.MaxValue < int.MaxValue)
                    {
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data);
                        if (value == null) return (false, $"{propDesc}不可为null", data);
                        // 同样先转decimal
                        if (!decimal.TryParse(value.ToString(), out decimal valueDecimal))
                        {
                            return (false, $"{propDesc}的值【{value}】不是有效的数值格式(需为整数或小数)", data);
                        }
                        // int转decimal比较
                        decimal maxValue = propertyAttribute.MaxValue;
                        if (propertyAttribute.IsContainMaxValue)
                        {
                            if (Convert.ToInt32(value.ToString()) >= propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值【{value}】不可大于【{propertyAttribute.MaxValue}】", data);
                            if (valueDecimal > maxValue)
                                return (false, $"{propDesc}的值【{value}】不可大于【{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 (valueDecimal >= maxValue)
                                return (false, $"{propDesc}的值【{value}】要小于【{maxValue}】", data);
                        }
                    }