You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HealthMonitorControllerBase.cs 7.3KB

1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.EntityFrameworkCore;
  3. using Microsoft.Extensions.Logging;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel.DataAnnotations;
  8. using System.Linq;
  9. using System.Linq.Expressions;
  10. using System.Threading.Tasks;
  11. using HealthMonitor.Core.Aop;
  12. using HealthMonitor.Core.Cache;
  13. using HealthMonitor.Core.Dal;
  14. using HealthMonitor.Core.Dal.Factory;
  15. using HealthMonitor.Core.Operator;
  16. using HealthMonitor.Core.Query;
  17. using HealthMonitor.Core.Query.Extensions;
  18. using HealthMonitor.Util.Common;
  19. using HealthMonitor.Util.Common.Operator;
  20. using HealthMonitor.Util.Entities.Base;
  21. using HealthMonitor.Util.Models;
  22. using HealthMonitor.Util.QueryObjects;
  23. using HealthMonitor.WebApi.Filters;
  24. namespace HealthMonitor.WebApi.Controllers.Base
  25. {
  26. [Produces("application/json")]
  27. [Route("api/HealthMonitor/[controller]/[action]")]
  28. [ApiController]
  29. public abstract class HealthMonitorControllerBase<T> : DefaultControllerBase<T> where T : HealthMonitorEntityBase
  30. {
  31. //protected readonly GpsCardAccessorFactory _accessorFactory;
  32. //protected readonly IGpsCardDataAccessor _dataAccessor;
  33. //protected readonly IGpsCardOperatorManager _operatorGpsCardManager;
  34. protected readonly IHealthMonitorDataAccessor _dataAccessor;
  35. protected readonly IHealthMonitorOperatorManager _operatorManager;
  36. protected readonly IDurableEntityManager _durableManager;
  37. protected readonly ILogger _logger;
  38. protected HealthMonitorControllerBase(
  39. IHealthMonitorDataAccessor dataAccessor,
  40. //IGpsCardDataAccessor gpsCardDataAccessor,
  41. IHealthMonitorOperatorManager operatorGpsCardManager,
  42. IDurableEntityManager durableManager,
  43. ILogger logger)
  44. {
  45. _dataAccessor = dataAccessor;
  46. _operatorManager = operatorGpsCardManager;
  47. _durableManager = durableManager;
  48. _logger = logger;
  49. }
  50. /// <summary>
  51. /// 通过主键值,获取实体
  52. /// </summary>
  53. /// <param name="id"></param>
  54. /// <param name="requestId">关联服务标识</param>
  55. /// <returns></returns>
  56. [HttpGet]
  57. //[QueryCacheInterceptor]
  58. public async virtual Task<T> GetById([Required] string id, [FromHeader] string requestId)
  59. {
  60. AssertModelStateIsValid();
  61. //var key = EntityUtils.GetWrapKeyValue<T>(id);
  62. return await _dataAccessor.GetByIDAsync<T>(id);
  63. }
  64. /// <summary>
  65. /// 通过查询条件,获取符合条件的第一个实体
  66. /// </summary>
  67. /// <param name="conditions"></param>
  68. /// <param name="requestId">关联服务标识</param>
  69. /// <returns></returns>
  70. [HttpPost]
  71. //[QueryCacheInterceptor]
  72. public async virtual Task<T?> GetFirst([FromBody] GeneralParam conditions, [FromHeader] string requestId)
  73. {
  74. AssertModelStateIsValid();
  75. var parser = new QueryExpressionParser<T>();
  76. var expression = parser.ParserConditions(conditions.Filters!);
  77. var list = await _dataAccessor.GetMany(expression).OrderConditions(conditions.OrderBys!).Take(1).ToListAsync();
  78. return list.Count > 0 ? list[0] : null;
  79. }
  80. /// <summary>
  81. /// 获取实体列表(只提取符合条件的前100条记录)
  82. /// </summary>
  83. /// <param name="conditions"></param>
  84. /// <param name="requestId">关联服务标识</param>
  85. /// <returns></returns>
  86. [HttpPost]
  87. //[QueryCacheInterceptor]
  88. public async virtual Task<PageData<T>> GetPageList([FromBody] Paging page, [FromHeader] string requestId)
  89. {
  90. AssertModelStateIsValid();
  91. var all = _dataAccessor.GetAll<T>();
  92. var list = await all.Skip((page.CurPage - 1) * page.PageSize).Take(page.PageSize).ToListAsync();
  93. int totals = await all.CountAsync();
  94. return new PageData<T> { Totals= totals, Rows =list};
  95. }
  96. [HttpPost]
  97. public async virtual Task<IEnumerable<T>> GetList([FromBody] GeneralParam conditions, [FromHeader] string requestId)
  98. {
  99. AssertModelStateIsValid();
  100. var parser = new QueryExpressionParser<T>();
  101. var expression = parser.ParserConditions(conditions.Filters!);
  102. var list = await _dataAccessor.GetMany(expression).OrderConditions(conditions.OrderBys!).Take(100).ToListAsync();
  103. return list;
  104. }
  105. [HttpPost]
  106. public async virtual Task<IEnumerable<T>> GetPages([FromBody] GeneralParam conditions, [FromHeader] string requestId)
  107. {
  108. AssertModelStateIsValid();
  109. //var parser = new QueryExpressionParser<T>();
  110. //var expression = parser.ParserConditions(conditions.Filters!);
  111. // var aa = await _dataAccessor.GetPageListAsync(1,1, expression, conditions.OrderBys!);
  112. //var parser = new QueryExpressionParser<T>();
  113. //var expression = parser.ParserConditions(conditions.Filters!);
  114. var pageSize = 1;
  115. var pageIdx = 1;
  116. //var bb = await _dataAccessor.OrderBy(conditions.OrderBys!).Skip(pageSize * (pageIdx - 1)).Take(pageSize);
  117. var list = await _dataAccessor.GetAll<T>().OrderConditions(conditions.OrderBys!).Skip(pageSize * (pageIdx - 1)).Take(pageSize).ToListAsync();
  118. return list;
  119. }
  120. /// <summary>
  121. /// Insert实体
  122. /// </summary>
  123. /// <param name="model"></param>
  124. /// <param name="requestId">关联服务标识</param>
  125. [HttpPost]
  126. //[InsertCacheInterceptor]
  127. public virtual void Add([FromBody] T model, [FromHeader] string requestId)
  128. {
  129. AssertModelStateIsValid(model);
  130. _dataAccessor.Add(model);
  131. _dataAccessor.Save();
  132. }
  133. /// <summary>
  134. /// Update实体
  135. /// </summary>
  136. /// <param name="model"></param>
  137. /// <param name="requestId">关联服务标识</param>
  138. [HttpPut]
  139. //[UpdateCacheInterceptor]
  140. public virtual void Update([FromBody] T model, [FromHeader] string requestId)
  141. {
  142. AssertModelStateIsValid(model);
  143. //_dataAccessor.GetByIDAsync<T>(model.GetPrimaryKey());
  144. _dataAccessor.Update(model);
  145. _dataAccessor.Save();
  146. //var item = new OperateCacheItem
  147. //{
  148. // Type = DelayOperateTypeEnum.Update,
  149. // CreateTime = DateTime.Now,
  150. // EntityType = typeof(T),
  151. // JsonData = JsonConvert.SerializeObject(model)
  152. //};
  153. //_operatorManager.Append(model.GetRequiredUniqueKey(), item);
  154. }
  155. /// <summary>
  156. /// Delete实体
  157. /// </summary>
  158. /// <param name="model"></param>
  159. /// <param name="requestId">关联服务标识</param>
  160. [HttpDelete]
  161. //[DeleteCacheInterceptor]
  162. public virtual void Delete([FromBody] T model, [FromHeader] string requestId)
  163. {
  164. //删除操作不需要验证模型的有效性
  165. //AssertModelStateIsValid();
  166. _dataAccessor.Delete(model);
  167. _dataAccessor.Save();
  168. //var item = new OperateCacheItem
  169. //{
  170. // Type = DelayOperateTypeEnum.Delete,
  171. // CreateTime = DateTime.Now,
  172. // EntityType = typeof(T),
  173. // JsonData = JsonConvert.SerializeObject(model),
  174. //};
  175. //_operatorManager.Append(model.GetRequiredUniqueKey(), item);
  176. }
  177. }
  178. }