From c707935008b9142f66037cb94c5c68950d70e665 Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 14 Sep 2022 12:48:20 +0800 Subject: [PATCH] =?UTF-8?q?[feature]=20flags=E7=B1=BB=E5=9E=8B=E7=9A=84enu?= =?UTF-8?q?m=E6=94=AF=E6=8C=81=E4=BB=A5=E6=9E=9A=E4=B8=BE=E9=A1=B9?= =?UTF-8?q?=E4=B8=BA=E5=AD=90=E5=88=97=E7=9A=84=E9=85=8D=E7=BD=AE=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/DataCreators/SheetDataCreator.cs | 83 ++++++++++++++++--- 1 file changed, 70 insertions(+), 13 deletions(-) diff --git a/src/Luban.Job.Cfg/Source/DataCreators/SheetDataCreator.cs b/src/Luban.Job.Cfg/Source/DataCreators/SheetDataCreator.cs index baf888a..95d3450 100644 --- a/src/Luban.Job.Cfg/Source/DataCreators/SheetDataCreator.cs +++ b/src/Luban.Job.Cfg/Source/DataCreators/SheetDataCreator.cs @@ -191,23 +191,80 @@ namespace Luban.Job.Cfg.DataCreators public DType Accept(TEnum type, RowColumnSheet sheet, TitleRow row) { - object x = row.Current; - if (CheckNull(type.IsNullable, x)) + if (row.Row != null) { - return null; + object x = row.Current; + if (CheckNull(type.IsNullable, x)) + { + return null; + } + if (CheckDefault(x)) + { + if (type.DefineEnum.IsFlags || type.DefineEnum.HasZeroValueItem) + { + return new DEnum(type, "0"); + } + else + { + throw new InvalidExcelDataException($"枚举类:'{type.DefineEnum.FullName}' 没有value为0的枚举项, 不支持默认值"); + } + } + return new DEnum(type, x.ToString()); } - if (CheckDefault(x)) + else if (row.Rows != null) { - if (type.DefineEnum.HasZeroValueItem) - { - return new DEnum(type, "0"); - } - else - { - throw new InvalidExcelDataException($"枚举类:'{type.DefineEnum.FullName}' 没有value为0的枚举项, 不支持默认值"); - } + throw new Exception($"{type.DefineEnum.FullName} 不支持多行格式"); + } + else if (row.Fields != null) + { + //throw new Exception($"array 不支持 子字段. 忘记将字段设为多行模式? {row.SelfTitle.Name} => *{row.SelfTitle.Name}"); + + var items = new List(); + var sortedFields = row.Fields.Values.ToList(); + sortedFields.Sort((a, b) => a.SelfTitle.FromIndex - b.SelfTitle.FromIndex); + foreach (var field in sortedFields) + { + string itemName = field.SelfTitle.Name; + if (!type.DefineEnum.TryValueByNameOrAlias(itemName, out _)) + { + throw new Exception($"列名:{itemName} 不是枚举类型'{type.DefineEnum.FullName}'的有效枚举项"); + } + if (field.IsBlank) + { + continue; + } + string cur = field.Current.ToString().ToLower(); + if (cur != "0" && cur != "false") + { + items.Add(itemName); + } + } + if (items.Count == 0) + { + if (type.IsNullable) + { + return null; + } + + if (type.DefineEnum.IsFlags || type.DefineEnum.HasZeroValueItem) + { + return new DEnum(type, "0"); + } + else + { + throw new InvalidExcelDataException($"枚举类:'{type.DefineEnum.FullName}' 没有value为0的枚举项, 不支持默认值"); + } + } + return new DEnum(type, string.Join('|', items)); + } + else if (row.Elements != null) + { + throw new Exception($"{type.DefineEnum.FullName} 不支持多行子字段格式"); + } + else + { + throw new Exception(); } - return new DEnum(type, x.ToString()); }