您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

GpsCardControllerBase.cs 5.3KB

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