dengjunjie
2024-12-05 46e98339480d853fc78a014c34d7ff9fcaf13890
ÏîÄ¿´úÂë/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/EntityProperties.cs
@@ -1,5 +1,6 @@
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@@ -8,7 +9,6 @@
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Const;
using WIDESEA_Core.DB.Models;
using WIDESEA_Core.Helper;
namespace WIDESEA_Core.Utilities
@@ -18,7 +18,6 @@
        /// <summary>
        /// éªŒè¯æ•°æ®åº“字段类型与值是否正确,
        /// </summary>
        /// <param name="dbType">数据库字段类型(如varchar,nvarchar,decimal,不要带后面长度如:varchar(50))</param>
        /// <param name="value">值</param>
        /// <param name="propertyInfo">要验证的类的属性,若不为null,则会判断字符串的长度是否正确</param>
        /// <returns>(bool, string, object)bool成否校验成功,string校验失败信息,object,当前校验的值</returns>
@@ -29,7 +28,7 @@
            if (propertyInfo != null)
            {
                sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>();
                dbType = propertyInfo != null ? propertyInfo.GetProperWithDbType() : SqlDbTypeName.NVarChar;
                dbType = propertyInfo.PropertyType != null ? propertyInfo.GetProperWithDbType() : SqlDbTypeName.NVarChar;
            }
            dbType = dbType.ToLower();
            string val = value?.ToString();
@@ -107,6 +106,16 @@
            return (reslutMsg == "" ? true : false, reslutMsg, value);
        }
        public static List<(bool, string, object)> ValidationValueForDbType(this PropertyInfo propertyInfo, params object[] values)
        {
            List<(bool, string, object)> result = new List<(bool, string, object)>();
            foreach (object value in values)
            {
                result.Add(propertyInfo.ValidationVal(value));
            }
            return result;
        }
        private static readonly Dictionary<Type, string> ProperWithDbType = new Dictionary<Type, string>() {
            {  typeof(string),SqlDbTypeName.NVarChar },
            { typeof(DateTime),SqlDbTypeName.DateTime},
@@ -118,6 +127,7 @@
            {  typeof(byte),SqlDbTypeName.Int },//类型待完
            { typeof(Guid),SqlDbTypeName.UniqueIdentifier}
        };
        public static string GetProperWithDbType(this PropertyInfo propertyInfo)
        {
            bool result = ProperWithDbType.TryGetValue(propertyInfo.PropertyType, out string value);
@@ -128,19 +138,6 @@
            return SqlDbTypeName.NVarChar;
        }
        public static string ValidateDicInEntity(this Type typeinfo, List<Dictionary<string, object>> dicList, bool removerKey, string[] ignoreFields = null)
        {
            PropertyInfo[] propertyInfo = typeinfo.GetProperties();
            string reslutMsg = string.Empty;
            foreach (Dictionary<string, object> dic in dicList)
            {
                reslutMsg = typeinfo.ValidateDicInEntity(dic, removerKey, propertyInfo, ignoreFields);
                if (!string.IsNullOrEmpty(reslutMsg))
                    return reslutMsg;
            }
            return reslutMsg;
        }
        /// <summary>
        /// åˆ¤æ–­hash的列是否为对应的实体,并且值是否有效
        /// </summary>
@@ -149,7 +146,7 @@
        /// <param name="removeNotContains">移除不存在字段</param>
        /// <param name="removerKey">移除主键</param>
        /// <returns></returns>
        public static string ValidateDicInEntity(this Type typeinfo, Dictionary<string, object> dic, bool removerKey, PropertyInfo[] propertyInfo, string[] ignoreFields = null)
        public static string ValidateDicInEntity(this Type typeinfo, Dictionary<string, object> dic, bool removerKey, PropertyInfo[] propertyInfo, string[]? ignoreFields = null)
        {
            if (dic == null || dic.Count == 0) { return "参数无效"; }
@@ -171,30 +168,55 @@
            foreach (PropertyInfo property in propertyInfo)
            {
                SugarColumn sugarColumn = property.GetCustomAttribute<SugarColumn>();
                SugarColumn? sugarColumn = property.GetCustomAttribute<SugarColumn>();
                if (sugarColumn == null)
                {
                    Navigate? navigate = property.GetCustomAttribute<Navigate>();
                    if(navigate != null)
                    {
                        continue;
                    }
                    return "请配置SugarColumn属性";
                }
                //忽略与主键的字段不做验证
                if (property.Name == keyName.FirstLetterToUpper() || (ignoreFields != null && ignoreFields.Contains(property.Name)) || sugarColumn.IsOnlyIgnoreInsert || sugarColumn.IsOnlyIgnoreUpdate || sugarColumn.IsIgnore)
                    continue;
                //不在编辑中的列,是否也要必填
                if (!dic.ContainsKey(property.Name.FirstLetterToLower()))
                if (!dic.ContainsKey(property.Name.FirstLetterToLower()) /*&& !dic.ContainsKey(property.Name.FirstLetterToUpper())*/)
                {
                    if (!sugarColumn.IsNullable)
                    {
                        if (sugarColumn.DefaultValue != null)
                        if (sugarColumn.DefaultValue == null)
                            return sugarColumn.ColumnDescription + "为必须提交项";
                        continue;
                    }
                    continue;
                }
                string str = dic[property.Name.FirstLetterToLower()].ToString();
                //将所有空值设置为null
                if (dic[property.Name.FirstLetterToLower()] != null && str == string.Empty)
                    dic[property.Name.FirstLetterToLower()] = null;
                if(dic[property.Name.FirstLetterToLower()] != null)
                {
                    string str = dic[property.Name.FirstLetterToLower()].ToString();
                    //将所有空值设置为null
                    if (str == string.Empty)
                        dic[property.Name.FirstLetterToLower()] = null;
                }
            }
            return string.Empty;
        }
        public static string ValidateDicInEntity(this Type typeinfo, List<Dictionary<string, object>> dicList, bool removerKey, string[] ignoreFields = null)
        {
            PropertyInfo[] propertyInfo = typeinfo.GetProperties();
            string reslutMsg = string.Empty;
            foreach (Dictionary<string, object> dic in dicList)
            {
                reslutMsg = typeinfo.ValidateDicInEntity(dic, removerKey, propertyInfo, ignoreFields);
                if (!string.IsNullOrEmpty(reslutMsg))
                    return reslutMsg;
            }
            return reslutMsg;
        }
        public static string GetKeyName(this Type typeinfo)
@@ -206,9 +228,12 @@
        {
            foreach (PropertyInfo property in properties)
            {
                SugarColumn sugarColumn = property.GetCustomAttribute<SugarColumn>();
                if (sugarColumn.IsPrimaryKey)
                    return property.Name;
                SugarColumn? sugarColumn = property.GetCustomAttribute<SugarColumn>();
                if (sugarColumn != null)
                {
                    if (sugarColumn.IsPrimaryKey)
                        return property.Name;
                }
            }
            return null;
        }