初步完成代码级重写通用接口功能
This commit is contained in:
@@ -6,10 +6,14 @@
|
||||
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;
|
||||
@@ -31,57 +35,55 @@ public class VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGet
|
||||
where TCreateInput : VmEditInput
|
||||
where TUpdateInput : VmEditInput
|
||||
{
|
||||
protected readonly IDataAccess _dataAccess;
|
||||
protected readonly ISqlSugarClient _db;
|
||||
private readonly Dictionary<string, Vmodel> _models = new();
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public VengineAppService(IDataAccess dataAccess)
|
||||
public VengineAppService(IDataAccess dataAccess) : base(dataAccess)
|
||||
{
|
||||
_dataAccess = dataAccess;
|
||||
_db = _dataAccess.GetSqlSugar();
|
||||
_db = dataAccess.GetSqlSugar();
|
||||
}
|
||||
|
||||
protected virtual async Task<Vmodel> GetVmodelAsync()
|
||||
{
|
||||
string? areaCode = null, vmCode = null;
|
||||
// 1.优先从路由中获取模型code
|
||||
IHttpContextAccessor httpContextAccessor = App.GetService<IHttpContextAccessor>();
|
||||
var routes = httpContextAccessor.HttpContext?.Request.RouteValues;
|
||||
//HttpContext.GetEndpoint()?.Metadata.GetMetadata<ControllerActionDescriptor>();
|
||||
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);
|
||||
string? id = null, area = null, code = null;
|
||||
var vset = tp.GetCustomAttribute<VmodelAttribute>();
|
||||
if (vset != null)
|
||||
{
|
||||
id = vset.Id;
|
||||
area = vset.Area;
|
||||
code = vset.Code;
|
||||
if (!string.IsNullOrEmpty(vset.Id))
|
||||
{
|
||||
return await GetVmodelAsync(vset.Id);
|
||||
}
|
||||
areaCode = vset.Area;
|
||||
vmCode = vset.Code;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(id))
|
||||
// 3.从实体命名空间中获取模型code
|
||||
if (string.IsNullOrEmpty(areaCode))
|
||||
{
|
||||
if (!_models.ContainsKey(id))
|
||||
{
|
||||
_models[id] = await _dataAccess.GetVmodelAsync(id, false);
|
||||
//_models[_models[id].fullCode] = _models[id];
|
||||
}
|
||||
return _models[id];
|
||||
ThrowIf.IsNullOrEmpty(tp.Namespace, $"类型 {nameof(tp)} 的命名空间不可为空");
|
||||
areaCode = tp.Namespace.RemovePreFix(ModuleConst.NsPrefix + ".").Replace(".Domain", "").Replace(".Entities", "").ToKebab();
|
||||
}
|
||||
else
|
||||
if (string.IsNullOrEmpty(vmCode))
|
||||
{
|
||||
if (string.IsNullOrEmpty(area))
|
||||
{
|
||||
ThrowIf.IsNullOrEmpty(tp.Namespace, $"类型 {nameof(tp)} 的命名空间不可为空");
|
||||
area = tp.Namespace.RemovePreFix(ModuleConst.NsPrefix + ".").Replace(".Domain", "").Replace(".Entities", "").ToKebab();
|
||||
}
|
||||
if (string.IsNullOrEmpty(code))
|
||||
{
|
||||
code = tp.Name.ToKebab();
|
||||
}
|
||||
var key = area + "/" + code;
|
||||
if (!_models.ContainsKey(key))
|
||||
{
|
||||
_models[key] = await _dataAccess.GetVmodelAsync(area, code, false);
|
||||
}
|
||||
return _models[key];
|
||||
vmCode = tp.Name.ToKebab();
|
||||
}
|
||||
return await GetVmodelAsync(areaCode, vmCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -92,11 +94,7 @@ public class VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGet
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
TQueryInput arg = input.Adapt<TQueryInput>();
|
||||
if (input.id != null)
|
||||
{
|
||||
if (arg.q == null) arg.q = new DObject();
|
||||
arg.q.Add(vm.GetPrimary().code, input.id);
|
||||
}
|
||||
arg.AddQueryParaIf(!string.IsNullOrEmpty(input.id), vm.GetPrimary().code, input.id!);
|
||||
|
||||
var data = (await ListAsync(arg)).items.FirstOrDefault();
|
||||
return data == null ? default! : data.Adapt<TGetOutput>();
|
||||
@@ -129,7 +127,7 @@ public class VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGet
|
||||
public virtual async Task<dynamic> CreateAsync([FromBody] TCreateInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
var ret = await _dataAccess.CreateDataAsync(vm, input);
|
||||
var ret = await _dataAccess.CreateDataAsync(vm, input.ToEditInput());
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -140,7 +138,7 @@ public class VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGet
|
||||
public virtual async Task<dynamic> UpdateAsync([FromBody] TUpdateInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
var ret = await _dataAccess.UpdateDataAsync(vm, input);
|
||||
var ret = await _dataAccess.UpdateDataAsync(vm, input.ToEditInput());
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user