refactor: 变量排序导出

This commit is contained in:
2248356998 qq.com
2025-06-28 21:43:06 +08:00
parent 77bfabc41d
commit 53f8fbe4b1
9 changed files with 52 additions and 38 deletions

View File

@@ -43,7 +43,7 @@ public static class ExportExcelExtensions
#region
var type = typeof(T);
var propertyInfos = type.GetRuntimeProperties().Where(a => a.GetCustomAttribute<IgnoreExcelAttribute>() == null)
var propertyInfos = type.GetRuntimeProperties().Where(a => a.GetCustomAttribute<IgnoreExcelAttribute>(false) == null)
.OrderBy(
a =>
{
@@ -102,7 +102,7 @@ public static class ExportExcelExtensions
int index = 0;
foreach (var item in data)
{
var ignore = item.GetCustomAttribute<IgnoreExcelAttribute>() != null;
var ignore = item.GetCustomAttribute<IgnoreExcelAttribute>(false) != null;
//描述
var desc = type.GetPropertyDisplayName(item.Name);
//数据源增加

View File

@@ -30,6 +30,13 @@ namespace ThingsGateway.Gateway.Application;
[SugarIndex("unique_deviceid_variable_name", nameof(Variable.Name), OrderByType.Asc, nameof(Variable.DeviceId), OrderByType.Asc, true)]
public class Variable : BaseDataEntity, IValidatableObject
{
/// <summary>
/// 主键Id
/// </summary>
[SugarColumn(ColumnDescription = "Id", IsPrimaryKey = true)]
[AutoGenerateColumn(Visible = false, IsVisibleWhenEdit = false, IsVisibleWhenAdd = false, Sortable = true, DefaultSort = true, DefaultSortOrder = SortOrder.Asc)]
public override long Id { get; set; }
/// <summary>
/// 导入验证专用
/// </summary>
@@ -102,6 +109,8 @@ public class Variable : BaseDataEntity, IValidatableObject
private string remark4;
private string remark5;
/// <summary>
/// 变量额外属性Json
/// </summary>

View File

@@ -36,7 +36,7 @@ public static class ChannelServiceHelpers
#region
var type = typeof(Channel);
var propertyInfos = type.GetRuntimeProperties().Where(a => a.GetCustomAttribute<IgnoreExcelAttribute>() == null)
var propertyInfos = type.GetRuntimeProperties().Where(a => a.GetCustomAttribute<IgnoreExcelAttribute>(false) == null)
.OrderBy(
a =>
{

View File

@@ -47,7 +47,7 @@ public static class DeviceServiceHelpers
#region
var type = typeof(Device);
var propertyInfos = type.GetRuntimeProperties().Where(a => a.GetCustomAttribute<IgnoreExcelAttribute>() == null)
var propertyInfos = type.GetRuntimeProperties().Where(a => a.GetCustomAttribute<IgnoreExcelAttribute>(false) == null)
.OrderBy(
a =>
{

View File

@@ -447,7 +447,7 @@ internal sealed class VariableService : BaseService<Variable>, IVariableService
public async Task<Dictionary<string, object>> ExportVariableAsync(ExportFilter exportFilter)
{
var data = (await PageAsync(exportFilter).ConfigureAwait(false));
var sheets = await VariableServiceHelpers.ExportCoreAsync(data.Items).ConfigureAwait(false);
var sheets = await VariableServiceHelpers.ExportCoreAsync(data.Items, sortName: exportFilter.QueryPageOptions.SortName, sortOrder: exportFilter.QueryPageOptions.SortOrder).ConfigureAwait(false);
return sheets;
}
@@ -661,7 +661,7 @@ internal sealed class VariableService : BaseService<Variable>, IVariableService
});
// 为未成功上传的变量生成新的ID
foreach (var item in variables.OrderBy(a => a.Row))
foreach (var item in variables)
{
if (!item.IsUp)
item.Id = CommonUtils.GetSingleId();

View File

@@ -21,14 +21,14 @@ namespace ThingsGateway.Gateway.Application;
public static class VariableServiceHelpers
{
public static async Task<USheetDatas> ExportVariableAsync(IEnumerable<Variable> models)
public static async Task<USheetDatas> ExportVariableAsync(IEnumerable<Variable> models, string sortName = nameof(Variable.Id), SortOrder sortOrder = SortOrder.Asc)
{
var data = await ExportCoreAsync(models).ConfigureAwait(false);
var data = await ExportCoreAsync(models, sortName: sortName, sortOrder: sortOrder).ConfigureAwait(false);
return USheetDataHelpers.GetUSheetDatas(data);
}
public static async Task<Dictionary<string, object>> ExportCoreAsync(IEnumerable<Variable> data, string deviceName = null)
public static async Task<Dictionary<string, object>> ExportCoreAsync(IEnumerable<Variable> data, string deviceName = null, string sortName = nameof(Variable.Id), SortOrder sortOrder = SortOrder.Asc)
{
if (data?.Any() != true)
{
@@ -48,7 +48,7 @@ public static class VariableServiceHelpers
#region
var type = typeof(Variable);
var propertyInfos = type.GetRuntimeProperties().Where(a => a.GetCustomAttribute<IgnoreExcelAttribute>() == null)
var propertyInfos = type.GetRuntimeProperties().Where(a => a.GetCustomAttribute<IgnoreExcelAttribute>(false) == null)
.OrderBy(
a =>
{
@@ -76,6 +76,11 @@ public static class VariableServiceHelpers
varExport.TryAdd(ExportString.DeviceName, device?.Name ?? deviceName);
foreach (var item in propertyInfos)
{
if (item.Name == nameof(Variable.Id))
{
if (sortName != nameof(Variable.Id))
continue;
}
//描述
var desc = type.GetPropertyDisplayName(item.Name);
if (item.Name == varName)
@@ -185,9 +190,21 @@ public static class VariableServiceHelpers
#endregion sheet
});
var sort = type.GetPropertyDisplayName(sortName);
if(variableExports.FirstOrDefault()?.ContainsKey(sort)==false)
sort = nameof(Variable.Id);
if (variableExports.FirstOrDefault()?.ContainsKey(sort) == false)
sort = type.GetPropertyDisplayName(nameof(Variable.Name));
variableExports = new(
sortOrder==SortOrder.Desc?
variableExports.OrderByDescending(a => a[sort])
:
variableExports.OrderBy(a => a[sort])
);
//variableExports = new(variableExports.OrderBy(a => a[ExportString.DeviceName]).ThenBy(a => a[varName]));
if (sortName == nameof(Variable.Id))
variableExports.ForEach(a => a.Remove("Id"));
//添加设备页
sheets.Add(ExportString.VariableName, variableExports);
@@ -196,19 +213,7 @@ public static class VariableServiceHelpers
foreach (var item in devicePropertys.Keys)
{
devicePropertys[item] = new(devicePropertys[item].OrderBy(a => a[ExportString.DeviceName]).ThenBy(a => a[ExportString.VariableName]));
//HashSet<string> allKeys = item.Value.SelectMany(a => a.Keys).ToHashSet();
//foreach (var dict in item.Value)
//{
// foreach (var key in allKeys)
// {
// if (!dict.ContainsKey(key))
// {
// // 添加缺失的键,并设置默认值
// dict.TryAdd(key, null);
// }
// }
//}
sheets.Add(item, devicePropertys[item]);
}

View File

@@ -241,7 +241,7 @@ public partial class ChannelTable : IDisposable
bool ret;
if (all)
{
ret = await GatewayExportService.OnChannelExport(new() { QueryPageOptions = new() });
ret = await GatewayExportService.OnChannelExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder } });
}
else
{
@@ -249,13 +249,13 @@ public partial class ChannelTable : IDisposable
{
case ChannelDevicePluginTypeEnum.PluginName:
ret = await GatewayExportService.OnChannelExport(new() { QueryPageOptions = new(), PluginName = SelectModel.PluginName });
ret = await GatewayExportService.OnChannelExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder }, PluginName = SelectModel.PluginName });
break;
case ChannelDevicePluginTypeEnum.Channel:
ret = await GatewayExportService.OnChannelExport(new() { QueryPageOptions = new(), ChannelId = SelectModel.ChannelRuntime.Id });
ret = await GatewayExportService.OnChannelExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder }, ChannelId = SelectModel.ChannelRuntime.Id });
break;
case ChannelDevicePluginTypeEnum.Device:
ret = await GatewayExportService.OnChannelExport(new() { QueryPageOptions = new(), DeviceId = SelectModel.DeviceRuntime.Id, PluginType = SelectModel.DeviceRuntime.PluginType });
ret = await GatewayExportService.OnChannelExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder }, DeviceId = SelectModel.DeviceRuntime.Id, PluginType = SelectModel.DeviceRuntime.PluginType });
break;
default:
ret = await GatewayExportService.OnChannelExport(new() { QueryPageOptions = new() });

View File

@@ -242,7 +242,7 @@ public partial class DeviceTable : IDisposable
bool ret;
if (all)
{
ret = await GatewayExportService.OnDeviceExport(new() { QueryPageOptions = new() });
ret = await GatewayExportService.OnDeviceExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder } });
}
else
{
@@ -250,16 +250,16 @@ public partial class DeviceTable : IDisposable
{
case ChannelDevicePluginTypeEnum.PluginName:
ret = await GatewayExportService.OnDeviceExport(new() { QueryPageOptions = new(), PluginName = SelectModel.PluginName });
ret = await GatewayExportService.OnDeviceExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder }, PluginName = SelectModel.PluginName });
break;
case ChannelDevicePluginTypeEnum.Channel:
ret = await GatewayExportService.OnDeviceExport(new() { QueryPageOptions = new(), ChannelId = SelectModel.ChannelRuntime.Id });
ret = await GatewayExportService.OnDeviceExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder }, ChannelId = SelectModel.ChannelRuntime.Id });
break;
case ChannelDevicePluginTypeEnum.Device:
ret = await GatewayExportService.OnDeviceExport(new() { QueryPageOptions = new(), DeviceId = SelectModel.DeviceRuntime.Id, PluginType = SelectModel.DeviceRuntime.PluginType });
ret = await GatewayExportService.OnDeviceExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder }, DeviceId = SelectModel.DeviceRuntime.Id, PluginType = SelectModel.DeviceRuntime.PluginType });
break;
default:
ret = await GatewayExportService.OnDeviceExport(new() { QueryPageOptions = new() });
ret = await GatewayExportService.OnDeviceExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder } });
break;
}

View File

@@ -301,7 +301,7 @@ public partial class VariableRuntimeInfo : IDisposable
bool ret;
if (all)
{
ret = await GatewayExportService.OnVariableExport(new() { QueryPageOptions = new() });
ret = await GatewayExportService.OnVariableExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder } });
}
else
{
@@ -309,16 +309,16 @@ public partial class VariableRuntimeInfo : IDisposable
{
case ChannelDevicePluginTypeEnum.PluginName:
ret = await GatewayExportService.OnVariableExport(new() { QueryPageOptions = new(), PluginName = SelectModel.PluginName });
ret = await GatewayExportService.OnVariableExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder }, PluginName = SelectModel.PluginName });
break;
case ChannelDevicePluginTypeEnum.Channel:
ret = await GatewayExportService.OnVariableExport(new() { QueryPageOptions = new(), ChannelId = SelectModel.ChannelRuntime.Id });
ret = await GatewayExportService.OnVariableExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder }, ChannelId = SelectModel.ChannelRuntime.Id });
break;
case ChannelDevicePluginTypeEnum.Device:
ret = await GatewayExportService.OnVariableExport(new() { QueryPageOptions = new(), DeviceId = SelectModel.DeviceRuntime.Id, PluginType = SelectModel.DeviceRuntime.PluginType });
ret = await GatewayExportService.OnVariableExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder }, DeviceId = SelectModel.DeviceRuntime.Id, PluginType = SelectModel.DeviceRuntime.PluginType });
break;
default:
ret = await GatewayExportService.OnVariableExport(new() { QueryPageOptions = new() });
ret = await GatewayExportService.OnVariableExport(new() { QueryPageOptions = new() { SortName = _option.SortName, SortOrder = _option.SortOrder } });
break;
}
@@ -349,7 +349,7 @@ public partial class VariableRuntimeInfo : IDisposable
await ToastService.Warning("online Excel max data count 50000");
return;
}
var uSheetDatas = await VariableServiceHelpers.ExportVariableAsync(models);
var uSheetDatas = await VariableServiceHelpers.ExportVariableAsync(models, _option.SortName, _option.SortOrder);
op.Component = BootstrapDynamicComponent.CreateComponent<USheet>(new Dictionary<string, object?>
{