Browse Source

4个服务加各种校验

master
me 7 days ago
parent
commit
42fb2cec58
  1. 8
      API/TaskManager.Entity/Data/SUPPLIER_EMPLOYEE_DT.cs
  2. 4
      API/TaskManager.Entity/Data/SUPPLIER_INFO_DT.cs
  3. 2
      API/TaskManager.Entity/Data/SUPPLIER_PRO_ATTACHMENT_DATA_DT.cs
  4. 4
      API/TaskManager.Entity/Data/SUPPLIER_PRO_PROCESS_EQUIPMENT_DT.cs
  5. 53
      API/Wood.Service/Commons/ValidationHelper.cs
  6. 9
      API/Wood.Service/Datas/SupplierEmployeeDtService.cs
  7. 9
      API/Wood.Service/Datas/SupplierInfoDtService.cs
  8. 9
      API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs
  9. 9
      API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs
  10. 412
      API/Wood.Util/ExtensionMethods/TypeConvertExtension.cs

8
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
/// </summary>
[ExporterHeader(DisplayName = "是否有资质(Y,N)")]
[ImporterHeader(Name = "是否有资质(Y,N)")]
[RegularExpression("^[YN]$", ErrorMessage = " Y N")]
public string HaveQuantity { get; set; }
/// <summary>
/// 供应商修改时间,格式(yyyy-MM-ddHH:mm:ss)
@ -113,18 +115,20 @@ namespace TaskManager.Entity
/// </summary>
[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; }
/// <summary>
/// 资质获取时间
/// </summary>
[ExporterHeader(DisplayName = "资质获取时间")]
[ImporterHeader(Name = "资质获取时间")]
[ImporterHeader(Name = "资质获取时间", Format = "yyyy-MM-dd HH:mm:ss")]
public string CheckInTime { get; set; }
/// <summary>
/// 资质失去时间
/// </summary>
[ExporterHeader(DisplayName = "资质失去时间")]
[ImporterHeader(Name = "资质失去时间")]
[ImporterHeader(Name = "资质失去时间", Format = "yyyy-MM-dd HH:mm:ss")]
public string CheckOutTime { get; set; }
}
}

4
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
/// </summary>
[ExporterHeader(DisplayName = "是否关键工位(Y/N)")]
[ImporterHeader(Name = "是否关键工位(Y/N)")]
[RegularExpression("^[YN]$", ErrorMessage = " Y N")]
public string KeyStation { get; set; }
/// <summary>
/// 供应商修改时间,格式(yyyy-MM-ddHH:mm:ss)
/// </summary>
[ExporterHeader(DisplayName = "供应商修改时间")]
[ImporterHeader(Name = "供应商修改时间")]
[ImporterHeader(Name = "供应商修改时间", Format = "yyyy-MM-dd HH:mm:ss")]
public string DataUpdateTime { get; set; }
/// <summary>
/// 产线顺序

2
API/TaskManager.Entity/Data/SUPPLIER_PRO_ATTACHMENT_DATA_DT.cs

@ -42,7 +42,7 @@ namespace TaskManager.Entity.Entitys
/// 生成时间,格式(yyyy-MM-ddHH:mm:ss)
/// </summary>
[ExporterHeader(DisplayName = "生成时间")]
[ImporterHeader(Name = "生成时间")]
[ImporterHeader(Name = "生成时间", Format = "yyyy-MM-dd HH:mm:ss")]
public string DateTime { get; set; }
/// <summary>
/// 产线名称

4
API/TaskManager.Entity/Data/SUPPLIER_PRO_PROCESS_EQUIPMENT_DT.cs

@ -83,7 +83,7 @@ namespace TaskManager.Entity.Entitys
/// 工艺装备制造日期,格式(yyyy-MM-dd)
/// </summary>
[ExporterHeader(DisplayName = "工艺装备制造日期")]
[ImporterHeader(Name = "工艺装备制造日期")]
[ImporterHeader(Name = "工艺装备制造日期", Format = "yyyy-MM-dd HH:mm:ss")]
public string ProductionDate { get; set; }
/// <summary>
/// 主要材质
@ -155,7 +155,7 @@ namespace TaskManager.Entity.Entitys
/// 最近校准日期,格式(yyyy-MM-ddHH:mm:ss)
/// </summary>
[ExporterHeader(DisplayName = "最近校准日期")]
[ImporterHeader(Name = "最近校准日期")]
[ImporterHeader(Name = "最近校准日期", Format = "yyyy-MM-dd HH:mm:ss")]
public string CalibrationDate { get; set; }
/// <summary>
/// 校准到期天数

53
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<string>();
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<ImporterHeaderAttribute>()?.Name;
return ret;
}
public static string GetImporterHeader_Format(this IProperty value)
{
string ret = value?.PropertyInfo?.GetCustomAttribute<ImporterHeaderAttribute>()?.Format;
return ret;
}
}
public static class SearchControlExtension
{
/// <summary>
/// 忽略双字节的PadRight功能,汉字的length = 2
/// </summary>
/// <param name="str"></param>
/// <param name="totalWidth"></param>
/// <param name="paddingChar"></param>
/// <returns></returns>
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);
}
}
}

9
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

9
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

9
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

9
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

412
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;
}
}
/// <summary>
/// 返回枚举项的描述信息。
/// </summary>
/// <param name="value">要获取描述信息的枚举项。</param>
/// <returns>枚举想的描述信息。
/// </returns>
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;
}
/// <summary>
/// 23:59:59
/// </summary>
/// <param name="p_dt"></param>
/// <returns></returns>
public static DateTime ToMaxTime(this DateTime p_dt)
{
string str = p_dt.ToString("yyyy-MM-dd 23:59:59");
return Convert.ToDateTime(str);
}
/// <summary>
/// 00:00:01
/// </summary>
/// <param name="p_dt"></param>
/// <returns></returns>
public static DateTime ToMinTime(this DateTime p_dt)
{
string str = p_dt.ToString("yyyy-MM-dd 00:00:01");
return Convert.ToDateTime(str);
}
}
}
Loading…
Cancel
Save