LogApiService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. using YiSha.Util;
  7. using YiSha.Util.Extension;
  8. using YiSha.Util.Model;
  9. using YiSha.Data.Repository;
  10. using YiSha.Entity.SystemManage;
  11. using YiSha.Model.Param.SystemManage;
  12. using System.Text;
  13. using System.Data.Common;
  14. using YiSha.Data;
  15. using YiSha.Enum;
  16. namespace YiSha.Service.SystemManage
  17. {
  18. public partial class LogApiService
  19. {
  20. private IRepositoryFactory _baseRepository;
  21. public LogApiService(IRepositoryFactory baseRepository)
  22. {
  23. _baseRepository = baseRepository;
  24. }
  25. #region 获取数据
  26. public async Task<List<LogApiEntity>> GetListPartial(LogApiListParam param)
  27. {
  28. var strSql = new StringBuilder();
  29. List<DbParameter> filter = ListFilterPartial(param, strSql);
  30. var list = await _baseRepository.BaseRepository(dbConnectType).FindList<LogApiEntity>(strSql.ToString(), filter.ToArray());
  31. return list.ToList();
  32. }
  33. public async Task<List<LogApiEntity>> GetPageListPartial(LogApiListParam param, Pagination pagination)
  34. {
  35. var strSql = new StringBuilder();
  36. List<DbParameter> filter = ListFilterPartial(param, strSql);
  37. var list = await _baseRepository.BaseRepository(dbConnectType).FindList<LogApiEntity>(strSql.ToString(), filter.ToArray(), pagination);
  38. return list.ToList();
  39. }
  40. public async Task<LogApiEntity> GetEntityPartial(int id)
  41. {
  42. return await _baseRepository.BaseRepository(dbConnectType).FindEntity<LogApiEntity>(id);
  43. }
  44. #endregion
  45. #region 提交数据
  46. public async Task SaveFormPartial(LogApiEntity entity)
  47. {
  48. if (entity.Id.IsNullOrZero())
  49. {
  50. await entity.Create();
  51. await _baseRepository.BaseRepository(dbConnectType).Insert<LogApiEntity>(entity);
  52. }
  53. else
  54. {
  55. await _baseRepository.BaseRepository(dbConnectType).Update<LogApiEntity>(entity);
  56. }
  57. }
  58. public async Task RemoveAllFormPartial()
  59. {
  60. await _baseRepository.BaseRepository(dbConnectType).ExecuteBySql("truncate table SysLogApi");
  61. }
  62. #endregion
  63. #region 私有方法
  64. private List<DbParameter> ListFilterPartial(LogApiListParam param, StringBuilder strSql)
  65. {
  66. strSql.Append(@"SELECT a.Id,
  67. a.BaseCreateTime,
  68. a.BaseCreatorId,
  69. a.LogStatus,
  70. a.Remark,
  71. a.ExecuteUrl,
  72. a.ExecuteParam,
  73. a.ExecuteResult,
  74. a.ExecuteTime,
  75. b.UserName,
  76. c.DepartmentName
  77. FROM SysLogApi a
  78. LEFT JOIN SysUser b ON a.BaseCreatorId = b.Id
  79. LEFT JOIN SysDepartment c ON b.DepartmentId = c.Id
  80. WHERE 1 = 1 and a.BaseIsDelete = @BaseIsDelete ");
  81. var parameter = new List<DbParameter>();
  82. parameter.Add(DbParameterExtension.CreateDbParameter("@BaseIsDelete", (int)IsDeleteEnum.No, dbConnectType));
  83. if (param != null)
  84. {
  85. if (!string.IsNullOrEmpty(param.UserName))
  86. {
  87. strSql.Append(" AND b.UserName like @UserName");
  88. parameter.Add(DbParameterExtension.CreateDbParameter("@UserName", '%' + param.UserName + '%', dbConnectType));
  89. }
  90. if (param.LogStatus > -1)
  91. {
  92. strSql.Append(" AND a.LogStatus = @LogStatus");
  93. parameter.Add(DbParameterExtension.CreateDbParameter("@LogStatus", param.LogStatus, dbConnectType));
  94. }
  95. if (!string.IsNullOrEmpty(param.ExecuteUrl))
  96. {
  97. strSql.Append(" AND a.ExecuteUrl like @ExecuteUrl");
  98. parameter.Add(DbParameterExtension.CreateDbParameter("@ExecuteUrl", '%' + param.ExecuteUrl + '%', dbConnectType));
  99. }
  100. if (!string.IsNullOrEmpty(param.StartTime.ParseToString()))
  101. {
  102. strSql.Append(" AND a.BaseCreateTime >= @StartTime");
  103. parameter.Add(DbParameterExtension.CreateDbParameter("@StartTime", param.StartTime, dbConnectType));
  104. }
  105. if (!string.IsNullOrEmpty(param.EndTime.ParseToString()))
  106. {
  107. param.EndTime = param.EndTime.Value.Date.Add(new TimeSpan(23, 59, 59));
  108. strSql.Append(" AND a.BaseCreateTime <= @EndTime");
  109. parameter.Add(DbParameterExtension.CreateDbParameter("@EndTime", param.EndTime, dbConnectType));
  110. }
  111. }
  112. return parameter;
  113. }
  114. /// <summary>
  115. /// 列表条件过滤
  116. /// 建议该方法放在Partial部分类中,因为代码生成时当前类会被覆盖(该方法在生成时将会被注释)
  117. /// </summary>
  118. /// <param name="param"></param>
  119. /// <returns></returns>
  120. private Expression<Func<LogApiEntity, bool>> ListFilterPartial(LogApiListParam param)
  121. {
  122. var expression = LinqExtensions.True<LogApiEntity>();
  123. expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);
  124. if (param != null)
  125. {
  126. }
  127. return expression;
  128. }
  129. #endregion
  130. }
  131. }