zhoufan преди 3 години
родител
ревизия
9e6abd8e85

+ 166 - 25
Net6Demo_Api.Business/BaseBusiness.cs

@@ -218,40 +218,72 @@ namespace Net6Demo_Api.Business
218 218
         /// 更新一条数据
219 219
         /// </summary>
220 220
         /// <param name="entity">实体对象</param>
221
-        public int Update(T entity)
221
+        /// <param name="properties">更新的实体属性</param>
222
+        public int Update(T entity, List<string> properties = null)
222 223
         {
223 224
             if (IsCache) OutRedis().Wait();
224
-            return _db.Update(entity);
225
+            if (properties == null || properties.Count == 0)
226
+            {
227
+                return _db.Update(entity);
228
+            }
229
+            else
230
+            {
231
+                return _db.Update(entity, properties);
232
+            }
225 233
         }
226 234
 
227 235
         /// <summary>
228 236
         /// 更新一条数据
229 237
         /// </summary>
230 238
         /// <param name="entity">实体对象</param>
231
-        public async Task<int> UpdateAsync(T entity)
239
+        /// <param name="properties">更新的实体属性</param>
240
+        public async Task<int> UpdateAsync(T entity, List<string> properties = null)
232 241
         {
233 242
             if (IsCache) await OutRedis();
234
-            return await _db.UpdateAsync(entity);
243
+            if (properties == null || properties.Count == 0)
244
+            {
245
+                return await _db.UpdateAsync(entity);
246
+            }
247
+            else
248
+            {
249
+                return await _db.UpdateAsync(entity, properties);
250
+            }
235 251
         }
236 252
 
237 253
         /// <summary>
238 254
         /// 更新多条数据
239 255
         /// </summary>
240 256
         /// <param name="entities">数据列表</param>
241
-        public int Update(List<T> entities)
257
+        /// <param name="properties">更新的实体属性</param>
258
+        public int Update(List<T> entities, List<string> properties = null)
242 259
         {
243 260
             if (IsCache) OutRedis().Wait();
244
-            return _db.Update(entities);
261
+            if (properties == null || properties.Count == 0)
262
+            {
263
+                return _db.Update(entities);
264
+            }
265
+            else
266
+            {
267
+                return _db.Update(entities, properties);
268
+            }
245 269
         }
246 270
 
247 271
         /// <summary>
248 272
         /// 更新多条数据
249 273
         /// </summary>
250 274
         /// <param name="entities">数据列表</param>
251
-        public async Task<int> UpdateAsync(List<T> entities)
275
+        /// <param name="properties">更新的实体属性</param>
276
+        public async Task<int> UpdateAsync(List<T> entities, List<string> properties = null)
252 277
         {
253 278
             if (IsCache) await OutRedis();
254
-            return await _db.UpdateAsync(entities);
279
+            if (properties == null || properties.Count == 0)
280
+            {
281
+                return await _db.UpdateAsync(entities);
282
+            }
283
+            else
284
+            {
285
+                return await _db.UpdateAsync(entities, properties);
286
+            }
255 287
         }
256 288
 
257 289
         /// <summary>
@@ -331,7 +363,7 @@ namespace Net6Demo_Api.Business
331 363
                 var listCache = GetRedis().Result;
332 364
                 if (listCache != null)
333 365
                 {
334
-                    return listCache.AsQueryable().Where(p=> KeyFiled.GetValue(p).ToString() == keyValue[0].ToString()).FirstOrDefault();
366
+                    return listCache.AsQueryable().Where(p => KeyFiled.GetValue(p).ToString() == keyValue[0].ToString()).FirstOrDefault();
335 367
                 }
336 368
             }
337 369
             #endregion
@@ -348,7 +380,7 @@ namespace Net6Demo_Api.Business
348 380
             #region 启用缓存
349 381
             if (IsCache)
350 382
             {
351
-                var listCache =await GetRedis();
383
+                var listCache = await GetRedis();
352 384
                 if (listCache != null)
353 385
                 {
354 386
                     return listCache.AsQueryable().Where(p => KeyFiled.GetValue(p).ToString() == keyValue[0].ToString()).FirstOrDefault();
@@ -360,40 +392,149 @@ namespace Net6Demo_Api.Business
360 392
         }
361 393
 
362 394
         /// <summary>
363
-        /// 分页获取数据
395
+        /// 根据条件获取数量
364 396
         /// </summary>
365
-        /// <param name="page">分页信息</param>
366
-        /// <param name="orderByString">排序</param>
367
-        /// <param name="condition">条件</param>
368 397
         /// <returns></returns>
369
-        public async Task<ValueTuple<List<T>, int>> GetListPageAsync(PageInput page, string orderByString, Expression<Func<T, bool>> condition)
398
+        public int GetCount(Expression<Func<T, bool>> condition)
370 399
         {
371
-            var iq = GetIQueryable().Where(condition);
372
-            var list = await iq.OrderBy(orderByString).Skip((page.PageIndex - 1) * page.PageRows).Take(page.PageRows).ToListAsync();
373
-            int total = await iq.CountAsync();
400
+            #region 启用缓存
401
+            if (IsCache)
402
+            {
403
+                var listCache = GetRedis().Result;
404
+                if (listCache != null)
405
+                {
406
+                    return listCache.AsQueryable().Where(condition).Count();
407
+                }
408
+            }
409
+            #endregion
374 410
 
375
-            return (list, total);
411
+            return _db.GetIQueryable<T>().AsNoTracking().Where(condition).Count();
376 412
         }
377 413
 
378 414
         /// <summary>
379
-        /// 获取实体对应的表,延迟加载,主要用于支持Linq查询操作
415
+        /// 根据条件获取数量
416
+        /// </summary>
417
+        /// <returns></returns>
418
+        public async Task<int> GetCountAsync(Expression<Func<T, bool>> condition)
419
+        {
420
+            #region 启用缓存
421
+            if (IsCache)
422
+            {
423
+                var listCache = await GetRedis();
424
+                if (listCache != null)
425
+                {
426
+                    return listCache.AsQueryable().Where(condition).Count();
427
+                }
428
+            }
429
+            #endregion
430
+
431
+            return await _db.GetIQueryable<T>().AsNoTracking().Where(condition).CountAsync();
432
+        }
433
+
434
+        /// <summary>
435
+        /// 获取表的所有数据,当数据量很大时不要使用!
436
+        /// </summary>
437
+        /// <returns></returns>
438
+        public List<T> GetList()
439
+        {
440
+            #region 启用缓存
441
+            if (IsCache)
442
+            {
443
+                var listCache = GetRedis().Result;
444
+                if (listCache != null)
445
+                {
446
+                    return listCache;
447
+                }
448
+            }
449
+            #endregion
450
+            return _db.GetIQueryable<T>().AsNoTracking().ToList();
451
+        }
452
+
453
+        /// <summary>
454
+        /// 根据条件获取列表,当数据量很大时不要使用!
380 455
         /// </summary>
381
-        /// <param name="IsNoCache">默认false,需要关联表查询时用true</param>
456
+        /// <param name="condition">条件</param>
382 457
         /// <returns></returns>
383
-        public IQueryable<T> GetIQueryable(bool IsNoCache = false)
458
+        public List<T> GetList(Expression<Func<T, bool>> condition)
384 459
         {
385 460
             #region 启用缓存
386
-            if (IsCache && !IsNoCache)
461
+            if (IsCache)
387 462
             {
388 463
                 var listCache = GetRedis().Result;
389 464
                 if (listCache != null)
390 465
                 {
391
-                    return listCache.AsQueryable();
466
+                    return listCache.AsQueryable().Where(condition).ToList();
392 467
                 }
393 468
             }
394 469
             #endregion
395 470
 
396
-            return _db.GetIQueryable<T>().AsNoTracking();
471
+            return _db.GetIQueryable<T>().AsNoTracking().Where(condition).ToList();
472
+        }
473
+
474
+        /// <summary>
475
+        /// 获取表的所有数据,当数据量很大时不要使用!
476
+        /// </summary>
477
+        /// <returns></returns>
478
+        public async Task<List<T>> GetListAsync()
479
+        {
480
+            #region 启用缓存
481
+            if (IsCache)
482
+            {
483
+                var listCache = await GetRedis();
484
+                if (listCache != null)
485
+                {
486
+                    return listCache;
487
+                }
488
+            }
489
+            #endregion
490
+            return await _db.GetIQueryable<T>().AsNoTracking().ToListAsync();
491
+        }
492
+
493
+        /// <summary>
494
+        /// 根据条件获取列表,当数据量很大时不要使用!
495
+        /// </summary>
496
+        /// <param name="condition">条件</param>
497
+        /// <returns></returns>
498
+        public async Task<List<T>> GetListAsync(Expression<Func<T, bool>> condition)
499
+        {
500
+            #region 启用缓存
501
+            if (IsCache)
502
+            {
503
+                var listCache = await GetRedis();
504
+                if (listCache != null)
505
+                {
506
+                    return listCache.AsQueryable().Where(condition).ToList();
507
+                }
508
+            }
509
+            #endregion
510
+
511
+            return await _db.GetIQueryable<T>().AsNoTracking().Where(condition).ToListAsync();
512
+        }
513
+
514
+        /// <summary>
515
+        /// 分页获取数据
516
+        /// </summary>
517
+        /// <param name="page">分页信息</param>
518
+        /// <param name="orderByString">排序</param>
519
+        /// <param name="condition">条件</param>
520
+        /// <returns></returns>
521
+        public async Task<ValueTuple<List<T>, int>> GetListPageAsync(PageInput page, string orderByString, Expression<Func<T, bool>> condition)
522
+        {
523
+            var iq = _db.GetIQueryable<T>().AsNoTracking().Where(condition).OrderBy(orderByString);
524
+
525
+            var list = await iq.Skip((page.PageIndex - 1) * page.PageRows).Take(page.PageRows).ToListAsync();
526
+            int total = await iq.CountAsync();
527
+
528
+            return (list, total);
529
+        }
530
+
531
+        /// <summary>
532
+        /// 获取实体对应的表,延迟加载,主要用于支持Linq查询操作
533
+        /// </summary>
534
+        /// <returns></returns>
535
+        public virtual IQueryable<T> GetIQueryable()
536
+        {
537
+            return _db.GetIQueryable<T>();
397 538
         }
398 539
 
399 540
         #endregion

+ 44 - 9
Net6Demo_Api.IBusiness/IBaseBusiness.cs

@@ -108,13 +108,15 @@ namespace Net6Demo_Api.IBusiness
108 108
         /// 更新一条数据
109 109
         /// </summary>
110 110
         /// <param name="entity">实体对象</param>
111
-        int Update(T entity);
111
+        /// <param name="properties">更新的实体属性</param>
112
+        int Update(T entity, List<string> properties = null);
112 113
 
113 114
         /// <summary>
114 115
         /// 更新一条数据
115 116
         /// </summary>
116 117
         /// <param name="entity">实体对象</param>
117
-        Task<int> UpdateAsync(T entity);
118
+        /// <param name="properties">更新的实体属性</param>
119
+        Task<int> UpdateAsync(T entity, List<string> properties = null);
118 120
 
119 121
         /// <summary>
120 122
         /// 指定条件更新
@@ -168,20 +170,53 @@ namespace Net6Demo_Api.IBusiness
168 170
         Task<T> GetEntityAsync(params object[] keyValue);
169 171
 
170 172
         /// <summary>
171
-        /// 分页获取数据
173
+        /// 根据条件获取数量
172 174
         /// </summary>
173
-        /// <param name="page">分页信息</param>
174
-        /// <param name="orderByString">排序</param>
175 175
         /// <param name="condition">条件</param>
176 176
         /// <returns></returns>
177
-        Task<ValueTuple<List<T>, int>> GetListPageAsync(PageInput page, string orderByString, Expression<Func<T, bool>> condition);
177
+        int GetCount(Expression<Func<T, bool>> condition);
178
+
179
+        /// <summary>
180
+        /// 根据条件获取数量
181
+        /// </summary>
182
+        /// <param name="condition">条件</param>
183
+        /// <returns></returns>
184
+        Task<int> GetCountAsync(Expression<Func<T, bool>> condition);
185
+
186
+        /// <summary>
187
+        /// 获取表的所有数据,当数据量很大时不要使用!
188
+        /// </summary>
189
+        /// <returns></returns>
190
+        List<T> GetList();
191
+
192
+        /// <summary>
193
+        /// 根据条件获取列表,当数据量很大时不要使用!
194
+        /// </summary>
195
+        /// <param name="condition">条件</param>
196
+        /// <returns></returns>
197
+        List<T> GetList(Expression<Func<T, bool>> condition);
198
+
199
+        /// <summary>
200
+        /// 获取表的所有数据,当数据量很大时不要使用!
201
+        /// </summary>
202
+        /// <returns></returns>
203
+        Task<List<T>> GetListAsync();
204
+
205
+        /// <summary>
206
+        /// 根据条件获取列表,当数据量很大时不要使用!
207
+        /// </summary>
208
+        /// <param name="condition">条件</param>
209
+        /// <returns></returns>
210
+        Task<List<T>> GetListAsync(Expression<Func<T, bool>> condition);
178 211
 
179 212
         /// <summary>
180
-        /// 获取实体对应的表,延迟加载,主要用于支持Linq查询操作
213
+        /// 分页获取数据
181 214
         /// </summary>
182
-        /// <param name="IsNoCache">默认false,需要关联表查询时用true</param>
215
+        /// <param name="page">分页信息</param>
216
+        /// <param name="orderByString">排序</param>
217
+        /// <param name="condition">条件</param>
183 218
         /// <returns></returns>
184
-        IQueryable<T> GetIQueryable(bool IsNoCache = false);
219
+        Task<ValueTuple<List<T>, int>> GetListPageAsync(PageInput page, string orderByString, Expression<Func<T, bool>> condition);
185 220
 
186 221
         #endregion
187 222
 

+ 6 - 4
Net6Demo_Api/Controllers/HomeController.cs

@@ -93,7 +93,7 @@ namespace Net6Demo_Api.Controllers
93 93
         [HttpPost]
94 94
         public async Task<ActionResult> SubmitLogin(LoginInput input)
95 95
         {
96
-            var user = await _userBus.GetIQueryable().Where(p => p.F_UserCode == input.usercode && p.F_Password == input.password.ToMD5String()).FirstOrDefaultAsync();
96
+            var user = (await _userBus.GetListAsync(p => p.F_UserCode == input.usercode && p.F_Password == input.password.ToMD5String())).FirstOrDefault();
97 97
 
98 98
             if (user == null)
99 99
             {
@@ -105,9 +105,11 @@ namespace Net6Demo_Api.Controllers
105 105
             }
106 106
 
107 107
             //await _userBus.ExecuteSqlAsync("update T_Sys_UserAccount set F_LastActiveTime=SYSDATE() where F_UserId=" + user.F_UserId);
108
-            Dictionary<string, object?> param = new Dictionary<string, object?>();
109
-            param.Add("F_LastActiveTime",DateTime.Now);
110
-            await _userBus.UpdateSqlAsync(p => p.F_UserId == user.F_UserId, param);
108
+            //Dictionary<string, object?> param = new Dictionary<string, object?>();
109
+            //param.Add("F_LastActiveTime",DateTime.Now);
110
+            //await _userBus.UpdateSqlAsync(p => p.F_UserId == user.F_UserId, param);
111
+            user.F_LastActiveTime = DateTime.Now;
112
+            await _userBus.UpdateAsync(user, new List<string>() { "F_LastActiveTime" });
111 113
 
112 114
             var claims = new[]
113 115
             {

+ 2 - 2
Net6Demo_Api/Controllers/IndexController.cs

@@ -40,8 +40,8 @@ namespace Net6Demo_Api.Controllers
40 40
         [HttpGet]
41 41
         public async Task<ActionResult> GetMenu()
42 42
         {
43
-            var mids = await _roleMenuBus.GetIQueryable().Where(p => p.F_RoleId == CurrentUser.RoleId).Select(p => p.F_MenuId).ToListAsync();
44
-            var menus = _mapper.Map<List<MenuTreeView>>(await _menuBus.GetIQueryable().Where(p => mids.Contains(p.F_MenuId) && p.F_State == 1).ToListAsync());
43
+            var mids = (await _roleMenuBus.GetListAsync(p => p.F_RoleId == CurrentUser.RoleId)).Select(p => p.F_MenuId).ToList();
44
+            var menus = _mapper.Map<List<MenuTreeView>>((await _menuBus.GetListAsync(p => mids.Contains(p.F_MenuId) && p.F_State == 1)).ToList());
45 45
             return Success("成功", GetTreeList(menus));
46 46
 
47 47
             List<MenuTreeView> GetTreeList(List<MenuTreeView> list, int parentId = 0)

+ 2 - 2
Net6Demo_Api/Controllers/System/CustomFiledController.cs

@@ -27,7 +27,7 @@ namespace Net6Demo_Api.Controllers
27 27
         [HttpGet]
28 28
         public async Task<ActionResult> GetData(string tableid)
29 29
         {
30
-            var data = await _configBus.GetIQueryable().Where(p => p.F_TableId == tableid && p.F_CreateUser == CurrentUser.UserCode).FirstOrDefaultAsync();
30
+            var data = (await _configBus.GetListAsync(p => p.F_TableId == tableid && p.F_CreateUser == CurrentUser.UserCode)).FirstOrDefault();
31 31
             if (data != null)
32 32
             {
33 33
                 return Success("成功", _mapper.Map<CustomFiledView>(data));
@@ -46,7 +46,7 @@ namespace Net6Demo_Api.Controllers
46 46
         public async Task<ActionResult> SaveData(CustomFiledAddEditInput input)
47 47
         {
48 48
             int result = 0;
49
-            var oldData = await _configBus.GetIQueryable().Where(p => p.F_TableId == input.TableId && p.F_CreateUser == CurrentUser.UserCode).FirstOrDefaultAsync();
49
+            var oldData = (await _configBus.GetListAsync(p => p.F_TableId == input.TableId && p.F_CreateUser == CurrentUser.UserCode)).FirstOrDefault();
50 50
             if (oldData != null)
51 51
             {
52 52
                 _mapper.Map(input, oldData);

+ 1 - 1
Net6Demo_Api/Controllers/System/DepartmentController.cs

@@ -58,7 +58,7 @@ namespace Net6Demo_Api.Controllers
58 58
         [HttpGet]
59 59
         public async Task<ActionResult> GetTreeList()
60 60
         {
61
-            var depts = _mapper.Map<List<DepartmentTreeView>>(await _deptBus.GetIQueryable().Where(p => p.F_State == 1).ToListAsync());
61
+            var depts = _mapper.Map<List<DepartmentTreeView>>(await _deptBus.GetListAsync(p => p.F_State == 1));
62 62
             return Success("成功", GetTreeList(depts));
63 63
 
64 64
             List<DepartmentTreeView> GetTreeList(List<DepartmentTreeView> list, int parentId = 0)

+ 2 - 2
Net6Demo_Api/Controllers/System/RoleController.cs

@@ -155,8 +155,8 @@ namespace Net6Demo_Api.Controllers
155 155
         [HttpGet]
156 156
         public async Task<ActionResult> GetRoleMenu(int roleid)
157 157
         {
158
-            var rolemenuids = await _roleMenuBus.GetIQueryable().Where(p => p.F_RoleId == roleid).Select(p => p.F_MenuId).ToListAsync();
159
-            var menus = await _menuBus.GetIQueryable().Where(p => p.F_State == 1).ToListAsync();
158
+            var rolemenuids = (await _roleMenuBus.GetListAsync(p => p.F_RoleId == roleid)).Select(p => p.F_MenuId).ToList();
159
+            var menus = await _menuBus.GetListAsync(p => p.F_State == 1);
160 160
             var result = menus.Select(x => new
161 161
             {
162 162
                 id = x.F_MenuId,

+ 1 - 1
Net6Demo_Api/Controllers/System/UserAccountController.cs

@@ -80,7 +80,7 @@ namespace Net6Demo_Api.Controllers
80 80
                 q = q.And(x => EF.Functions.Like(x.F_UserName, "%" + input.username + "%"));
81 81
             }
82 82
 
83
-            var result = await _userBus.GetIQueryable().Where(q).ToListAsync();
83
+            var result = await _userBus.GetListAsync(q);
84 84
             return Success("成功", _mapper.Map<List<UserAccountView>>(result));
85 85
         }
86 86
         /// <summary>

+ 2 - 2
Net6Demo_Api/Filters/AuthorityFilter.cs

@@ -64,12 +64,12 @@ namespace Net6Demo_Api.Filters
64 64
                             string usercode = cls.Where(x => x.Type == "UserCode").FirstOrDefault()?.Value??"";
65 65
                             string roleid = cls.Where(x => x.Type == "RoleId").FirstOrDefault()?.Value ?? "";
66 66
                             //判断用户存在和角色是否改变
67
-                            var user = await _userBus.GetIQueryable().Where(p => p.F_UserCode == usercode && p.F_DeleteFlag == 0).FirstOrDefaultAsync(); 
67
+                            var user = (await _userBus.GetListAsync(p => p.F_UserCode == usercode && p.F_DeleteFlag == 0)).FirstOrDefault(); 
68 68
                             if (user != null && user.F_RoleId.ToString() == roleid)
69 69
                             {
70 70
                                 //判断此请求是否需要授权
71 71
                                 var path = ad.RouteValues["controller"] + "/" + ad.RouteValues["action"];
72
-                                var mid = await _menuBus.GetIQueryable().Where(p => p.F_State == 1 && p.F_Action == path).Select(p => p.F_MenuId).FirstOrDefaultAsync();
72
+                                var mid = (await _menuBus.GetListAsync(p => p.F_State == 1 && p.F_Action == path)).Select(p => p.F_MenuId).FirstOrDefault();
73 73
                                 if (mid > 0)
74 74
                                 {
75 75
                                     //判断此角色是否有权限