.net6.0 webapi demo

UserAccountBusiness.cs 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using EFCore.Sharding;
  2. using Microsoft.EntityFrameworkCore;
  3. using Microsoft.Extensions.Caching.Distributed;
  4. using Net6Demo_Api.Entity;
  5. using Net6Demo_Api.IBusiness;
  6. using Net6Demo_Api.Util;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Linq.Expressions;
  11. using System.Linq.Dynamic.Core;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using AutoMapper;
  15. namespace Net6Demo_Api.Business
  16. {
  17. public class UserAccountBusiness : BaseBusiness<T_Sys_UserAccount>, IUserAccountBusiness, ITransientDependency
  18. {
  19. private readonly IMapper _mapper;
  20. public UserAccountBusiness(IDbAccessor db, IDistributedCache cache, IMapper mapper) : base(db, cache, true)
  21. {
  22. _mapper = mapper;
  23. }
  24. /// <summary>
  25. /// 多表关联查询分页
  26. /// </summary>
  27. /// <param name="page">分页信息</param>
  28. /// <param name="condition">条件</param>
  29. /// <param name="orderBy">排序</param>
  30. /// <returns></returns>
  31. public async Task<ValueTuple<List<UserAccountView>, int>> GetMultipleListPageAsync(PageInput page, Expression<Func<T_Sys_UserAccount, bool>> condition, Func<IQueryable<T_Sys_UserAccount>, IOrderedQueryable<T_Sys_UserAccount>> orderBy)
  32. {
  33. var iq = _db.GetIQueryable<T_Sys_UserAccount>().AsNoTracking().Where(condition);
  34. var miq = from a in orderBy(iq)
  35. join b in _db.GetIQueryable<T_Sys_Role>().AsNoTracking() on a.F_RoleId equals b.F_RoleId into ab
  36. from b in ab.DefaultIfEmpty()
  37. join c in _db.GetIQueryable<T_Sys_Department>().AsNoTracking() on a.F_DeptId equals c.F_DeptId into ac
  38. from c in ac.DefaultIfEmpty()
  39. select new { a, b, c };
  40. var oldlist = await miq.Skip((page.PageIndex - 1) * page.PageRows).Take(page.PageRows).ToListAsync();
  41. var list = oldlist.Select(p =>
  42. {
  43. var a = _mapper.Map<UserAccountView>(p.a);
  44. a.RoleCode = p.b?.F_RoleCode;
  45. a.RoleName = p.b?.F_RoleName;
  46. a.DeptCode = p.c?.F_DeptCode;
  47. a.DeptName = p.c?.F_DeptName;
  48. return a;
  49. }).ToList();
  50. int total = await iq.CountAsync();
  51. return (list, total);
  52. }
  53. }
  54. }