diff --git a/API/TaskManager.Entity/Data/SUPPLIER_EMPLOYEE_DT.cs b/API/TaskManager.Entity/Data/SUPPLIER_EMPLOYEE_DT.cs index ec3571c..642bfdd 100644 --- a/API/TaskManager.Entity/Data/SUPPLIER_EMPLOYEE_DT.cs +++ b/API/TaskManager.Entity/Data/SUPPLIER_EMPLOYEE_DT.cs @@ -1,6 +1,7 @@ using Magicodes.ExporterAndImporter.Core; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -89,6 +90,7 @@ namespace TaskManager.Entity /// [ExporterHeader(DisplayName = "是否有资质(Y,N)")] [ImporterHeader(Name = "是否有资质(Y,N)")] + [RegularExpression("^[YN]$", ErrorMessage = "是否有资质 只能填写 Y 或 N")] public string HaveQuantity { get; set; } /// /// 供应商修改时间,格式(yyyy-MM-ddHH:mm:ss) @@ -113,18 +115,20 @@ namespace TaskManager.Entity /// [ExporterHeader(DisplayName = "资质等级")] [ImporterHeader(Name = "资质等级")] + [RegularExpression("^(Level_4|Level_3|Level_2|Level_1)$", ErrorMessage = "资质等级 只能填写 Level_4,Level_3,Level_2, Level_1 其中之一")] + public string QualificationLevel { get; set; } /// /// 资质获取时间 /// [ExporterHeader(DisplayName = "资质获取时间")] - [ImporterHeader(Name = "资质获取时间")] + [ImporterHeader(Name = "资质获取时间", Format = "yyyy-MM-dd HH:mm:ss")] public string CheckInTime { get; set; } /// /// 资质失去时间 /// [ExporterHeader(DisplayName = "资质失去时间")] - [ImporterHeader(Name = "资质失去时间")] + [ImporterHeader(Name = "资质失去时间", Format = "yyyy-MM-dd HH:mm:ss")] public string CheckOutTime { get; set; } } } diff --git a/API/TaskManager.Entity/Data/SUPPLIER_INFO_DT.cs b/API/TaskManager.Entity/Data/SUPPLIER_INFO_DT.cs index 63782a1..8b5430d 100644 --- a/API/TaskManager.Entity/Data/SUPPLIER_INFO_DT.cs +++ b/API/TaskManager.Entity/Data/SUPPLIER_INFO_DT.cs @@ -1,6 +1,7 @@ using Magicodes.ExporterAndImporter.Core; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -77,12 +78,13 @@ namespace TaskManager.Entity /// [ExporterHeader(DisplayName = "是否关键工位(Y/N)")] [ImporterHeader(Name = "是否关键工位(Y/N)")] + [RegularExpression("^[YN]$", ErrorMessage = "是否关键工位 只能填写 Y 或 N")] public string KeyStation { get; set; } /// /// 供应商修改时间,格式(yyyy-MM-ddHH:mm:ss) /// [ExporterHeader(DisplayName = "供应商修改时间")] - [ImporterHeader(Name = "供应商修改时间")] + [ImporterHeader(Name = "供应商修改时间", Format = "yyyy-MM-dd HH:mm:ss")] public string DataUpdateTime { get; set; } /// /// 产线顺序 diff --git a/API/TaskManager.Entity/Data/SUPPLIER_PRO_ATTACHMENT_DATA_DT.cs b/API/TaskManager.Entity/Data/SUPPLIER_PRO_ATTACHMENT_DATA_DT.cs index a14ef00..86aa33d 100644 --- a/API/TaskManager.Entity/Data/SUPPLIER_PRO_ATTACHMENT_DATA_DT.cs +++ b/API/TaskManager.Entity/Data/SUPPLIER_PRO_ATTACHMENT_DATA_DT.cs @@ -42,7 +42,7 @@ namespace TaskManager.Entity.Entitys /// 生成时间,格式(yyyy-MM-ddHH:mm:ss) /// [ExporterHeader(DisplayName = "生成时间")] - [ImporterHeader(Name = "生成时间")] + [ImporterHeader(Name = "生成时间", Format = "yyyy-MM-dd HH:mm:ss")] public string DateTime { get; set; } /// /// 产线名称 diff --git a/API/TaskManager.Entity/Data/SUPPLIER_PRO_PROCESS_EQUIPMENT_DT.cs b/API/TaskManager.Entity/Data/SUPPLIER_PRO_PROCESS_EQUIPMENT_DT.cs index 3ca0595..c1f2726 100644 --- a/API/TaskManager.Entity/Data/SUPPLIER_PRO_PROCESS_EQUIPMENT_DT.cs +++ b/API/TaskManager.Entity/Data/SUPPLIER_PRO_PROCESS_EQUIPMENT_DT.cs @@ -83,7 +83,7 @@ namespace TaskManager.Entity.Entitys /// 工艺装备制造日期,格式(yyyy-MM-dd) /// [ExporterHeader(DisplayName = "工艺装备制造日期")] - [ImporterHeader(Name = "工艺装备制造日期")] + [ImporterHeader(Name = "工艺装备制造日期", Format = "yyyy-MM-dd HH:mm:ss")] public string ProductionDate { get; set; } /// /// 主要材质 @@ -155,7 +155,7 @@ namespace TaskManager.Entity.Entitys /// 最近校准日期,格式(yyyy-MM-ddHH:mm:ss) /// [ExporterHeader(DisplayName = "最近校准日期")] - [ImporterHeader(Name = "最近校准日期")] + [ImporterHeader(Name = "最近校准日期", Format = "yyyy-MM-dd HH:mm:ss")] public string CalibrationDate { get; set; } /// /// 校准到期天数 diff --git a/API/Wood.Service/Commons/ValidationHelper.cs b/API/Wood.Service/Commons/ValidationHelper.cs index b1b6878..ad4fa4a 100644 --- a/API/Wood.Service/Commons/ValidationHelper.cs +++ b/API/Wood.Service/Commons/ValidationHelper.cs @@ -1,9 +1,12 @@ using Magicodes.ExporterAndImporter.Core; +using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.IdentityModel.Tokens; +using System.Globalization; using System.Reflection; - +using Wood.Util.ExtensionMethods; +using static Dapper.SqlMapper; namespace Wood.Service { public static class ValidationHelper @@ -12,7 +15,7 @@ namespace Wood.Service { var errors = new List(); var entityType = dbContext.Model.FindEntityType(typeof(T)); - int rowNum = 1; + int rowNum = 2; foreach (var data in dataList) { foreach (var property in entityType.GetProperties()) @@ -25,10 +28,24 @@ namespace Wood.Service if (!string.IsNullOrEmpty(value) && value.Length > maxLength) { - string title = property.GetImporterHeader(); + string title = property.GetImporterHeader_Name(); errors.Add($"第{rowNum}行数据错误:字段 {(title.IsNullOrEmpty() ? property.Name : title)} 的长度超过限制 ({value.Length} > {maxLength})"); } } + string format = property.GetImporterHeader_Format(); + if (format.HasValue()) + { + if (format == "yyyy-MM-dd" || format == "yyyy-MM-dd HH:mm:ss") + { + var value = property.PropertyInfo.GetValue(data) as string; + if (DateTime.TryParse(value, out DateTime result) == false) + { + string title = property.GetImporterHeader_Name(); + errors.Add($"第{rowNum}行数据错误:字段 {(title.IsNullOrEmpty() ? property.Name : title)} 的格式错误 (格式:{format},字段值:{value})"); + } + + } + } } rowNum++; } @@ -39,11 +56,39 @@ namespace Wood.Service public static class PropertyExtension { - public static string GetImporterHeader(this IProperty value) + public static string GetImporterHeader_Name(this IProperty value) { string ret = value?.PropertyInfo?.GetCustomAttribute()?.Name; return ret; } + + public static string GetImporterHeader_Format(this IProperty value) + { + string ret = value?.PropertyInfo?.GetCustomAttribute()?.Format; + return ret; + } + } + + public static class SearchControlExtension + { + /// + /// 忽略双字节的PadRight功能,汉字的length = 2 + /// + /// + /// + /// + /// + public static string PadRightByte(this string str, int totalWidth, char paddingChar = ' ') + { + if (str == null) + { + str = ""; + } + int strLen = System.Text.UTF8Encoding.Default.GetBytes(str).Length; + int actualWidth = (totalWidth > strLen) ? (totalWidth - strLen) : 0; + + return str + "".PadLeft(actualWidth, paddingChar); + } } } diff --git a/API/Wood.Service/Datas/SupplierEmployeeDtService.cs b/API/Wood.Service/Datas/SupplierEmployeeDtService.cs index 96e2daf..4c3f4a6 100644 --- a/API/Wood.Service/Datas/SupplierEmployeeDtService.cs +++ b/API/Wood.Service/Datas/SupplierEmployeeDtService.cs @@ -70,7 +70,14 @@ namespace Wood.Service.Datas } throw new Exception(sb.ToString()); } - var entityLst = impResult.Data; + var entityLst = impResult.Data.ToList(); + + // 校验数据长度 + var validationErrors = ValidationHelper.ValidateDataLength(entityLst, _context); + if (validationErrors.Any()) + { + throw new Exception("数据校验失败:" + string.Join(", ", validationErrors)); + } using var transaction = _context.Database.BeginTransaction(); try diff --git a/API/Wood.Service/Datas/SupplierInfoDtService.cs b/API/Wood.Service/Datas/SupplierInfoDtService.cs index 916b1e0..c8ca4d3 100644 --- a/API/Wood.Service/Datas/SupplierInfoDtService.cs +++ b/API/Wood.Service/Datas/SupplierInfoDtService.cs @@ -69,7 +69,14 @@ namespace Wood.Service.Datas } throw new Exception(sb.ToString()); } - var entityLst = impResult.Data; + var entityLst = impResult.Data.ToList(); + + // 校验数据长度 + var validationErrors = ValidationHelper.ValidateDataLength(entityLst, _context); + if (validationErrors.Any()) + { + throw new Exception("数据校验失败:" + string.Join(", ", validationErrors)); + } using var transaction = _context.Database.BeginTransaction(); try diff --git a/API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs b/API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs index bfbe29f..003a4fe 100644 --- a/API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs +++ b/API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs @@ -70,7 +70,14 @@ namespace Wood.Service.Datas } throw new Exception(sb.ToString()); } - var entityLst = impResult.Data; + var entityLst = impResult.Data.ToList(); + + // 校验数据长度 + var validationErrors = ValidationHelper.ValidateDataLength(entityLst, _context); + if (validationErrors.Any()) + { + throw new Exception("数据校验失败:" + string.Join(", ", validationErrors)); + } using var transaction = _context.Database.BeginTransaction(); try diff --git a/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs b/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs index a7ffca2..d4ac2ff 100644 --- a/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs +++ b/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs @@ -70,7 +70,14 @@ namespace Wood.Service.Datas } throw new Exception(sb.ToString()); } - var entityLst = impResult.Data; + var entityLst = impResult.Data.ToList(); + + // 校验数据长度 + var validationErrors = ValidationHelper.ValidateDataLength(entityLst, _context); + if (validationErrors.Any()) + { + throw new Exception("数据校验失败:" + string.Join(", ", validationErrors)); + } using var transaction = _context.Database.BeginTransaction(); try diff --git a/API/Wood.Util/ExtensionMethods/TypeConvertExtension.cs b/API/Wood.Util/ExtensionMethods/TypeConvertExtension.cs new file mode 100644 index 0000000..f101e40 --- /dev/null +++ b/API/Wood.Util/ExtensionMethods/TypeConvertExtension.cs @@ -0,0 +1,412 @@ +using System; +using System.ComponentModel; +using System.Reflection; + +namespace Wood.Util.ExtensionMethods +{ + public static class TypeConvertExtension + { + #region string + public static int? TryToInt(this string p_str) + { + int resu; + bool isSucc = int.TryParse(p_str, out resu); + if (isSucc) + { + return resu; + } + else + { + return null; + } + } + + public static int TryToIntZero(this string p_str) + { + return p_str.TryToInt() ?? 0; + } + + public static double? TryToDouble(this string p_str) + { + double resu; + bool isSucc = double.TryParse(p_str, out resu); + if (isSucc) + { + return resu; + } + else + { + return null; + } + } + + public static double TryToDoubleZero(this string p_str) + { + return p_str.TryToDouble() ?? 0; + } + + public static DateTime? TryToDateTime(this string p_str) + { + if (p_str == null) + { + return null; + } + string str = p_str.ToString(); + DateTime resu; + bool isSucc = DateTime.TryParse(str, out resu); + if (isSucc) + { + return resu; + } + else + { + return null; + } + } + + public static DateTime TryToDateTime1900(this string p_str) + { + var ret = p_str.TryToDateTime(); + if (ret == null) + { + return Convert.ToDateTime("1900-01-01"); + } + if (ret < Convert.ToDateTime("1900-01-01")) + { + return Convert.ToDateTime("1900-01-01"); + } + else + { + return (DateTime)ret; + } + } + + + public static bool? TryToBool(this string p_str) + { + if (p_str == null) + { + return null; + } + bool ret; + bool isSucc = Boolean.TryParse(p_str, out ret); + if (isSucc) + { + return ret; + } + else + { + return null; + } + + } + + public static decimal? TryToDecimal(this string p_str) + { + if (p_str == null) + { + return null; + } + decimal ret; + bool isSucc = Decimal.TryParse(p_str, out ret); + if (isSucc) + { + return ret; + } + else + { + return null; + } + } + + public static decimal TryToDecimalZero(this string p_str) + { + return p_str.TryToDecimal() ?? 0.0M; + } + + #endregion + + #region object + public static int? TryToInt(this object p_obj) + { + if (p_obj == null) + { + return null; + } + string str = p_obj?.ToString(); + int resu; + bool isSucc = int.TryParse(str, out resu); + if (isSucc) + { + return resu; + } + else + { + return null; + } + } + + public static int TryToIntZero(this object p_obj) + { + int? ret = TryToInt(p_obj); + return ret ?? 0; + } + + public static double? TryToDouble(this object p_obj) + { + if (p_obj == null) + { + return null; + } + string str = p_obj?.ToString(); + double resu; + bool isSucc = double.TryParse(str, out resu); + if (isSucc) + { + return resu; + } + else + { + return null; + } + } + + public static double TryToDoubleZero(this object p_obj) + { + return p_obj.TryToDouble() ?? 0; + } + + public static DateTime? TryToDateTime(this object p_obj) + { + if (p_obj == null) + { + return null; + } + string str = p_obj?.ToString(); + DateTime resu; + bool isSucc = DateTime.TryParse(str, out resu); + if (isSucc) + { + return resu; + } + else + { + return null; + } + } + + public static DateTime TryToDateTime1900(this object p_obj) + { + var ret = p_obj.TryToDateTime(); + if (ret == null) + { + return Convert.ToDateTime("1900-01-01"); + } + if (ret < Convert.ToDateTime("1900-01-01")) + { + return Convert.ToDateTime("1900-01-01"); + } + else + { + return (DateTime)ret; + } + } + + public static bool? TryToBool(this object p_obj) + { + if (p_obj == null) + { + return null; + } + string str = p_obj?.ToString(); + bool resu; + bool isSucc = Boolean.TryParse(str, out resu); + if (isSucc) + { + return resu; + } + else + { + return null; + } + } + + public static decimal? TryToDecimal(this object p_obj) + { + if (p_obj == null) + { + return null; + } + string str = p_obj?.ToString(); + decimal ret; + bool isSucc = Decimal.TryParse(str, out ret); + if (isSucc) + { + return ret; + } + else + { + return null; + } + } + + public static decimal TryToDecimalZero(this object p_obj) + { + return p_obj.TryToDecimal() ?? 0.0M; + } + #endregion + + public static bool HasValue(this string p_str) + { + return string.IsNullOrEmpty(p_str?.Trim()) ? false : true; + } + + public static bool IsNullOrEmpty(this string p_str) + { + return string.IsNullOrEmpty(p_str?.Trim()) ? true : false; + } + + public static string TryToEmptyIfNull(this string p_str) + { + if (p_str == null) + { + return string.Empty; + } + else + { + return p_str; + } + } + + public static string Left(this string str, int len) + { + string result = str.Substring(0, len); + return result; + } + + public static string Right(this string str, int len) + { + if (str.Length < len) + { + return str; + } + string result = str.Substring(str.Length - len, len); + return result; + } + + public static object IntDBNull(this object p_obj) + { + if (p_obj == null) + { + return DBNull.Value; + } + string str = p_obj?.ToString(); + int resu; + bool isSucc = int.TryParse(str, out resu); + if (isSucc) + { + return resu; + } + else + { + return DBNull.Value; + } + } + + public static object DoubleDBNull(this object p_obj) + { + if (p_obj == null) + { + return DBNull.Value; + } + string str = p_obj?.ToString(); + double resu; + bool isSucc = double.TryParse(str, out resu); + if (isSucc) + { + return resu; + } + else + { + return DBNull.Value; + } + } + + public static object DateTimeDBNull(this object p_obj) + { + if (p_obj == null) + { + return DBNull.Value; + } + string str = p_obj?.ToString(); + DateTime resu; + bool isSucc = DateTime.TryParse(str, out resu); + if (isSucc) + { + return resu; + } + else + { + return DBNull.Value; + } + } + + /// + /// 返回枚举项的描述信息。 + /// + /// 要获取描述信息的枚举项。 + /// 枚举想的描述信息。 + /// + public static string GetDescription(this Enum value, bool isTop = false) + { + Type enumType = value.GetType(); + DescriptionAttribute attr = null; + if (isTop) + { + attr = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute)); + } + else + { + // 获取枚举常数名称。 + string name = Enum.GetName(enumType, value); + if (name != null) + { + // 获取枚举字段。 + FieldInfo fieldInfo = enumType.GetField(name); + if (fieldInfo != null) + { + // 获取描述的属性。 + attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute; + } + } + } + if (attr != null && !string.IsNullOrEmpty(attr.Description)) + return attr.Description; + else + return string.Empty; + } + + /// + /// 23:59:59 + /// + /// + /// + public static DateTime ToMaxTime(this DateTime p_dt) + { + string str = p_dt.ToString("yyyy-MM-dd 23:59:59"); + return Convert.ToDateTime(str); + } + + /// + /// 00:00:01 + /// + /// + /// + public static DateTime ToMinTime(this DateTime p_dt) + { + string str = p_dt.ToString("yyyy-MM-dd 00:00:01"); + return Convert.ToDateTime(str); + } + } + +}