/////////////////////////////////////////////////////////////////////////////////
// 宁波拓通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;
///
/// 增删改查基类
///
[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 TGetListInput : VmGetListInput
where TCreateInput : VmEditInput
where TUpdateInput : VmEditInput
{
protected readonly IDataAccess _dataAccess;
protected readonly ISqlSugarClient _db;
private readonly Dictionary _models = new();
///
/// 构造函数
///
public VengineAppService(IDataAccess dataAccess)
{
_dataAccess = dataAccess;
_db = _dataAccess.GetSqlSugar();
}
protected virtual async Task GetVmodelAsync()
{
var tp = typeof(TEntity);
string? id = null, area = null, code = null;
var vset = tp.GetCustomAttribute();
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];
}
}
///
/// 获取一条 数据信息
///
[HttpGet]
public virtual async Task GetAsync([FromQuery] TGetInput input)
{
var vm = await GetVmodelAsync();
TQueryInput arg = input.Adapt();
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();
}
///
/// 获取多条 数据列表
///
[HttpGet]
public virtual async Task> GetListAsync([FromQuery] TGetListInput input)
{
return await ListAsync(input.Adapt());
}
///
/// 获取多条 数据列表
///
[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);
return ret;
}
///
/// 更新 数据
///
[HttpPut]
public virtual async Task UpdateAsync([FromBody] TUpdateInput input)
{
var vm = await GetVmodelAsync();
var ret = await _dataAccess.UpdateDataAsync(vm, input);
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 TGetListInput : VmGetListInput
where TQueryInput : VmQueryInput
{
public VengineAppService(IDataAccess da) : base(da)
{
}
}
public class VengineAppService :
VengineAppService
where TEntity : Entity
where TGetInput : VmGetInput
where TGetListInput : VmGetListInput
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
{
public VengineAppService(IDataAccess da) : base(da)
{
}
}
#endregion