using System.Reflection; using YLErp.Modules.SuperviseReportModule.SAC.Model; namespace YLErp.Modules.SuperviseReportModule.SAC.Common { class Sac_TranslateHelper { private static SacMap? Map = null; private static List GetSacInfo(PropertyInfo[] properties) { var result = new List(); foreach (var item in properties) { var sacInfo = new SacMap(item.Name, item.Name); if (item.GetCustomAttributes(typeof(SacDescriptionAttribute)).FirstOrDefault() is SacDescriptionAttribute info) { sacInfo.FieldName = info.FieldName; sacInfo.FieldName_CN = info.FieldName_CN; sacInfo.FieldLength = info.FieldLength; sacInfo.Required = info.Required; sacInfo.FieldDescription = info.FieldDescription; } var att = item.PropertyType.GetGenericArguments(); if (att.Length == 0) { att = new[] { item.PropertyType }; } if (att.Length == 1 && att[0].Namespace.StartsWith("YLErp.Modules.SuperviseReportModule")) { sacInfo.SubMaps.AddRange(GetSacInfo(att[0].GetProperties())); } //if(item.) result.Add(sacInfo); } return result; } private static void Init() { Map = new SacMap("Root", "报文"); var root = typeof(ReportRootModel); var properties = root.GetProperties(); Map.SubMaps.AddRange(GetSacInfo(properties)); } public static SacMap TranslateFieldName(params string[] paths) { if (paths.Length == 0) { throw new ArgumentNullException(nameof(paths)); } if (Map == null) { Init(); } var map = Map; foreach (var item in paths) { if (item == "Root") { continue; } if (map == null || map.SubMaps == null || (map = map.SubMaps.Where(O => O.FieldName == item).FirstOrDefault()) == null) { map = new SacMap(item, item); } } return map; } public static string TranslateFieldName(string fieldName, string fileType = "") { if (Map == null) { Init(); } SacMap map = Map; return getFieldName_CN(map, fieldName, fileType); } private static string getFieldName_CN(SacMap map, string fieldName, string fileType = "") { if (map.FieldName == fieldName) { return map.FieldName_CN; } if (map.SubMaps.Count > 0) { foreach (var m in map.SubMaps) { if (map.FieldName == "Body") { if (!string.IsNullOrWhiteSpace(fileType) && m.FieldName != fileType) { continue; } } var ret = getFieldName_CN(m, fieldName, fileType); if (!string.IsNullOrWhiteSpace(ret)) { return ret; } } } return ""; } } /// /// 字段说明属性类 /// [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)] public class SacDescriptionAttribute : Attribute { /// /// 字段名 /// public string FieldName { get; set; } /// /// 字段中文名 /// public string FieldName_CN { get; set; } /// /// 字段长度 /// public string FieldLength { get; set; } /// /// 必填 /// public bool Required { get; set; } /// /// 字段说明 /// public string FieldDescription { get; set; } public SacDescriptionAttribute() { } public SacDescriptionAttribute(string fieldName, string fieldName_CN, string fileLength, bool required, string fieldDescription) { FieldName = fieldName; FieldName_CN = fieldName_CN; FieldLength = fileLength; Required = required; FieldDescription = fieldDescription; } } /// /// 用于预览 /// public class SacMap : SacDescriptionAttribute { /// /// 子节点 /// public List SubMaps { get; set; } public SacMap() { } public SacMap(string fieldName, string fieldName_CN) { FieldName = fieldName; FieldName_CN = fieldName_CN; SubMaps = new List(); } public override string ToString() { return $"{FieldName}_{FieldDescription}_{SubMaps.Count}"; } } public class SacInfo : SacDescriptionAttribute { /// /// 对象序号 /// public int Index { get; set; } /// /// 值 /// public string FieldValue { get; set; } /// /// 检查结果 /// public string Message { get; set; } /// /// 子信息 /// public List SubMaps { get; set; } public SacInfo() { } public SacInfo(string fileName) : this(fileName, "", "") { } public SacInfo(string fileName, int index) : this(fileName, "", "") { Index = index; } public SacInfo(string fileName, object value) : this(fileName, value, "") { } public SacInfo(string fileName, object value, string message) { FieldName = fileName; FieldValue = (value as string) ?? ""; Message = message; Index = -1; SubMaps = new List(); } public void formatFieldDescription(params string[] paths) { var list = new List(paths) { FieldName }; var arr = list.ToArray(); var info = Sac_TranslateHelper.TranslateFieldName(arr); FieldName = info.FieldName; FieldName_CN = info.FieldName_CN; FieldLength = info.FieldLength; FieldDescription = info.FieldDescription; Required = info.Required; if (this.SubMaps != null && this.SubMaps.Count > 0) { SubMaps?.ForEach(O => { if (O == null) { return; } O.formatFieldDescription(arr); }); } } } public class ExceptionExtensionInfo { public List Tag { get; set; } public string ErrFilePath { get; set; } } }