Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

GpsCardControllerBase.cs 4.8KB

před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
před 1 rokem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using HealthMonitor.Core.Aop;
  2. using HealthMonitor.Core.Cache;
  3. using HealthMonitor.Core.Dal;
  4. using HealthMonitor.Core.Operator;
  5. using HealthMonitor.Core.Query;
  6. using HealthMonitor.Core.Query.Extensions;
  7. using HealthMonitor.Util.Common.Operator;
  8. using HealthMonitor.Util.Entities.Base;
  9. using HealthMonitor.Util.Models;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.EntityFrameworkCore;
  12. using Newtonsoft.Json;
  13. using System.ComponentModel.DataAnnotations;
  14. namespace HealthMonitor.WebApi.Controllers.Base
  15. {
  16. [Produces("application/json")]
  17. [Route("api/GpsCard/[controller]/[action]")]
  18. [ApiController]
  19. public abstract class GpsCardControllerBase<T> : DefaultControllerBase<T> where T : GpsCardEntityBase
  20. {
  21. //protected readonly GpsCardAccessorFactory _accessorFactory;
  22. protected readonly IGpsCardDataAccessor _dataAccessor;
  23. protected readonly IGpsCardOperatorManager _operatorManager;
  24. protected readonly IDurableEntityManager _durableManager;
  25. protected readonly ILogger _logger;
  26. protected GpsCardControllerBase(
  27. IGpsCardDataAccessor dataAccessor,
  28. IGpsCardOperatorManager operatorManager,
  29. IDurableEntityManager durableManager,
  30. ILogger logger)
  31. {
  32. _dataAccessor=dataAccessor;
  33. _operatorManager = operatorManager;
  34. _durableManager = durableManager;
  35. _logger = logger;
  36. }
  37. /// <summary>
  38. /// 通过主键值,获取实体
  39. /// </summary>
  40. /// <param name="id"></param>
  41. /// <param name="requestId">关联服务标识</param>
  42. /// <returns></returns>
  43. [HttpGet]
  44. [QueryCacheInterceptor]
  45. public async virtual Task<T> GetById([Required] string id, [FromHeader] string requestId)
  46. {
  47. AssertModelStateIsValid();
  48. //var key = EntityUtils.GetWrapKeyValue<T>(id);
  49. return await _dataAccessor.GetByIDAsync<T>(id);
  50. }
  51. /// <summary>
  52. /// 通过查询条件,获取符合条件的第一个实体
  53. /// </summary>
  54. /// <param name="conditions"></param>
  55. /// <param name="requestId">关联服务标识</param>
  56. /// <returns></returns>
  57. [HttpPost]
  58. [QueryCacheInterceptor]
  59. public async virtual Task<T?> GetFirst([FromBody] GeneralParam conditions, [FromHeader] string requestId)
  60. {
  61. AssertModelStateIsValid();
  62. var parser = new QueryExpressionParser<T>();
  63. var expression = parser.ParserConditions(conditions.Filters!);
  64. var list = await _dataAccessor.GetMany(expression).OrderConditions(conditions.OrderBys!).Take(1).ToListAsync();
  65. return list.Count > 0 ? list[0] : null;
  66. }
  67. /// <summary>
  68. /// 获取实体列表(只提取符合条件的前100条记录)
  69. /// </summary>
  70. /// <param name="conditions"></param>
  71. /// <param name="requestId">关联服务标识</param>
  72. /// <returns></returns>
  73. [HttpPost]
  74. [QueryCacheInterceptor]
  75. public async virtual Task<IEnumerable<T>> GetList([FromBody] GeneralParam conditions, [FromHeader] string requestId)
  76. {
  77. AssertModelStateIsValid();
  78. var parser = new QueryExpressionParser<T>();
  79. var expression = parser.ParserConditions(conditions.Filters!);
  80. var list = await _dataAccessor.GetMany(expression).OrderConditions(conditions.OrderBys!).Take(100).ToListAsync();
  81. return list;
  82. }
  83. /// <summary>
  84. /// Insert实体
  85. /// </summary>
  86. /// <param name="model"></param>
  87. /// <param name="requestId">关联服务标识</param>
  88. [HttpPost]
  89. [InsertCacheInterceptor]
  90. public virtual void Add([FromBody] T model, [FromHeader] string requestId)
  91. {
  92. AssertModelStateIsValid(model);
  93. var item = new OperateCacheItem
  94. {
  95. Type = DelayOperateTypeEnum.Insert,
  96. CreateTime = DateTime.Now,
  97. EntityType = typeof(T),
  98. JsonData = JsonConvert.SerializeObject(model)
  99. };
  100. _operatorManager.Append(model.GetUniqueKey(), item);
  101. }
  102. /// <summary>
  103. /// Update实体
  104. /// </summary>
  105. /// <param name="model"></param>
  106. /// <param name="requestId">关联服务标识</param>
  107. [HttpPut]
  108. [UpdateCacheInterceptor]
  109. public virtual void Update([FromBody] T model, [FromHeader] string requestId)
  110. {
  111. AssertModelStateIsValid(model);
  112. var item = new OperateCacheItem
  113. {
  114. Type = DelayOperateTypeEnum.Update,
  115. CreateTime = DateTime.Now,
  116. EntityType = typeof(T),
  117. JsonData = JsonConvert.SerializeObject(model)
  118. };
  119. _operatorManager.Append(model.GetRequiredUniqueKey(), item);
  120. }
  121. /// <summary>
  122. /// Delete实体
  123. /// </summary>
  124. /// <param name="model"></param>
  125. /// <param name="requestId">关联服务标识</param>
  126. [HttpDelete]
  127. [DeleteCacheInterceptor]
  128. public virtual void Delete([FromBody] T model, [FromHeader] string requestId)
  129. {
  130. //删除操作不需要验证模型的有效性
  131. //AssertModelStateIsValid();
  132. var item = new OperateCacheItem
  133. {
  134. Type = DelayOperateTypeEnum.Delete,
  135. CreateTime = DateTime.Now,
  136. EntityType = typeof(T),
  137. JsonData = JsonConvert.SerializeObject(model),
  138. };
  139. _operatorManager.Append(model.GetRequiredUniqueKey(), item);
  140. }
  141. }
  142. }