238 lines
9.6 KiB
C#
238 lines
9.6 KiB
C#
/////////////////////////////////////////////////////////////////////////////////
|
|
// 宁波拓通e智造平台 ToTong Next Builder //
|
|
// https://git.tuotong-tech.com/tnb/tnb-server //
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
using JNPF;
|
|
using JNPF.Common.Security;
|
|
using JNPF.ViewEngine;
|
|
using Mapster;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using SqlSugar;
|
|
using Tnb.Core;
|
|
using Tnb.Vengine.DataAccess;
|
|
using Tnb.Vengine.Domain;
|
|
|
|
namespace Tnb.Vengine.AppService;
|
|
|
|
/// <summary>
|
|
/// 视图模型服务类
|
|
/// </summary>
|
|
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, KeepVerb = true, Order = 1102)]
|
|
[Route("api/[area]/[controller]/[action]")]
|
|
public class VmodelAppService : VengineAppService<Vmodel>, IVmodelAppService
|
|
{
|
|
private readonly IViewEngine _viewEngine;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
public VmodelAppService(IDataAccess da, IViewEngine viewEngine) : base(da)
|
|
{
|
|
_viewEngine = viewEngine;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取一条 数据信息
|
|
/// </summary>
|
|
public override async Task<dynamic> GetAsync(VmGetInput input)
|
|
{
|
|
VmodelGetInput para = new VmodelGetInput();
|
|
if (!string.IsNullOrEmpty(input.q))
|
|
{
|
|
para = input.q.ToObject<VmodelGetInput>();
|
|
}
|
|
var query = _db.Queryable<Vmodel>().Where(a => a.deleted == 0);
|
|
Vmodel? vm = null;
|
|
if (!string.IsNullOrEmpty(input.id))
|
|
{
|
|
vm = await _dataAccess.GetVmodelAsync(input.id, para.drill);
|
|
}
|
|
else if (!string.IsNullOrEmpty(para.areaCode) && !string.IsNullOrEmpty(para.vmCode))
|
|
{
|
|
vm = await _dataAccess.GetVmodelAsync(para.areaCode, para.vmCode, para.drill);
|
|
}
|
|
ThrowIf.IsNull(vm, "输入参数有误, id 和 areaCode,vmCode 不可同时为空");
|
|
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.Adapt<List<DObject>>();
|
|
return ret;
|
|
}
|
|
|
|
[NonAction]
|
|
public override Task<VmPagedOutput> ListAsync(VmQueryInput input)
|
|
{
|
|
return base.ListAsync(input);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增 模型
|
|
/// </summary>
|
|
public override async Task<dynamic> CreateAsync(VmCreateInput input)
|
|
{
|
|
ThrowIf.IsNull(input.data);
|
|
//ArgumentNullException.ThrowIfNull(input.data);
|
|
Vmodel vm = input.data.ToObject<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.ToObject<Vmodel>();
|
|
vm.areaCode = vm.areaCode.ToKebab();
|
|
vm.vmCode = vm.vmCode.ToKebab();
|
|
vm.navProps.ForEach(a => a.naviModel = null);
|
|
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 n = tb.Name.IndexOf('_');
|
|
if (string.IsNullOrEmpty(input.removePrefix) && n > 0 && n < 5)
|
|
{
|
|
input.removePrefix = tb.Name.Substring(0, n);
|
|
}
|
|
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;
|
|
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;
|
|
}
|
|
} |