199 lines
6.6 KiB
C#
199 lines
6.6 KiB
C#
/////////////////////////////////////////////////////////////////////////////////
|
|
// 宁波拓通e智造平台 ToTong Next Builder //
|
|
// https://git.tuotong-tech.com/tnb/tnb-server //
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
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;
|
|
|
|
/// <summary>
|
|
/// 增删改查基类
|
|
/// </summary>
|
|
[Authorize]
|
|
//[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 1102)]
|
|
[Route("api/[area]/[controller]/[action]")]
|
|
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 : VmEditInput
|
|
where TUpdateInput : VmEditInput
|
|
{
|
|
protected readonly ISqlSugarClient _db;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
public VengineAppService(IDataAccess dataAccess) : base(dataAccess)
|
|
{
|
|
_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);
|
|
var vset = tp.GetCustomAttribute<VmodelAttribute>();
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取一条 数据信息
|
|
/// </summary>
|
|
[HttpGet]
|
|
public virtual async Task<TGetOutput> GetAsync([FromQuery] TGetInput input)
|
|
{
|
|
var vm = await GetVmodelAsync();
|
|
TQueryInput arg = input.Adapt<TQueryInput>();
|
|
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>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取多条 数据列表
|
|
/// </summary>
|
|
[HttpGet]
|
|
public virtual async Task<PagedOutput<TGetListOutput>> GetListAsync([FromQuery] TGetListInput input)
|
|
{
|
|
return await ListAsync(input.Adapt<TQueryInput>());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取多条 数据列表
|
|
/// </summary>
|
|
[HttpPost]
|
|
public virtual async Task<PagedOutput<TGetListOutput>> ListAsync([FromBody] TQueryInput input)
|
|
{
|
|
var vm = await GetVmodelAsync();
|
|
var ls = await _dataAccess.QueryDataAsync(vm, input);
|
|
return ls.ToPagedOutput<TGetListOutput>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增 数据
|
|
/// </summary>
|
|
[HttpPost]
|
|
public virtual async Task<dynamic> CreateAsync([FromBody] TCreateInput input)
|
|
{
|
|
var vm = await GetVmodelAsync();
|
|
var ret = await _dataAccess.CreateDataAsync(vm, input.ToEditInput());
|
|
return ret;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新 数据
|
|
/// </summary>
|
|
[HttpPut]
|
|
public virtual async Task<dynamic> UpdateAsync([FromBody] TUpdateInput input)
|
|
{
|
|
var vm = await GetVmodelAsync();
|
|
var ret = await _dataAccess.UpdateDataAsync(vm, input.ToEditInput());
|
|
return ret;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除 数据
|
|
/// </summary>
|
|
[HttpDelete]
|
|
public virtual async Task<dynamic> DeleteAsync([FromQuery] VmDeleteInput input)
|
|
{
|
|
var vm = await GetVmodelAsync();
|
|
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, VmEditInput, VmEditInput>
|
|
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, dynamic, VmEditInput, VmEditInput>
|
|
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, dynamic, VmEditInput, VmEditInput>
|
|
where TEntity : Entity
|
|
where TGetInput : VmGetInput
|
|
{
|
|
public VengineAppService(IDataAccess da) : base(da)
|
|
{
|
|
}
|
|
}
|
|
public class VengineAppService<TEntity> :
|
|
VengineAppService<TEntity, VmGetInput, dynamic, VmQueryInput, VmGetListInput, dynamic, VmEditInput, VmEditInput>
|
|
where TEntity : Entity
|
|
{
|
|
public VengineAppService(IDataAccess da) : base(da)
|
|
{
|
|
}
|
|
}
|
|
|
|
#endregion
|