Files
tnb.server/visualdev/Tnb.Vengine/AppService/VengineAppServiceT.cs

201 lines
6.3 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.Common.Contracts;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
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 : VmCreateInput
where TUpdateInput : VmUpdateInput
{
protected readonly IDataAccess _dataAccess;
protected readonly ISqlSugarClient _db;
private readonly Dictionary<string, Vmodel> _models = new();
/// <summary>
/// 构造函数
/// </summary>
public VengineAppService(IDataAccess dataAccess)
{
_dataAccess = dataAccess;
_db = _dataAccess.GetSqlSugar();
}
protected virtual async Task<Vmodel> GetVmodelAsync()
{
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(id))
{
if (_models.ContainsKey(id))
{
_models[id] = await _dataAccess.GetVmodelAsync(id, false);
//_models[_models[id].fullCode] = _models[id];
}
return _models[id];
}
else
{
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];
}
}
/// <summary>
/// 获取一条 数据信息
/// </summary>
[HttpGet]
public virtual async Task<TGetOutput> GetAsync([FromQuery] TGetInput input)
{
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);
}
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);
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);
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, 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, dynamic, 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, dynamic, 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