Skip to content

Commit

Permalink
Merge pull request #544 from netty2019/master
Browse files Browse the repository at this point in the history
增加ValueMappingsBase值映射特性基类,方便从外部(如:Abp框架)继承实现枚举、bool类型的多语言显示
  • Loading branch information
xl-wenqiang authored Jan 3, 2024
2 parents 79e8e09 + d506daf commit a34c5ea
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 9 deletions.
25 changes: 25 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,31 @@
public Genders Gender { get; set; }
```

- **也可以继承“ValueMappingsBaseAttribute”特性基类实现值映射关系,目前仅可用于枚举和Bool类型,支持导入导出。**
```csharp
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class GenderLocalAttribute : ValueMappingsBaseAttribute
{
public override Dictionary<string, object> GetMappings(PropertyInfo propertyInfo)
{
var res= new Dictionary<string, object>();
res.Add("",0);
res.Add("",1);
return res;
}
}


/// <summary>
/// 性别
/// </summary>
[ImporterHeader(Name = "性别")]
[Required(ErrorMessage = "性别不能为空")]
[GenderLocal]
public Genders Gender { get; set; }
```


- **支持枚举和Bool类型的导入数据验证项的生成,以及相关数据转换**
- **枚举默认情况下会自动获取枚举的描述、显示名、名称和值生成数据项**

Expand Down
13 changes: 12 additions & 1 deletion src/Magicodes.ExporterAndImporter.Core/Extension/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,18 @@ public static int GetLargestContinuous(this List<int> numList)
public static void ValueMapping(this PropertyInfo propertyInfo, ref Dictionary<string, dynamic> directory)
{
#region 处理值映射
//ValueMappingsBaseAttribute
var valueMappings = propertyInfo.GetAttributes<ValueMappingsBaseAttribute>(true).FirstOrDefault()?.GetMappings(propertyInfo);
if(valueMappings != null )
{
foreach (var valueMapping in valueMappings)
{
if (!directory.ContainsKey(valueMapping.Key)) directory.Add(valueMapping.Key, valueMapping.Value);
}
if (valueMappings.Count > 0) return;
}

//ValueMappingAttribute
var mappings = propertyInfo.GetAttributes<ValueMappingAttribute>().ToList();
var objects = directory;
foreach (var mappingAttribute in mappings.Where(mappingAttribute =>
Expand Down Expand Up @@ -363,4 +374,4 @@ public static void ValueMapping(this PropertyInfo propertyInfo, ref Dictionary<s
#endregion 处理值映射
}
}
}
}
2 changes: 1 addition & 1 deletion src/Magicodes.ExporterAndImporter.Core/IImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ public interface IImporter
/// <param name="stream"></param>
/// <param name="labelingFileStream"></param>
/// <returns></returns>
Task<ImportResult<T>> Import<T>(Stream stream,Stream labelingFileStream) where T : class, new();
Task<ImportResult<T>> Import<T>(Stream stream, Stream labelingFileStream, Func<ImportResult<T>, ImportResult<T>> importResultCallback = null) where T : class, new();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Magicodes.IE.Core
{
/// <summary>
/// 值映射
/// </summary>
public abstract class ValueMappingsBaseAttribute : Attribute
{
/// <summary>
/// 根据字段信息获取映射
/// </summary>
/// <param name="propertyInfo"></param>
/// <returns></returns>
public abstract Dictionary<string, object> GetMappings(PropertyInfo propertyInfo);
}
}
2 changes: 1 addition & 1 deletion src/Magicodes.ExporterAndImporter.Csv/CsvImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class CsvImporter : ICsvImporter
/// <param name="stream"></param>
/// <param name="labelingFileStream"></param>
/// <returns></returns>
public Task<ImportResult<T>> Import<T>(Stream stream, Stream labelingFileStream) where T : class, new()
public Task<ImportResult<T>> Import<T>(Stream stream, Stream labelingFileStream, Func<ImportResult<T>, ImportResult<T>> importResultCallback = null) where T : class, new()
{
using (var importer = new ImportHelper<T>(stream))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Magicodes.ExporterAndImporter.Excel/ExcelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ public class ExcelImporter : IExcelImporter
/// <param name="stream"></param>
/// <param name="labelingFileStream"></param>
/// <returns></returns>
public Task<ImportResult<T>> Import<T>(Stream stream, Stream labelingFileStream) where T : class, new()
public Task<ImportResult<T>> Import<T>(Stream stream, Stream labelingFileStream, Func<ImportResult<T>, ImportResult<T>> importResultCallback = null) where T : class, new()
{
using (var importer = new ImportHelper<T>(stream, labelingFileStream))
{
return importer.Import();
return importer.Import(importResultCallback: importResultCallback);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,55 @@ public async Task ValueMapping_Test()
}
}



[Fact(DisplayName = "ValueMappingsBase测试#544")]
public async Task ValueMappingsBase_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ValueMappingsBase_Test)}.xlsx");
DeleteFile(filePath);
var list = new List<Issue544>()
{
new Issue544()
{
Gender ="",
IsAlumni = true,
Name ="张三",
IsAlumni2 = true,
},
new Issue544()
{
Gender ="",
IsAlumni = false,
Name ="张三",
IsAlumni2 = true,
},
new Issue544()
{
Gender ="",
IsAlumni = null,
Name ="张三",
IsAlumni2 = false,
},
};
var result = await exporter.ExportWithXSSFWorkbook(filePath, list);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
var sheet = pck.Workbook.Worksheets.First();
sheet.Cells["D2"].Text.ShouldBe("");
sheet.Cells["D3"].Text.ShouldBe("");
sheet.Cells["D4"].Text.ShouldBe("");

sheet.Cells["C2"].Text.ShouldBe("");
sheet.Cells["C3"].Text.ShouldBe("");
sheet.Cells["C4"].Text.ShouldBe("");
}
}



[Fact(DisplayName = "导出日期格式化#331")]
public async Task DateTimeExport_Test()
{
Expand Down
49 changes: 49 additions & 0 deletions src/Magicodes.ExporterAndImporter.Tests/ExcelExporter_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,55 @@ public async Task ValueMapping_Test()
}


[Fact(DisplayName = "ValueMappingsBase测试#544")]
public async Task ValueMappingsBase_Test()
{
IExporter exporter = new ExcelExporter();
var filePath = GetTestFilePath($"{nameof(ValueMappingsBase_Test)}.xlsx");
DeleteFile(filePath);
var list = new List<Issue544>()
{
new Issue544()
{
Gender ="",
IsAlumni = true,
Name ="张三",
IsAlumni2 = true,
},
new Issue544()
{
Gender ="",
IsAlumni = false,
Name ="张三",
IsAlumni2 = true,
},
new Issue544()
{
Gender ="",
IsAlumni = null,
Name ="张三",
IsAlumni2 = false,
},
};
var result = await exporter.Export(filePath, list);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
using (var pck = new ExcelPackage(new FileInfo(filePath)))
{
pck.Workbook.Worksheets.Count.ShouldBe(1);
var sheet = pck.Workbook.Worksheets.First();
sheet.Cells["D2"].Text.ShouldBe("");
sheet.Cells["D3"].Text.ShouldBe("");
sheet.Cells["D4"].Text.ShouldBe("");

sheet.Cells["C2"].Text.ShouldBe("");
sheet.Cells["C3"].Text.ShouldBe("");
sheet.Cells["C4"].Text.ShouldBe("");
}
}




[Fact(DisplayName = "导出日期格式化#331")]
public async Task DateTimeExport_Test()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public class ExporterHeaderFilterTestData1
[ExporterHeader(DisplayName = "加粗文本", IsBold = true)]
public string Text { get; set; }

[ExporterHeader(DisplayName = "普通文本")] public string Text2 { get; set; }
[ExporterHeader(DisplayName = "普通文本")]
public string Text2 { get; set; }

[ExporterHeader(DisplayName = "忽略", IsIgnore = true)]
public string Text3 { get; set; }
Expand Down
49 changes: 49 additions & 0 deletions src/Magicodes.ExporterAndImporter.Tests/Models/Export/Issue544.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using Magicodes.IE.Core;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Magicodes.ExporterAndImporter.Tests.Models.Export
{
[ExcelExporter(Name = "导出结果", TableStyle = OfficeOpenXml.Table.TableStyles.None)]
public class Issue544
{
/// <summary>
/// 名称
/// </summary>
[ExporterHeader(DisplayName = "姓名")]
public string Name { get; set; }

/// <summary>
/// 性别
/// </summary>
[ExporterHeader(DisplayName = "性别")]
public string Gender { get; set; }

/// <summary>
/// 是否校友
/// </summary>
[ExporterHeader(DisplayName = "是否校友")]
[BoolLocal337]
public bool? IsAlumni { get; set; }

[ExporterHeader(DisplayName = "是否校友2")]
[BoolLocal337]
public bool IsAlumni2 { get; set; }
}


[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class BoolLocal337Attribute : ValueMappingsBaseAttribute
{
public override Dictionary<string, object> GetMappings(PropertyInfo propertyInfo)
{
var res= new Dictionary<string, object>();
res.Add("",true);
res.Add("",false);
return res;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ImportWithOnlyErrorRowsDto
/// </summary>
[ImporterHeader(Name = "身份证号", IsAllowRepeat = false)]
[Required(ErrorMessage = "身份证号不能为空")]
[MaxLength(18, ErrorMessage = "身份证字数超出最大限制,请修改!")]
[MaxLength(18, ErrorMessage = "身份证号字数超出最大限制,请修改!")]
public string IdCard { get; set; }

/// <summary>
Expand Down

0 comments on commit a34c5ea

Please sign in to comment.