AcQueryManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Abp.Application.Services.Dto;
  7. using Abp.Dependency;
  8. using Abp.Domain.Repositories;
  9. using ContractService.BaseInfo;
  10. namespace ContractService.CommonManager.Query
  11. {
  12. public class AcQueryManager : IAcQueryManager, ISingletonDependency
  13. {
  14. public AcQueryManager(IRepository<SysQuery> repository)
  15. {
  16. Repository = repository;
  17. }
  18. private IRepository<SysQuery> Repository { get; }
  19. /// <summary>
  20. /// 添加/修改新的查询
  21. /// </summary>
  22. /// <param name="tKey"></param>
  23. /// <param name="cKey"></param>
  24. /// <param name="vKey"></param>
  25. /// <param name="nKey"></param>
  26. /// <returns></returns>
  27. public async Task InsertOrUpdate(string tKey, string cKey, string vKey, string nKey)
  28. {
  29. await InsertOrUpdate(tKey, cKey, vKey, nKey, null);
  30. }
  31. /// <summary>
  32. /// 添加/修改新的查询
  33. /// </summary>
  34. /// <param name="tKey"></param>
  35. /// <param name="cKey"></param>
  36. /// <param name="vKey"></param>
  37. /// <param name="nKey"></param>
  38. /// <param name="fun">给记录添加扩展值</param>
  39. /// <returns></returns>
  40. public async Task InsertOrUpdate(string tKey, string cKey, string vKey, string nKey,
  41. Func<SysQuery, SysQuery> fun)
  42. {
  43. var entity =
  44. await Repository.FirstOrDefaultAsync(a =>
  45. a.TableKey == tKey && a.ColumnKey == cKey && a.ValueKey == vKey);
  46. var date = DateTime.Now;
  47. if (entity == null)
  48. {
  49. entity = new SysQuery()
  50. {
  51. TableKey = tKey,
  52. ColumnKey = cKey,
  53. ValueKey = vKey,
  54. NameKey = nKey,
  55. UseCount = 1,
  56. LastUseDate = date
  57. };
  58. fun?.Invoke(entity);
  59. await Repository.InsertAsync(entity);
  60. }
  61. else
  62. {
  63. entity.UseCount++;
  64. entity.LastUseDate = date;
  65. fun?.Invoke(entity);
  66. await Repository.UpdateAsync(entity);
  67. }
  68. }
  69. /// <summary>
  70. /// 查询指定记录
  71. /// </summary>
  72. /// <param name="tKey"></param>
  73. /// <param name="cKey"></param>
  74. /// <param name="vKey"></param>
  75. /// <returns></returns>
  76. public async Task<SysQuery> QueryItem(string tKey, string cKey, string vKey)
  77. {
  78. var item = await Repository.FirstOrDefaultAsync(a =>
  79. a.TableKey == tKey && a.ColumnKey == cKey && a.ValueKey == vKey);
  80. return item;
  81. }
  82. /// <summary>
  83. /// 查询记录列表
  84. /// </summary>
  85. /// <param name="tKey"></param>
  86. /// <param name="cKey"></param>
  87. /// <param name="nKey"></param>
  88. /// <returns></returns>
  89. public async Task<List<SysQuery>> QueryItems(string tKey, string cKey, string nKey)
  90. {
  91. var list = await Repository.GetAll().Where(a => a.TableKey == tKey && a.ColumnKey == cKey && (string.IsNullOrEmpty(nKey) || a.NameKey.Contains(nKey))).OrderByDescending(a => a.UseCount).ThenByDescending(a => a.LastUseDate).ToListAsync();
  92. return list;
  93. }
  94. /// <summary>
  95. /// 查询记录列表分页
  96. /// </summary>
  97. /// <param name="tKey"></param>
  98. /// <param name="cKey"></param>
  99. /// <param name="nKey"></param>
  100. /// <param name="skip"></param>
  101. /// <param name="take"></param>
  102. /// <returns></returns>
  103. public async Task<PagedResultDto<SysQuery>> PageItems(string tKey, string cKey, string nKey, int skip, int take)
  104. {
  105. take = take == 0 ? 20 : take;
  106. var query = Repository.GetAll().Where(a => a.TableKey == tKey && a.ColumnKey == cKey && (string.IsNullOrEmpty(nKey) || a.NameKey.Contains(nKey)));
  107. var total = await query.CountAsync();
  108. var list = await query.OrderByDescending(a => a.UseCount).ThenByDescending(a => a.LastUseDate).Skip(skip).Take(take).ToListAsync();
  109. return new PagedResultDto<SysQuery>(total, list);
  110. }
  111. }
  112. }