vengine初步实现增删改查
This commit is contained in:
16
visualdev/Tnb.Vengine/AppService/IVengineAppService.cs
Normal file
16
visualdev/Tnb.Vengine/AppService/IVengineAppService.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb-server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using JNPF.DependencyInjection;
|
||||
|
||||
namespace Tnb.Vengine.AppService;
|
||||
|
||||
/// <summary>
|
||||
/// 通用服务接口
|
||||
/// </summary>
|
||||
public interface IVengineAppService : ITransient
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
// https://git.tuotong-tech.com/tnb/tnb-server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using JNPF.DependencyInjection;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
// https://git.tuotong-tech.com/tnb/tnb-server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using JNPF.DependencyInjection;
|
||||
|
||||
203
visualdev/Tnb.Vengine/AppService/VengineAppService.cs
Normal file
203
visualdev/Tnb.Vengine/AppService/VengineAppService.cs
Normal file
@@ -0,0 +1,203 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb-server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using JNPF.Common.Security;
|
||||
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 = 100)]
|
||||
[Route("/")]
|
||||
public class VengineAppService : BaseAppService, IVengineAppService
|
||||
{
|
||||
private readonly IDataAccess _dataAccess;
|
||||
private readonly ISqlSugarClient _db;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public VengineAppService(IDataAccess da)
|
||||
{
|
||||
_dataAccess = da;
|
||||
_db = _dataAccess.GetSqlSugar();
|
||||
}
|
||||
|
||||
#region 按模型的id进行增删改查接口
|
||||
|
||||
/// <summary>
|
||||
/// 获取一条 数据信息
|
||||
/// </summary>
|
||||
[HttpGet("api/[area]/[controller]/{vmid}/get")]
|
||||
public async Task<dynamic?> GetAsync(string vmid, VmGetInput input)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid, true);
|
||||
VmQueryInput arg = input.Adapt<VmQueryInput>();
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
[HttpGet("api/[area]/[controller]/{vmid}/get-list")]
|
||||
public async Task<VmPagedOutput> GetListAsync(string vmid, VmGetListInput input)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid, true);
|
||||
VmQueryInput arg = input.Adapt<VmQueryInput>();
|
||||
if (!string.IsNullOrEmpty(input.q))
|
||||
{
|
||||
arg.q = input.q.ToObject<DObject>();
|
||||
}
|
||||
var ls = await _dataAccess.QueryDataAsync(vm, arg);
|
||||
return ls;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
[HttpPost("api/[area]/[controller]/{vmid}/query")]
|
||||
public async Task<VmPagedOutput> QueryAsync(string vmid, VmQueryInput input)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid, true);
|
||||
var ls = await _dataAccess.QueryDataAsync(vm, input);
|
||||
return ls;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增 数据
|
||||
/// </summary>
|
||||
[HttpPost("api/[area]/[controller]/{vmid}/create")]
|
||||
public async Task<dynamic> CreateAsync(string vmid, VmCreateInput input)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid);
|
||||
var ret = await _dataAccess.CreateDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 数据
|
||||
/// </summary>
|
||||
[HttpPut("api/[area]/[controller]/{vmid}/update")]
|
||||
public async Task<dynamic> UpdateAsync(string vmid, VmUpdateInput input)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid);
|
||||
var ret = await _dataAccess.UpdateDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除 数据
|
||||
/// </summary>
|
||||
[HttpDelete("api/[area]/[controller]/{vmid}/delete")]
|
||||
public async Task<dynamic> DeleteAsync(string vmid, VmDeleteInput input)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid);
|
||||
var ret = await _dataAccess.DeleteDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 按模型的areaCode和vmcode进行增删改查接口
|
||||
|
||||
private async Task<Vmodel> GetVmodelAsync(string areaCode, string vmCode)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(areaCode, vmCode, false);
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取一条 数据信息
|
||||
/// </summary>
|
||||
[HttpGet("api/{areaCode}/{vmCode}/get")]
|
||||
public async Task<dynamic?> GetAsync(string areaCode, string vmCode, VmGetInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync(areaCode, vmCode);
|
||||
VmQueryInput arg = input.Adapt<VmQueryInput>();
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
[HttpGet("api/{areaCode}/{vmCode}/get-list")]
|
||||
public async Task<VmPagedOutput> GetListAsync(string areaCode, string vmCode, VmGetListInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync(areaCode, vmCode);
|
||||
VmQueryInput arg = input.Adapt<VmQueryInput>();
|
||||
if (!string.IsNullOrEmpty(input.q))
|
||||
{
|
||||
arg.q = input.q.ToObject<DObject>();
|
||||
}
|
||||
var ls = await _dataAccess.QueryDataAsync(vm, arg);
|
||||
return ls;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
[HttpPost("api/{areaCode}/{vmCode}/query")]
|
||||
public async Task<VmPagedOutput> QueryAsync(string areaCode, string vmCode, VmQueryInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync(areaCode, vmCode);
|
||||
var ls = await _dataAccess.QueryDataAsync(vm, input);
|
||||
return ls;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增 数据
|
||||
/// </summary>
|
||||
[HttpPost("api/{areaCode}/{vmCode}/create")]
|
||||
public async Task<dynamic> CreateAsync(string areaCode, string vmCode, VmCreateInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync(areaCode, vmCode);
|
||||
var ret = await _dataAccess.CreateDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 数据
|
||||
/// </summary>
|
||||
[HttpPut("api/{areaCode}/{vmCode}/update")]
|
||||
public async Task<dynamic> UpdateAsync(string areaCode, string vmCode, VmUpdateInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync(areaCode, vmCode);
|
||||
var ret = await _dataAccess.UpdateDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除 数据
|
||||
/// </summary>
|
||||
[HttpDelete("api/{areaCode}/{vmCode}/delete")]
|
||||
public async Task<dynamic> DeleteAsync(string areaCode, string vmCode, VmDeleteInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync(areaCode, vmCode);
|
||||
var ret = await _dataAccess.DeleteDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
140
visualdev/Tnb.Vengine/AppService/VengineAppServiceT.cs
Normal file
140
visualdev/Tnb.Vengine/AppService/VengineAppServiceT.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb-server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using System.Reflection;
|
||||
using JNPF;
|
||||
using JNPF.Common.Security;
|
||||
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> : BaseAppService where TEntity : Entity
|
||||
{
|
||||
protected readonly IDataAccess _dataAccess;
|
||||
protected readonly ISqlSugarClient _db;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public VengineAppService(IDataAccess dataAccess)
|
||||
{
|
||||
_dataAccess = dataAccess;
|
||||
_db = _dataAccess.GetSqlSugar();
|
||||
}
|
||||
|
||||
protected async Task<Vmodel> GetVmodelAsync()
|
||||
{
|
||||
var tp = typeof(TEntity);
|
||||
string? area = null, code = null;
|
||||
var vset = tp.GetCustomAttribute<VmodelSettingAttribute>();
|
||||
if (vset != null)
|
||||
{
|
||||
area = vset.Area;
|
||||
code = vset.Code;
|
||||
}
|
||||
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 vm = await _dataAccess.GetVmodelAsync(area, code, true);
|
||||
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取一条 数据信息
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public virtual async Task<dynamic> GetAsync(VmGetInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
VmQueryInput arg = input.Adapt<VmQueryInput>();
|
||||
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()!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public virtual async Task<VmPagedOutput> GetListAsync(VmGetListInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
VmQueryInput arg = input.Adapt<VmQueryInput>();
|
||||
if (!string.IsNullOrEmpty(input.q))
|
||||
{
|
||||
arg.q = input.q.ToObject<DObject>();
|
||||
}
|
||||
var ls = await _dataAccess.QueryDataAsync(vm, arg);
|
||||
return ls;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public virtual async Task<VmPagedOutput> QueryAsync(VmQueryInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
var ls = await _dataAccess.QueryDataAsync(vm, input);
|
||||
return ls;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增 数据
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public virtual async Task<dynamic> CreateAsync(VmCreateInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
var ret = await _dataAccess.CreateDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 数据
|
||||
/// </summary>
|
||||
[HttpPut]
|
||||
public virtual async Task<dynamic> UpdateAsync(VmUpdateInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
var ret = await _dataAccess.UpdateDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除 数据
|
||||
/// </summary>
|
||||
[HttpDelete]
|
||||
public virtual async Task<dynamic> DeleteAsync(VmDeleteInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
var ret = await _dataAccess.DeleteDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,198 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using JNPF.Common.Security;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
using Tnb.Vengine.DataAccess;
|
||||
using Tnb.Vengine.Domain;
|
||||
|
||||
namespace Tnb.Vengine.AppService;
|
||||
|
||||
/// <summary>
|
||||
/// 增删改查基类
|
||||
/// </summary>
|
||||
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 10, KeepVerb = true)]
|
||||
[Route("api")]
|
||||
public class VmAppService : BaseAppService
|
||||
{
|
||||
private readonly IDataAccess _dataAccess;
|
||||
private readonly ISqlSugarClient _db;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public VmAppService(IDataAccess da)
|
||||
{
|
||||
_dataAccess = da;
|
||||
_db = _dataAccess.GetSqlSugar();
|
||||
}
|
||||
|
||||
#region 根据vmodel id进行增删改查接口
|
||||
/// <summary>
|
||||
/// 获取一条 数据信息
|
||||
/// </summary>
|
||||
[HttpGet("[area]/[controller]/{vmid}/[action]")]
|
||||
public async Task<dynamic?> GetAsync(string vmid, [FromQuery] VmGetInput input)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid, true);
|
||||
VmQueryInput arg = input.Adapt<VmQueryInput>();
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
[HttpGet("[area]/[controller]/{vmid}/[action]")]
|
||||
public async Task<VmPagedOutput> GetListAsync(string vmid, [FromQuery] VmGetListInput input)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid, true);
|
||||
VmQueryInput arg = input.Adapt<VmQueryInput>();
|
||||
if (!string.IsNullOrEmpty(input.q))
|
||||
{
|
||||
arg.q = input.q.ToObject<DObject>();
|
||||
}
|
||||
var ls = await _dataAccess.QueryDataAsync(vm, arg);
|
||||
return ls;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
[HttpPost("[area]/[controller]/{vmid}/[action]")]
|
||||
public async Task<VmPagedOutput> QueryAsync(string vmid, [FromBody] VmQueryInput input)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid, true);
|
||||
var ls = await _dataAccess.QueryDataAsync(vm, input);
|
||||
return ls;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增 数据
|
||||
/// </summary>
|
||||
[HttpPost("[area]/[controller]/{vmid}/[action]")]
|
||||
public async Task<dynamic> CreateAsync(string vmid, VmCreateInput input)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid);
|
||||
var ret = await _dataAccess.CreateDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 数据
|
||||
/// </summary>
|
||||
[HttpPut("[area]/[controller]/{vmid}/[action]")]
|
||||
public async Task<dynamic> UpdateAsync(string vmid, VmUpdateInput input)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid);
|
||||
var ret = await _dataAccess.UpdateDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除 数据
|
||||
/// </summary>
|
||||
[HttpDelete("[area]/[controller]/{vmid}/[action]")]
|
||||
public async Task<dynamic> DeleteAsync(string vmid, [FromQuery] VmDeleteInput input)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid);
|
||||
var ret = await _dataAccess.DeleteDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private async Task<Vmodel> GetVmodelAsync(string area, string vmCode)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(area.SnakeToPascalCase(), vmCode.SnakeToPascalCase(), true);
|
||||
return vm;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 根据vmode的area和code进行增删改查接口
|
||||
/// <summary>
|
||||
/// 获取一条 数据信息
|
||||
/// </summary>
|
||||
[HttpGet("{areaCode}/{vmCode}/[action]")]
|
||||
public async Task<dynamic?> GetAsync(string areaCode, string vmCode, [FromQuery] VmGetInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync(areaCode, vmCode);
|
||||
VmQueryInput arg = input.Adapt<VmQueryInput>();
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
[HttpGet("{areaCode}/{vmCode}/[action]")]
|
||||
public async Task<VmPagedOutput> GetListAsync(string areaCode, string vmCode, [FromQuery] VmGetListInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync(areaCode, vmCode);
|
||||
VmQueryInput arg = input.Adapt<VmQueryInput>();
|
||||
if (!string.IsNullOrEmpty(input.q))
|
||||
{
|
||||
arg.q = input.q.ToObject<DObject>();
|
||||
}
|
||||
var ls = await _dataAccess.QueryDataAsync(vm, arg);
|
||||
return ls;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
[HttpPost("{areaCode}/{vmCode}/[action]")]
|
||||
public async Task<VmPagedOutput> QueryAsync(string areaCode, string vmCode, [FromBody] VmQueryInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync(areaCode, vmCode);
|
||||
var ls = await _dataAccess.QueryDataAsync(vm, input);
|
||||
return ls;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增 数据
|
||||
/// </summary>
|
||||
[HttpPost("{areaCode}/{vmCode}/[action]")]
|
||||
public async Task<dynamic> CreateAsync(string areaCode, string vmCode, VmCreateInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync(areaCode, vmCode);
|
||||
var ret = await _dataAccess.CreateDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 数据
|
||||
/// </summary>
|
||||
[HttpPut("{areaCode}/{vmCode}/[action]")]
|
||||
public async Task<dynamic> UpdateAsync(string areaCode, string vmCode, VmUpdateInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync(areaCode, vmCode);
|
||||
var ret = await _dataAccess.UpdateDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除 数据
|
||||
/// </summary>
|
||||
[HttpDelete("{areaCode}/{vmCode}/[action]")]
|
||||
public async Task<dynamic> DeleteAsync(string areaCode, string vmCode, [FromQuery] VmDeleteInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync(areaCode, vmCode);
|
||||
var ret = await _dataAccess.DeleteDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using JNPF.Common.Contracts;
|
||||
using JNPF.Common.Security;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
using Tnb.Vengine.DataAccess;
|
||||
using Tnb.Vengine.Domain;
|
||||
|
||||
namespace Tnb.Vengine.AppService;
|
||||
|
||||
/// <summary>
|
||||
/// 增删改查基类
|
||||
/// </summary>
|
||||
[ApiDescriptionSettings(Area = ModuleConst.Area, Order = 10, KeepVerb = true)]
|
||||
[Route("api/[area]/[controller]/[action]")]
|
||||
public class VmAppService<TEntity> : BaseAppService where TEntity : IEntity
|
||||
{
|
||||
protected readonly IDataAccess _dataAccess;
|
||||
protected readonly ISqlSugarClient _db;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public VmAppService(IDataAccess dataAccess)
|
||||
{
|
||||
_dataAccess = dataAccess;
|
||||
_db = _dataAccess.GetSqlSugar();
|
||||
}
|
||||
|
||||
protected async Task<Vmodel> GetVmodelAsync()
|
||||
{
|
||||
var tp = typeof(TEntity);
|
||||
if (string.IsNullOrEmpty(tp?.Namespace))
|
||||
{
|
||||
throw new ArgumentNullException($"类型 {nameof(tp)} 的命名空间不可为空");
|
||||
}
|
||||
var area = tp.Namespace.Split('.').Last().ToKebabCase();
|
||||
var vm = await _dataAccess.GetVmodelAsync(area, tp.Name, true);
|
||||
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取一条 数据信息
|
||||
/// </summary>
|
||||
public virtual async Task<dynamic> GetAsync([FromQuery] VmGetInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
VmQueryInput arg = input.Adapt<VmQueryInput>();
|
||||
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()!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
public virtual async Task<VmPagedOutput> GetListAsync([FromQuery] VmGetListInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
VmQueryInput arg = input.Adapt<VmQueryInput>();
|
||||
if (!string.IsNullOrEmpty(input.q))
|
||||
{
|
||||
arg.q = input.q.ToObject<DObject>();
|
||||
}
|
||||
var ls = await _dataAccess.QueryDataAsync(vm, arg);
|
||||
return ls;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public virtual async Task<VmPagedOutput> QueryAsync([FromBody] VmQueryInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
var ls = await _dataAccess.QueryDataAsync(vm, input);
|
||||
return ls;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增 数据
|
||||
/// </summary>
|
||||
public virtual async Task<dynamic> CreateAsync(VmCreateInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
var ret = await _dataAccess.CreateDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 数据
|
||||
/// </summary>
|
||||
public virtual async Task<dynamic> UpdateAsync(VmUpdateInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
var ret = await _dataAccess.UpdateDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除 数据
|
||||
/// </summary>
|
||||
public virtual async Task<dynamic> DeleteAsync([FromQuery] VmDeleteInput input)
|
||||
{
|
||||
var vm = await GetVmodelAsync();
|
||||
var ret = await _dataAccess.DeleteDataAsync(vm, input);
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,151 +1,229 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
// https://git.tuotong-tech.com/tnb/tnb-server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using JNPF.Common.Configuration;
|
||||
using JNPF.Systems.Entitys.Dto.Database;
|
||||
using JNPF;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
using Tnb.Core;
|
||||
using Tnb.Vengine.DataAccess;
|
||||
using Tnb.Vengine.Domain;
|
||||
using JNPF.ViewEngine;
|
||||
|
||||
namespace Tnb.Vengine.AppService;
|
||||
|
||||
/// <summary>
|
||||
/// 视图模型服务类
|
||||
/// </summary>
|
||||
public class VmodelAppService : VmAppService<Vmodel>, IVmodelAppService
|
||||
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, KeepVerb = true, Order = 1102)]
|
||||
[Route("api/[area]/[controller]/[action]")]
|
||||
public class VmodelAppService : VengineAppService<Vmodel>, IVmodelAppService
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public VmodelAppService(IDataAccess da) : base(da)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取一条 数据信息
|
||||
/// </summary>
|
||||
public override async Task<dynamic> GetAsync(VmGetInput input)
|
||||
{
|
||||
//return await _dataAccess.GetVmodelAsync(input.id);
|
||||
var query = _db.Queryable<Vmodel>().Where(a => a.deleted == 0);
|
||||
Vmodel vm;
|
||||
if (long.TryParse(input.id, out long id))
|
||||
private readonly ViewEngine _viewEngine;
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public VmodelAppService(IDataAccess da, ViewEngine viewEngine) : base(da)
|
||||
{
|
||||
vm = await query.FirstAsync(a => a.id == input.id);
|
||||
_viewEngine = viewEngine;
|
||||
}
|
||||
else
|
||||
|
||||
/// <summary>
|
||||
/// 获取一条 数据信息
|
||||
/// </summary>
|
||||
public override async Task<dynamic> GetAsync(VmGetInput input)
|
||||
{
|
||||
vm = await query.FirstAsync(a => a.vmCode == input.id);
|
||||
var query = _db.Queryable<Vmodel>().Where(a => a.deleted == 0);
|
||||
Vmodel vm;
|
||||
if (long.TryParse(input.id, out long id))
|
||||
{
|
||||
query.Where(a => a.id == input.id);
|
||||
vm = await query.FirstAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
vm = await query.FirstAsync(a => a.vmCode == input.id);
|
||||
}
|
||||
return vm;
|
||||
}
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
public override async Task<VmPagedOutput> GetListAsync(VmGetListInput input)
|
||||
{
|
||||
VmPagedOutput ret = new();
|
||||
var q = _db.Queryable<Vmodel>().WhereIF(!string.IsNullOrEmpty(input.k), a => a.vmCode.Contains(input.k!) || a.vmName.Contains(input.k!));
|
||||
RefAsync<int> total = 0;
|
||||
var data = await q.OrderBy(input.sort).ToPageListAsync((input.pnum - 1) * input.psize, input.psize, total);
|
||||
ret.total = total;
|
||||
ret.items = data.ConvertAll<dynamic>(a => a);
|
||||
return ret;
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
public override Task<VmPagedOutput> QueryAsync(VmQueryInput input)
|
||||
{
|
||||
return base.QueryAsync(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增 模型
|
||||
/// </summary>
|
||||
public override async Task<dynamic> CreateAsync(VmCreateInput input)
|
||||
{
|
||||
//ThrowIf.IsNull(input.data, nameof(input));
|
||||
ArgumentNullException.ThrowIfNull(input.data);
|
||||
Vmodel vm = input.data.Adapt<Vmodel>();
|
||||
await _db.Insertable(vm).ExecuteCommandAsync();
|
||||
return input;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 数据
|
||||
/// </summary>
|
||||
public override async Task<dynamic> UpdateAsync(VmUpdateInput input)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(input.data);
|
||||
Vmodel vm = input.data.Adapt<Vmodel>();
|
||||
await _db.Updateable(vm).WhereColumns(a => a.id).ExecuteCommandAsync();
|
||||
return input;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除 数据
|
||||
/// </summary>
|
||||
public override async Task<dynamic> DeleteAsync(VmDeleteInput input)
|
||||
{
|
||||
var ret = await _db.Deleteable<Vmodel>(input.id).ExecuteCommandAsync();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从数据表创建模型
|
||||
/// </summary>
|
||||
public async Task<List<Vmodel>> CreateFromTable(VmodelCreateFromTableInput input)
|
||||
{
|
||||
ThrowIf.IsNull(input.tableName, nameof(input.tableName));
|
||||
var sugar = _dataAccess.GetSqlSugar(input.dbCode);
|
||||
var lsTable = sugar.DbMaintenance.GetTableInfoList().WhereIF(input.tableName != "ALL", a => a.Name == input.tableName);
|
||||
|
||||
List<Vmodel> lsToAdd = new List<Vmodel>();
|
||||
List<Vmodel> lsToUpdate = new List<Vmodel>();
|
||||
foreach (var tb in lsTable)
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
public override async Task<VmPagedOutput> GetListAsync(VmGetListInput input)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(input.removePrefix) && !tb.Name.StartsWith(input.removePrefix)) continue;
|
||||
var colInfo = sugar.DbMaintenance.GetColumnInfosByTableName(tb.Name);
|
||||
Vmodel model = new() { dbCode = input.dbCode, vmName = tb.Description, tableName = tb.Name };
|
||||
model.area = input.area;
|
||||
model.vmCode = (string.IsNullOrEmpty(input.removePrefix) ? tb.Name : tb.Name.RemovePreFix(input.removePrefix)).SnakeToPascalCase();
|
||||
//model.createId = CurrentUser.Id;
|
||||
int n = 1;
|
||||
foreach (var p in colInfo)
|
||||
{
|
||||
var prop = p.Adapt<VmDbProp>();
|
||||
prop.ordinal = n++;
|
||||
prop.csType = sugar.Ado.DbBind.GetPropertyTypeName(p.DataType);
|
||||
model.dbProps.Add(prop);
|
||||
}
|
||||
var exist = await _db.Queryable<Vmodel>().FirstAsync(a => a.dbCode == input.dbCode && a.tableName == tb.Name);
|
||||
if (exist == null)
|
||||
{
|
||||
lsToAdd.Add(model);
|
||||
}
|
||||
else
|
||||
{
|
||||
exist.area = model.area;
|
||||
model.dbProps.Adapt(exist.dbProps);
|
||||
//exist.dbProps.Clear();
|
||||
//exist.dbProps.AddRange(model.dbProps.OrderBy(a => a.ordinal));
|
||||
lsToUpdate.Add(exist);
|
||||
}
|
||||
VmPagedOutput ret = new();
|
||||
var q = _db.Queryable<Vmodel>().WhereIF(!string.IsNullOrEmpty(input.k), a => a.vmCode.Contains(input.k!) || a.vmName.Contains(input.k!));
|
||||
RefAsync<int> total = 0;
|
||||
var data = await q.OrderBy(input.sort).ToPageListAsync((input.pnum - 1) * input.psize, input.psize, total);
|
||||
ret.total = total;
|
||||
ret.items = data.ConvertAll<dynamic>(a => a);
|
||||
return ret;
|
||||
}
|
||||
if (lsToAdd.Count > 0)
|
||||
{
|
||||
await _db.Insertable(lsToAdd).ExecuteCommandAsync();
|
||||
}
|
||||
if (lsToUpdate.Count > 0)
|
||||
{
|
||||
await _db.Updateable(lsToUpdate).ExecuteCommandAsync();
|
||||
}
|
||||
return lsToAdd.Union(lsToUpdate).ToList();
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
public override Task<VmPagedOutput> QueryAsync(VmQueryInput input)
|
||||
{
|
||||
return base.QueryAsync(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增 模型
|
||||
/// </summary>
|
||||
public override async Task<dynamic> CreateAsync(VmCreateInput input)
|
||||
{
|
||||
ThrowIf.IsNull(input.data);
|
||||
//ArgumentNullException.ThrowIfNull(input.data);
|
||||
Vmodel vm = input.data.Adapt<Vmodel>();
|
||||
vm.areaCode = vm.areaCode.ToKebab();
|
||||
vm.vmCode = vm.vmCode.ToKebab();
|
||||
await _db.Insertable(vm).ExecuteCommandAsync();
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 数据
|
||||
/// </summary>
|
||||
public override async Task<dynamic> UpdateAsync(VmUpdateInput input)
|
||||
{
|
||||
ThrowIf.IsNull(input.data);
|
||||
//ArgumentNullException.ThrowIfNull(input.data);
|
||||
Vmodel vm = input.data.Adapt<Vmodel>();
|
||||
vm.areaCode = vm.areaCode.ToKebab();
|
||||
vm.vmCode = vm.vmCode.ToKebab();
|
||||
await _db.Updateable(vm).WhereColumns(a => a.id).ExecuteCommandAsync();
|
||||
return input;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除 数据
|
||||
/// </summary>
|
||||
public override async Task<dynamic> DeleteAsync(VmDeleteInput input)
|
||||
{
|
||||
var ret = await _db.Deleteable<Vmodel>(input.id).ExecuteCommandAsync();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从数据表创建模型
|
||||
/// </summary>
|
||||
public async Task<List<Vmodel>> CreateFromTable(VmodelCreateFromTableInput input)
|
||||
{
|
||||
ThrowIf.IsNullOrEmpty(input.tableName);
|
||||
var sugar = _dataAccess.GetSqlSugar(input.dbCode);
|
||||
var lsTable = sugar.DbMaintenance.GetTableInfoList().WhereIF(input.tableName != "ALL", a => a.Name == input.tableName);
|
||||
input.areaCode = input.areaCode.ToPascal();
|
||||
List<Vmodel> lsToAdd = new List<Vmodel>();
|
||||
List<Vmodel> lsToUpdate = new List<Vmodel>();
|
||||
foreach (var tb in lsTable)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(input.removePrefix) && !tb.Name.StartsWith(input.removePrefix)) continue;
|
||||
var colInfo = sugar.DbMaintenance.GetColumnInfosByTableName(tb.Name);
|
||||
Vmodel model = new() { dbCode = input.dbCode, vmName = tb.Description, tableName = tb.Name };
|
||||
model.areaCode = input.areaCode.ToKebab();
|
||||
model.vmCode = (string.IsNullOrEmpty(input.removePrefix) ? tb.Name.ToKebab() : tb.Name.RemovePreFix(input.removePrefix)).ToKebab();
|
||||
//model.createId = CurrentUser.Id;
|
||||
int n = 1;
|
||||
foreach (var p in colInfo)
|
||||
{
|
||||
var prop = p.Adapt<VmDbProp>();
|
||||
prop.ordinal = n++;
|
||||
prop.csType = sugar.Ado.DbBind.GetPropertyTypeName(p.DataType);
|
||||
//var s1 = sugar.Ado.DbBind.GetCsharpTypeName(p.DataType); // 和DataType是一样的
|
||||
//var s2 = sugar.Ado.DbBind.GetCsharpTypeNameByDbTypeName(p.DataType); // 和GetPropertyTypeName是一样的
|
||||
//Console.WriteLine($"dbType = {p.DataType}, csType = {prop.csType} | {s1} | {s2}");
|
||||
model.dbProps.Add(prop);
|
||||
}
|
||||
var exist = await _db.Queryable<Vmodel>().FirstAsync(a => a.dbCode == input.dbCode && a.tableName == tb.Name);
|
||||
if (exist == null)
|
||||
{
|
||||
lsToAdd.Add(model);
|
||||
}
|
||||
else
|
||||
{
|
||||
exist.dbProps.Clear();
|
||||
exist.dbProps.AddRange(model.dbProps.OrderBy(a => a.ordinal));
|
||||
lsToUpdate.Add(exist);
|
||||
}
|
||||
}
|
||||
if (lsToAdd.Count > 0)
|
||||
{
|
||||
await _db.Insertable(lsToAdd).ExecuteCommandAsync();
|
||||
}
|
||||
if (lsToUpdate.Count > 0)
|
||||
{
|
||||
await _db.Updateable(lsToUpdate).ExecuteCommandAsync();
|
||||
}
|
||||
return lsToAdd.Union(lsToUpdate).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成模型代码
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<dynamic> BuildEntityCodeAsync(string vmid)
|
||||
{
|
||||
var vm = await _dataAccess.GetVmodelAsync(vmid);
|
||||
var ignores = new string[] { };//"SysViewModel", "SysViewProp"
|
||||
ThrowIf.When(ignores.Contains(vm.vmCode), $"模型 {vm.vmCode} 不允许自动生成");
|
||||
ThrowIf.When(!vm.dbProps.Any(), $"模型 {vm.vmCode} 属性不可为空");
|
||||
|
||||
vm.areaCode = vm.areaCode.ToPascal();
|
||||
vm.vmCode = vm.vmCode.ToPascal();
|
||||
AcmenVmodelContext ctx = new AcmenVmodelContext(_dataAccess, vm);
|
||||
string tplPath = Path.Combine(App.WebHostEnvironment.WebRootPath, "Template", "VengineSqlSugar");
|
||||
string entityPath = Path.Combine(ctx.BasePath, ctx.NsPrefix + "." + vm.areaCode, "AppService", vm.vmCode);
|
||||
|
||||
string filePath, code, tplContent;
|
||||
//创建模型码农类
|
||||
tplContent = File.ReadAllText(Path.Combine(tplPath, "EntityInfoAcmen.cshtml"));
|
||||
code = _viewEngine.RunCompileFromCached(tplContent, ctx);
|
||||
CodeHelper.SaveCodeToFile(code, Path.Combine(entityPath, $"{vm.vmCode}.acmen.cs"));
|
||||
//创建模型手动代码类
|
||||
filePath = Path.Combine(entityPath, $"{vm.vmCode}.cs");
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
tplContent = File.ReadAllText(Path.Combine(tplPath, "EntityInfo.cshtml"));
|
||||
code = _viewEngine.RunCompileFromCached(tplContent, ctx);
|
||||
CodeHelper.SaveCodeToFile(code, filePath);
|
||||
}
|
||||
//创建模型Dto码农类
|
||||
tplContent = File.ReadAllText(Path.Combine(tplPath, "EntityDtoAcmen.cshtml"));
|
||||
code = _viewEngine.RunCompileFromCached(tplContent, ctx);
|
||||
CodeHelper.SaveCodeToFile(code, Path.Combine(entityPath, $"{vm.vmCode}Dto.acmen.cs"));
|
||||
//创建模型Dto手动代码类
|
||||
filePath = Path.Combine(entityPath, $"{vm.vmCode}Dto.cs");
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
tplContent = File.ReadAllText(Path.Combine(tplPath, "EntityDto.cshtml"));
|
||||
code = _viewEngine.RunCompileFromCached(tplContent, ctx);
|
||||
CodeHelper.SaveCodeToFile(code, filePath);
|
||||
}
|
||||
//创建模型服务手动代码类
|
||||
filePath = Path.Combine(entityPath, $"{vm.vmCode}AppService.cs");
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
tplContent = File.ReadAllText(Path.Combine(tplPath, "AppService.cshtml"));
|
||||
code = _viewEngine.RunCompileFromCached(tplContent, ctx);
|
||||
CodeHelper.SaveCodeToFile(code, filePath);
|
||||
}
|
||||
////创建页面视图文件
|
||||
////string vueFileName = view.ViewCode == entity.EntityCode ? "index" : view.ViewCode.ToKebabCase();
|
||||
//string vueFileName = "index";
|
||||
//filePath = Path.Combine(ctx.PagePath, entityKebabName, vueFileName + ".vue");
|
||||
//if (File.Exists(filePath))
|
||||
//{
|
||||
// filePath = Path.Combine(ctx.PagePath, entityKebabName, vueFileName + ".acmen.txt");
|
||||
//}
|
||||
//code = CodeHelper.RunCompile($"{TemplateContext.KeyEntityPageVue}.cshtml", ctx, null, "SqlSugar");
|
||||
//CodeHelper.SaveCodeToFile(code, filePath);
|
||||
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
34
visualdev/Tnb.Vengine/AppService/VmodelDto.cs
Normal file
34
visualdev/Tnb.Vengine/AppService/VmodelDto.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace Tnb.Vengine.AppService;
|
||||
|
||||
public class VmodelCreateFromTableInput
|
||||
{
|
||||
public string? dbCode { get; set; }
|
||||
|
||||
public string tableName { get; set; } = string.Empty;
|
||||
|
||||
public string? removePrefix { get; set; }
|
||||
public string areaCode { get; set; } = "edp";
|
||||
}
|
||||
|
||||
public class CreatePageFromVmodelInput
|
||||
{
|
||||
public Guid? viewId { get; set; }
|
||||
public string? vmid { get; set; }
|
||||
}
|
||||
|
||||
public class VmodelGetInput
|
||||
{
|
||||
public long? id { get; set; }
|
||||
public string? moduleCode { get; set; }
|
||||
public string? vmCode { get; set; }
|
||||
public string? dbCode { get; set; }
|
||||
public string? tableName { get; set; }
|
||||
public bool drill { get; set; }
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
// https://git.tuotong-tech.com/tnb/tnb-server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using System.Text;
|
||||
using JNPF.Common.Security;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SqlSugar;
|
||||
using System.Text;
|
||||
using Tnb.Core;
|
||||
using Tnb.Vengine.DataAccess;
|
||||
using Tnb.Vengine.Domain;
|
||||
|
||||
@@ -16,150 +19,153 @@ namespace Tnb.Vengine.AppService;
|
||||
/// <summary>
|
||||
/// 视图模型服务类
|
||||
/// </summary>
|
||||
public class VmodelPageAppService : VmAppService<VmodelPage>, IVmodelPageAppService
|
||||
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 1104, KeepVerb = true)]
|
||||
[Route("api/[area]/[controller]/[action]")]
|
||||
public class VmodelPageAppService : VengineAppService<VmodelPage>, IVmodelPageAppService
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public VmodelPageAppService(IDataAccess da) : base(da)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取一条 数据信息
|
||||
/// </summary>
|
||||
public override async Task<dynamic> GetAsync(VmGetInput input)
|
||||
{
|
||||
var query = _db.Queryable<VmodelPage>().Where(a => a.deleted == 0);
|
||||
VmodelPage vm = await query.FirstAsync(a => a.id == input.id);
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
public override async Task<VmPagedOutput> GetListAsync(VmGetListInput input)
|
||||
{
|
||||
VmPagedOutput ret = new();
|
||||
var q = _db.Queryable<VmodelPage>().WhereIF(!string.IsNullOrEmpty(input.k), a => a.code.Contains(input.k!) || a.name.Contains(input.k!));
|
||||
RefAsync<int> total = 0;
|
||||
var data = await q.OrderBy(input.sort).ToPageListAsync((input.pnum - 1) * input.psize, input.psize, total);
|
||||
ret.total = total;
|
||||
ret.items = data.ConvertAll<dynamic>(a => a);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增 模型
|
||||
/// </summary>
|
||||
public override async Task<dynamic> CreateAsync(VmCreateInput input)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(input.data);
|
||||
VmodelPage vpage = input.data.Adapt<VmodelPage>();
|
||||
await _db.Insertable(vpage).ExecuteCommandAsync();
|
||||
return vpage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新 数据
|
||||
/// </summary>
|
||||
public override async Task<dynamic> UpdateAsync(VmUpdateInput input)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(input.data);
|
||||
if (!input.data.ContainsKey(nameof(VmodelPage.id)))
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public VmodelPageAppService(IDataAccess da) : base(da)
|
||||
{
|
||||
throw new Exception($"更新数据时主键({nameof(VmodelPage.id)})不可为空");
|
||||
}
|
||||
var id = input.data[nameof(VmodelPage.id)].ToString();
|
||||
var model = await _db.Queryable<VmodelPage>().FirstAsync(a => a.id == id);
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
input.data.Adapt(model, TypeAdapter.IgnoreNull);
|
||||
await _db.Updateable(model).WhereColumns(a => a.id).ExecuteCommandAsync();
|
||||
return model;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除 数据
|
||||
/// </summary>
|
||||
public override async Task<dynamic> DeleteAsync(VmDeleteInput input)
|
||||
{
|
||||
var ret = await _db.Deleteable<VmodelPage>(input.id).ExecuteCommandAsync();
|
||||
return ret;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取一条 数据信息
|
||||
/// </summary>
|
||||
public override async Task<dynamic> GetAsync(VmGetInput input)
|
||||
{
|
||||
var query = _db.Queryable<VmodelPage>().Where(a => a.deleted == 0 && a.id == input.id);
|
||||
VmodelPage vm = await query.FirstAsync();
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从数据表创建模型
|
||||
/// </summary>
|
||||
public async Task<VmodelPage> CreateByVmodel(CreatePageFromVmodelInput input)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(input.vmid);
|
||||
var vm = await _dataAccess.GetVmodelAsync(input.vmid);
|
||||
ArgumentNullException.ThrowIfNull(vm);
|
||||
/// <summary>
|
||||
/// 获取多条 数据列表
|
||||
/// </summary>
|
||||
public override async Task<VmPagedOutput> GetListAsync(VmGetListInput input)
|
||||
{
|
||||
VmPagedOutput ret = new();
|
||||
var q = _db.Queryable<VmodelPage>().WhereIF(!string.IsNullOrEmpty(input.k), a => a.code.Contains(input.k!) || a.name.Contains(input.k!));
|
||||
RefAsync<int> total = 0;
|
||||
var data = await q.OrderBy(input.sort).ToPageListAsync((input.pnum - 1) * input.psize, input.psize, total);
|
||||
ret.total = total;
|
||||
ret.items = data.ConvertAll<dynamic>(a => a);
|
||||
return ret;
|
||||
}
|
||||
|
||||
var page = await _db.Queryable<VmodelPage>().FirstAsync(a => a.vmid == vm.id);
|
||||
if (page == null)
|
||||
/// <summary>
|
||||
/// 新增 模型
|
||||
/// </summary>
|
||||
public override async Task<dynamic> CreateAsync(VmCreateInput input)
|
||||
{
|
||||
page = new VmodelPage { vmid = vm.id, code = vm.vmCode, name = vm.vmName };
|
||||
page.pageSchema = CreatePageSchema(vm, page.id);
|
||||
await _db.Insertable(page).ExecuteCommandAsync();
|
||||
ThrowIf.IsNull(input.data);
|
||||
VmodelPage vpage = input.data.Adapt<VmodelPage>();
|
||||
await _db.Insertable(vpage).ExecuteCommandAsync();
|
||||
return vpage;
|
||||
}
|
||||
else
|
||||
{
|
||||
page.pageSchema = CreatePageSchema(vm, page.id);
|
||||
await _db.Updateable(page).ExecuteCommandAsync();
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
private JObject CreatePageSchema(Vmodel vm, string pageid)
|
||||
{
|
||||
StringBuilder str = new StringBuilder();
|
||||
str.AppendLine("{");
|
||||
str.AppendLine($"\"page\": {{ \"loadList\": true, \"watchClient\": false }},");
|
||||
str.AppendLine($"\"queryData\": {{ }},");
|
||||
str.AppendLine($"\"queryForm\": {{");
|
||||
str.AppendLine($"\"show\": false,");
|
||||
str.AppendLine($"\"attr\": {{ \"labelWidth\": \"106px\", \"hasKeyword\":false }},");
|
||||
str.AppendLine($"\"cols\": {{");
|
||||
var pQuery = vm.dbProps.Skip(1).Take(1).FirstOrDefault();
|
||||
if (pQuery != null)
|
||||
/// <summary>
|
||||
/// 更新 数据
|
||||
/// </summary>
|
||||
public override async Task<dynamic> UpdateAsync(VmUpdateInput input)
|
||||
{
|
||||
str.AppendLine($"\"{pQuery.code}\": {{ \"label\": \"{pQuery.name}\", \"span\": 8, \"qtype\": 2, \"isQuick\": true, \"comp\": {{ \"type\": \"el-input\", \"attr\": {{ \"placeholder\": \"{pQuery.name}\", \"clearable\": true, \"maxlength\": 20 }} }} }}");
|
||||
ThrowIf.IsNull(input.data);
|
||||
if (!input.data.ContainsKey(nameof(VmodelPage.id)))
|
||||
{
|
||||
throw new Exception($"更新数据时主键({nameof(VmodelPage.id)})不可为空");
|
||||
}
|
||||
var id = input.data[nameof(VmodelPage.id)].ToString();
|
||||
var model = await _db.Queryable<VmodelPage>().FirstAsync(a => a.id == id);
|
||||
ThrowIf.IsNull(model, $"找不到id={id}的视图页面数据");
|
||||
input.data.Adapt(model, TypeAdapter.IgnoreNull);
|
||||
await _db.Updateable(model).WhereColumns(a => a.id).ExecuteCommandAsync();
|
||||
return model;
|
||||
}
|
||||
str.AppendLine($"}}");
|
||||
str.AppendLine($"}},");
|
||||
str.AppendLine($"\"list\": {{");
|
||||
str.AppendLine($"\"opt\": {{ \"isPage\": true, \"isCheck\": true, \"sortBy\": \"\", \"pkey\": \"{vm.GetPrimary().code}\" }},");
|
||||
str.AppendLine($"\"attr\": {{ \"border\": false }},");
|
||||
str.AppendLine($"\"cols\": {{");
|
||||
foreach (var p in vm.dbProps)
|
||||
|
||||
/// <summary>
|
||||
/// 删除 数据
|
||||
/// </summary>
|
||||
public override async Task<dynamic> DeleteAsync(VmDeleteInput input)
|
||||
{
|
||||
str.AppendLine($"\"{p.code}\":{{ \"label\": \"{p.name}\", \"show\": true, \"attr\": {{ {p.GetDefaultWidth()} }}, \"comp\": {{}} }},");
|
||||
var ret = await _db.Deleteable<VmodelPage>(input.id).ExecuteCommandAsync();
|
||||
return ret;
|
||||
}
|
||||
str.AppendLine($"}}");
|
||||
str.AppendLine($"}},");
|
||||
str.AppendLine($"\"editData\": {vm.GetDefaultDObject().ToJsonString()},");
|
||||
str.AppendLine($"\"editDlg\": {{ \"isAdd\": true, \"tabHeight\": 300, \"name\": \"{vm.vmName}\" }},");
|
||||
str.AppendLine($"\"editForm\": {{");
|
||||
str.AppendLine($"\"attr\": {{ \"labelWidth\": \"106px\" }},");
|
||||
str.AppendLine($"\"rules\": {{");
|
||||
foreach (var p in vm.dbProps.Where(a => a.required && !a.pkey))
|
||||
|
||||
/// <summary>
|
||||
/// 从数据表创建模型
|
||||
/// </summary>
|
||||
public async Task<VmodelPage> CreateByVmodel(CreatePageFromVmodelInput input)
|
||||
{
|
||||
str.AppendLine($"\"{p.code}\": [{{ \"required\": true, \"message\": \"必填项不能为空\", \"trigger\": \"blur\" }}],");
|
||||
ThrowIf.IsNull(input.vmid);
|
||||
var vm = await _dataAccess.GetVmodelAsync(input.vmid);
|
||||
ThrowIf.IsNull(vm, $"找不到id={input.vmid}的模型数据");
|
||||
|
||||
var page = await _db.Queryable<VmodelPage>().FirstAsync(a => a.vmid == vm.id);
|
||||
if (page == null)
|
||||
{
|
||||
page = new VmodelPage { vmid = vm.id, code = vm.fullCode, name = vm.vmName };
|
||||
page.pageSchema = CreatePageSchema(vm, page.id);
|
||||
await _db.Insertable(page).ExecuteCommandAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
page.code = vm.fullCode;
|
||||
page.pageSchema = CreatePageSchema(vm, page.id);
|
||||
await _db.Updateable(page).ExecuteCommandAsync();
|
||||
}
|
||||
return page;
|
||||
}
|
||||
str.AppendLine($"}},");
|
||||
str.AppendLine($"\"cols\": {{");
|
||||
foreach (var p in vm.dbProps)
|
||||
|
||||
private JObject CreatePageSchema(Vmodel vm, string pageid)
|
||||
{
|
||||
str.AppendLine($"\"{p.code}\": {{ \"label\": \"{p.name}\", \"show\": true, \"comp\": {p.GetDefaultComp().ToJsonString()} }},");
|
||||
StringBuilder str = new StringBuilder();
|
||||
str.AppendLine("{");
|
||||
str.AppendLine($"\"page\": {{ \"loadList\": true, \"watchClient\": false }},");
|
||||
str.AppendLine($"\"queryData\": {{ }},");
|
||||
str.AppendLine($"\"queryForm\": {{");
|
||||
str.AppendLine($"\"show\": false,");
|
||||
str.AppendLine($"\"attr\": {{ \"labelWidth\": \"106px\", \"hasKeyword\":false }},");
|
||||
str.AppendLine($"\"cols\": {{");
|
||||
var pQuery = vm.dbProps.Skip(1).Take(1).FirstOrDefault();
|
||||
if (pQuery != null)
|
||||
{
|
||||
str.AppendLine($"\"{pQuery.code}\": {{ \"label\": \"{pQuery.name}\", \"span\": 8, \"qtype\": 2, \"isQuick\": true, \"comp\": {{ \"type\": \"el-input\", \"attr\": {{ \"placeholder\": \"{pQuery.name}\", \"clearable\": true, \"maxlength\": 20 }} }} }}");
|
||||
}
|
||||
str.AppendLine($"}}");
|
||||
str.AppendLine($"}},");
|
||||
str.AppendLine($"\"list\": {{");
|
||||
str.AppendLine($"\"opt\": {{ \"isPage\": true, \"isCheck\": true, \"sortBy\": \"\", \"pkey\": \"{vm.GetPrimary().code}\" }},");
|
||||
str.AppendLine($"\"attr\": {{ \"border\": false }},");
|
||||
str.AppendLine($"\"cols\": {{");
|
||||
foreach (var p in vm.dbProps)
|
||||
{
|
||||
str.AppendLine($"\"{p.code}\":{{ \"label\": \"{p.name}\", \"show\": true, \"attr\": {{ {p.GetDefaultWidth()} }}, \"comp\": {{}} }},");
|
||||
}
|
||||
str.AppendLine($"}}");
|
||||
str.AppendLine($"}},");
|
||||
str.AppendLine($"\"editData\": {vm.GetDefaultDObject().ToJsonString()},");
|
||||
str.AppendLine($"\"editDlg\": {{ \"isAdd\": true, \"tabHeight\": 300, \"name\": \"{vm.vmName}\" }},");
|
||||
str.AppendLine($"\"editForm\": {{");
|
||||
str.AppendLine($"\"attr\": {{ \"labelWidth\": \"106px\" }},");
|
||||
str.AppendLine($"\"rules\": {{");
|
||||
foreach (var p in vm.dbProps.Where(a => a.required && !a.pkey))
|
||||
{
|
||||
str.AppendLine($"\"{p.code}\": [{{ \"required\": true, \"message\": \"必填项不能为空\", \"trigger\": \"blur\" }}],");
|
||||
}
|
||||
str.AppendLine($"}},");
|
||||
str.AppendLine($"\"cols\": {{");
|
||||
foreach (var p in vm.dbProps)
|
||||
{
|
||||
str.AppendLine($"\"{p.code}\": {{ \"label\": \"{p.name}\", \"show\": true, \"comp\": {p.GetDefaultComp().ToJsonString()} }},");
|
||||
}
|
||||
str.AppendLine($"}}");
|
||||
str.AppendLine($"}},");
|
||||
str.AppendLine($"\"tree\": {{ \"key\": \"id\", \"height\": 300, \"props\": {{ \"label\": \"enumName\" }}, \"data\": [] }}");
|
||||
str.AppendLine($"}}");
|
||||
var s = str.ToString();
|
||||
Console.WriteLine(s);
|
||||
return JObject.Parse(s);
|
||||
}
|
||||
str.AppendLine($"}}");
|
||||
str.AppendLine($"}},");
|
||||
str.AppendLine($"\"tree\": {{ \"key\": \"id\", \"height\": 300, \"props\": {{ \"label\": \"enumName\" }}, \"data\": [] }}");
|
||||
str.AppendLine($"}}");
|
||||
var s = str.ToString();
|
||||
Console.WriteLine(s);
|
||||
return JObject.Parse(s);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
17
visualdev/Tnb.Vengine/Atrributes/VmodelSettingAttribute.cs
Normal file
17
visualdev/Tnb.Vengine/Atrributes/VmodelSettingAttribute.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Tnb.Vengine;
|
||||
|
||||
/// <summary>
|
||||
/// 字典对象
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class VmodelSettingAttribute : Attribute
|
||||
{
|
||||
public string Area { get; set; }
|
||||
public string? Code { get; set; }
|
||||
|
||||
public VmodelSettingAttribute(string area, string? code = null)
|
||||
{
|
||||
Area = area;
|
||||
Code = code;
|
||||
}
|
||||
}
|
||||
29
visualdev/Tnb.Vengine/CodeGenerator/AcmenVmodelContext.cs
Normal file
29
visualdev/Tnb.Vengine/CodeGenerator/AcmenVmodelContext.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Tnb.Vengine.DataAccess;
|
||||
using Tnb.Vengine.Domain;
|
||||
|
||||
namespace Tnb.Vengine;
|
||||
|
||||
/// <summary>
|
||||
/// 代码生成AppService的数据上下文
|
||||
/// </summary>
|
||||
public class AcmenVmodelContext : TemplateContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public AcmenVmodelContext(IDataAccess da, Vmodel vm) : base(vm.areaCode)
|
||||
{
|
||||
_da = da;
|
||||
Vm = vm;
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public IDataAccess _da { get; set; }
|
||||
public Vmodel Vm { get; }
|
||||
|
||||
public string NsPrefix { get { return "Tnb"; } }
|
||||
|
||||
}
|
||||
|
||||
|
||||
123
visualdev/Tnb.Vengine/CodeGenerator/CodeHelper.cs
Normal file
123
visualdev/Tnb.Vengine/CodeGenerator/CodeHelper.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Tnb.Vengine;
|
||||
|
||||
public class CodeHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 从当前目录往上查找解决方案sln文件所在的目录
|
||||
/// </summary>
|
||||
public static string? GetSolutionDirectoryPath(bool includeParent = false)
|
||||
{
|
||||
var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
|
||||
|
||||
while (Directory.GetParent(currentDirectory.FullName) != null)
|
||||
{
|
||||
currentDirectory = Directory.GetParent(currentDirectory.FullName);
|
||||
if (currentDirectory == null) return null;
|
||||
|
||||
if (Directory.GetFiles(currentDirectory!.FullName).FirstOrDefault(f => f.EndsWith(".sln")) != null)
|
||||
{
|
||||
if (includeParent && currentDirectory.Parent != null) return currentDirectory.Parent.FullName;
|
||||
else return currentDirectory.FullName;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存代码到文件
|
||||
/// </summary>
|
||||
public static void SaveCodeToFile(string code, string saveTo)
|
||||
{
|
||||
var dir = Path.GetDirectoryName(saveTo);
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
Directory.CreateDirectory(dir!);
|
||||
}
|
||||
code = System.Web.HttpUtility.HtmlDecode(code);
|
||||
File.WriteAllText(saveTo, code);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MD5加密
|
||||
/// </summary>
|
||||
/// <param name="szValue"></param>
|
||||
/// <returns></returns>
|
||||
public static string String2MD5(string szValue)
|
||||
{
|
||||
string pwd = string.Empty;
|
||||
MD5 md5 = MD5.Create();
|
||||
byte[] byt = md5.ComputeHash(Encoding.Unicode.GetBytes(szValue));
|
||||
for (int i = 0; i < byt.Length; i++)
|
||||
{
|
||||
pwd += byt[i].ToString("x"); //16进制
|
||||
}
|
||||
char[] arr = pwd.ToCharArray();
|
||||
Array.Reverse(arr);
|
||||
return new string(arr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// newtonsoft json 转小驼峰
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToCamelCase(string s)
|
||||
{
|
||||
if (string.IsNullOrEmpty(s) || !char.IsUpper(s[0]))
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
char[] array = s.ToCharArray();
|
||||
for (int i = 0; i < array.Length && (i != 1 || char.IsUpper(array[i])); i++)
|
||||
{
|
||||
bool flag = i + 1 < array.Length;
|
||||
if (i > 0 && flag && !char.IsUpper(array[i + 1]))
|
||||
{
|
||||
if (char.IsSeparator(array[i + 1]))
|
||||
{
|
||||
array[i] = char.ToLower(array[i], CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
array[i] = char.ToLower(array[i], CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
return new string(array);
|
||||
}
|
||||
|
||||
public static string LengthToString(int strLength)
|
||||
{
|
||||
var l = "DbConsts.";
|
||||
if (strLength <= 0) { l += nameof(DbConsts.LengthText); }
|
||||
else if (strLength <= DbConsts.LengthXXS) { l += nameof(DbConsts.LengthXXS); }
|
||||
else if (strLength <= DbConsts.LengthXS) { l += nameof(DbConsts.LengthXS); }
|
||||
else if (strLength <= DbConsts.LengthS) { l += nameof(DbConsts.LengthS); }
|
||||
else if (strLength <= DbConsts.LengthM) { l += nameof(DbConsts.LengthM); }
|
||||
else if (strLength <= DbConsts.LengthL) { l += nameof(DbConsts.LengthL); }
|
||||
else if (strLength <= DbConsts.LengthXL) { l += nameof(DbConsts.LengthXL); }
|
||||
else if (strLength <= DbConsts.LengthXXL) { l += nameof(DbConsts.LengthXXL); }
|
||||
else if (strLength <= DbConsts.LengthXXXL) { l += nameof(DbConsts.LengthXXXL); }
|
||||
else { l += nameof(DbConsts.LengthXXXXL); }
|
||||
return l;
|
||||
}
|
||||
|
||||
public static int PropDefineToLength(string? propDefine)
|
||||
{
|
||||
float.TryParse(propDefine, out float f);
|
||||
return (int)f;
|
||||
}
|
||||
public static int PropDefineToScale(string? propDefine)
|
||||
{
|
||||
float.TryParse(propDefine, out float f);
|
||||
return (int)(f * 10) % 10;
|
||||
}
|
||||
}
|
||||
77
visualdev/Tnb.Vengine/CodeGenerator/TemplateContext.cs
Normal file
77
visualdev/Tnb.Vengine/CodeGenerator/TemplateContext.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Tnb.Core;
|
||||
|
||||
namespace Tnb.Vengine;
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class TemplateContext
|
||||
{
|
||||
#region 模板文件名,同时作为模板Key
|
||||
public const string KeyAppService = "AppService";
|
||||
public const string KeyDto = "AppServiceDto";
|
||||
public const string KeyEntity = "EntityInfo";
|
||||
public const string KeyEntityDto = "EntityDto";
|
||||
public const string KeyEntityAppService = "EntityController";
|
||||
public const string KeyEntityPageVue = "EntityPageVue";
|
||||
public const string KeyTableForeigns = "TableForeigns";
|
||||
public const string KeyVmodel = "Vmodel";
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public TemplateContext(string moduleCode)
|
||||
{
|
||||
#if DEBUG
|
||||
BasePath = CodeHelper.GetSolutionDirectoryPath(false)!;
|
||||
#else
|
||||
BasePath = EApp.Options.App.AcmenBasePath;
|
||||
#endif
|
||||
ModuleCode = moduleCode;
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string BasePath { get; set; }
|
||||
private string ModuleCode { get; }
|
||||
|
||||
private string ModulePath => Path.Combine(BasePath, $"{ModuleCode}.Acmen");
|
||||
private string UiProjPath => Path.Combine(BasePath, "ItMgrWeb");
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string AppServicePath => Path.Combine(ModulePath, "AppServices");
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string PagePath => Path.Combine(UiProjPath, "src", "views");
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
public string GetCsName(string name)
|
||||
{
|
||||
name = Regex.Replace(name.TrimStart('@', '.'), @"[^\w]", "_");
|
||||
name = char.IsLetter(name, 0) ? name : string.Concat("_", name);
|
||||
IEnumerable<string> arrs = name.Split('_');
|
||||
//if (arrs.Count() > 1 && Config.IsRemovePrefix) { arrs = arrs.Skip(1); }
|
||||
//if (Config.IsSnakeCaseToPascalCase) { arrs = arrs.Select(a => a.ToPascalCase()); }
|
||||
if (arrs.Count() > 0) { arrs = arrs.Select(a => a.ToPascal()); }
|
||||
|
||||
return string.Join("", arrs);
|
||||
}
|
||||
}
|
||||
|
||||
public enum eLambdaType
|
||||
{
|
||||
Join,
|
||||
ManyToMany,
|
||||
Where,
|
||||
MemberBinding,
|
||||
OneToMany
|
||||
}
|
||||
@@ -4,4 +4,5 @@ public class ModuleConst
|
||||
{
|
||||
public const string Tag = "Tnb";
|
||||
public const string Area = "tnb";
|
||||
public const string NsPrefix = "Tnb";
|
||||
}
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
using JNPF;
|
||||
using JNPF;
|
||||
using JNPF.Common.Core.Manager;
|
||||
using JNPF.DependencyInjection;
|
||||
using Mapster;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using SqlSugar;
|
||||
using System.Collections.Concurrent;
|
||||
using Tnb.Core;
|
||||
using Tnb.Vengine.Domain;
|
||||
|
||||
namespace Tnb.Vengine.DataAccess;
|
||||
@@ -17,406 +14,415 @@ namespace Tnb.Vengine.DataAccess;
|
||||
/// </summary>
|
||||
public class DataAccess : IDataAccess, ITransient, IDisposable
|
||||
{
|
||||
const int MAX_PAGE_SIZE = 1000;
|
||||
private ISqlSugarClient? sugar;
|
||||
protected ISqlSugarClient Db
|
||||
{
|
||||
get
|
||||
const int MAX_PAGE_SIZE = 1000;
|
||||
private ISqlSugarClient? _db;
|
||||
protected ISqlSugarClient Db
|
||||
{
|
||||
if (sugar == null)
|
||||
{
|
||||
ConnectionStringsOptions conn = App.GetConfig<ConnectionStringsOptions>("ConnectionStrings", true);
|
||||
//var DBType = (DbType)Enum.Parse(typeof(DbType), conn.DBType);
|
||||
sugar = new SqlSugarScope(new ConnectionConfig
|
||||
get
|
||||
{
|
||||
ConnectionString = conn.ConnectString,
|
||||
DbType = conn.DBType.Adapt<DbType>(),
|
||||
IsAutoCloseConnection = true,
|
||||
ConfigId = conn.ConfigId,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
MoreSettings = new ConnMoreSettings()
|
||||
{
|
||||
IsAutoRemoveDataCache = true, // 自动清理缓存
|
||||
IsAutoToUpper = false,
|
||||
PgSqlIsAutoToLower = false,
|
||||
DisableNvarchar = true
|
||||
},
|
||||
}, SugarHelper.ConfigSugar);
|
||||
}
|
||||
return sugar;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 全局缓存
|
||||
/// </summary>
|
||||
static ConcurrentDictionary<string, ISqlSugarClient> DbCache = new ConcurrentDictionary<string, ISqlSugarClient>();
|
||||
|
||||
/// <summary>
|
||||
/// 构造
|
||||
/// </summary>
|
||||
public DataAccess()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var item in DbCache)
|
||||
{
|
||||
item.Value.Dispose();
|
||||
}
|
||||
DbCache.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 ISqlSugarClient
|
||||
/// </summary>
|
||||
public ISqlSugarClient GetSqlSugar(string? dbCode = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(dbCode) || dbCode == DbConsts.DefaultDbCode)
|
||||
{
|
||||
return Db;
|
||||
}
|
||||
if (DbCache.ContainsKey(dbCode))
|
||||
{
|
||||
return DbCache[dbCode];
|
||||
}
|
||||
|
||||
var dblink = GetVmLink(dbCode);
|
||||
if (dblink == null)
|
||||
{
|
||||
throw new Exception($"没有此数据库{dbCode}连接信息");
|
||||
}
|
||||
var dbType = Enum.Parse<DbType>(dblink.dbType.ToString(), true);
|
||||
var sugar = new SqlSugarScope(new ConnectionConfig
|
||||
{
|
||||
ConnectionString = dblink.dbConnection,
|
||||
DbType = dbType,
|
||||
IsAutoCloseConnection = true,
|
||||
ConfigId = dblink.dbCode,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
MoreSettings = new ConnMoreSettings()
|
||||
{
|
||||
IsAutoRemoveDataCache = true, // 自动清理缓存
|
||||
IsAutoToUpper = false,
|
||||
PgSqlIsAutoToLower = false,
|
||||
DisableNvarchar = true
|
||||
},
|
||||
}, SugarHelper.ConfigSugar);
|
||||
if (sugar.Ado.IsValidConnection())
|
||||
{
|
||||
DbCache[dbCode] = sugar;
|
||||
}
|
||||
else
|
||||
{
|
||||
sugar.Dispose();
|
||||
throw new Exception($"无法连接到数据库{dbCode}");
|
||||
}
|
||||
return DbCache[dbCode];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 DbLink
|
||||
/// </summary>
|
||||
public VmodelLink GetVmLink(string dbCode)
|
||||
{
|
||||
var model = Db.Queryable<VmodelLink>().First(a => a.dbCode == dbCode);
|
||||
return model;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时不抛异常
|
||||
/// </summary>
|
||||
public async Task<Vmodel?> TryGetVmodelAsync(string id, bool loadNavigate = false)
|
||||
{
|
||||
Vmodel vm = await Db.Queryable<Vmodel>().FirstAsync(a => a.id == id && a.deleted == 0);
|
||||
if (vm != null && loadNavigate)
|
||||
{
|
||||
await LoadVmodelNavigateAsync(vm);
|
||||
}
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时抛异常
|
||||
/// </summary>
|
||||
public async Task<Vmodel> GetVmodelAsync(string id, bool loadNavigate = false)
|
||||
{
|
||||
Vmodel vm = await Db.Queryable<Vmodel>().FirstAsync(a => a.id == id && a.deleted == 0);
|
||||
ArgumentNullException.ThrowIfNull(vm, $"找不到vmid={id}的模型");
|
||||
if (loadNavigate)
|
||||
{
|
||||
await LoadVmodelNavigateAsync(vm);
|
||||
}
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时不抛异常
|
||||
/// </summary>
|
||||
public async Task<Vmodel?> TryGetVmodelAsync(string area, string vmCode, bool loadNavigate = false)
|
||||
{
|
||||
Vmodel vm = await Db.Queryable<Vmodel>().FirstAsync(a => a.area == area && a.vmCode == vmCode && a.deleted == 0);
|
||||
if (vm != null && loadNavigate)
|
||||
{
|
||||
await LoadVmodelNavigateAsync(vm);
|
||||
}
|
||||
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时抛异常
|
||||
/// </summary>
|
||||
public async Task<Vmodel> GetVmodelAsync(string area, string vmCode, bool loadNavigate = false)
|
||||
{
|
||||
Vmodel vm = await Db.Queryable<Vmodel>().FirstAsync(a => a.area == area && a.vmCode == vmCode && a.deleted == 0);
|
||||
ArgumentNullException.ThrowIfNull(vm, $"找不到area={area}, vmCode={vmCode}的模型");
|
||||
if (loadNavigate)
|
||||
{
|
||||
await LoadVmodelNavigateAsync(vm);
|
||||
}
|
||||
|
||||
return vm;
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// 获取 Vmodel
|
||||
///// </summary>
|
||||
//public async Task<Vmodel?> GetVmodelAsync(string tableName, string? dbCode)
|
||||
//{
|
||||
// Vmodel vm = await _db.Queryable<Vmodel>().FirstAsync(a => a.tableName == tableName && a.dbCode == dbCode && a.deleted == 0);
|
||||
// return vm;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 加载模型的导航属性
|
||||
/// </summary>
|
||||
/// <param name="vm"></param>
|
||||
/// <returns></returns>
|
||||
private async Task LoadVmodelNavigateAsync(Vmodel vm)
|
||||
{
|
||||
Dictionary<string, Vmodel> dictVm = new();
|
||||
foreach (var navProp in vm.navProps)
|
||||
{
|
||||
if (!dictVm.ContainsKey(navProp.vmid))
|
||||
{
|
||||
var navModel = await GetVmodelAsync(navProp.vmid);
|
||||
dictVm.Add(navProp.vmid, navModel);
|
||||
}
|
||||
navProp.naviModel = dictVm[navProp.vmid];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询数据 默认方法
|
||||
/// </summary>
|
||||
public async Task<VmPagedOutput> QueryDataAsync(Vmodel vm, VmQueryInput input)
|
||||
{
|
||||
ISqlSugarClient db = GetSqlSugar(vm.dbCode);
|
||||
var query = db.Queryable<object>().AS(vm.tableName, VmSelectProp.MAIN_ALIES);
|
||||
var selProps = vm.GetVmSelectProps(input.o);
|
||||
//处理导航属性联表
|
||||
List<JoinInfoParameter> joins = vm.GetJoinInfos(selProps);
|
||||
query.AddJoinInfo(joins);
|
||||
List<IConditionalModel> wheres = vm.GetConditionalModels(input.q);
|
||||
if (!string.IsNullOrEmpty(input.k))
|
||||
{
|
||||
var lsCondition = new List<KeyValuePair<WhereType, ConditionalModel>>();
|
||||
var wType = WhereType.And;
|
||||
foreach (var prop in vm.dbProps.Where(a => a.fuzzy))
|
||||
{
|
||||
lsCondition.Add(new(wType, new ConditionalModel() { FieldName = prop.field, ConditionalType = ConditionalType.Like, FieldValue = input.k }));
|
||||
wType = WhereType.Or;
|
||||
}
|
||||
wheres.Add(new ConditionalCollections() { ConditionalList = lsCondition });
|
||||
}
|
||||
//处理查询参数
|
||||
query.Where(wheres);
|
||||
if (!string.IsNullOrEmpty(input.sort))
|
||||
{
|
||||
query.OrderBy(input.sort);
|
||||
}
|
||||
//处理输出字段
|
||||
List<SelectModel> selects = vm.GetSelectModels(selProps);
|
||||
query.Select(selects);
|
||||
//查询数据
|
||||
VmPagedOutput result = new();
|
||||
List<Dictionary<string, object>> ls = new();
|
||||
int skip = input.pnum > 0 ? (input.pnum - 1) * input.psize : 0;
|
||||
int take = input.psize == 0 ? MAX_PAGE_SIZE : input.psize;
|
||||
if (input.pnum > 0) { result.total = await query.CountAsync(); }
|
||||
ls = await query.Skip(skip).Take(take).ToDictionaryListAsync();
|
||||
//组装输出对象
|
||||
foreach (var data in ls)
|
||||
{
|
||||
DObject ret = await NestedOutputAsync(vm, data, selProps);
|
||||
result.items.Add(ret);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 组装子模型对象
|
||||
/// </summary>
|
||||
/// <param name="vm"></param>
|
||||
/// <param name="src"></param>
|
||||
/// <param name="selProps"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<DObject> NestedOutputAsync(Vmodel vm, Dictionary<string, object> src, List<VmSelectProp> selProps)
|
||||
{
|
||||
DObject ret = new();
|
||||
foreach (var prop in selProps)
|
||||
{
|
||||
if (prop.navType == eNavigateType.None || prop.navCode == VmSelectProp.MAIN_ALIES)
|
||||
{
|
||||
if (src.ContainsKey(prop.code))
|
||||
{
|
||||
ret.Add(prop.code, src[prop.code]);
|
||||
if (_db == null)
|
||||
{
|
||||
var dbType = App.Configuration.GetConnectionString("DbType");
|
||||
var dbConn = App.Configuration.GetConnectionString("DbConn");
|
||||
_db = SugarHelper.CreateSugarClient("_db_default_", dbType, dbConn);
|
||||
}
|
||||
return _db;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (prop.navType == eNavigateType.OneToOne)
|
||||
{
|
||||
var key = prop.navCode + "_" + prop.code;
|
||||
ret.Add(key, src[key]);
|
||||
//if (!ret.ContainsKey(prop.navCode))
|
||||
//{
|
||||
// ret.Add(prop.navCode, new DObject());
|
||||
//}
|
||||
//var key = prop.navCode + "_" + prop.code;
|
||||
//if (src.ContainsKey(key))
|
||||
//{
|
||||
// ((DObject)ret[prop.navCode]).Add(prop.code, src[key]);
|
||||
//}
|
||||
}
|
||||
else if (prop.navType == eNavigateType.OneToMany)
|
||||
{
|
||||
if (!ret.ContainsKey(prop.navCode))
|
||||
{
|
||||
ret.Add(prop.navCode, new List<DObject>());
|
||||
}
|
||||
var navProp = vm.navProps.First(a => a.code == prop.navCode);
|
||||
if (navProp != null && navProp.naviModel != null && src.ContainsKey(navProp.refField))
|
||||
{
|
||||
VmQueryInput input = new VmQueryInput();
|
||||
input.q = new DObject(navProp.refField, src[navProp.refField]);
|
||||
input.o = string.Join(',', selProps.Where(a => a.navCode == prop.navCode).Select(a => a.code));
|
||||
ret[prop.navCode] = (await QueryDataAsync(navProp.naviModel, input)).items;
|
||||
}
|
||||
}
|
||||
else if (prop.navType == eNavigateType.ManyToMany)
|
||||
{
|
||||
if (!ret.ContainsKey(prop.navCode))
|
||||
{
|
||||
ret.Add(prop.navCode, new List<DObject>());
|
||||
}
|
||||
}
|
||||
|
||||
private readonly IUserManager _user;
|
||||
|
||||
/// <summary>
|
||||
/// 全局缓存
|
||||
/// </summary>
|
||||
static ConcurrentDictionary<string, ISqlSugarClient> DbCache = new ConcurrentDictionary<string, ISqlSugarClient>();
|
||||
|
||||
/// <summary>
|
||||
/// 构造
|
||||
/// </summary>
|
||||
public DataAccess(IUserManager currentUser)
|
||||
{
|
||||
_user = currentUser;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var item in DbCache)
|
||||
{
|
||||
item.Value.Dispose();
|
||||
}
|
||||
DbCache.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 ISqlSugarClient
|
||||
/// </summary>
|
||||
public ISqlSugarClient GetSqlSugar(string? dbCode = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(dbCode) || dbCode == DbConsts.DefaultDbCode)
|
||||
{
|
||||
return Db;
|
||||
}
|
||||
if (DbCache.ContainsKey(dbCode))
|
||||
{
|
||||
return DbCache[dbCode];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增数据 默认方法
|
||||
/// </summary>
|
||||
public async Task<dynamic> CreateDataAsync(Vmodel vm, VmCreateInput input)
|
||||
{
|
||||
ISqlSugarClient db = GetSqlSugar(vm.dbCode);
|
||||
int num = 0;
|
||||
if (input.data != null)
|
||||
{
|
||||
var model = vm.PropToField(input.data);
|
||||
num = await db.Insertable(model).AS(vm.tableName).ExecuteCommandAsync();
|
||||
return input.data;
|
||||
}
|
||||
else if (input.items != null)
|
||||
{
|
||||
List<DObject> lst = new List<DObject>();
|
||||
foreach (var item in input.items)
|
||||
{
|
||||
lst.Add(vm.PropToField(item));
|
||||
}
|
||||
num = await db.Insertable(lst).AS(vm.tableName).ExecuteCommandAsync();
|
||||
return input.items;
|
||||
}
|
||||
else
|
||||
{
|
||||
input.data = vm.GetDefaultDObject();
|
||||
var model = vm.PropToField(input.data);
|
||||
num = await db.Insertable(model).AS(vm.tableName).ExecuteCommandAsync();
|
||||
return input.data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新数据 默认方法
|
||||
/// </summary>
|
||||
public async Task<dynamic> UpdateDataAsync(Vmodel vm, VmUpdateInput input)
|
||||
{
|
||||
ISqlSugarClient db = GetSqlSugar(vm.dbCode);
|
||||
var pk = vm.GetPrimary();
|
||||
int num = 0;
|
||||
if (input.data != null)
|
||||
{
|
||||
var model = vm.PropToField(input.data);
|
||||
if (!model.ContainsKey(pk.field))
|
||||
{
|
||||
throw new Exception($"更新数据时主键({pk.code})不可为空");
|
||||
}
|
||||
//if (!model.ContainsKey(pk.field) && input.id != null)
|
||||
//{
|
||||
// model.Add(pk.field, input.id);
|
||||
//}
|
||||
num = await db.Updateable(model).AS(vm.tableName).WhereColumns(pk.field).ExecuteCommandAsync();
|
||||
}
|
||||
else if (input.items != null)
|
||||
{
|
||||
List<DObject> lst = new();
|
||||
foreach (var item in input.items)
|
||||
{
|
||||
var model = vm.PropToField(item);
|
||||
if (model.ContainsKey(pk.field))
|
||||
var dblink = GetVmodelLink(dbCode);
|
||||
ThrowIf.IsNull(dblink, $"没有此数据库{dbCode}连接信息");
|
||||
var sugar = SugarHelper.CreateSugarClient(dblink.dbCode, dblink.dbType, dblink.dbConnection);
|
||||
if (sugar.Ado.IsValidConnection())
|
||||
{
|
||||
lst.Add(model);
|
||||
DbCache[dbCode] = sugar;
|
||||
}
|
||||
}
|
||||
num = await db.Updateable(lst).AS(vm.tableName).WhereColumns(pk.field).ExecuteCommandAsync();
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除数据 默认方法
|
||||
/// </summary>
|
||||
public async Task<int> DeleteDataAsync(Vmodel vm, VmDeleteInput input)
|
||||
{
|
||||
ISqlSugarClient db = GetSqlSugar(vm.dbCode);
|
||||
var pk = vm.GetPrimary();
|
||||
int num = 0;
|
||||
List<Dictionary<string, object>> ids = new();
|
||||
if (input.id != null)
|
||||
{
|
||||
ids.Add(new DObject(pk.field, input.id));
|
||||
}
|
||||
else if (input.ids != null)
|
||||
{
|
||||
ids.AddRange(input.ids.Select(a => new DObject(pk.field, a)));
|
||||
}
|
||||
if (ids.Count > 0)
|
||||
{
|
||||
num = await db.Deleteable<object>().AS(vm.tableName).WhereColumns(ids).ExecuteCommandAsync();
|
||||
else
|
||||
{
|
||||
sugar.Dispose();
|
||||
throw new Exception($"无法连接到数据库{dbCode}");
|
||||
}
|
||||
return DbCache[dbCode];
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取 DbLink
|
||||
/// </summary>
|
||||
public VmodelLink GetVmodelLink(string dbCode)
|
||||
{
|
||||
var model = Db.Queryable<VmodelLink>().First(a => a.dbCode == dbCode);
|
||||
return model;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时不抛异常
|
||||
/// </summary>
|
||||
public async Task<Vmodel?> TryGetVmodelAsync(string id, bool loadNavigate = false)
|
||||
{
|
||||
Vmodel vm = await Db.Queryable<Vmodel>().FirstAsync(a => a.id == id && a.deleted == 0);
|
||||
if (vm != null && loadNavigate)
|
||||
{
|
||||
await LoadVmodelNavigateAsync(vm);
|
||||
}
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时抛异常
|
||||
/// </summary>
|
||||
public async Task<Vmodel> GetVmodelAsync(string id, bool loadNavigate = false)
|
||||
{
|
||||
Vmodel vm = await Db.Queryable<Vmodel>().FirstAsync(a => a.id == id && a.deleted == 0);
|
||||
ThrowIf.IsNull(vm, $"找不到id={id}的模型");
|
||||
if (loadNavigate)
|
||||
{
|
||||
await LoadVmodelNavigateAsync(vm);
|
||||
}
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时不抛异常
|
||||
/// </summary>
|
||||
public async Task<Vmodel?> TryGetVmodelAsync(string areaCode, string vmCode, bool loadNavigate = false)
|
||||
{
|
||||
Vmodel vm = await Db.Queryable<Vmodel>().FirstAsync(a => a.areaCode == areaCode && a.vmCode == vmCode && a.deleted == 0);
|
||||
if (vm != null && loadNavigate)
|
||||
{
|
||||
await LoadVmodelNavigateAsync(vm);
|
||||
}
|
||||
|
||||
return vm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时抛异常
|
||||
/// </summary>
|
||||
public async Task<Vmodel> GetVmodelAsync(string areaCode, string vmCode, bool loadNavigate = false)
|
||||
{
|
||||
Vmodel vm = await Db.Queryable<Vmodel>().FirstAsync(a => a.areaCode == areaCode && a.vmCode == vmCode && a.deleted == 0);
|
||||
ThrowIf.IsNull(vm, $"找不到areaCode={areaCode}, vmCode={vmCode}的模型");
|
||||
if (loadNavigate)
|
||||
{
|
||||
await LoadVmodelNavigateAsync(vm);
|
||||
}
|
||||
|
||||
return vm;
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// 获取 Vmodel
|
||||
///// </summary>
|
||||
//public async Task<Vmodel?> GetVmodelAsync(string tableName, string? dbCode)
|
||||
//{
|
||||
// Vmodel vm = await _db.Queryable<Vmodel>().FirstAsync(a => a.tableName == tableName && a.dbCode == dbCode && a.deleted == 0);
|
||||
// return vm;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 加载模型的导航属性
|
||||
/// </summary>
|
||||
/// <param name="vm"></param>
|
||||
/// <returns></returns>
|
||||
private async Task LoadVmodelNavigateAsync(Vmodel vm)
|
||||
{
|
||||
Dictionary<string, Vmodel> dictVm = new();
|
||||
foreach (var navProp in vm.navProps)
|
||||
{
|
||||
if (!dictVm.ContainsKey(navProp.vmid))
|
||||
{
|
||||
var navModel = await GetVmodelAsync(navProp.vmid);
|
||||
dictVm.Add(navProp.vmid, navModel);
|
||||
}
|
||||
navProp.naviModel = dictVm[navProp.vmid];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询数据 默认方法
|
||||
/// </summary>
|
||||
public async Task<VmPagedOutput> QueryDataAsync(Vmodel vm, VmQueryInput input)
|
||||
{
|
||||
ISqlSugarClient db = GetSqlSugar(vm.dbCode);
|
||||
var query = db.Queryable<object>().AS(vm.tableName, VmSelectProp.MAIN_ALIES);
|
||||
var selProps = vm.GetVmSelectProps(input.o);
|
||||
//处理导航属性联表
|
||||
List<JoinInfoParameter> joins = vm.GetJoinInfos(selProps);
|
||||
query.AddJoinInfo(joins);
|
||||
//if (joins.Count > 0)
|
||||
//{
|
||||
//}
|
||||
List<IConditionalModel> wheres = vm.GetConditionalModels(input.q);
|
||||
if (!string.IsNullOrEmpty(input.k))
|
||||
{
|
||||
var lsCondition = new List<KeyValuePair<WhereType, ConditionalModel>>();
|
||||
var wType = WhereType.And;
|
||||
foreach (var prop in vm.dbProps.Where(a => a.fuzzy))
|
||||
{
|
||||
lsCondition.Add(new(wType, new ConditionalModel() { FieldName = prop.field, ConditionalType = ConditionalType.Like, FieldValue = input.k, CSharpTypeName = prop.csType }));
|
||||
wType = WhereType.Or;
|
||||
}
|
||||
wheres.Add(new ConditionalCollections() { ConditionalList = lsCondition });
|
||||
}
|
||||
//处理查询参数
|
||||
query.Where(wheres);
|
||||
//if (wheres.Count > 0)
|
||||
//{
|
||||
//}
|
||||
if (!string.IsNullOrEmpty(input.sort))
|
||||
{
|
||||
query.OrderBy(input.sort);
|
||||
}
|
||||
//处理输出字段
|
||||
List<SelectModel> selects = vm.GetSelectModels(selProps);
|
||||
query.Select(selects);
|
||||
//查询数据
|
||||
VmPagedOutput result = new();
|
||||
List<Dictionary<string, object>> ls = new();
|
||||
int skip = input.pnum > 0 ? (input.pnum - 1) * input.psize : 0;
|
||||
int take = input.psize == 0 ? MAX_PAGE_SIZE : input.psize;
|
||||
if (input.pnum > 0) { result.total = await query.CountAsync(); }
|
||||
ls = await query.Skip(skip).Take(take).ToDictionaryListAsync();
|
||||
//组装输出对象
|
||||
foreach (var data in ls)
|
||||
{
|
||||
DObject ret = await NestedOutputAsync(vm, new DObject(data), selProps);
|
||||
result.items.Add(ret);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 组装子模型对象
|
||||
/// </summary>
|
||||
/// <param name="vm"></param>
|
||||
/// <param name="src"></param>
|
||||
/// <param name="selProps"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<DObject> NestedOutputAsync(Vmodel vm, DObject src, List<VmSelectProp> selProps)
|
||||
{
|
||||
DObject ret = new();
|
||||
foreach (var prop in selProps)
|
||||
{
|
||||
if (prop.navType == eNavigateType.None || prop.navCode == VmSelectProp.MAIN_ALIES)
|
||||
{
|
||||
if (src.ContainsKey(prop.code))
|
||||
{
|
||||
ret.Add(prop.code, src[prop.code]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (prop.navType == eNavigateType.OneToOne)
|
||||
{
|
||||
var key = prop.navCode + "_" + prop.code;
|
||||
ret.Add(key, src[key]);
|
||||
//if (!ret.ContainsKey(prop.navCode))
|
||||
//{
|
||||
// ret.Add(prop.navCode, new DObject());
|
||||
//}
|
||||
//var key = prop.navCode + "_" + prop.code;
|
||||
//if (src.ContainsKey(key))
|
||||
//{
|
||||
// ((DObject)ret[prop.navCode]).Add(prop.code, src[key]);
|
||||
//}
|
||||
}
|
||||
else if (prop.navType == eNavigateType.OneToMany)
|
||||
{
|
||||
if (!ret.ContainsKey(prop.navCode))
|
||||
{
|
||||
ret.Add(prop.navCode, new List<DObject>());
|
||||
}
|
||||
var navProp = vm.navProps.First(a => a.code == prop.navCode);
|
||||
if (navProp != null && navProp.naviModel != null && src.ContainsKey(navProp.refField))
|
||||
{
|
||||
VmQueryInput input = new VmQueryInput();
|
||||
input.q = new DObject(navProp.refField, src[navProp.refField]);
|
||||
input.o = string.Join(',', selProps.Where(a => a.navCode == prop.navCode).Select(a => a.code));
|
||||
ret[prop.navCode] = (await QueryDataAsync(navProp.naviModel, input)).items;
|
||||
}
|
||||
}
|
||||
else if (prop.navType == eNavigateType.ManyToMany)
|
||||
{
|
||||
if (!ret.ContainsKey(prop.navCode))
|
||||
{
|
||||
ret.Add(prop.navCode, new List<DObject>());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增数据 默认方法
|
||||
/// </summary>
|
||||
public async Task<dynamic> CreateDataAsync(Vmodel vm, VmCreateInput input)
|
||||
{
|
||||
ISqlSugarClient db = GetSqlSugar(vm.dbCode);
|
||||
ThrowIf.When(input.data == null && input.items == null, "新增数据时,data和items不可同时为空");
|
||||
//新增一条数据
|
||||
if (input.data != null)
|
||||
{
|
||||
var pkey = vm.GetPrimary();
|
||||
var model = vm.ToCreateEntity(input.data, _user);
|
||||
if (pkey.csType == "int" || pkey.csType == "long")
|
||||
{
|
||||
var id = await db.Insertable(model).AS(vm.tableName).ExecuteReturnBigIdentityAsync();
|
||||
if ((long)input.data[pkey.code] != id) input.data[pkey.code] = id;
|
||||
}
|
||||
else
|
||||
{
|
||||
await db.Insertable(model).AS(vm.tableName).ExecuteCommandAsync();
|
||||
}
|
||||
return input.data;
|
||||
}
|
||||
//批量新增数据
|
||||
else
|
||||
{
|
||||
var pkey = vm.GetPrimary();
|
||||
List<DObject> lst = new List<DObject>();
|
||||
foreach (var item in input.items!)
|
||||
{
|
||||
lst.Add(vm.ToCreateEntity(item, _user));
|
||||
}
|
||||
if (pkey.csType == "int")
|
||||
{
|
||||
var ids = await db.Insertable(lst).AS(vm.tableName).ExecuteReturnPkListAsync<int>();
|
||||
for (int i = 0; i < input.items.Count; i++)
|
||||
{
|
||||
input.items[i][pkey.code] = ids[i];
|
||||
}
|
||||
}
|
||||
else if (pkey.csType == "long")
|
||||
{
|
||||
var ids = await db.Insertable(lst).AS(vm.tableName).ExecuteReturnPkListAsync<long>();
|
||||
for (int i = 0; i < input.items.Count; i++)
|
||||
{
|
||||
input.items[i][pkey.code] = ids[i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await db.Insertable(lst).AS(vm.tableName).ExecuteCommandAsync();
|
||||
}
|
||||
return input.items;
|
||||
}
|
||||
//else
|
||||
//{
|
||||
// input.data = vm.GetDefaultDObject();
|
||||
// var model = vm.PropToField(input.data);
|
||||
// num = await db.Insertable(model).AS(vm.tableName).ExecuteCommandAsync();
|
||||
// return input.data;
|
||||
//}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新数据 默认方法
|
||||
/// </summary>
|
||||
public async Task<dynamic> UpdateDataAsync(Vmodel vm, VmUpdateInput input)
|
||||
{
|
||||
ISqlSugarClient db = GetSqlSugar(vm.dbCode);
|
||||
var pk = vm.GetPrimary();
|
||||
int num = 0;
|
||||
//修改一条数据
|
||||
if (input.data != null)
|
||||
{
|
||||
var model = vm.ToUpdateEntity(input.data, _user);
|
||||
if (!model.ContainsKey(pk.field))
|
||||
{
|
||||
throw new Exception($"更新数据时主键({pk.code})不可为空");
|
||||
}
|
||||
//if (!model.ContainsKey(pk.field) && input.id != null)
|
||||
//{
|
||||
// model.Add(pk.field, input.id);
|
||||
//}
|
||||
num = await db.Updateable(model).AS(vm.tableName).WhereColumns(pk.field).ExecuteCommandAsync();
|
||||
}
|
||||
//批量修改数据
|
||||
else if (input.items != null)
|
||||
{
|
||||
List<DObject> lst = new();
|
||||
foreach (var item in input.items)
|
||||
{
|
||||
var model = vm.ToUpdateEntity(item, _user);
|
||||
if (model.ContainsKey(pk.field))
|
||||
{
|
||||
lst.Add(model);
|
||||
}
|
||||
}
|
||||
num = await db.Updateable(lst).AS(vm.tableName).WhereColumns(pk.field).ExecuteCommandAsync();
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除数据 默认方法
|
||||
/// </summary>
|
||||
public async Task<int> DeleteDataAsync(Vmodel vm, VmDeleteInput input)
|
||||
{
|
||||
ISqlSugarClient db = GetSqlSugar(vm.dbCode);
|
||||
var pk = vm.GetPrimary();
|
||||
int num = 0;
|
||||
List<Dictionary<string, object>> ids = new();
|
||||
if (input.id != null)
|
||||
{
|
||||
ids.Add(new DObject(pk.field, input.id));
|
||||
}
|
||||
else if (input.ids != null)
|
||||
{
|
||||
ids.AddRange(input.ids.Select(a => new DObject(pk.field, a)));
|
||||
}
|
||||
if (ids.Count > 0)
|
||||
{
|
||||
num = await db.Deleteable<object>().AS(vm.tableName).WhereColumns(ids).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using JNPF.DependencyInjection;
|
||||
using JNPF.DependencyInjection;
|
||||
using SqlSugar;
|
||||
using Tnb.Vengine.Domain;
|
||||
|
||||
@@ -14,58 +9,58 @@ namespace Tnb.Vengine.DataAccess;
|
||||
/// </summary>
|
||||
public interface IDataAccess : ITransient
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取 SqlSugar
|
||||
/// </summary>
|
||||
/// <param name="dbCode"></param>
|
||||
/// <returns></returns>
|
||||
ISqlSugarClient GetSqlSugar(string? dbCode = null);
|
||||
/// <summary>
|
||||
/// 获取 SqlSugar
|
||||
/// </summary>
|
||||
/// <param name="dbCode"></param>
|
||||
/// <returns></returns>
|
||||
ISqlSugarClient GetSqlSugar(string? dbCode = null);
|
||||
|
||||
/// <summary>
|
||||
/// 获取DbLink
|
||||
/// </summary>
|
||||
/// <param name="dbCode"></param>
|
||||
/// <returns></returns>
|
||||
VmodelLink GetVmLink(string dbCode);
|
||||
/// <summary>
|
||||
/// 获取DbLink
|
||||
/// </summary>
|
||||
/// <param name="dbCode"></param>
|
||||
/// <returns></returns>
|
||||
VmodelLink GetVmodelLink(string dbCode);
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时不抛异常
|
||||
/// </summary>
|
||||
Task<Vmodel?> TryGetVmodelAsync(string id, bool loadNavigate = false);
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时抛异常
|
||||
/// </summary>
|
||||
Task<Vmodel> GetVmodelAsync(string id, bool loadNavigate = false);
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时不抛异常
|
||||
/// </summary>
|
||||
Task<Vmodel?> TryGetVmodelAsync(string area, string vmCode, bool loadNavigate = false);
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时抛异常
|
||||
/// </summary>
|
||||
Task<Vmodel> GetVmodelAsync(string area, string vmCode, bool loadNavigate = false);
|
||||
//Task<VmPagedOutput> QueryDataAsync(VmBaseInput input);
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时不抛异常
|
||||
/// </summary>
|
||||
Task<Vmodel?> TryGetVmodelAsync(string id, bool loadNavigate = false);
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时抛异常
|
||||
/// </summary>
|
||||
Task<Vmodel> GetVmodelAsync(string id, bool loadNavigate = false);
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时不抛异常
|
||||
/// </summary>
|
||||
Task<Vmodel?> TryGetVmodelAsync(string area, string vmCode, bool loadNavigate = false);
|
||||
/// <summary>
|
||||
/// 获取 Vmodel, 为空时抛异常
|
||||
/// </summary>
|
||||
Task<Vmodel> GetVmodelAsync(string area, string vmCode, bool loadNavigate = false);
|
||||
//Task<VmPagedOutput> QueryDataAsync(VmBaseInput input);
|
||||
|
||||
/// <summary>
|
||||
/// 查询数据 默认方法
|
||||
/// </summary>
|
||||
Task<VmPagedOutput> QueryDataAsync(Vmodel vm, VmQueryInput input);
|
||||
/// <summary>
|
||||
/// 查询数据 默认方法
|
||||
/// </summary>
|
||||
Task<VmPagedOutput> QueryDataAsync(Vmodel vm, VmQueryInput input);
|
||||
|
||||
//Task<dynamic> CreateDataAsync(VmCreateInput input);
|
||||
/// <summary>
|
||||
/// 新增数据 默认方法
|
||||
/// </summary>
|
||||
Task<dynamic> CreateDataAsync(Vmodel vm, VmCreateInput input);
|
||||
//Task<dynamic> CreateDataAsync(VmCreateInput input);
|
||||
/// <summary>
|
||||
/// 新增数据 默认方法
|
||||
/// </summary>
|
||||
Task<dynamic> CreateDataAsync(Vmodel vm, VmCreateInput input);
|
||||
|
||||
//Task<dynamic> UpdateDataAsync(VmUpdateInput input);
|
||||
/// <summary>
|
||||
/// 更新数据 默认方法
|
||||
/// </summary>
|
||||
Task<dynamic> UpdateDataAsync(Vmodel vm, VmUpdateInput input);
|
||||
//Task<dynamic> UpdateDataAsync(VmUpdateInput input);
|
||||
/// <summary>
|
||||
/// 更新数据 默认方法
|
||||
/// </summary>
|
||||
Task<dynamic> UpdateDataAsync(Vmodel vm, VmUpdateInput input);
|
||||
|
||||
//Task<int> DeleteDataAsync(VmDeleteInput input);
|
||||
/// <summary>
|
||||
/// 删除数据 默认方法
|
||||
/// </summary>
|
||||
Task<int> DeleteDataAsync(Vmodel vm, VmDeleteInput input);
|
||||
//Task<int> DeleteDataAsync(VmDeleteInput input);
|
||||
/// <summary>
|
||||
/// 删除数据 默认方法
|
||||
/// </summary>
|
||||
Task<int> DeleteDataAsync(Vmodel vm, VmDeleteInput input);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,41 @@
|
||||
using JNPF.Logging;
|
||||
using Mapster;
|
||||
using SqlSugar;
|
||||
using Yitter.IdGenerator;
|
||||
|
||||
namespace Tnb.Vengine.DataAccess;
|
||||
|
||||
public class SugarHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建SugarClient实例
|
||||
/// </summary>
|
||||
public static ISqlSugarClient CreateSugarClient(string dbCode, string dbType, string dbConnection, Action<ConnectionConfig>? configConnection = null, Action<ISqlSugarClient>? configSugar = null)
|
||||
{
|
||||
var config = new ConnectionConfig
|
||||
{
|
||||
ConnectionString = dbConnection,
|
||||
DbType = dbType.Adapt<DbType>(),
|
||||
IsAutoCloseConnection = true,
|
||||
ConfigId = dbCode,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
MoreSettings = new ConnMoreSettings()
|
||||
{
|
||||
IsAutoRemoveDataCache = true, // 自动清理缓存
|
||||
IsAutoToUpper = false,
|
||||
PgSqlIsAutoToLower = false,
|
||||
DisableNvarchar = true
|
||||
},
|
||||
};
|
||||
configConnection?.Invoke(config);
|
||||
var sugar = new SqlSugarScope(config, configSugar == null ? ConfigSugar : configSugar);
|
||||
return sugar;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SqlSugar默认配置
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
public static void ConfigSugar(ISqlSugarClient db)
|
||||
{
|
||||
// 设置超时时间
|
||||
@@ -31,4 +62,12 @@ public class SugarHelper
|
||||
Log.Error(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, ex.Sql, (SugarParameter[])ex.Parametres));
|
||||
};
|
||||
}
|
||||
|
||||
public static void CustomSnowId()
|
||||
{
|
||||
StaticConfig.CustomSnowFlakeFunc = () =>
|
||||
{
|
||||
return YitIdHelper.NextId();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
155
visualdev/Tnb.Vengine/Domain/VengineDto.cs
Normal file
155
visualdev/Tnb.Vengine/Domain/VengineDto.cs
Normal file
@@ -0,0 +1,155 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using Tnb.Core;
|
||||
|
||||
namespace Tnb.Vengine.Domain;
|
||||
|
||||
public class VmBaseInput
|
||||
{
|
||||
///// <summary>
|
||||
///// 视图模型id
|
||||
///// </summary>
|
||||
//public string vmid { get; set; } = string.Empty;
|
||||
}
|
||||
public class VmGetInput : VmBaseInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 要获取数据的id
|
||||
/// </summary>
|
||||
public string? id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 过滤条件
|
||||
/// </summary>
|
||||
public string? q { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 输出字段
|
||||
/// </summary>
|
||||
public string o { get; set; } = "*";
|
||||
}
|
||||
|
||||
public class VmGetListInput : VmBaseInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 当前页数
|
||||
/// </summary>
|
||||
public int pnum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 每页记录数
|
||||
/// </summary>
|
||||
public int psize { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 排序
|
||||
/// </summary>
|
||||
public string? sort { get; set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// 模糊查询
|
||||
/// </summary>
|
||||
public string? k { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 过滤条件
|
||||
/// </summary>
|
||||
public string? q { get; set; }
|
||||
/// <summary>
|
||||
/// 输出字段
|
||||
/// </summary>
|
||||
public string o { get; set; } = "*";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条数据输入参数
|
||||
/// </summary>
|
||||
public class VmQueryInput : VmGetListInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 查询条件
|
||||
/// </summary>
|
||||
public new DObject? q { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 高级查询
|
||||
/// </summary>
|
||||
public DObject? adv { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增数据输入参数
|
||||
/// </summary>
|
||||
public class VmCreateInput : VmBaseInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据
|
||||
/// </summary>
|
||||
public DObject? data { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 批量添加
|
||||
/// </summary>
|
||||
public List<DObject>? items { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改数据输入参数
|
||||
/// </summary>
|
||||
public class VmUpdateInput : VmCreateInput
|
||||
{
|
||||
///// <summary>
|
||||
///// 要更新的数据id
|
||||
///// </summary>
|
||||
//public string? id { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除数据输入参数
|
||||
/// </summary>
|
||||
public class VmDeleteInput : VmBaseInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 要删除的数据id
|
||||
/// </summary>
|
||||
public string? id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 要删除的id列表
|
||||
/// </summary>
|
||||
public List<string>? ids { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页列表输出对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class PagedOutput<T>
|
||||
{
|
||||
public int total { get; set; }
|
||||
public List<T> items { get; set; } = new List<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态分页列表输出对象
|
||||
/// </summary>
|
||||
public class VmPagedOutput : PagedOutput<dynamic>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询属性信息
|
||||
/// </summary>
|
||||
public class VmSelectProp
|
||||
{
|
||||
public const string MAIN_ALIES = "m";
|
||||
public string code { get; set; } = string.Empty;
|
||||
public string field { get; set; } = string.Empty;
|
||||
public string navCode { get; set; } = MAIN_ALIES;
|
||||
public ePropType propType { get; set; }
|
||||
public eNavigateType navType { get; set; }
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using JNPF.Common.Extension;
|
||||
using Newtonsoft.Json;
|
||||
using Yitter.IdGenerator;
|
||||
|
||||
namespace Tnb.Vengine.Domain;
|
||||
@@ -12,138 +14,197 @@ namespace Tnb.Vengine.Domain;
|
||||
/// </summary>
|
||||
public class VmDbProp : VmBaseProp
|
||||
{
|
||||
#region Properties
|
||||
/// <summary>
|
||||
/// 字段名称
|
||||
/// </summary>
|
||||
public string field { get; set; } = string.Empty;
|
||||
#region Properties
|
||||
/// <summary>
|
||||
/// 字段名称
|
||||
/// </summary>
|
||||
public string field { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 数据类型
|
||||
/// </summary>
|
||||
public string dataType { get; set; } = "varchar";
|
||||
/// <summary>
|
||||
/// 数据类型
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public string dataType { get; set; } = "varchar";
|
||||
|
||||
/// <summary>
|
||||
/// 数据类型
|
||||
/// </summary>
|
||||
public string? csType { get; set; }
|
||||
/// <summary>
|
||||
/// 数据类型
|
||||
/// </summary>
|
||||
public string? csType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 长度
|
||||
/// </summary>
|
||||
public int length { get; set; }
|
||||
/// <summary>
|
||||
/// 长度
|
||||
/// </summary>
|
||||
public int length { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 精度
|
||||
/// </summary>
|
||||
public int digit { get; set; }
|
||||
/// <summary>
|
||||
/// 精度
|
||||
/// </summary>
|
||||
public int digit { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 排序
|
||||
/// </summary>
|
||||
public int ordinal { get; set; }
|
||||
/// <summary>
|
||||
/// 排序
|
||||
/// </summary>
|
||||
public int ordinal { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 非空
|
||||
/// </summary>
|
||||
public bool required { get; set; }
|
||||
/// <summary>
|
||||
/// 非空
|
||||
/// </summary>
|
||||
public bool required { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否主键
|
||||
/// </summary>
|
||||
public bool pkey { get; set; }
|
||||
/// <summary>
|
||||
/// 是否主键
|
||||
/// </summary>
|
||||
public bool pkey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否模糊搜索
|
||||
/// </summary>
|
||||
public bool fuzzy { get; set; }
|
||||
/// <summary>
|
||||
/// 是否模糊搜索
|
||||
/// </summary>
|
||||
public bool fuzzy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 默认值
|
||||
/// </summary>
|
||||
public string? defValue { get; set; }
|
||||
/// <summary>
|
||||
/// 默认值
|
||||
/// </summary>
|
||||
public string? defValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 描述
|
||||
/// </summary>
|
||||
public string? descrip { get; set; }
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// 描述
|
||||
/// </summary>
|
||||
public string? descrip { get; set; }
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 获取默认值
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public object? GetDefaultValue()
|
||||
{
|
||||
object? val = null;
|
||||
if (string.IsNullOrEmpty(defValue))
|
||||
/// <summary>
|
||||
/// 获取默认值
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public object? GetDefaultValue()
|
||||
{
|
||||
val = defValue switch
|
||||
{
|
||||
"@@snowid" => YitIdHelper.NextId().ToString(),
|
||||
"@@now" => DateTime.Now,
|
||||
"@@userid" => YitIdHelper.NextId().ToString(),
|
||||
"@@orgid" => YitIdHelper.NextId().ToString(),
|
||||
_ => null
|
||||
};
|
||||
object? val = null;
|
||||
if(!required) { return val; }
|
||||
if (string.IsNullOrEmpty(defValue))
|
||||
{
|
||||
val = csType switch
|
||||
{
|
||||
"string" => string.Empty,
|
||||
"short" or "int" or "long" => 0,
|
||||
"float" or "double" or "decimal" => 0f,
|
||||
"DateTime" => DateTime.Now,
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
val = defValue switch
|
||||
{
|
||||
"@@snowid" => YitIdHelper.NextId().ToString(),
|
||||
"@@now" => DateTime.Now,
|
||||
"@@userid" => YitIdHelper.NextId().ToString(),
|
||||
"@@orgid" => YitIdHelper.NextId().ToString(),
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
return val;
|
||||
}
|
||||
else
|
||||
{
|
||||
val = csType switch
|
||||
{
|
||||
"string" => string.Empty,
|
||||
"short" or "int" or "long" => 0,
|
||||
"float" or "double" or "decimal" => 0f,
|
||||
"DateTime" => DateTime.Now,
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取默认宽度
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetDefaultWidth()
|
||||
{
|
||||
return csType switch
|
||||
/// <summary>
|
||||
/// 获取默认值文本
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public object? GetDefaultValueString()
|
||||
{
|
||||
"string" => "\"width\": \"auto\"",
|
||||
"int" or "short" or "long" => "\"width\": 80",
|
||||
"DateTime" => "\"width\": 150",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取默认组件
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public CompOption GetDefaultComp()
|
||||
{
|
||||
CompOption comp = new CompOption();
|
||||
if (pkey)
|
||||
{
|
||||
comp.attr.Add("disabled", true);
|
||||
return comp;
|
||||
string val = "";
|
||||
if (!required) return val;
|
||||
if (!string.IsNullOrWhiteSpace(defValue))
|
||||
{
|
||||
val = defValue switch
|
||||
{
|
||||
"@@snowid" => "YitIdHelper.NextId().ToString()",
|
||||
"@@now" => "DateTime.Now",
|
||||
_ => defValue
|
||||
};
|
||||
}
|
||||
if (string.IsNullOrEmpty(val))
|
||||
{
|
||||
val = csType switch
|
||||
{
|
||||
"string" => "string.Empty;",
|
||||
"DateTime" => "DateTime.Now;",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
return string.IsNullOrEmpty(val) ? "" : " = " + val;
|
||||
}
|
||||
switch (csType)
|
||||
|
||||
/// <summary>
|
||||
/// 获取默认宽度
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetDefaultWidth()
|
||||
{
|
||||
case "string":
|
||||
comp.attr.Add("clearable", true);
|
||||
comp.attr.Add("maxlength", length);
|
||||
comp.attr.Add("showWordLimit", true);
|
||||
break;
|
||||
case "int":
|
||||
case "short":
|
||||
case "long":
|
||||
comp.type = "el-input-number";
|
||||
break;
|
||||
case "DateTime":
|
||||
comp.type = "el-date-picker";
|
||||
break;
|
||||
};
|
||||
return comp;
|
||||
}
|
||||
return csType switch
|
||||
{
|
||||
"string" => "\"width\": \"auto\"",
|
||||
"int" or "short" or "long" => "\"width\": 80",
|
||||
"DateTime" => "\"width\": 150",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取默认组件
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public CompOption GetDefaultComp()
|
||||
{
|
||||
CompOption comp = new CompOption();
|
||||
if (pkey)
|
||||
{
|
||||
comp.attr.Add("disabled", true);
|
||||
return comp;
|
||||
}
|
||||
switch (csType)
|
||||
{
|
||||
case "string":
|
||||
comp.attr.Add("clearable", true);
|
||||
comp.attr.Add("maxlength", length);
|
||||
comp.attr.Add("showWordLimit", true);
|
||||
break;
|
||||
case "int":
|
||||
case "short":
|
||||
case "long":
|
||||
comp.type = "el-input-number";
|
||||
break;
|
||||
case "DateTime":
|
||||
comp.type = "el-date-picker";
|
||||
break;
|
||||
};
|
||||
return comp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取SqlSugar特性文本
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetAttributeString()
|
||||
{
|
||||
var attr = "";
|
||||
var sb = new List<string>();
|
||||
if (!string.IsNullOrEmpty(field) && code != field) sb.Add($"ColumnName = \"{field}\"");
|
||||
if (pkey) sb.Add("IsPrimaryKey = true");
|
||||
//sb.Add("IsIdentity = true");
|
||||
if (required && !pkey) sb.Add("IsNullable = false");
|
||||
if (csType == "string" && length > 0)
|
||||
{
|
||||
sb.Add($"Length = {CodeHelper.LengthToString(length)}");
|
||||
}
|
||||
|
||||
if (csType == "decimal")
|
||||
{
|
||||
if (length > 0) sb.Add($"Length = {length}");
|
||||
if (digit > 0) sb.Add($"DecimalDigits = {digit}");
|
||||
}
|
||||
//if(isJson) sb.Add("IsJson = true")
|
||||
if (sb.Any()) attr += $"\t[SugarColumn({sb.JoinAsString(", ")})]{Environment.NewLine}";
|
||||
return attr;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,198 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace Tnb.Vengine.Domain;
|
||||
|
||||
/// <summary>
|
||||
/// 字典对象
|
||||
/// </summary>
|
||||
public class DObject : Dictionary<string, object>
|
||||
{
|
||||
public DObject() { }
|
||||
public DObject(string key, object value)
|
||||
{
|
||||
Add(key, value);
|
||||
}
|
||||
public DObject(Dictionary<string, object> dictionary) : base(dictionary)
|
||||
{
|
||||
}
|
||||
public void AddCascade(string code, object value)
|
||||
{
|
||||
var keys = code.Split('.');
|
||||
if (keys.Length == 1)
|
||||
{
|
||||
Add(code, value);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < keys.Length; i++)
|
||||
{
|
||||
DObject temp = this;
|
||||
if (i < keys.Length - 1)
|
||||
{
|
||||
if (!ContainsKey(keys[i]))
|
||||
{
|
||||
temp = new DObject();
|
||||
Add(keys[i], temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
temp = (DObject)temp[keys[i]];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
temp.Add(keys[i], value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class VmBaseInput
|
||||
{
|
||||
///// <summary>
|
||||
///// 视图模型id
|
||||
///// </summary>
|
||||
//public string vmid { get; set; } = string.Empty;
|
||||
}
|
||||
public class VmGetInput : VmBaseInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 要获取数据的id
|
||||
/// </summary>
|
||||
public string? id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 过滤条件
|
||||
/// </summary>
|
||||
public string? q { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 输出字段
|
||||
/// </summary>
|
||||
public string o { get; set; } = "*";
|
||||
}
|
||||
|
||||
public class VmGetListInput : VmBaseInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 当前页数
|
||||
/// </summary>
|
||||
public int pnum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 每页记录数
|
||||
/// </summary>
|
||||
public int psize { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 排序
|
||||
/// </summary>
|
||||
public string? sort { get; set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// 模糊查询
|
||||
/// </summary>
|
||||
public string? k { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 过滤条件
|
||||
/// </summary>
|
||||
public string? q { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 输出字段
|
||||
/// </summary>
|
||||
public string o { get; set; } = "*";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多条数据输入参数
|
||||
/// </summary>
|
||||
public class VmQueryInput : VmGetListInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 查询条件
|
||||
/// </summary>
|
||||
public new DObject? q { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 高级查询
|
||||
/// </summary>
|
||||
public DObject? adv { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增数据输入参数
|
||||
/// </summary>
|
||||
public class VmCreateInput : VmBaseInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据
|
||||
/// </summary>
|
||||
public DObject? data { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 批量添加
|
||||
/// </summary>
|
||||
public List<DObject>? items { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改数据输入参数
|
||||
/// </summary>
|
||||
public class VmUpdateInput : VmCreateInput
|
||||
{
|
||||
///// <summary>
|
||||
///// 要更新的数据id
|
||||
///// </summary>
|
||||
//public string? id { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除数据输入参数
|
||||
/// </summary>
|
||||
public class VmDeleteInput : VmBaseInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 要删除的数据id
|
||||
/// </summary>
|
||||
public string? id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 要删除的id列表
|
||||
/// </summary>
|
||||
public List<string>? ids { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页列表输出对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class PagedOutput<T>
|
||||
{
|
||||
public int total { get; set; }
|
||||
public List<T> items { get; set; } = new List<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态分页列表输出对象
|
||||
/// </summary>
|
||||
public class VmPagedOutput : PagedOutput<dynamic>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询属性信息
|
||||
/// </summary>
|
||||
public class VmSelectProp
|
||||
{
|
||||
public const string MAIN_ALIES = "m";
|
||||
public string code { get; set; } = string.Empty;
|
||||
public string field { get; set; } = string.Empty;
|
||||
public string navCode { get; set; } = MAIN_ALIES;
|
||||
public ePropType propType { get; set; }
|
||||
public eNavigateType navType { get; set; }
|
||||
}
|
||||
@@ -12,43 +12,47 @@ namespace Tnb.Vengine.Domain;
|
||||
/// </summary>
|
||||
public class VmNavProp : VmBaseProp
|
||||
{
|
||||
/// <summary>
|
||||
/// 导航属性模型id
|
||||
/// </summary>
|
||||
public string vmid { get; set; } = string.Empty;
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// 导航关联类型
|
||||
/// </summary>
|
||||
public eNavigateType navType { get; set; }
|
||||
/// <summary>
|
||||
/// 导航属性模型id
|
||||
/// </summary>
|
||||
public string vmid { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 源表字段
|
||||
/// </summary>
|
||||
public string refCode { get; set; } = VmSelectProp.MAIN_ALIES;
|
||||
/// <summary>
|
||||
/// 导航关联类型
|
||||
/// </summary>
|
||||
public eNavigateType navType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 被引用字段
|
||||
/// </summary>
|
||||
public string refField { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 源表字段
|
||||
/// </summary>
|
||||
public string refCode { get; set; } = VmSelectProp.MAIN_ALIES;
|
||||
|
||||
/// <summary>
|
||||
/// 源表字段
|
||||
/// </summary>
|
||||
public string fkField { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 被引用字段
|
||||
/// </summary>
|
||||
public string refField { get; set; } = string.Empty;
|
||||
|
||||
///// <summary>
|
||||
///// 关联表表名
|
||||
///// </summary>
|
||||
//[JsonIgnore]
|
||||
//public string refTable { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 源表字段
|
||||
/// </summary>
|
||||
public string fkField { get; set; } = string.Empty;
|
||||
|
||||
///// <summary>
|
||||
///// 被引用表(中间表)
|
||||
///// </summary>
|
||||
//[JsonIgnore]
|
||||
//public string? midTable { get; set; }
|
||||
///// <summary>
|
||||
///// 关联表表名
|
||||
///// </summary>
|
||||
//[JsonIgnore]
|
||||
//public string refTable { get; set; } = string.Empty;
|
||||
|
||||
///// <summary>
|
||||
///// 被引用表(中间表)
|
||||
///// </summary>
|
||||
//[JsonIgnore]
|
||||
//public string? midTable { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public Vmodel? naviModel { get; set; }
|
||||
#endregion
|
||||
|
||||
[JsonIgnore]
|
||||
public Vmodel? naviModel { get; set; }
|
||||
}
|
||||
|
||||
@@ -3,13 +3,16 @@
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using JNPF.Common.Core.Manager;
|
||||
using JNPF.Common.Extension;
|
||||
using Mapster;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SqlSugar;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Reflection;
|
||||
using Mapster;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SqlSugar;
|
||||
using Tnb.Core;
|
||||
using Yitter.IdGenerator;
|
||||
|
||||
namespace Tnb.Vengine.Domain;
|
||||
@@ -20,424 +23,486 @@ namespace Tnb.Vengine.Domain;
|
||||
[SugarTable("sys_vmodel")]
|
||||
public partial class Vmodel : Entity
|
||||
{
|
||||
#region Properties
|
||||
/// <summary>
|
||||
/// 主键标识
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public string id { get; set; } = YitIdHelper.NextId().ToString();
|
||||
#region Properties
|
||||
/// <summary>
|
||||
/// 主键标识
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public string id { get; set; } = YitIdHelper.NextId().ToString();
|
||||
|
||||
/// <summary>
|
||||
/// 模块代码
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "area", Length = DbConsts.LengthS)]
|
||||
public string area { get; set; } = "edp";
|
||||
/// <summary>
|
||||
/// 模块代码
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "area_code", Length = DbConsts.LengthS)]
|
||||
public string areaCode { get; set; } = "edp";
|
||||
|
||||
/// <summary>
|
||||
/// 视图代码
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "vm_code", IsNullable = false, Length = DbConsts.LengthM)]
|
||||
public string vmCode { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 视图代码
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "vm_code", IsNullable = false, Length = DbConsts.LengthM)]
|
||||
public string vmCode { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 视图名称
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "vm_name", IsNullable = false, Length = DbConsts.LengthM)]
|
||||
public string vmName { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 视图名称
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "vm_name", IsNullable = false, Length = DbConsts.LengthM)]
|
||||
public string vmName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 数据库连接
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "db_code", Length = DbConsts.LengthS)]
|
||||
public string? dbCode { get; set; }
|
||||
/// <summary>
|
||||
/// 数据库连接
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "db_code", Length = DbConsts.LengthS)]
|
||||
public string? dbCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 主表名称
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "table_name", IsNullable = false, Length = DbConsts.LengthS)]
|
||||
public string tableName { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 主表名称
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "table_name", IsNullable = false, Length = DbConsts.LengthS)]
|
||||
public string tableName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 表字段属性
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "db_props", IsNullable = false, IsJson = true)]
|
||||
public List<VmDbProp> dbProps { get; set; } = new List<VmDbProp>();
|
||||
/// <summary>
|
||||
/// 表字段属性
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "db_props", IsNullable = false, IsJson = true)]
|
||||
public List<VmDbProp> dbProps { get; set; } = new List<VmDbProp>();
|
||||
|
||||
/// <summary>
|
||||
/// 导航属性
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "nav_props", IsNullable = true, IsJson = true)]
|
||||
public List<VmNavProp> navProps { get; set; } = new List<VmNavProp>();
|
||||
/// <summary>
|
||||
/// 导航属性
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "nav_props", IsNullable = true, IsJson = true)]
|
||||
public List<VmNavProp> navProps { get; set; } = new List<VmNavProp>();
|
||||
|
||||
/// <summary>
|
||||
/// 计算属性
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "cal_props", IsNullable = true, IsJson = true)]
|
||||
public List<VmCalProp> calProps { get; set; } = new List<VmCalProp>();
|
||||
/// <summary>
|
||||
/// 计算属性
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "cal_props", IsNullable = true, IsJson = true)]
|
||||
public List<VmCalProp> calProps { get; set; } = new List<VmCalProp>();
|
||||
|
||||
/// <summary>
|
||||
/// 排序
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "ordinal", IsNullable = false)]
|
||||
public int ordinal { get; set; }
|
||||
/// <summary>
|
||||
/// 排序
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "ordinal", IsNullable = false)]
|
||||
public int ordinal { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 软删除
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "soft_delete", IsNullable = false)]
|
||||
public short softDelete { get; set; }
|
||||
/// <summary>
|
||||
/// 软删除
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "soft_delete", IsNullable = false)]
|
||||
public short softDelete { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否激活
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "enabled", IsNullable = false)]
|
||||
public short enabled { get; set; } = 1;
|
||||
/// <summary>
|
||||
/// 是否激活
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "enabled", IsNullable = false)]
|
||||
public short enabled { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 是否删除
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "deleted", IsNullable = false)]
|
||||
public short deleted { get; set; }
|
||||
/// <summary>
|
||||
/// 是否删除
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "deleted", IsNullable = false)]
|
||||
public short deleted { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 描述
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "descrip", Length = DbConsts.LengthL)]
|
||||
public string? descrip { get; set; }
|
||||
/// <summary>
|
||||
/// 描述
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "descrip", Length = DbConsts.LengthL)]
|
||||
public string? descrip { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "create_time", IsNullable = false)]
|
||||
public DateTime createTime { get; set; } = DateTime.Now;
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "create_time", IsNullable = false)]
|
||||
public DateTime createTime { get; set; } = DateTime.Now;
|
||||
|
||||
/// <summary>
|
||||
/// 创建人
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "create_id", Length = DbConsts.LengthS)]
|
||||
public string? createId { get; set; }
|
||||
/// <summary>
|
||||
/// 创建人
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "create_id", Length = DbConsts.LengthS)]
|
||||
public string? createId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 修改时间
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "modify_time", Length = DbConsts.LengthS)]
|
||||
public DateTime? modifyTime { get; set; }
|
||||
/// <summary>
|
||||
/// 修改时间
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "modify_time", Length = DbConsts.LengthS)]
|
||||
public DateTime? modifyTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 修改人
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "modify_id", Length = DbConsts.LengthS)]
|
||||
public string? modifyId { get; set; }
|
||||
/// <summary>
|
||||
/// 修改人
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "modify_id", Length = DbConsts.LengthS)]
|
||||
public string? modifyId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 主键
|
||||
/// </summary>
|
||||
public override object[] GetKeys()
|
||||
{
|
||||
return new object[] { id };
|
||||
}
|
||||
#endregion
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public string fullCode { get { return areaCode + "/" + vmCode; } }
|
||||
|
||||
/// <summary>
|
||||
/// 通过实体创建模型
|
||||
/// </summary>
|
||||
/// <param name="tpEntity"></param>
|
||||
/// <param name="dbCode"></param>
|
||||
/// <returns></returns>
|
||||
public static Vmodel CreateByEntity(Type tpEntity, string? dbCode = null)
|
||||
{
|
||||
Vmodel model = new() { dbCode = dbCode, vmCode = tpEntity.Name };
|
||||
var sugarTableAttr = tpEntity.GetCustomAttribute<SugarTable>();
|
||||
if (sugarTableAttr != null)
|
||||
/// <summary>
|
||||
/// 主键
|
||||
/// </summary>
|
||||
public override object[] GetKeys()
|
||||
{
|
||||
model.tableName = sugarTableAttr.TableName;
|
||||
model.vmName = sugarTableAttr.TableDescription;
|
||||
return new object[] { id };
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.tableName))
|
||||
{
|
||||
model.tableName = tpEntity.GetCustomAttribute<TableAttribute>()?.Name ?? tpEntity.Name;
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.vmName))
|
||||
{
|
||||
model.vmName = tpEntity.GetCustomAttribute<DisplayAttribute>()?.Name ?? tpEntity.GetCustomAttribute<DescriptionAttribute>()?.Description ?? model.vmCode;
|
||||
}
|
||||
var props = tpEntity.GetProperties(BindingFlags.Public);
|
||||
int n = 1;
|
||||
foreach (var p in props)
|
||||
{
|
||||
VmDbProp prop = new();
|
||||
var sugarColumn = p.GetCustomAttribute<SugarColumn>();
|
||||
if (sugarColumn != null)
|
||||
{
|
||||
prop = sugarColumn.Adapt<VmDbProp>();
|
||||
}
|
||||
prop.code = p.Name;
|
||||
prop.ordinal = n++;
|
||||
model.dbProps.Add(prop);
|
||||
}
|
||||
return model;
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 获取模型的主键字段属性
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public VmDbProp GetPrimary()
|
||||
{
|
||||
return dbProps.First(a => a.pkey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据属性名获取字段名
|
||||
/// </summary>
|
||||
/// <param name="propCode"></param>
|
||||
/// <returns></returns>
|
||||
public string? PropCodeToFieldCode(string propCode)
|
||||
{
|
||||
return dbProps.Where(a => a.code == propCode).Select(a => a.field).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据字段名获取属性名
|
||||
/// </summary>
|
||||
/// <param name="fieldCode"></param>
|
||||
/// <returns></returns>
|
||||
public string? FieldCodeToPropCode(string fieldCode)
|
||||
{
|
||||
return dbProps.Where(a => a.field == fieldCode).Select(a => a.code).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 属性代码转换为字段代码
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="ignoreNotMapped"></param>
|
||||
/// <returns></returns>
|
||||
public DObject PropToField(DObject input, bool ignoreNotMapped = true)
|
||||
{
|
||||
DObject ret = new();
|
||||
foreach (var item in input)
|
||||
/// <summary>
|
||||
/// 通过实体创建模型
|
||||
/// </summary>
|
||||
/// <param name="tpEntity"></param>
|
||||
/// <param name="dbCode"></param>
|
||||
/// <returns></returns>
|
||||
public static Vmodel CreateByEntity(Type tpEntity, string? dbCode = null)
|
||||
{
|
||||
var fcode = PropCodeToFieldCode(item.Key);
|
||||
if (!string.IsNullOrEmpty(fcode))
|
||||
{
|
||||
ret.Add(fcode, item.Value);
|
||||
}
|
||||
else if (!ignoreNotMapped)
|
||||
{
|
||||
ret.Add(item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字段代码转换为属性代码
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="ignoreNotMapped"></param>
|
||||
/// <returns></returns>
|
||||
public DObject FieldToProp(DObject input, bool ignoreNotMapped = true)
|
||||
{
|
||||
DObject ret = new();
|
||||
foreach (var item in input)
|
||||
{
|
||||
var pcode = FieldCodeToPropCode(item.Key);
|
||||
if (!string.IsNullOrEmpty(pcode))
|
||||
{
|
||||
ret.Add(pcode, item.Value);
|
||||
}
|
||||
else if (!ignoreNotMapped)
|
||||
{
|
||||
ret.Add(item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询字段的属性信息
|
||||
/// </summary>
|
||||
/// <param name="outputProps"></param>
|
||||
/// <returns></returns>
|
||||
public List<VmSelectProp> GetVmSelectProps(string? outputProps)
|
||||
{
|
||||
if (string.IsNullOrEmpty(outputProps) || outputProps == "*")
|
||||
{
|
||||
return dbProps.Select(a => new VmSelectProp { code = a.code, field = a.field }).ToList();
|
||||
}
|
||||
List<VmSelectProp> selProps = new();
|
||||
var outputs = outputProps.Split(',').Distinct().ToList();
|
||||
foreach (var propCode in outputs)
|
||||
{
|
||||
if (!propCode.Contains("."))
|
||||
{
|
||||
var fieldCode = PropCodeToFieldCode(propCode);
|
||||
if (!string.IsNullOrEmpty(fieldCode))
|
||||
Vmodel model = new() { dbCode = dbCode, vmCode = tpEntity.Name };
|
||||
var sugarTableAttr = tpEntity.GetCustomAttribute<SugarTable>();
|
||||
if (sugarTableAttr != null)
|
||||
{
|
||||
selProps.Add(new VmSelectProp { code = propCode, field = fieldCode });
|
||||
model.tableName = sugarTableAttr.TableName;
|
||||
model.vmName = sugarTableAttr.TableDescription;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
var codes = propCode.Split('.');
|
||||
if (codes.Length != 2) continue;
|
||||
if (codes[0] == VmSelectProp.MAIN_ALIES)
|
||||
{
|
||||
var fieldCode = PropCodeToFieldCode(propCode);
|
||||
if (!string.IsNullOrEmpty(fieldCode))
|
||||
if (string.IsNullOrEmpty(model.tableName))
|
||||
{
|
||||
selProps.Add(new VmSelectProp { code = propCode, field = fieldCode });
|
||||
model.tableName = tpEntity.GetCustomAttribute<TableAttribute>()?.Name ?? tpEntity.Name;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
var navProp = navProps.FirstOrDefault(a => a.code == codes[0]);
|
||||
if (navProp?.naviModel != null)
|
||||
{
|
||||
var fieldCode = navProp.naviModel.PropCodeToFieldCode(codes[1]);
|
||||
if (!string.IsNullOrEmpty(fieldCode))
|
||||
if (string.IsNullOrEmpty(model.vmName))
|
||||
{
|
||||
selProps.Add(new VmSelectProp { code = codes[1], field = fieldCode, navCode = codes[0], propType = ePropType.Navigate, navType = navProp.navType });
|
||||
model.vmName = tpEntity.GetCustomAttribute<DisplayAttribute>()?.Name ?? tpEntity.GetCustomAttribute<DescriptionAttribute>()?.Description ?? model.vmCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
return selProps;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取联表配置信息
|
||||
/// </summary>
|
||||
/// <param name="selProps"></param>
|
||||
/// <returns></returns>
|
||||
public List<JoinInfoParameter> GetJoinInfos(List<VmSelectProp> selProps)
|
||||
{
|
||||
var navigates = selProps.Where(a => a.propType == ePropType.Navigate).Select(a => a.navCode).Distinct().ToList();
|
||||
List<JoinInfoParameter> joins = new();
|
||||
foreach (var navCode in navigates)
|
||||
{
|
||||
if (navCode == VmSelectProp.MAIN_ALIES) continue;
|
||||
var navProp = navProps.First(a => a.code == navCode);
|
||||
if (navProp.naviModel == null || navProp.navType != eNavigateType.OneToOne) continue;
|
||||
JoinInfoParameter join = new JoinInfoParameter { TableName = navProp.naviModel.tableName, ShortName = navCode, Type = JoinType.Inner };
|
||||
var fkField = navProp.naviModel.PropCodeToFieldCode(navProp.fkField);
|
||||
var refField = navProp.refField;
|
||||
if (navProp.refCode != VmSelectProp.MAIN_ALIES)
|
||||
{
|
||||
var refProp = navProps.First(a => a.code == navProp.refCode);
|
||||
refField = refProp.naviModel!.PropCodeToFieldCode(navProp.refField);
|
||||
}
|
||||
join.Models = ObjectFuncModel.Create("Equals", $"{navCode}.{fkField}", $"{navProp.refCode}.{refField}");
|
||||
joins.Add(join);
|
||||
}
|
||||
return joins;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为查询过滤条件
|
||||
/// </summary>
|
||||
/// <param name="filter"></param>
|
||||
/// <returns></returns>
|
||||
public List<IConditionalModel> GetConditionalModels(DObject? filter)
|
||||
{
|
||||
List<IConditionalModel> wheres = new List<IConditionalModel>();
|
||||
if (filter == null) return wheres;
|
||||
foreach (var item in filter)
|
||||
{
|
||||
// TODO 按子表条件查询
|
||||
if (item.Key.Contains("."))
|
||||
{
|
||||
|
||||
}
|
||||
var prop = dbProps.FirstOrDefault(a => a.code == item.Key);
|
||||
if (prop == null) continue;
|
||||
if (item.Value is JArray val)
|
||||
{
|
||||
var op = val[0].ToString();
|
||||
switch (op)
|
||||
var props = tpEntity.GetProperties(BindingFlags.Public);
|
||||
int n = 1;
|
||||
foreach (var p in props)
|
||||
{
|
||||
case "><":
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[1].ToString(), ConditionalType = ConditionalType.GreaterThan });
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[2].ToString(), ConditionalType = ConditionalType.LessThan });
|
||||
break;
|
||||
case ">=<":
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[1].ToString(), ConditionalType = ConditionalType.GreaterThanOrEqual });
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[2].ToString(), ConditionalType = ConditionalType.LessThan });
|
||||
break;
|
||||
case "><=":
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[1].ToString(), ConditionalType = ConditionalType.GreaterThan });
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[2].ToString(), ConditionalType = ConditionalType.LessThanOrEqual });
|
||||
break;
|
||||
case ">=<=":
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[1].ToString(), ConditionalType = ConditionalType.GreaterThanOrEqual });
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[2].ToString(), ConditionalType = ConditionalType.LessThanOrEqual });
|
||||
break;
|
||||
case "in":
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val.Skip(1).ToString(), ConditionalType = ConditionalType.In });
|
||||
break;
|
||||
default: op = string.Empty; break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//if (item.Value == null) continue;
|
||||
var conditionalType = ConditionalType.Equal;
|
||||
string? value = item.Value?.ToString();
|
||||
if (string.IsNullOrEmpty(value)) continue;
|
||||
if (value.Length >= 2)
|
||||
{
|
||||
var op = value.Substring(0, 2);
|
||||
switch (op)
|
||||
{
|
||||
case "%%": conditionalType = ConditionalType.Like; break;
|
||||
case ">>": conditionalType = ConditionalType.GreaterThan; break;
|
||||
case "<<": conditionalType = ConditionalType.LessThan; break;
|
||||
case ">=": conditionalType = ConditionalType.GreaterThanOrEqual; break;
|
||||
case "<=": conditionalType = ConditionalType.LessThanOrEqual; break;
|
||||
case "==": conditionalType = ConditionalType.Equal; break;
|
||||
default: op = string.Empty; break;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(op))
|
||||
{
|
||||
value = value.RemovePreFix(op);
|
||||
if (value.ToLower() == "null")
|
||||
VmDbProp prop = new();
|
||||
var sugarColumn = p.GetCustomAttribute<SugarColumn>();
|
||||
if (sugarColumn != null)
|
||||
{
|
||||
value = null;
|
||||
prop = sugarColumn.Adapt<VmDbProp>();
|
||||
}
|
||||
}
|
||||
prop.code = p.Name;
|
||||
prop.ordinal = n++;
|
||||
model.dbProps.Add(prop);
|
||||
}
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = value, ConditionalType = conditionalType });
|
||||
}
|
||||
return model;
|
||||
}
|
||||
return wheres;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为查询字段列表
|
||||
/// </summary>
|
||||
/// <param name="selProps"></param>
|
||||
/// <returns></returns>
|
||||
public List<SelectModel> GetSelectModels(List<VmSelectProp> selProps)
|
||||
{
|
||||
return selProps.Where(a => a.navType != eNavigateType.OneToMany && a.navType != eNavigateType.ManyToMany).Select(a => new SelectModel
|
||||
/// <summary>
|
||||
/// 获取模型的主键字段属性
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public VmDbProp GetPrimary()
|
||||
{
|
||||
FiledName = (a.navCode == VmSelectProp.MAIN_ALIES ? "" : a.navCode + ".") + a.field,
|
||||
AsName = (a.navCode == VmSelectProp.MAIN_ALIES ? "" : a.navCode + "_") + a.code
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取默认对象
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public DObject GetDefaultDObject()
|
||||
{
|
||||
DObject obj = new();
|
||||
foreach (var p in dbProps)
|
||||
{
|
||||
obj.Add(p.code, p.GetDefaultValue()!);
|
||||
return dbProps.First(a => a.pkey);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据属性名获取字段名
|
||||
/// </summary>
|
||||
/// <param name="propCode"></param>
|
||||
/// <returns></returns>
|
||||
public string? PropCodeToFieldCode(string propCode)
|
||||
{
|
||||
return dbProps.Where(a => a.code == propCode).Select(a => a.field).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据字段名获取属性名
|
||||
/// </summary>
|
||||
/// <param name="fieldCode"></param>
|
||||
/// <returns></returns>
|
||||
public string? FieldCodeToPropCode(string fieldCode)
|
||||
{
|
||||
return dbProps.Where(a => a.field == fieldCode).Select(a => a.code).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 属性代码转换为字段代码
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="ignoreNotMapped"></param>
|
||||
/// <returns></returns>
|
||||
public DObject PropToField(DObject input, bool ignoreNotMapped = true)
|
||||
{
|
||||
DObject ret = new();
|
||||
foreach (var item in input)
|
||||
{
|
||||
var fcode = PropCodeToFieldCode(item.Key);
|
||||
if (!string.IsNullOrEmpty(fcode))
|
||||
{
|
||||
ret.Add(fcode, item.Value);
|
||||
}
|
||||
else if (!ignoreNotMapped)
|
||||
{
|
||||
ret.Add(item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字段代码转换为属性代码
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="ignoreNotMapped"></param>
|
||||
/// <returns></returns>
|
||||
public DObject FieldToProp(DObject input, bool ignoreNotMapped = true)
|
||||
{
|
||||
DObject ret = new();
|
||||
foreach (var item in input)
|
||||
{
|
||||
var pcode = FieldCodeToPropCode(item.Key);
|
||||
if (!string.IsNullOrEmpty(pcode))
|
||||
{
|
||||
ret.Add(pcode, item.Value);
|
||||
}
|
||||
else if (!ignoreNotMapped)
|
||||
{
|
||||
ret.Add(item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询字段的属性信息
|
||||
/// </summary>
|
||||
/// <param name="outputProps"></param>
|
||||
/// <returns></returns>
|
||||
public List<VmSelectProp> GetVmSelectProps(string? outputProps)
|
||||
{
|
||||
if (string.IsNullOrEmpty(outputProps) || outputProps == "*")
|
||||
{
|
||||
return dbProps.Select(a => new VmSelectProp { code = a.code, field = a.field }).ToList();
|
||||
}
|
||||
List<VmSelectProp> selProps = new();
|
||||
var outputs = outputProps.Split(',').Distinct().ToList();
|
||||
foreach (var propCode in outputs)
|
||||
{
|
||||
if (!propCode.Contains("."))
|
||||
{
|
||||
var fieldCode = PropCodeToFieldCode(propCode);
|
||||
if (!string.IsNullOrEmpty(fieldCode))
|
||||
{
|
||||
selProps.Add(new VmSelectProp { code = propCode, field = fieldCode });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
var codes = propCode.Split('.');
|
||||
if (codes.Length != 2) continue;
|
||||
if (codes[0] == VmSelectProp.MAIN_ALIES)
|
||||
{
|
||||
var fieldCode = PropCodeToFieldCode(propCode);
|
||||
if (!string.IsNullOrEmpty(fieldCode))
|
||||
{
|
||||
selProps.Add(new VmSelectProp { code = propCode, field = fieldCode });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
var navProp = navProps.FirstOrDefault(a => a.code == codes[0]);
|
||||
if (navProp?.naviModel != null)
|
||||
{
|
||||
var fieldCode = navProp.naviModel.PropCodeToFieldCode(codes[1]);
|
||||
if (!string.IsNullOrEmpty(fieldCode))
|
||||
{
|
||||
selProps.Add(new VmSelectProp { code = codes[1], field = fieldCode, navCode = codes[0], propType = ePropType.Navigate, navType = navProp.navType });
|
||||
}
|
||||
}
|
||||
}
|
||||
return selProps;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取联表配置信息
|
||||
/// </summary>
|
||||
/// <param name="selProps"></param>
|
||||
/// <returns></returns>
|
||||
public List<JoinInfoParameter> GetJoinInfos(List<VmSelectProp> selProps)
|
||||
{
|
||||
var navigates = selProps.Where(a => a.propType == ePropType.Navigate).Select(a => a.navCode).Distinct().ToList();
|
||||
List<JoinInfoParameter> joins = new();
|
||||
foreach (var navCode in navigates)
|
||||
{
|
||||
if (navCode == VmSelectProp.MAIN_ALIES) continue;
|
||||
var navProp = navProps.First(a => a.code == navCode);
|
||||
if (navProp.naviModel == null || navProp.navType != eNavigateType.OneToOne) continue;
|
||||
JoinInfoParameter join = new JoinInfoParameter { TableName = navProp.naviModel.tableName, ShortName = navCode, Type = JoinType.Inner };
|
||||
var fkField = navProp.naviModel.PropCodeToFieldCode(navProp.fkField);
|
||||
var refField = navProp.refField;
|
||||
if (navProp.refCode != VmSelectProp.MAIN_ALIES)
|
||||
{
|
||||
var refProp = navProps.First(a => a.code == navProp.refCode);
|
||||
refField = refProp.naviModel!.PropCodeToFieldCode(navProp.refField);
|
||||
}
|
||||
join.Models = ObjectFuncModel.Create("Equals", $"{navCode}.{fkField}", $"{navProp.refCode}.{refField}");
|
||||
joins.Add(join);
|
||||
}
|
||||
return joins;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为查询过滤条件
|
||||
/// </summary>
|
||||
/// <param name="filter"></param>
|
||||
/// <returns></returns>
|
||||
public List<IConditionalModel> GetConditionalModels(DObject? filter)
|
||||
{
|
||||
List<IConditionalModel> wheres = new List<IConditionalModel>();
|
||||
if (filter == null) return wheres;
|
||||
foreach (var item in filter)
|
||||
{
|
||||
// TODO 按子表条件查询
|
||||
if (item.Key.Contains("."))
|
||||
{
|
||||
|
||||
}
|
||||
var prop = dbProps.FirstOrDefault(a => a.code == item.Key);
|
||||
if (prop == null) continue;
|
||||
if (item.Value is JArray val)
|
||||
{
|
||||
var op = val[0].ToString();
|
||||
switch (op)
|
||||
{
|
||||
case "><":
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[1].ToString(), ConditionalType = ConditionalType.GreaterThan, CSharpTypeName = prop.csType });
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[2].ToString(), ConditionalType = ConditionalType.LessThan, CSharpTypeName = prop.csType });
|
||||
break;
|
||||
case ">=<":
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[1].ToString(), ConditionalType = ConditionalType.GreaterThanOrEqual, CSharpTypeName = prop.csType });
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[2].ToString(), ConditionalType = ConditionalType.LessThan, CSharpTypeName = prop.csType });
|
||||
break;
|
||||
case "><=":
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[1].ToString(), ConditionalType = ConditionalType.GreaterThan, CSharpTypeName = prop.csType });
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[2].ToString(), ConditionalType = ConditionalType.LessThanOrEqual, CSharpTypeName = prop.csType });
|
||||
break;
|
||||
case ">=<=":
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[1].ToString(), ConditionalType = ConditionalType.GreaterThanOrEqual, CSharpTypeName = prop.csType });
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val[2].ToString(), ConditionalType = ConditionalType.LessThanOrEqual, CSharpTypeName = prop.csType });
|
||||
break;
|
||||
case "in":
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = val.Skip(1).ToString(), ConditionalType = ConditionalType.In, CSharpTypeName = prop.csType });
|
||||
break;
|
||||
default: op = string.Empty; break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//if (item.Value == null) continue;
|
||||
var conditionalType = ConditionalType.Equal;
|
||||
string? value = item.Value?.ToString();
|
||||
if (string.IsNullOrEmpty(value)) continue;
|
||||
if (value.Length >= 2)
|
||||
{
|
||||
var op = value.Substring(0, 2);
|
||||
switch (op)
|
||||
{
|
||||
case "%%": conditionalType = ConditionalType.Like; break;
|
||||
case ">>": conditionalType = ConditionalType.GreaterThan; break;
|
||||
case "<<": conditionalType = ConditionalType.LessThan; break;
|
||||
case ">=": conditionalType = ConditionalType.GreaterThanOrEqual; break;
|
||||
case "<=": conditionalType = ConditionalType.LessThanOrEqual; break;
|
||||
case "==": conditionalType = ConditionalType.Equal; break;
|
||||
default: op = string.Empty; break;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(op))
|
||||
{
|
||||
value = value.RemovePreFix(op);
|
||||
if (value.ToLower() == "null")
|
||||
{
|
||||
value = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
wheres.Add(new ConditionalModel { FieldName = prop.field, FieldValue = value, ConditionalType = conditionalType, CSharpTypeName = prop.csType });
|
||||
}
|
||||
}
|
||||
return wheres;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为查询字段列表
|
||||
/// </summary>
|
||||
/// <param name="selProps"></param>
|
||||
/// <returns></returns>
|
||||
public List<SelectModel> GetSelectModels(List<VmSelectProp> selProps)
|
||||
{
|
||||
return selProps.Where(a => a.navType != eNavigateType.OneToMany && a.navType != eNavigateType.ManyToMany).Select(a => new SelectModel
|
||||
{
|
||||
FiledName = (a.navCode == VmSelectProp.MAIN_ALIES ? "" : a.navCode + ".") + a.field,
|
||||
AsName = (a.navCode == VmSelectProp.MAIN_ALIES ? "" : a.navCode + "_") + a.code
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取默认对象
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public DObject GetDefaultDObject()
|
||||
{
|
||||
DObject obj = new();
|
||||
foreach (var p in dbProps)
|
||||
{
|
||||
obj.Add(p.code, p.GetDefaultValue()!);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为待新增的实体对象
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public DObject ToCreateEntity(DObject input, IUserManager user)
|
||||
{
|
||||
DObject obj = new();
|
||||
foreach (var p in dbProps)
|
||||
{
|
||||
if (input.ContainsKey(p.code))
|
||||
{
|
||||
obj.Add(p.field, input[p.code]);
|
||||
}
|
||||
//当提交的数据与内置规则有重复时,采用内置规则,如果要优先采用提交的数据,这里要使用else if
|
||||
if ((p.pkey && p.code == "id") || p.defValue == "@snowid")
|
||||
{
|
||||
obj[p.field] = YitIdHelper.NextId().ToString();
|
||||
}
|
||||
else if (p.csType == "DateTime" && (p.code == "createTime" || p.defValue == "@createTime"))
|
||||
{
|
||||
obj[p.field] = DateTime.Now;
|
||||
}
|
||||
else if (p.csType == "string" && (p.code == "createId" || p.defValue == "@createId"))
|
||||
{
|
||||
obj[p.field] = user.UserId;
|
||||
}
|
||||
else if (obj.GetOrDefault(p.field) == null && (p.required || !string.IsNullOrEmpty(p.defValue)))
|
||||
{
|
||||
obj[p.field] = p.GetDefaultValue()!;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为待修改的实体对象
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public DObject ToUpdateEntity(DObject input, IUserManager user)
|
||||
{
|
||||
DObject obj = new();
|
||||
foreach (var p in dbProps)
|
||||
{
|
||||
if (input.ContainsKey(p.code))
|
||||
{
|
||||
obj.Add(p.field, input[p.code]);
|
||||
}
|
||||
//当提交的数据与内置规则有重复时,采用内置规则,如果要优先采用提交的数据,这里要使用else if
|
||||
if (p.csType == "DateTime" && (p.code == "updateTime" || p.code == "modifyTime" || p.defValue == "@updateTime"))
|
||||
{
|
||||
obj[p.field] = DateTime.Now;
|
||||
}
|
||||
else if (p.csType == "string" && (p.code == "updateId" || p.code == "modifyId" || p.defValue == "@updateId"))
|
||||
{
|
||||
obj[p.field] = user.UserId;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace Tnb.Vengine.Domain;
|
||||
|
||||
public class VmodelCreateFromTableInput
|
||||
{
|
||||
public string? dbCode { get; set; }
|
||||
|
||||
public string tableName { get; set; } = string.Empty;
|
||||
|
||||
public string? removePrefix { get; set; }
|
||||
public string area { get; set; } = "edp";
|
||||
}
|
||||
|
||||
public class CreatePageFromVmodelInput
|
||||
{
|
||||
public Guid? viewId { get; set; }
|
||||
public string? vmid { get; set; }
|
||||
}
|
||||
|
||||
public class VmodelGetInput
|
||||
{
|
||||
public long? id { get; set; }
|
||||
public string? moduleCode { get; set; }
|
||||
public string? vmCode { get; set; }
|
||||
public string? dbCode { get; set; }
|
||||
public string? tableName { get; set; }
|
||||
public bool drill { get; set; }
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using SqlSugar;
|
||||
using Tnb.Core;
|
||||
using Yitter.IdGenerator;
|
||||
|
||||
namespace Tnb.Vengine.Domain;
|
||||
@@ -14,36 +15,36 @@ namespace Tnb.Vengine.Domain;
|
||||
[SugarTable("sys_vmodel_link")]
|
||||
public partial class VmodelLink : Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 主键标识
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public string id { get; set; } = YitIdHelper.NextId().ToString();
|
||||
/// <summary>
|
||||
/// 数据库连接
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "db_code", Length = DbConsts.LengthS)]
|
||||
public string? dbCode { get; set; }
|
||||
/// <summary>
|
||||
/// 主键标识
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public string id { get; set; } = YitIdHelper.NextId().ToString();
|
||||
/// <summary>
|
||||
/// 数据库连接
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "db_code", IsNullable = false, Length = DbConsts.LengthS)]
|
||||
public string dbCode { get; set; } = "PostgreSQL";
|
||||
|
||||
/// <summary>
|
||||
/// 数据库类型
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "db_type", IsNullable = false)]
|
||||
public eDbType dbType { get; set; }
|
||||
/// <summary>
|
||||
/// 数据库类型
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "db_type", IsNullable = false, Length = DbConsts.LengthS)]
|
||||
public string dbType { get; set; } = "PostgreSQL";
|
||||
|
||||
/// <summary>
|
||||
/// 连接串
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "db_connection", IsNullable = false, Length = DbConsts.LengthXL)]
|
||||
public string dbConnection { get; set; } = "";
|
||||
/// <summary>
|
||||
/// 连接串
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "db_connection", IsNullable = false, Length = DbConsts.LengthXL)]
|
||||
public string dbConnection { get; set; } = "";
|
||||
|
||||
/// <summary>
|
||||
/// 主键
|
||||
/// </summary>
|
||||
public override object[] GetKeys()
|
||||
{
|
||||
return new object[] { id };
|
||||
}
|
||||
/// <summary>
|
||||
/// 主键
|
||||
/// </summary>
|
||||
public override object[] GetKeys()
|
||||
{
|
||||
return new object[] { id };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SqlSugar;
|
||||
using Tnb.Core;
|
||||
using Yitter.IdGenerator;
|
||||
|
||||
namespace Tnb.Vengine.Domain;
|
||||
@@ -15,93 +16,92 @@ namespace Tnb.Vengine.Domain;
|
||||
[SugarTable("sys_vmodel_page")]
|
||||
public partial class VmodelPage : Entity
|
||||
{
|
||||
#region Properties
|
||||
/// <summary>
|
||||
/// 主键标识
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public string id { get; set; } = YitIdHelper.NextId().ToString();
|
||||
#region Properties
|
||||
/// <summary>
|
||||
/// 主键标识
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public string id { get; set; } = YitIdHelper.NextId().ToString();
|
||||
|
||||
/// <summary>
|
||||
/// 模型id
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "vmid", Length = DbConsts.LengthS)]
|
||||
public string? vmid { get; set; }
|
||||
/// <summary>
|
||||
/// 模型id
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "vmid", Length = DbConsts.LengthS)]
|
||||
public string? vmid { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 页面代码
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "code", Length = DbConsts.LengthS)]
|
||||
public string code { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 页面代码
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "code", Length = DbConsts.LengthS)]
|
||||
public string code { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 页面名称
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "name", Length = DbConsts.LengthM)]
|
||||
public string name { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 页面名称
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "name", Length = DbConsts.LengthM)]
|
||||
public string name { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 页面类型
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "page_type", Length = DbConsts.LengthS)]
|
||||
public string pageType { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 页面类型
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "page_type", Length = DbConsts.LengthS)]
|
||||
public string pageType { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 页面配置
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "page_schema", Length = DbConsts.LengthS, IsJson = true)]
|
||||
public JObject pageSchema { get; set; } = new JObject();
|
||||
/// <summary>
|
||||
/// 页面配置
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "page_schema", Length = DbConsts.LengthS, IsJson = true)]
|
||||
public JObject pageSchema { get; set; } = new JObject();
|
||||
|
||||
/// <summary>
|
||||
/// 页面配置
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "option", Length = DbConsts.LengthS)]
|
||||
public string? option { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 页面配置
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "option", Length = DbConsts.LengthS)]
|
||||
public string? option { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "enabled")]
|
||||
public short enabled { get; set; } = 1;
|
||||
/// <summary>
|
||||
/// 是否启用
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "enabled")]
|
||||
public short enabled { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 是否删除
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "deleted")]
|
||||
public short deleted { get; set; }
|
||||
/// <summary>
|
||||
/// 是否删除
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "deleted")]
|
||||
public short deleted { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "create_time")]
|
||||
public DateTime createTime { get; set; } = DateTime.Now;
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "create_time")]
|
||||
public DateTime createTime { get; set; } = DateTime.Now;
|
||||
|
||||
/// <summary>
|
||||
/// 创建人
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "create_id", Length = DbConsts.LengthS)]
|
||||
public string? createId { get; set; }
|
||||
/// <summary>
|
||||
/// 创建人
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "create_id", Length = DbConsts.LengthS)]
|
||||
public string? createId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 修改时间
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "modify_time", Length = DbConsts.LengthS)]
|
||||
public DateTime? modifyTime { get; set; }
|
||||
/// <summary>
|
||||
/// 修改时间
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "modify_time", Length = DbConsts.LengthS)]
|
||||
public DateTime? modifyTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 修改人
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "modify_id", Length = DbConsts.LengthS)]
|
||||
public string? modifyId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 主键
|
||||
/// </summary>
|
||||
public override object[] GetKeys()
|
||||
{
|
||||
return new object[] { id };
|
||||
}
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// 修改人
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "modify_id", Length = DbConsts.LengthS)]
|
||||
public string? modifyId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 主键
|
||||
/// </summary>
|
||||
public override object[] GetKeys()
|
||||
{
|
||||
return new object[] { id };
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
||||
@@ -1,577 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// 宁波拓通e智造平台 ToTong Next Builder //
|
||||
// https://git.tuotong-tech.com/tnb/tnb.server //
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using JNPF.Common.Extension;
|
||||
using JNPF.DependencyInjection;
|
||||
|
||||
namespace System;
|
||||
|
||||
/// <summary>
|
||||
/// 字符串扩展类,来自Abp
|
||||
/// </summary>
|
||||
[SuppressSniffer]
|
||||
public static class StringExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a char to end of given string if it does not ends with the char.
|
||||
/// </summary>
|
||||
public static string EnsureEndsWith(this string str, char c, StringComparison comparisonType = StringComparison.Ordinal)
|
||||
{
|
||||
ThrowIf.IsNull(str, nameof(str));
|
||||
|
||||
if (str.EndsWith(c.ToString(), comparisonType))
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
return str + c;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a char to beginning of given string if it does not starts with the char.
|
||||
/// </summary>
|
||||
public static string EnsureStartsWith(this string str, char c, StringComparison comparisonType = StringComparison.Ordinal)
|
||||
{
|
||||
ThrowIf.IsNull(str, nameof(str));
|
||||
|
||||
if (str.StartsWith(c.ToString(), comparisonType))
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
return c + str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether this string is null or an System.String.Empty string.
|
||||
/// </summary>
|
||||
public static bool IsNullOrEmpty(this string str)
|
||||
{
|
||||
return string.IsNullOrEmpty(str);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// indicates whether this string is null, empty, or consists only of white-space characters.
|
||||
/// </summary>
|
||||
public static bool IsNullOrWhiteSpace(this string str)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(str);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a substring of a string from beginning of the string.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="str"/> is null</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="len"/> is bigger that string's length</exception>
|
||||
public static string Left(this string str, int len)
|
||||
{
|
||||
ThrowIf.IsNull(str, nameof(str));
|
||||
|
||||
if (str.Length < len)
|
||||
{
|
||||
throw new ArgumentException("len argument can not be bigger than given string's length!");
|
||||
}
|
||||
|
||||
return str.Substring(0, len);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts line endings in the string to <see cref="Environment.NewLine"/>.
|
||||
/// </summary>
|
||||
public static string NormalizeLineEndings(this string str)
|
||||
{
|
||||
return str.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", Environment.NewLine);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets index of nth occurrence of a char in a string.
|
||||
/// </summary>
|
||||
/// <param name="str">source string to be searched</param>
|
||||
/// <param name="c">Char to search in <paramref name="str"/></param>
|
||||
/// <param name="n">Count of the occurrence</param>
|
||||
public static int NthIndexOf(this string str, char c, int n)
|
||||
{
|
||||
ThrowIf.IsNull(str, nameof(str));
|
||||
|
||||
var count = 0;
|
||||
for (var i = 0; i < str.Length; i++)
|
||||
{
|
||||
if (str[i] != c)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((++count) == n)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes first occurrence of the given postfixes from end of the given string.
|
||||
/// </summary>
|
||||
/// <param name="str">The string.</param>
|
||||
/// <param name="postFixes">one or more postfix.</param>
|
||||
/// <returns>Modified string or the same string if it has not any of given postfixes</returns>
|
||||
public static string RemovePostFix(this string str, params string[] postFixes)
|
||||
{
|
||||
return str.RemovePostFix(StringComparison.Ordinal, postFixes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes first occurrence of the given postfixes from end of the given string.
|
||||
/// </summary>
|
||||
/// <param name="str">The string.</param>
|
||||
/// <param name="comparisonType">String comparison type</param>
|
||||
/// <param name="postFixes">one or more postfix.</param>
|
||||
/// <returns>Modified string or the same string if it has not any of given postfixes</returns>
|
||||
public static string RemovePostFix(this string str, StringComparison comparisonType, params string[] postFixes)
|
||||
{
|
||||
if (str.IsNullOrEmpty())
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
if (postFixes.IsNullOrEmpty())
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
foreach (var postFix in postFixes)
|
||||
{
|
||||
if (str.EndsWith(postFix, comparisonType))
|
||||
{
|
||||
return str.Left(str.Length - postFix.Length);
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes first occurrence of the given prefixes from beginning of the given string.
|
||||
/// </summary>
|
||||
/// <param name="str">The string.</param>
|
||||
/// <param name="preFixes">one or more prefix.</param>
|
||||
/// <returns>Modified string or the same string if it has not any of given prefixes</returns>
|
||||
public static string RemovePreFix(this string str, params string[] preFixes)
|
||||
{
|
||||
return str.RemovePreFix(StringComparison.Ordinal, preFixes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes first occurrence of the given prefixes from beginning of the given string.
|
||||
/// </summary>
|
||||
/// <param name="str">The string.</param>
|
||||
/// <param name="comparisonType">String comparison type</param>
|
||||
/// <param name="preFixes">one or more prefix.</param>
|
||||
/// <returns>Modified string or the same string if it has not any of given prefixes</returns>
|
||||
public static string RemovePreFix(this string str, StringComparison comparisonType, params string[] preFixes)
|
||||
{
|
||||
if (str.IsNullOrEmpty())
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
if (preFixes.IsNullOrEmpty())
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
foreach (var preFix in preFixes)
|
||||
{
|
||||
if (str.StartsWith(preFix, comparisonType))
|
||||
{
|
||||
return str.Right(str.Length - preFix.Length);
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static string ReplaceFirst(this string str, string search, string replace, StringComparison comparisonType = StringComparison.Ordinal)
|
||||
{
|
||||
|
||||
var pos = str.IndexOf(search, comparisonType);
|
||||
if (pos < 0)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
return str.Substring(0, pos) + replace + str.Substring(pos + search.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a substring of a string from end of the string.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="str"/> is null</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="len"/> is bigger that string's length</exception>
|
||||
public static string Right(this string str, int len)
|
||||
{
|
||||
|
||||
if (str.Length < len)
|
||||
{
|
||||
throw new ArgumentException("len argument can not be bigger than given string's length!");
|
||||
}
|
||||
|
||||
return str.Substring(str.Length - len, len);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses string.Split method to split given string by given separator.
|
||||
/// </summary>
|
||||
public static string[] Split(this string str, string separator)
|
||||
{
|
||||
return str.Split(new[] { separator }, StringSplitOptions.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses string.Split method to split given string by given separator.
|
||||
/// </summary>
|
||||
public static string[] Split(this string str, string separator, StringSplitOptions options)
|
||||
{
|
||||
return str.Split(new[] { separator }, options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses string.Split method to split given string by <see cref="Environment.NewLine"/>.
|
||||
/// </summary>
|
||||
public static string[] SplitToLines(this string str)
|
||||
{
|
||||
return str.Split(Environment.NewLine);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses string.Split method to split given string by <see cref="Environment.NewLine"/>.
|
||||
/// </summary>
|
||||
public static string[] SplitToLines(this string str, StringSplitOptions options)
|
||||
{
|
||||
return str.Split(Environment.NewLine, options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts PascalCase string to camelCase string.
|
||||
/// </summary>
|
||||
/// <param name="str">String to convert</param>
|
||||
/// <param name="useCurrentCulture">set true to use current culture. Otherwise, invariant culture will be used.</param>
|
||||
/// <param name="handleAbbreviations">set true to if you want to convert 'XYZ' to 'xyz'.</param>
|
||||
/// <returns>camelCase of the string</returns>
|
||||
public static string ToCamelCase(this string str, bool useCurrentCulture = false, bool handleAbbreviations = false)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
if (str.Length == 1)
|
||||
{
|
||||
return useCurrentCulture ? str.ToLower() : str.ToLowerInvariant();
|
||||
}
|
||||
|
||||
if (handleAbbreviations && IsAllUpperCase(str))
|
||||
{
|
||||
return useCurrentCulture ? str.ToLower() : str.ToLowerInvariant();
|
||||
}
|
||||
|
||||
return (useCurrentCulture ? char.ToLower(str[0]) : char.ToLowerInvariant(str[0])) + str.Substring(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts given PascalCase/camelCase string to sentence (by splitting words by space).
|
||||
/// Example: "ThisIsSampleSentence" is converted to "This is a sample sentence".
|
||||
/// </summary>
|
||||
/// <param name="str">String to convert.</param>
|
||||
/// <param name="useCurrentCulture">set true to use current culture. Otherwise, invariant culture will be used.</param>
|
||||
public static string ToSentenceCase(this string str, bool useCurrentCulture = false)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
return useCurrentCulture
|
||||
? Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + " " + char.ToLower(m.Value[1]))
|
||||
: Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + " " + char.ToLowerInvariant(m.Value[1]));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts given PascalCase/camelCase string to kebab-case.
|
||||
/// </summary>
|
||||
/// <param name="str">String to convert.</param>
|
||||
/// <param name="useCurrentCulture">set true to use current culture. Otherwise, invariant culture will be used.</param>
|
||||
public static string ToKebabCase(this string str, bool useCurrentCulture = false)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
str = str.ToCamelCase();
|
||||
|
||||
return useCurrentCulture
|
||||
? Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + "-" + char.ToLower(m.Value[1]))
|
||||
: Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + "-" + char.ToLowerInvariant(m.Value[1]));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts given PascalCase/camelCase string to snake case.
|
||||
/// Example: "ThisIsSampleSentence" is converted to "this_is_a_sample_sentence".
|
||||
/// https://github.com/npgsql/npgsql/blob/dev/src/Npgsql/NameTranslation/NpgsqlSnakeCaseNameTranslator.cs#L51
|
||||
/// </summary>
|
||||
/// <param name="str">String to convert.</param>
|
||||
/// <returns></returns>
|
||||
public static string ToSnakeCase(this string str)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
var builder = new StringBuilder(str.Length + Math.Min(2, str.Length / 5));
|
||||
var previousCategory = default(UnicodeCategory?);
|
||||
|
||||
for (var currentIndex = 0; currentIndex < str.Length; currentIndex++)
|
||||
{
|
||||
var currentChar = str[currentIndex];
|
||||
if (currentChar == '_')
|
||||
{
|
||||
builder.Append('_');
|
||||
previousCategory = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
var currentCategory = char.GetUnicodeCategory(currentChar);
|
||||
switch (currentCategory)
|
||||
{
|
||||
case UnicodeCategory.UppercaseLetter:
|
||||
case UnicodeCategory.TitlecaseLetter:
|
||||
if (previousCategory == UnicodeCategory.SpaceSeparator ||
|
||||
previousCategory == UnicodeCategory.LowercaseLetter ||
|
||||
previousCategory != UnicodeCategory.DecimalDigitNumber &&
|
||||
previousCategory != null &&
|
||||
currentIndex > 0 &&
|
||||
currentIndex + 1 < str.Length &&
|
||||
char.IsLower(str[currentIndex + 1]))
|
||||
{
|
||||
builder.Append('_');
|
||||
}
|
||||
|
||||
currentChar = char.ToLower(currentChar);
|
||||
break;
|
||||
|
||||
case UnicodeCategory.LowercaseLetter:
|
||||
case UnicodeCategory.DecimalDigitNumber:
|
||||
if (previousCategory == UnicodeCategory.SpaceSeparator)
|
||||
{
|
||||
builder.Append('_');
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
if (previousCategory != null)
|
||||
{
|
||||
previousCategory = UnicodeCategory.SpaceSeparator;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
builder.Append(currentChar);
|
||||
previousCategory = currentCategory;
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts string to enum value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of enum</typeparam>
|
||||
/// <param name="value">String value to convert</param>
|
||||
/// <returns>Returns enum object</returns>
|
||||
public static T ToEnum<T>(this string value)
|
||||
where T : struct
|
||||
{
|
||||
ThrowIf.IsNull(value, nameof(value));
|
||||
return (T)Enum.Parse(typeof(T), value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts string to enum value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of enum</typeparam>
|
||||
/// <param name="value">String value to convert</param>
|
||||
/// <param name="ignoreCase">Ignore case</param>
|
||||
/// <returns>Returns enum object</returns>
|
||||
public static T ToEnum<T>(this string value, bool ignoreCase)
|
||||
where T : struct
|
||||
{
|
||||
ThrowIf.IsNull(value, nameof(value));
|
||||
return (T)Enum.Parse(typeof(T), value, ignoreCase);
|
||||
}
|
||||
|
||||
public static string ToMd5(this string str)
|
||||
{
|
||||
using (var md5 = MD5.Create())
|
||||
{
|
||||
var inputBytes = Encoding.UTF8.GetBytes(str);
|
||||
var hashBytes = md5.ComputeHash(inputBytes);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
foreach (var hashByte in hashBytes)
|
||||
{
|
||||
sb.Append(hashByte.ToString("X2"));
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts camelCase string to PascalCase string.
|
||||
/// </summary>
|
||||
/// <param name="str">String to convert</param>
|
||||
/// <param name="useCurrentCulture">set true to use current culture. Otherwise, invariant culture will be used.</param>
|
||||
/// <returns>PascalCase of the string</returns>
|
||||
public static string ToPascalCase(this string str, bool useCurrentCulture = false)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
if (str.Length == 1)
|
||||
{
|
||||
return useCurrentCulture ? str.ToUpper() : str.ToUpperInvariant();
|
||||
}
|
||||
|
||||
return (useCurrentCulture ? char.ToUpper(str[0]) : char.ToUpperInvariant(str[0])) + str.Substring(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a substring of a string from beginning of the string if it exceeds maximum length.
|
||||
/// </summary>
|
||||
public static string Truncate(this string str, int maxLength)
|
||||
{
|
||||
if (str.Length <= maxLength)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
return str.Left(maxLength);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a substring of a string from Ending of the string if it exceeds maximum length.
|
||||
/// </summary>
|
||||
public static string TruncateFromBeginning(this string str, int maxLength)
|
||||
{
|
||||
if (str.Length <= maxLength)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
return str.Right(maxLength);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a substring of a string from beginning of the string if it exceeds maximum length.
|
||||
/// It adds a "..." postfix to end of the string if it's truncated.
|
||||
/// Returning string can not be longer than maxLength.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="str"/> is null</exception>
|
||||
public static string TruncateWithPostfix(this string str, int maxLength)
|
||||
{
|
||||
return TruncateWithPostfix(str, maxLength, "...");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a substring of a string from beginning of the string if it exceeds maximum length.
|
||||
/// It adds given <paramref name="postfix"/> to end of the string if it's truncated.
|
||||
/// Returning string can not be longer than maxLength.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="str"/> is null</exception>
|
||||
public static string TruncateWithPostfix(this string str, int maxLength, string postfix)
|
||||
{
|
||||
if (str == string.Empty || maxLength == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (str.Length <= maxLength)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
if (maxLength <= postfix.Length)
|
||||
{
|
||||
return postfix.Left(maxLength);
|
||||
}
|
||||
|
||||
return str.Left(maxLength - postfix.Length) + postfix;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts given string to a byte array using <see cref="Encoding.UTF8"/> encoding.
|
||||
/// </summary>
|
||||
public static byte[] GetBytes(this string str)
|
||||
{
|
||||
return str.GetBytes(Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts given string to a byte array using the given <paramref name="encoding"/>
|
||||
/// </summary>
|
||||
public static byte[] GetBytes([NotNull] this string str, [NotNull] Encoding encoding)
|
||||
{
|
||||
ThrowIf.IsNull(str, nameof(str));
|
||||
ThrowIf.IsNull(encoding, nameof(encoding));
|
||||
|
||||
return encoding.GetBytes(str);
|
||||
}
|
||||
|
||||
private static bool IsAllUpperCase(string input)
|
||||
{
|
||||
for (int i = 0; i < input.Length; i++)
|
||||
{
|
||||
if (Char.IsLetter(input[i]) && !Char.IsUpper(input[i]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts snake_case string to PascalCase string.
|
||||
/// </summary>
|
||||
/// <param name="str">String to convert</param>
|
||||
/// <param name="useCurrentCulture">set true to use current culture. Otherwise, invariant culture will be used.</param>
|
||||
/// <returns>PascalCase of the string</returns>
|
||||
public static string SnakeToPascalCase(this string str, bool useCurrentCulture = false)
|
||||
{
|
||||
var sArr = str.Split(new char[] { '-', '_' }).Select(a => a.ToPascalCase(useCurrentCulture));
|
||||
return string.Join("", sArr);
|
||||
}
|
||||
/// <summary>
|
||||
/// Converts snake_case string to PascalCase string.
|
||||
/// </summary>
|
||||
/// <param name="str">String to convert</param>
|
||||
/// <param name="useCurrentCulture">set true to use current culture. Otherwise, invariant culture will be used.</param>
|
||||
/// <returns>PascalCase of the string</returns>
|
||||
public static string SnakeToCamelCase(this string str, bool useCurrentCulture = false)
|
||||
{
|
||||
return SnakeToPascalCase(str, useCurrentCulture).ToCamelCase();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
using JNPF.Common.Security;
|
||||
using Mapster;
|
||||
using SqlSugar;
|
||||
using Tnb.Core;
|
||||
using Tnb.Vengine.Domain;
|
||||
|
||||
namespace Tnb.Vengine;
|
||||
@@ -21,7 +22,7 @@ public class VmodelMapper : IRegister
|
||||
config.ForType<VmGetListInput, VmQueryInput>()
|
||||
.Map(dest => dest.q, src => string.IsNullOrEmpty(src.q) ? null : src.q.ToObject<DObject>());
|
||||
config.ForType<DbColumnInfo, VmDbProp>()
|
||||
.Map(dest => dest.code, src => src.DbColumnName.SnakeToCamelCase(false))
|
||||
.Map(dest => dest.code, src => src.DbColumnName.ToCamel())
|
||||
.Map(dest => dest.name, src => src.ColumnDescription)
|
||||
.Map(dest => dest.field, src => src.DbColumnName)
|
||||
.Map(dest => dest.dataType, src => src.DataType)
|
||||
|
||||
@@ -7,7 +7,12 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\common\Tnb.Common.Core\Tnb.Common.Core.csproj" />
|
||||
<ProjectReference Include="..\..\common\Tnb.Common\Tnb.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Extension\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,137 +0,0 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace System;
|
||||
|
||||
public static class ThrowIf
|
||||
{
|
||||
public static void When(bool isMatch, string msg)
|
||||
{
|
||||
if (isMatch)
|
||||
{
|
||||
throw new ArgumentException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static T IsNull<T>([NotNull] T? value, string? msg = null, [CallerArgumentExpression("value")] string? paraName = null)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw string.IsNullOrEmpty(msg) ? new ArgumentNullException(paraName) : new ArgumentException(msg);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static T IsNullOrDefault<T>([NotNull] T? value, string? msg = null, [CallerArgumentExpression("value")] string? paraName = null) where T : struct
|
||||
{
|
||||
if (!value.HasValue || value.Value.Equals(default(T)))
|
||||
{
|
||||
throw string.IsNullOrEmpty(msg) ? new ArgumentException("值不可为空或默认值", paraName) : new ArgumentException(msg);
|
||||
}
|
||||
|
||||
return value.Value;
|
||||
}
|
||||
|
||||
|
||||
public static string IsNullOrWhiteSpace([NotNull] string? value, string? msg = null, [CallerArgumentExpression("value")] string? paraName = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
throw string.IsNullOrEmpty(msg) ? new ArgumentException("值不可为空或空格", paraName) : new ArgumentException(msg);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public static string IsNullOrEmpty([NotNull] string? value, string? msg = null, [CallerArgumentExpression("value")] string? paraName = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
throw string.IsNullOrEmpty(msg) ? new ArgumentException("值不可为空", paraName) : new ArgumentException(msg);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
//public static ICollection<T> NotNullOrEmpty<T>(ICollection<T> value, string paraName)
|
||||
//{
|
||||
// if (value.IsNullOrEmpty())
|
||||
// {
|
||||
// throw new ArgumentException(paraName + " can not be null or empty!", paraName);
|
||||
// }
|
||||
|
||||
// return value;
|
||||
//}
|
||||
|
||||
//public static Type AssignableTo<TBaseType>(Type type, string paraName)
|
||||
//{
|
||||
// NotNull(type, paraName);
|
||||
// if (!type.IsAssignableTo<TBaseType>())
|
||||
// {
|
||||
// throw new ArgumentException(paraName + " (type of " + type.AssemblyQualifiedName + ") should be assignable to the " + typeof(TBaseType).GetFullNameWithAssemblyName() + "!");
|
||||
// }
|
||||
|
||||
// return type;
|
||||
//}
|
||||
|
||||
public static short OutOfRange(short value, string paraName, short minimumValue, short maximumValue = short.MaxValue)
|
||||
{
|
||||
if (value < minimumValue || value > maximumValue)
|
||||
{
|
||||
throw new ArgumentException($"{paraName} is out of range min: {minimumValue} - max: {maximumValue}");
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static int OutOfRange(int value, string paraName, int minimumValue, int maximumValue = int.MaxValue)
|
||||
{
|
||||
if (value < minimumValue || value > maximumValue)
|
||||
{
|
||||
throw new ArgumentException($"{paraName} is out of range min: {minimumValue} - max: {maximumValue}");
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static long OutOfRange(long value, string paraName, long minimumValue, long maximumValue = long.MaxValue)
|
||||
{
|
||||
if (value < minimumValue || value > maximumValue)
|
||||
{
|
||||
throw new ArgumentException($"{paraName} is out of range min: {minimumValue} - max: {maximumValue}");
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static float OutOfRange(float value, string paraName, float minimumValue, float maximumValue = float.MaxValue)
|
||||
{
|
||||
if (value < minimumValue || value > maximumValue)
|
||||
{
|
||||
throw new ArgumentException($"{paraName} is out of range min: {minimumValue} - max: {maximumValue}");
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static double OutOfRange(double value, string paraName, double minimumValue, double maximumValue = double.MaxValue)
|
||||
{
|
||||
if (value < minimumValue || value > maximumValue)
|
||||
{
|
||||
throw new ArgumentException($"{paraName} is out of range min: {minimumValue} - max: {maximumValue}");
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static decimal OutOfRange(decimal value, string paraName, decimal minimumValue, decimal maximumValue = decimal.MaxValue)
|
||||
{
|
||||
if (value < minimumValue || value > maximumValue)
|
||||
{
|
||||
throw new ArgumentException($"{paraName} is out of range min: {minimumValue} - max: {maximumValue}");
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user