😎代码优化
This commit is contained in:
parent
14a305b198
commit
c1240eb550
@ -13,6 +13,21 @@ namespace Admin.NET.Core;
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
|
||||
public class DictAttribute : ValidationAttribute, ITransient
|
||||
{
|
||||
/// <summary>
|
||||
/// 字典编码
|
||||
/// </summary>
|
||||
public string DictTypeCode { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否允许空字符串
|
||||
/// </summary>
|
||||
public bool AllowEmptyStrings { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 允许空值,有值才验证,默认 false
|
||||
/// </summary>
|
||||
public bool AllowNullValue { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 字典值合规性校验特性
|
||||
/// </summary>
|
||||
@ -43,27 +58,16 @@ public class DictAttribute : ValidationAttribute, ITransient
|
||||
var sysDictDataServiceProvider = App.GetRequiredService<SysDictDataService>();
|
||||
var dictDataList = sysDictDataServiceProvider.GetDataList(DictTypeCode).Result;
|
||||
|
||||
// 获取枚举类型,可能存在Nullable类型,所以需要尝试获取最终类型
|
||||
var type = value?.GetType();
|
||||
type = type != null ? Nullable.GetUnderlyingType(type) ?? type : null;
|
||||
|
||||
// 使用HashSet来提高查找效率
|
||||
var valueList = (value?.GetType().IsEnum ?? DictTypeCode.EndsWith("Enum")) ? dictDataList.Select(u => u.Name) : dictDataList.Select(u => u.Code);
|
||||
var valueList = (type?.IsEnum ?? DictTypeCode.EndsWith("Enum")) ? dictDataList.Select(u => u.Name) : dictDataList.Select(u => u.Value);
|
||||
var dictHash = new HashSet<string>(valueList);
|
||||
|
||||
if (!dictHash.Contains(valueAsString))
|
||||
return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{valueAsString}】!");
|
||||
return ValidationResult.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字典编码
|
||||
/// </summary>
|
||||
public string DictTypeCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否允许空字符串
|
||||
/// </summary>
|
||||
public bool AllowEmptyStrings { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 允许空值,有值才验证,默认 false
|
||||
/// </summary>
|
||||
public bool AllowNullValue { get; set; } = false;
|
||||
}
|
||||
@ -31,8 +31,8 @@ public partial class SysDictData : EntityBase
|
||||
/// <summary>
|
||||
/// 值
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "值", Length = 128)]
|
||||
[Required, MaxLength(128)]
|
||||
[SugarColumn(ColumnDescription = "值", Length = 256)]
|
||||
[Required, MaxLength(256)]
|
||||
public virtual string Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -310,7 +310,7 @@ public enum ErrorCodeEnum
|
||||
/// <summary>
|
||||
/// 字典值已存在
|
||||
/// </summary>
|
||||
[ErrorCodeItemMetadata("字典值已存在,名称或编码重复")]
|
||||
[ErrorCodeItemMetadata("字典值已存在")]
|
||||
D3003,
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -33,7 +33,7 @@ public class ExcelHelper
|
||||
for (int i = 0; i < rows.Count; i++) pageItems[i].Id = rows[i].Id;
|
||||
|
||||
for (int i = 0; i < storageable.TotalList.Count; i++)
|
||||
pageItems[i].Error = storageable.TotalList[i].StorageMessage;
|
||||
pageItems[i].Error ??= storageable.TotalList[i].StorageMessage;
|
||||
}
|
||||
}));
|
||||
});
|
||||
@ -59,7 +59,7 @@ public class ExcelHelper
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 导出Xlsx数据
|
||||
/// 导出xlsx数据
|
||||
/// </summary>
|
||||
/// <param name="list"></param>
|
||||
/// <param name="fileName"></param>
|
||||
@ -72,7 +72,7 @@ public class ExcelHelper
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据类型导出Xlsx模板
|
||||
/// 根据类型导出xlsx模板
|
||||
/// </summary>
|
||||
/// <param name="list"></param>
|
||||
/// <param name="filename"></param>
|
||||
@ -98,9 +98,27 @@ public class ExcelHelper
|
||||
if (++columnIndex > 0 && item.Text.Equals(headerAttr.DisplayName)) break;
|
||||
if (columnIndex <= 0) continue;
|
||||
|
||||
// 优先从代理函数中获取下列列表,若为空且字段为枚举型,则填充枚举项为下列列表,否则不设置下列列表
|
||||
// 优先从代理函数中获取下列列表,若为空且字段为枚举型,则填充枚举项为下列列表,若为字典字段,则填充字典值value列表为下列列表
|
||||
var dataList = addListValidationFun?.Invoke(worksheet, prop)?.ToList();
|
||||
if (dataList == null && propType.IsEnum()) dataList = propType.EnumToList()?.Select(it => it.Describe).ToList();
|
||||
if (dataList == null)
|
||||
{
|
||||
if (propType.IsEnum())
|
||||
{
|
||||
// 填充枚举项为下列列表
|
||||
dataList = propType.EnumToList()?.Select(u => u.Describe).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 获取字段上的字典特性
|
||||
var dict = prop.GetCustomAttribute<DictAttribute>();
|
||||
if (dict != null)
|
||||
{
|
||||
// 填充字典值value为下列列表
|
||||
dataList = App.GetService<SysDictTypeService>().GetDataList(new GetDataDictTypeInput
|
||||
{ Code = dict.DictTypeCode }).Result?.Select(u => u.Value).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dataList != null) AddListValidation(columnIndex, dataList);
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user