|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using HealthMonitor.Core.Aop;
- using HealthMonitor.Core.Cache;
- using HealthMonitor.Core.Dal;
- using HealthMonitor.Core.Operator;
- using HealthMonitor.Core.Query;
- using HealthMonitor.Util.Entities.Base;
- using HealthMonitor.Util.Models;
- using HealthMonitor.WebApi.Controllers.Base;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Logging;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Threading.Tasks;
- using HealthMonitor.Core.Aop;
- using HealthMonitor.Core.Cache;
- using HealthMonitor.Core.Dal;
- using HealthMonitor.Core.Dal.Factory;
- using HealthMonitor.Core.Operator;
- using HealthMonitor.Core.Query;
- using HealthMonitor.Core.Query.Extensions;
- using HealthMonitor.Util.Common;
- using HealthMonitor.Util.Common.Operator;
- using HealthMonitor.Util.Entities.Base;
- using HealthMonitor.Util.Models;
-
- namespace HealthMonitor.WebApi.Controllers.Base
- {
- [Produces("application/json")]
- [Route("api/GpsCard/[controller]/[action]")]
- [ApiController]
- public abstract class GpsCardControllerBase<T> : DefaultControllerBase<T> where T : GpsCardEntityBase
- {
- //protected readonly GpsCardAccessorFactory _accessorFactory;
- protected readonly IGpsCardDataAccessor _dataAccessor;
- protected readonly IGpsCardOperatorManager _operatorManager;
- protected readonly IDurableEntityManager _durableManager;
- protected readonly ILogger _logger;
-
- protected GpsCardControllerBase(
- IGpsCardDataAccessor dataAccessor,
- IGpsCardOperatorManager operatorManager,
- IDurableEntityManager durableManager,
- ILogger logger)
- {
- _dataAccessor=dataAccessor;
- _operatorManager = operatorManager;
- _durableManager = durableManager;
- _logger = logger;
- }
-
- /// <summary>
- /// 通过主键值,获取实体
- /// </summary>
- /// <param name="id"></param>
- /// <param name="requestId">关联服务标识</param>
- /// <returns></returns>
- [HttpGet]
- [QueryCacheInterceptor]
- public async virtual Task<T> GetById([Required] string id, [FromHeader] string requestId)
- {
- AssertModelStateIsValid();
-
- //var key = EntityUtils.GetWrapKeyValue<T>(id);
- return await _dataAccessor.GetByIDAsync<T>(id);
- }
-
- /// <summary>
- /// 通过查询条件,获取符合条件的第一个实体
- /// </summary>
- /// <param name="conditions"></param>
- /// <param name="requestId">关联服务标识</param>
- /// <returns></returns>
- [HttpPost]
- [QueryCacheInterceptor]
- public async virtual Task<T> GetFirst([FromBody] GeneralParam conditions, [FromHeader] string requestId)
- {
- AssertModelStateIsValid();
-
- var parser = new QueryExpressionParser<T>();
- var expression = parser.ParserConditions(conditions.Filters);
- var list = await _dataAccessor.GetMany(expression).OrderConditions(conditions.OrderBys).Take(1).ToListAsync();
-
- return list.Count > 0 ? list[0] : null;
- }
-
- /// <summary>
- /// 获取实体列表(只提取符合条件的前100条记录)
- /// </summary>
- /// <param name="conditions"></param>
- /// <param name="requestId">关联服务标识</param>
- /// <returns></returns>
- [HttpPost]
- [QueryCacheInterceptor]
- public async virtual Task<IEnumerable<T>> GetList([FromBody] GeneralParam conditions, [FromHeader] string requestId)
- {
- AssertModelStateIsValid();
-
- var parser = new QueryExpressionParser<T>();
- var expression = parser.ParserConditions(conditions.Filters);
- var list = await _dataAccessor.GetMany(expression).OrderConditions(conditions.OrderBys).Take(100).ToListAsync();
- return list;
- }
-
- /// <summary>
- /// Insert实体
- /// </summary>
- /// <param name="model"></param>
- /// <param name="requestId">关联服务标识</param>
- [HttpPost]
- [InsertCacheInterceptor]
- public virtual void Add([FromBody] T model, [FromHeader] string requestId)
- {
- AssertModelStateIsValid(model);
-
- var item = new OperateCacheItem
- {
- Type = DelayOperateTypeEnum.Insert,
- CreateTime = DateTime.Now,
- EntityType = typeof(T),
- JsonData = JsonConvert.SerializeObject(model)
- };
- _operatorManager.Append(model.GetUniqueKey(), item);
- }
-
- /// <summary>
- /// Update实体
- /// </summary>
- /// <param name="model"></param>
- /// <param name="requestId">关联服务标识</param>
- [HttpPut]
- [UpdateCacheInterceptor]
- public virtual void Update([FromBody] T model, [FromHeader] string requestId)
- {
- AssertModelStateIsValid(model);
-
- var item = new OperateCacheItem
- {
- Type = DelayOperateTypeEnum.Update,
- CreateTime = DateTime.Now,
- EntityType = typeof(T),
- JsonData = JsonConvert.SerializeObject(model)
- };
- _operatorManager.Append(model.GetRequiredUniqueKey(), item);
- }
-
- /// <summary>
- /// Delete实体
- /// </summary>
- /// <param name="model"></param>
- /// <param name="requestId">关联服务标识</param>
- [HttpDelete]
- [DeleteCacheInterceptor]
- public virtual void Delete([FromBody] T model, [FromHeader] string requestId)
- {
- //删除操作不需要验证模型的有效性
- //AssertModelStateIsValid();
-
- var item = new OperateCacheItem
- {
- Type = DelayOperateTypeEnum.Delete,
- CreateTime = DateTime.Now,
- EntityType = typeof(T),
- JsonData = JsonConvert.SerializeObject(model),
- };
- _operatorManager.Append(model.GetRequiredUniqueKey(), item);
- }
- }
- }
|