using System.Reflection; using System.Security.Cryptography; using Aop.Api.Domain; using JNPF; using JNPF.Common.Contracts; using JNPF.Common.Extension; using Mapster; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Controllers; using SqlSugar; using Tnb.Core; using Tnb.Vengine.DataAccess; using Tnb.Vengine.Domain; namespace Tnb.Vengine.AppService; /// /// 增删改查基类 /// [Authorize] //[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 1102)] [Route("api/[area]/[controller]/[action]")] public class VengineAppService : BaseAppService where TEntity : Entity where TGetInput : VmGetInput where TQueryInput : VmQueryInput where TCreateInput : VmEditInput where TUpdateInput : VmEditInput { protected readonly ISqlSugarClient _db; /// /// 构造函数 /// public VengineAppService(IDataAccess dataAccess) : base(dataAccess) { _db = dataAccess.GetSqlSugar(); } protected virtual async Task GetVmodelAsync() { string? areaCode = null, vmCode = null; // 1.优先从路由中获取模型code IHttpContextAccessor httpContextAccessor = App.GetService(); var routes = httpContextAccessor.HttpContext?.Request.RouteValues; //HttpContext.GetEndpoint()?.Metadata.GetMetadata(); if (routes != null) { areaCode = routes.GetOrDefault("area")?.ToString(); vmCode = routes.GetOrDefault("controller")?.ToString(); if (!string.IsNullOrEmpty(areaCode) && !string.IsNullOrEmpty(vmCode)) { return await GetVmodelAsync(areaCode, vmCode); } } // 2.其次从实体特性中获取模型code var tp = typeof(TEntity); var vset = tp.GetCustomAttribute(); if (vset != null) { if (!string.IsNullOrEmpty(vset.Id)) { return await GetVmodelAsync(vset.Id); } areaCode = vset.Area; vmCode = vset.Code; } // 3.从实体命名空间中获取模型code if (string.IsNullOrEmpty(areaCode)) { ThrowIf.IsNullOrEmpty(tp.Namespace, $"类型 {nameof(tp)} 的命名空间不可为空"); areaCode = tp.Namespace.RemovePreFix(ModuleConst.NsPrefix + ".").Replace(".Domain", "").Replace(".Entities", "").ToKebab(); } if (string.IsNullOrEmpty(vmCode)) { vmCode = tp.Name.ToKebab(); } return await GetVmodelAsync(areaCode, vmCode); } /// /// 获取一条 数据信息 /// [HttpGet] public virtual async Task GetAsync([FromQuery] TGetInput input) { var vm = await GetVmodelAsync(); if (!string.IsNullOrEmpty(input.id)) input.q.Add(vm.GetPrimary().code, input.id); input.LoadFromHttpContext(App.HttpContext); var data = (await ListAsync(input.Adapt())).items.FirstOrDefault(); return data == null ? default! : data.Adapt(); } /// /// 获取多条 数据列表 /// [HttpGet] public virtual async Task> GetListAsync([FromQuery] TQueryInput input) { input.LoadFromHttpContext(App.HttpContext); return await ListAsync(input); } /// /// 获取多条 数据列表 /// [HttpPost] public virtual async Task> ListAsync([FromBody] TQueryInput input) { var vm = await GetVmodelAsync(); var ls = await _dataAccess.QueryDataAsync(vm, input); return ls.ToPagedOutput(); } /// /// 新增 数据 /// [HttpPost] public virtual async Task CreateAsync([FromBody] TCreateInput input) { var vm = await GetVmodelAsync(); var ret = await _dataAccess.CreateDataAsync(vm, input.ToEditInput()); return ret; } /// /// 更新 数据 /// [HttpPut] public virtual async Task UpdateAsync([FromBody] TUpdateInput input) { var vm = await GetVmodelAsync(); var ret = await _dataAccess.UpdateDataAsync(vm, input.ToEditInput()); return ret; } /// /// 删除 数据 /// [HttpDelete] public virtual async Task DeleteAsync([FromQuery] VmDeleteInput input) { var vm = await GetVmodelAsync(); var ret = await _dataAccess.DeleteDataAsync(vm, input); return ret; } } #region 泛型变种 public class VengineAppService : VengineAppService where TEntity : Entity where TGetInput : VmGetInput where TQueryInput : VmQueryInput where TCreateInput : VmEditInput { public VengineAppService(IDataAccess da) : base(da) { } } public class VengineAppService : VengineAppService where TEntity : Entity where TGetInput : VmGetInput where TQueryInput : VmQueryInput { public VengineAppService(IDataAccess da) : base(da) { } } public class VengineAppService : VengineAppService where TEntity : Entity where TGetInput : VmGetInput where TQueryInput : VmQueryInput { public VengineAppService(IDataAccess da) : base(da) { } } public class VengineAppService : VengineAppService where TEntity : Entity where TGetInput : VmGetInput { public VengineAppService(IDataAccess da) : base(da) { } } public class VengineAppService : VengineAppService where TEntity : Entity where TGetInput : VmGetInput { public VengineAppService(IDataAccess da) : base(da) { } } public class VengineAppService : VengineAppService where TEntity : Entity { public VengineAppService(IDataAccess da) : base(da) { } } #endregion