导入规则优化,主动抛出名称重复错误

This commit is contained in:
2248356998 qq.com
2023-05-19 22:55:53 +08:00
parent c3276889cf
commit 2e948eb5b6
3 changed files with 58 additions and 3 deletions

View File

@@ -4,6 +4,8 @@ using Microsoft.AspNetCore.Components.Forms;
using MiniExcelLibs;
using NewLife.Serialization;
using System.Dynamic;
using System.IO;
using System.Linq;
@@ -240,6 +242,19 @@ namespace ThingsGateway.Web.Foundation
}
#region
//查找出列表中的所有重复元素及其重复次数
private static Dictionary<string, int> QueryRepeatElementAndCountOfList(IEnumerable<IDictionary<string, object>> list)
{
Dictionary<string, int> DicTmp = new Dictionary<string, int>();
if (list != null && list.Count() > 0)
{
DicTmp= list.GroupBy(x => ((ExpandoObject)x).ConvertToEntity<CollectDevice>().Name)
.Where(g => g.Count() > 1)
.ToDictionary(x => x.Key, y => y.Count());
}
return DicTmp;
}
/// <summary>
/// 插件前置名称
/// </summary>
@@ -345,7 +360,11 @@ namespace ThingsGateway.Web.Foundation
ImportPreviews.Add(sheetName, importPreviewOutput);
deviceImportPreview = importPreviewOutput;
var DicTmp= QueryRepeatElementAndCountOfList(rows);
if(DicTmp.Count > 0)
{
throw new Exception("发现重复名称"+ Environment.NewLine + DicTmp.Select(a=>a.Key).ToJson());
}
List<CollectDevice> devices = new List<CollectDevice>();
foreach (var item in rows)
{

View File

@@ -4,6 +4,8 @@ using Microsoft.AspNetCore.Components.Forms;
using MiniExcelLibs;
using NewLife.Serialization;
using System.Dynamic;
using System.IO;
using System.Linq;
@@ -185,6 +187,18 @@ namespace ThingsGateway.Web.Foundation
}
#region
//查找出列表中的所有重复元素及其重复次数
private static Dictionary<string, int> QueryRepeatElementAndCountOfList(IEnumerable<IDictionary<string, object>> list)
{
Dictionary<string, int> DicTmp = new Dictionary<string, int>();
if (list != null && list.Count() > 0)
{
DicTmp = list.GroupBy(x => ((ExpandoObject)x).ConvertToEntity<UploadDevice>().Name)
.Where(g => g.Count() > 1)
.ToDictionary(x => x.Key, y => y.Count());
}
return DicTmp;
}
/// <summary>
/// 插件前置名称
/// </summary>
@@ -288,7 +302,11 @@ namespace ThingsGateway.Web.Foundation
ImportPreviews.Add(sheetName, importPreviewOutput);
deviceImportPreview = importPreviewOutput;
var DicTmp = QueryRepeatElementAndCountOfList(rows);
if (DicTmp.Count > 0)
{
throw new Exception("发现重复名称" + Environment.NewLine + DicTmp.Select(a => a.Key).ToJson());
}
List<UploadDevice> devices = new List<UploadDevice>();
foreach (var item in rows)
{

View File

@@ -204,6 +204,20 @@ namespace ThingsGateway.Web.Foundation
#region
//查找出列表中的所有重复元素及其重复次数
private static Dictionary<string, int> QueryRepeatElementAndCountOfList(IEnumerable<IDictionary<string, object>> list)
{
Dictionary<string, int> DicTmp = new Dictionary<string, int>();
if (list != null && list.Count() > 0)
{
DicTmp = list.GroupBy(x => ((ExpandoObject)x).ConvertToEntity<CollectDeviceVariable>().Name)
.Where(g => g.Count() > 1)
.ToDictionary(x => x.Key, y => y.Count());
}
return DicTmp;
}
/// <summary>
/// 插件前置名称
/// </summary>
@@ -325,7 +339,11 @@ namespace ThingsGateway.Web.Foundation
ImportPreviews.Add(sheetName, importPreviewOutput);
deviceImportPreview = importPreviewOutput;
var DicTmp = QueryRepeatElementAndCountOfList(rows);
if (DicTmp.Count > 0)
{
throw new Exception("发现重复名称" + Environment.NewLine + DicTmp.Select(a => a.Key).ToJson());
}
List<CollectDeviceVariable> devices = new List<CollectDeviceVariable>();
foreach (var item in rows)
{