修复排序错误

This commit is contained in:
2023-11-16 14:55:37 +08:00
parent 5b49890a4f
commit a5f3d473e6
15 changed files with 261 additions and 154 deletions

View File

@@ -4,6 +4,9 @@
/////////////////////////////////////////////////////////////////////////////////
using System.Reflection;
using System.Security.Cryptography;
using Aop.Api.Domain;
using JNPF.Common.Contracts;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -20,10 +23,17 @@ namespace Tnb.Vengine.AppService;
[Authorize]
//[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 1102)]
[Route("api/[area]/[controller]/[action]")]
public class VengineAppService<TEntity> : BaseAppService where TEntity : Entity
public class VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGetListInput, TGetListOutput, TCreateInput, TUpdateInput> : BaseAppService
where TEntity : Entity
where TGetInput : VmGetInput
where TQueryInput : VmQueryInput
where TGetListInput : VmGetListInput
where TCreateInput : VmCreateInput
where TUpdateInput : VmUpdateInput
{
protected readonly IDataAccess _dataAccess;
protected readonly ISqlSugarClient _db;
private readonly Dictionary<string, Vmodel> _models = new();
/// <summary>
/// 构造函数
@@ -37,71 +47,86 @@ public class VengineAppService<TEntity> : BaseAppService where TEntity : Entity
protected virtual async Task<Vmodel> GetVmodelAsync()
{
var tp = typeof(TEntity);
string? area = null, code = null;
var vset = tp.GetCustomAttribute<VmodelSettingAttribute>();
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(area))
if (!string.IsNullOrEmpty(id))
{
ThrowIf.IsNullOrEmpty(tp.Namespace, $"类型 {nameof(tp)} 的命名空间不可为空");
area = tp.Namespace.RemovePreFix(ModuleConst.NsPrefix + ".").Replace(".Domain", "").Replace(".Entities", "").ToKebab();
if (_models.ContainsKey(id))
{
_models[id] = await _dataAccess.GetVmodelAsync(id, false);
//_models[_models[id].fullCode] = _models[id];
}
return _models[id];
}
if (string.IsNullOrEmpty(code))
else
{
code = tp.Name.ToKebab();
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];
}
var vm = await _dataAccess.GetVmodelAsync(area, code, false);
return vm;
}
/// <summary>
/// 获取一条 数据信息
/// </summary>
[HttpGet]
public virtual async Task<dynamic> GetAsync([FromQuery] VmGetInput input)
public virtual async Task<TGetOutput> GetAsync([FromQuery] TGetInput input)
{
var vm = await GetVmodelAsync();
VmQueryInput arg = input.Adapt<VmQueryInput>();
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);
}
var ls = await _dataAccess.QueryDataAsync(vm, arg);
return ls.items.FirstOrDefault()!;
var data = (await ListAsync(arg)).items.FirstOrDefault();
return data == null ? default! : data.Adapt<TGetOutput>();
}
/// <summary>
/// 获取多条 数据列表
/// </summary>
[HttpGet]
public virtual async Task<VmPagedOutput> GetListAsync([FromQuery] VmGetListInput input)
public virtual async Task<PagedOutput<TGetListOutput>> GetListAsync([FromQuery] TGetListInput input)
{
var vm = await GetVmodelAsync();
var ls = await _dataAccess.QueryDataAsync(vm, input.ToListInput());
return ls;
return await ListAsync(input.Adapt<TQueryInput>());
}
/// <summary>
/// 获取多条 数据列表
/// </summary>
[HttpPost]
public virtual async Task<VmPagedOutput> ListAsync([FromBody] VmQueryInput input)
public virtual async Task<PagedOutput<TGetListOutput>> ListAsync([FromBody] TQueryInput input)
{
var vm = await GetVmodelAsync();
var ls = await _dataAccess.QueryDataAsync(vm, input);
return ls;
return ls.ToPagedOutput<TGetListOutput>();
}
/// <summary>
/// 新增 数据
/// </summary>
[HttpPost]
public virtual async Task<dynamic> CreateAsync([FromBody] VmCreateInput input)
public virtual async Task<dynamic> CreateAsync([FromBody] TCreateInput input)
{
var vm = await GetVmodelAsync();
var ret = await _dataAccess.CreateDataAsync(vm, input);
@@ -112,7 +137,7 @@ public class VengineAppService<TEntity> : BaseAppService where TEntity : Entity
/// 更新 数据
/// </summary>
[HttpPut]
public virtual async Task<dynamic> UpdateAsync([FromBody] VmUpdateInput input)
public virtual async Task<dynamic> UpdateAsync([FromBody] TUpdateInput input)
{
var vm = await GetVmodelAsync();
var ret = await _dataAccess.UpdateDataAsync(vm, input);
@@ -129,4 +154,47 @@ public class VengineAppService<TEntity> : BaseAppService where TEntity : Entity
var ret = await _dataAccess.DeleteDataAsync(vm, input);
return ret;
}
}
}
#region
public class VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGetListInput, TGetListOutput> :
VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGetListInput, TGetListOutput, VmCreateInput, VmUpdateInput>
where TEntity : Entity
where TGetInput : VmGetInput
where TGetListInput : VmGetListInput
where TQueryInput : VmQueryInput
{
public VengineAppService(IDataAccess da) : base(da)
{
}
}
public class VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGetListInput> :
VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGetListInput, DObject, VmCreateInput, VmUpdateInput>
where TEntity : Entity
where TGetInput : VmGetInput
where TGetListInput : VmGetListInput
where TQueryInput : VmQueryInput
{
public VengineAppService(IDataAccess da) : base(da)
{
}
}
public class VengineAppService<TEntity, TGetInput, TGetOutput> :
VengineAppService<TEntity, TGetInput, TGetOutput, VmQueryInput, VmGetListInput, DObject, VmCreateInput, VmUpdateInput>
where TEntity : Entity
where TGetInput : VmGetInput
{
public VengineAppService(IDataAccess da) : base(da)
{
}
}
public class VengineAppService<TEntity> :
VengineAppService<TEntity, VmGetInput, dynamic, VmQueryInput, VmGetListInput, dynamic, VmCreateInput, VmUpdateInput>
where TEntity : Entity
{
public VengineAppService(IDataAccess da) : base(da)
{
}
}
#endregion