服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-05-31 d4c326deaa51e7d4897a84afc339684012b8cfbe
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using Rhea.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
 
namespace Tiger.Business.SqlSugarHepler
{
    /// <summary>
    ///
    /// </summary>
    public class BizSqlsugar
    {
        #region 私有变量
 
        /// <summary>
        /// 最大插入数
        /// </summary>
        private const int MaxCount = 1000;
 
        /// <summary>
        /// T100接口URL
        /// </summary>
        public static string t100Url { get; set; } = @"http://172.18.8.11/wstopprd/ws/r/awsp920";
 
        #endregion 私有变量
 
        /// <summary>
        /// 循环插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <param name="db"></param>
        public static int InsertDataTable<T>(List<T> input, DbClient db) where T : class, new()
        {
            int count = input.Count();
            int result = 0;
            if (input.Count() > MaxCount)
            {
                int round = (count / MaxCount) + ((count % MaxCount) > 0 ? 1 : 0);
                List<T> addLst = new List<T>();
                for (int i = 0; i < round; i++)
                {
                    int start = i * MaxCount;
                    int end = i == (round - 1) ? count : (start + MaxCount);
                    for (int j = start; j < end; j++)
                    {
                        addLst.Add(input[j]);
                    }
                    result += db.Fastest<T>().BulkCopy(addLst);
                    addLst.Clear();
                }
            }
            else
            {
                //result += db.Insertable(input).ExecuteCommand();
                result += db.Fastest<T>().BulkCopy(input);
            }
            return result;
        }
 
        /// <summary>
        /// 循环更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static int UpdateDataTable<T>(List<T> input, DbClient db) where T : class, new()
        {
            int res = 0;
            foreach (var item in input)
            {
                res += db.Updateable(item).ExecuteCommand();
            }
            return res;
        }
 
        #region 事务
 
        /// <summary>
        /// 同步事务
        /// </summary>
        /// <param name="action"></param>
        /// <exception cref="Exception"></exception>
        /// <exception cref="BizException"></exception>
        public static void CreateTran(Action<ISqlSugarClient> action, DbClient _dbContext)
        {
            if (action == null)
            {
                throw new Exception("方法CreateTran的参数不能为空!");
            }
            string bizErrorMsg = string.Empty;
            Exception error = null;
            var res = _dbContext.Ado.UseTran(() =>
            {
                try
                {
                    action.Invoke(_dbContext);
                    _dbContext.Ado.CommitTran();
                }
                catch (Exception ex)
                {
                    _dbContext.Ado.RollbackTran();
                    if (ex is Exception)
                    {
                        bizErrorMsg = ex.Message;
                    }
                    else
                    {
                        error = ex;
                    }
                }
                finally
                {
                    _dbContext.Ado.Close();
                }
            });
 
            if (!string.IsNullOrWhiteSpace(bizErrorMsg))
            {
                throw new Exception(bizErrorMsg);
            }
            if (error != null)
            {
                throw error;
            }
            if (!res.IsSuccess)
            {
                throw new Exception(res.ErrorMessage);
            }
        }
 
        /// <summary>
        /// 同步事务
        /// </summary>
        /// <param name="action"></param>
        /// <param name="_dbContext"></param>
        /// <exception cref="Exception"></exception>
        public static void CreateTran(Action action, DbClient _dbContext)
        {
            if (action == null)
            {
                throw new Exception("方法CreateTran的参数不能为空!");
            }
            string bizErrorMsg = string.Empty;
            Exception error = null;
            var res = _dbContext.Ado.UseTran(() =>
            {
                try
                {
                    action.Invoke();
                    _dbContext.Ado.CommitTran();
                }
                catch (Exception ex)
                {
                    _dbContext.Ado.RollbackTran();
                    if (ex is Exception)
                    {
                        bizErrorMsg = ex.Message;
                    }
                    else
                    {
                        error = ex;
                    }
                }
                finally
                {
                    _dbContext.Ado.Close();
                }
            });
            if (!string.IsNullOrWhiteSpace(bizErrorMsg))
            {
                throw new Exception(bizErrorMsg);
            }
            if (error != null)
            {
                throw error;
            }
            if (!res.IsSuccess)
            {
                throw new Exception(res.ErrorMessage);
            }
        }
 
        /// <summary>
        /// 同步带返回类型的事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="action"></param>
        /// <param name="_dbContext"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static T CreateTran<T>(Func<T> action, DbClient _dbContext)
        {
            if (action == null)
            {
                throw new Exception("方法CreateTran的参数不能为空!");
            }
            var res = _dbContext.Ado.UseTran(() =>
            {
                return action.Invoke();
            }, (exception) =>
            {
                throw exception;
            });
            if (!res.IsSuccess)
            {
                throw new Exception(res.ErrorMessage);
            }
            return res.Data;
        }
 
        /// <summary>
        /// 异步带返回类型的事务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="action"></param>
        /// <param name="_dbContext"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static async Task<T> CreateTranAsync<T>(Func<T> action, DbClient _dbContext)
        {
            if (action == null)
            {
                throw new Exception("方法CreateTran的参数不能为空!");
            }
            var res = await _dbContext.Ado.UseTranAsync(() =>
            {
                return Task.FromResult(action.Invoke());
            }, (exception) =>
            {
                throw exception;
            });
            if (!res.IsSuccess)
            {
                throw new Exception(res.ErrorMessage);
            }
            return res.Data;
        }
 
        #endregion 事务
    }
}