IDingTalk_ReimbursementRepository.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using XYY.Core.Standard.Data.Infrastructure;
  7. using XYY.Model.Standard.DingTalk;
  8. using Dapper;
  9. using XYY.Common.Standard;
  10. namespace XYY.Data.Standard.DingTalk
  11. {
  12. public interface IDingTalk_ReimbursementRepository : IBaseRepository<DingTalk_Reimbursement>
  13. {
  14. Task insertDetail(int ReimbursementId, List<DingTalk_ReimbursementDetail> details);
  15. Task<List<DingTalk_ReimbursementOptions>> GetAllReimbursementOptions();
  16. Task<bool> BatchUpdateOptionsEntiy(List<DingTalk_ReimbursementOptions> models);
  17. Task insertOptions(List<DingTalk_ReimbursementOptions> models);
  18. Task<PageResult<DingTalk_Reimbursement>> QueryPage(QueryModel qm);
  19. Task<List<DingTalk_ReimbursementOptions>> GetOptionsByType(int OptionType);
  20. Task<List<DingTalk_ReimbursementDetail>> GetReimbursementDetails(int ReimbursementId);
  21. }
  22. public class DingTalk_ReimbursementRepository : BaseRepository<DingTalk_Reimbursement>, IDingTalk_ReimbursementRepository
  23. {
  24. public DingTalk_ReimbursementRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
  25. {
  26. }
  27. public async Task insertDetail(int ReimbursementId, List<DingTalk_ReimbursementDetail> details)
  28. {
  29. if (details.Count != 0)
  30. {
  31. string deleteSql = $" delete DingTalk_ReimbursementDetail where ReimbursementId = {ReimbursementId} ";
  32. await _unitOfWork.ExecuteAsync(deleteSql);
  33. details.ForEach(x => { x.ReimbursementId = ReimbursementId; });
  34. await _unitOfWork.BulkToDBAsync(details);
  35. }
  36. }
  37. public async Task<List<DingTalk_ReimbursementOptions>> GetAllReimbursementOptions()
  38. {
  39. var result = await _unitOfWork.QueryAsync<DingTalk_ReimbursementOptions>();
  40. return result.ToList();
  41. }
  42. public async Task<bool> BatchUpdateOptionsEntiy(List<DingTalk_ReimbursementOptions> models)
  43. {
  44. bool result = true;
  45. try
  46. {
  47. //比逐条更新稍微快点
  48. string sql = " update DingTalk_ReimbursementOptions set DName = @DName,LevelNum=@LevelNum,DParentId=@DParentId where DId = @DId ";
  49. await _unitOfWork.ExecuteAsync(sql, models);
  50. }
  51. catch (Exception ex)
  52. {
  53. result = false;
  54. }
  55. return result;
  56. }
  57. public async Task insertOptions(List<DingTalk_ReimbursementOptions> models)
  58. {
  59. await _unitOfWork.BulkToDBAsync(models);
  60. }
  61. public async Task<PageResult<DingTalk_Reimbursement>> QueryPage(QueryModel qm)
  62. {
  63. #region 基础sql
  64. int Option = 0;
  65. var qmCategories= qm.QueryParamer.Where(x => x.Filed == "Option").FirstOrDefault();
  66. if (qmCategories != null && !string.IsNullOrEmpty(qmCategories.Value)) { Option = Convert.ToInt32(qmCategories.Value.Split(',')[1]); qm.QueryParamer.Remove(qmCategories); }
  67. StringBuilder sb = new StringBuilder();
  68. sb.Append(" select * from DingTalk_Reimbursement(nolock) where 1=1 ");
  69. string where = _unitOfWork.GetWhere(qm.QueryParamer);
  70. if (Option > 0 )
  71. {
  72. where += $" and Id in (select distinct ReimbursementId from DingTalk_ReimbursementDetail where Categories2_Id={Option}) ";
  73. }
  74. sb.Append(where);
  75. #endregion
  76. #region 分页
  77. StringBuilder pager = new StringBuilder();
  78. string sortString = " Id desc";
  79. pager.Append($@"select * from (select Row_Number()over(order by {sortString})rowId,*
  80. from ({sb.ToString()})a where 1=1 ) T where rowId between {(qm.PageIndex - 1) * qm.PageSize + 1} and {qm.PageSize * qm.PageIndex};");
  81. pager.Append(@$"select count(0)TotalCount from (select Row_Number()over(order by {sortString})rowId,*
  82. from({sb.ToString()})a where 1 = 1 ) T where 1=1");
  83. #endregion
  84. #region 取值
  85. string sql = pager.ToString();
  86. var dr = await _unitOfWork.Connection.QueryMultipleAsync(sql, null, _unitOfWork.Transaction);
  87. //需要注意读取顺序,依据语句执行顺序进行读取
  88. var pageResult = new PageResult<DingTalk_Reimbursement>()
  89. {
  90. Result = dr.Read<DingTalk_Reimbursement>(),
  91. PageIndex = qm.PageIndex,
  92. PageSize = qm.PageIndex,
  93. TotalCount = dr.Read<int>().FirstOrDefault()
  94. };
  95. #endregion
  96. return pageResult;
  97. }
  98. public async Task<List<DingTalk_ReimbursementOptions>> GetOptionsByType(int OptionType)
  99. {
  100. var result = await _unitOfWork.QueryAsync<DingTalk_ReimbursementOptions>(x => x.OptionType == (OptionTypeEnum)OptionType);
  101. return result.ToList();
  102. }
  103. public async Task<List<DingTalk_ReimbursementDetail>> GetReimbursementDetails(int ReimbursementId)
  104. {
  105. var result = await _unitOfWork.QueryAsync<DingTalk_ReimbursementDetail>(x => x.ReimbursementId == ReimbursementId);
  106. return result.ToList();
  107. }
  108. }
  109. }