hutongqing
2024-12-14 e7cf443b37f8f4d8a1bc4fe4cd6f058f39e5c7f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
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.CodeConfigEnum;
using WIDESEA_Core.DB;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Seed;
using WIDESEA_Core.Utilities;
 
namespace WIDESEA_Core.Helper
{
    public class CodeAnalysisHelper
    {
        /// <summary>
        /// 解析编码成对象集
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="analysisCode">规则编号</param>
        /// <param name="code">需解析的字符串</param>
        /// <returns></returns>
        public static T CodeAnalysis<T>(AnalysisCodeEnum 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 (AnalysisRuleEnum.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 == AnalysisFormatTypeEnum.BD)
                                        {
                                            propertyInfo.SetValue(result, code.ChangeType(propertyInfo.PropertyType));
                                        }
                                        else
                                        {
                                            int index = items.IndexOf($"[{analysisItemRule.AnalysisFormaType}]");
                                            if (index != -1)
                                            {
                                                propertyInfo.SetValue(result, codes[index]);
                                            }
                                            else
                                            {
                                                string? codeItem = items.FirstOrDefault(x => x.Contains($"[{analysisItemRule.AnalysisFormaType}]"));
                                                if (!string.IsNullOrEmpty(codeItem))
                                                {
                                                    index = items.IndexOf(codeItem);
                                                    if (index != -1)
                                                    {
                                                        string value = codes[index];
                                                        string replaceStr = codeItem.Replace($"[{analysisItemRule.AnalysisFormaType}]", "");
 
                                                        propertyInfo.SetValue(result, value.Replace(replaceStr, "").ChangeType(propertyInfo.PropertyType));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"解析错误,{ex.Message}");
            }
 
            (bool, string, object?) validateResult = ModelValidate.ValidateModelData(result, type);
            if (!validateResult.Item1)
            {
                throw new Exception($"解析错误,{validateResult.Item2}");
            }
 
            return result;
        }
 
        /// <summary>
        /// 解析编码成对象集合
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="analysisCode">规则编号</param>
        /// <param name="codeList">需解析的字符串集合</param>
        /// <returns></returns>
        public static List<T> CodeAnalysis<T>(AnalysisCodeEnum 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 (AnalysisRuleEnum.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 == AnalysisFormatTypeEnum.BD)
                                            {
                                                propertyInfo.SetValue(result, code.ChangeType(propertyInfo.PropertyType));
                                            }
                                            else
                                            {
                                                int index = items.IndexOf($"[{analysisItemRule.AnalysisFormaType}]");
                                                if (index != -1)
                                                {
                                                    propertyInfo.SetValue(result, codes[index]);
                                                }
                                                else
                                                {
                                                    string? codeItem = items.FirstOrDefault(x => x.Contains($"[{analysisItemRule.AnalysisFormaType}]"));
                                                    if (!string.IsNullOrEmpty(codeItem))
                                                    {
                                                        index = items.IndexOf(codeItem);
                                                        if (index != -1)
                                                        {
                                                            string value = codes[index];
                                                            string replaceStr = codeItem.Replace($"[{analysisItemRule.AnalysisFormaType}]", "");
 
                                                            propertyInfo.SetValue(result, value.Replace(replaceStr, "").ChangeType(propertyInfo.PropertyType));
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                list.Add(result);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
 
            }
            return list;
        }
    }
}