From c020f31a67fc5aa5644511bddff075f7ecc85234 Mon Sep 17 00:00:00 2001 From: qinchulong <qinchulong@hnkhzn.com> Date: 星期二, 27 五月 2025 15:35:27 +0800 Subject: [PATCH] Merge branch 'master' of http://115.159.85.185:8098/r/HuaYiZhongHeng/ZhongHeLiTiKu --- 代码管理/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/CodeAnalysisHelper.cs | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 155 insertions(+), 0 deletions(-) diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/CodeAnalysisHelper.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/CodeAnalysisHelper.cs" new file mode 100644 index 0000000..6dbad0f --- /dev/null +++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/CodeAnalysisHelper.cs" @@ -0,0 +1,155 @@ +锘縰sing OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using WIDESEA_Core.Attributes; +using WIDESEA_Core.DB; +using WIDESEA_Core.Enums; +using WIDESEA_Core.Seed; + +namespace WIDESEA_Core.Helper +{ + public class CodeAnalysisHelper + { + /// <summary> + /// 瑙f瀽缂栫爜鎴愬璞¢泦 + /// </summary> + /// <typeparam name="T">娉涘瀷</typeparam> + /// <param name="analysisCode">瑙勫垯缂栧彿</param> + /// <param name="code">闇�瑙f瀽鐨勫瓧绗︿覆</param> + /// <returns></returns> + public static T CodeAnalysis<T>(AnalysisCode analysisCode, string code) + { + Type type = typeof(T); + object? obj = Activator.CreateInstance(type); + if (obj == null) + throw new Exception("瀹炰緥鍖栧璞¢敊璇�"); + T result = (T)obj; + try + { + AnalysisRuleAttribute? analysisRule = type.GetCustomAttribute<AnalysisRuleAttribute>(); + if (analysisRule != null) + { + SqlSugarClient sugarClient = new SqlSugarClient(new ConnectionConfig + { + ConfigId = MainDb.CurrentDbConnId, + ConnectionString = DBContext.GetMainConnectionDb().Connection, + IsAutoCloseConnection = true, + DbType = MainDb.DbType, + }); + + dynamic ruleConfig = sugarClient.Queryable(MainDb.AnalysisRuleConfig, "x").Where(MainDb.AnalysisCode, "=", analysisCode.ToString()).First(); + if (ruleConfig != null) + { + string format = ruleConfig.Format; + string splitStr = ruleConfig.SplitStr; + List<string> items = format.Split(splitStr).ToList(); + List<string> codes = code.Split(splitStr).ToList(); + if (items.Count == codes.Count) + { + PropertyInfo[] propertyInfos = type.GetProperties(); + if (AnalysisRule.Split == analysisRule.AnalysisRule) + { + for (int i = 0; i < propertyInfos.Length; i++) + { + PropertyInfo propertyInfo = propertyInfos[i]; + AnalysisItemRuleAttribute? analysisItemRule = propertyInfo.GetCustomAttribute<AnalysisItemRuleAttribute>(); + if (analysisItemRule != null) + { + int index = items.IndexOf($"[{analysisItemRule.AnalysisFormaType}]"); + if (index != -1) + propertyInfo.SetValue(result, codes[index]); + } + } + } + } + + } + } + } + catch (Exception ex) + { + + } + return result; + } + + /// <summary> + /// 瑙f瀽缂栫爜鎴愬璞¢泦鍚� + /// </summary> + /// <typeparam name="T">娉涘瀷</typeparam> + /// <param name="analysisCode">瑙勫垯缂栧彿</param> + /// <param name="codeList">闇�瑙f瀽鐨勫瓧绗︿覆闆嗗悎</param> + /// <returns></returns> + public static List<T> CodeAnalysis<T>(AnalysisCode analysisCode, List<string> codeList) + { + Type type = typeof(T); + List<T> list = new List<T>(); + + try + { + AnalysisRuleAttribute? analysisRule = type.GetCustomAttribute<AnalysisRuleAttribute>(); + if (analysisRule != null) + { + SqlSugarClient sugarClient = new SqlSugarClient(new ConnectionConfig + { + ConfigId = MainDb.CurrentDbConnId, + ConnectionString = DBContext.GetMainConnectionDb().Connection, + IsAutoCloseConnection = true, + DbType = MainDb.DbType, + }); + + dynamic ruleConfig = sugarClient.Queryable(MainDb.AnalysisRuleConfig, "x").Where(MainDb.AnalysisCode, "=", analysisCode.ToString()).First(); + if (ruleConfig != null) + { + for (int j = 0; j < codeList.Count; j++) + { + T result = (T)Activator.CreateInstance(type); + string code = codeList[j]; + string format = ruleConfig.Format; + string splitStr = ruleConfig.SplitStr; + List<string> items = format.Split(splitStr).ToList(); + List<string> codes = code.Split(splitStr).ToList(); + if (items.Count == codes.Count) + { + PropertyInfo[] propertyInfos = type.GetProperties(); + if (AnalysisRule.Split == analysisRule.AnalysisRule) + { + for (int i = 0; i < propertyInfos.Length; i++) + { + PropertyInfo propertyInfo = propertyInfos[i]; + AnalysisItemRuleAttribute? analysisItemRule = propertyInfo.GetCustomAttribute<AnalysisItemRuleAttribute>(); + if (analysisItemRule != null) + { + if(analysisItemRule.AnalysisFormaType == AnalysisFormatType.BD) + { + propertyInfo.SetValue(result, code.ChangeType(propertyInfo.PropertyType)); + } + else + { + int index = items.IndexOf($"[{analysisItemRule.AnalysisFormaType}]"); + if (index != -1) + propertyInfo.SetValue(result, codes[index].ChangeType(propertyInfo.PropertyType)); + } + + } + } + } + list.Add(result); + } + } + } + } + } + catch (Exception ex) + { + + } + return list; + } + } +} -- Gitblit v1.9.3