【优化】convert_xlsx输出记录按key排序

main v2021.11
walon 2021-11-05 17:10:43 +08:00
parent 575d0b2533
commit 3612d4e2d8
3 changed files with 28 additions and 19 deletions

View File

@ -7,6 +7,7 @@ using Luban.Job.Cfg.l10n;
using Luban.Job.Cfg.RawDefs;
using Luban.Job.Cfg.TypeVisitors;
using Luban.Job.Common.Defs;
using Luban.Job.Common.Types;
using Luban.Server.Common;
using System;
using System.Collections.Concurrent;
@ -140,6 +141,29 @@ namespace Luban.Job.Cfg.Defs
}
}
public static List<Record> ToSortByKeyDataList(DefTable table, List<Record> originRecords)
{
var sortedRecords = new List<Record>(originRecords);
DefField keyField = table.IndexField;
if (keyField != null && (keyField.CType is TInt || keyField.CType is TLong))
{
string keyFieldName = keyField.Name;
sortedRecords.Sort((a, b) =>
{
DType keya = a.Data.GetField(keyFieldName);
DType keyb = b.Data.GetField(keyFieldName);
switch (keya)
{
case DInt ai: return ai.Value.CompareTo((keyb as DInt).Value);
case DLong al: return al.Value.CompareTo((keyb as DLong).Value);
default: throw new NotSupportedException();
}
});
}
return sortedRecords;
}
public TableDataInfo GetTableDataInfo(DefTable table)
{
return _recordsByTables[table.FullName];

View File

@ -1,6 +1,7 @@
using ClosedXML.Excel;
using Luban.Job.Cfg.Cache;
using Luban.Job.Cfg.DataConverts;
using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils;
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
@ -25,7 +26,8 @@ namespace Luban.Job.Cfg.Generate
{
ctx.Tasks.Add(Task.Run(() =>
{
var records = ctx.Assembly.GetTableAllDataList(table);
var records = DefAssembly.ToSortByKeyDataList(table, ctx.Assembly.GetTableAllDataList(table));
string dirName = table.FullName;
var fileName = table.FullName;
var filePath = $"{dirName}/{fileName}.xlsx";

View File

@ -195,24 +195,7 @@ namespace LubanAssistant
//int nextRowIndex = titleRowNum + 2;
// 对于 int和long类型记录按值排序
var records = tableDataInfo.MainRecords;
DefField keyField = tableDataInfo.Table.IndexField;
if (keyField != null && (keyField.CType is TInt || keyField.CType is TLong))
{
string keyFieldName = keyField.Name;
records.Sort((a, b) =>
{
DType keya = a.Data.GetField(keyFieldName);
DType keyb = b.Data.GetField(keyFieldName);
switch (keya)
{
case DInt ai: return ai.Value.CompareTo((keyb as DInt).Value);
case DLong al: return al.Value.CompareTo((keyb as DLong).Value);
default: throw new NotSupportedException();
}
});
}
var records = DefAssembly.ToSortByKeyDataList(tableDataInfo.Table, tableDataInfo.MainRecords);
int totalRowCount = 0;
var dataRangeArray = new List<object[]>();