346 lines
16 KiB
C#
346 lines
16 KiB
C#
/////////////////////////////////////////////////////////////////////////////////
|
|
// 宁波拓通e智造平台 ToTong Next Builder //
|
|
// https://git.tuotong-tech.com/tnb/tnb-server //
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
using JNPF;
|
|
using JNPF.Common.Core.Manager;
|
|
using JNPF.Common.Extension;
|
|
using JNPF.Common.Manager;
|
|
using JNPF.Common.Security;
|
|
using JNPF.ViewEngine;
|
|
using Mapster;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using Org.BouncyCastle.Crypto;
|
|
using SqlSugar;
|
|
using Tnb.Core;
|
|
using Tnb.Vengine.DataAccess;
|
|
using Tnb.Vengine.Domain;
|
|
using Yitter.IdGenerator;
|
|
|
|
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, VmodelGetInput, Vmodel, VmQueryInput, Vmodel, VmodelCreateInput>, IVmodelAppService
|
|
{
|
|
private readonly IViewEngine _viewEngine;
|
|
private readonly ICacheManager _cache;
|
|
private readonly IUserManager _user;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
public VmodelAppService(IDataAccess da, IViewEngine viewEngine, ICacheManager cache, IUserManager user) : base(da)
|
|
{
|
|
_viewEngine = viewEngine;
|
|
_cache = cache;
|
|
_user = user;
|
|
}
|
|
|
|
protected override Task<Vmodel> GetVmodelAsync()
|
|
{
|
|
var vm = new Vmodel
|
|
{
|
|
|
|
//id = "30285088677397",
|
|
areaCode = "tnb",
|
|
vmCode = "vmodel",
|
|
vmName = "视图模型",
|
|
dbCode = "",
|
|
tableName = "sys_vmodel",
|
|
dbProps = new List<VmDbProp> {
|
|
new VmDbProp{field="id",csType="string",length=50,digit=0,ordinal=1,required=true,pkey=true,fuzzy=false,defValue=null,descrip=null,code="id",name="主键标识"},
|
|
new VmDbProp{field="area_code",csType="string",length=50,digit=0,ordinal=2,required=true,pkey=false,fuzzy=false,defValue=null,descrip=null,code="areaCode",name="模块代码"},
|
|
new VmDbProp{field="vm_code",csType="string",length=100,digit=0,ordinal=3,required=true,pkey=false,fuzzy=true,defValue=null,descrip=null,code="vmCode",name="视图代码"},
|
|
new VmDbProp{field="vm_name",csType="string",length=100,digit=0,ordinal=4,required=true,pkey=false,fuzzy=true,defValue=null,descrip=null,code="vmName",name="视图名称"},
|
|
new VmDbProp{field="db_code",csType="string",length=50,digit=0,ordinal=5,required=false,pkey=false,fuzzy=false,defValue=null,descrip=null,code="dbCode",name="数据库连接"},
|
|
new VmDbProp{field="table_name",csType="string",length=50,digit=0,ordinal=6,required=true,pkey=false,fuzzy=true,defValue=null,descrip=null,code="tableName",name="主表名称"},
|
|
new VmDbProp{field="db_props",csType="string",length=0,digit=0,ordinal=7,required=true,pkey=false,fuzzy=false,defValue=null,descrip=null,code="dbProps",name="表字段属性"},
|
|
new VmDbProp{field="nav_props",csType="string",length=0,digit=0,ordinal=8,required=false,pkey=false,fuzzy=false,defValue=null,descrip=null,code="navProps",name="导航属性"},
|
|
new VmDbProp{field="cal_props",csType="string",length=0,digit=0,ordinal=9,required=false,pkey=false,fuzzy=false,defValue=null,descrip=null,code="calProps",name="计算配置"},
|
|
new VmDbProp{field="ordinal",csType="int",length=0,digit=0,ordinal=10,required=true,pkey=false,fuzzy=false,defValue=null,descrip=null,code="ordinal",name="排序"},
|
|
new VmDbProp{field="soft_delete",csType="short",length=0,digit=0,ordinal=11,required=true,pkey=false,fuzzy=false,defValue=null,descrip=null,code="softDelete",name="软删除"},
|
|
new VmDbProp{field="enabled",csType="short",length=0,digit=0,ordinal=12,required=true,pkey=false,fuzzy=false,defValue=null,descrip=null,code="enabled",name="是否激活"},
|
|
new VmDbProp{field="deleted",csType="short",length=0,digit=0,ordinal=13,required=true,pkey=false,fuzzy=false,defValue=null,descrip=null,code="deleted",name="是否删除"},
|
|
new VmDbProp{field="descrip",csType="string",length=250,digit=0,ordinal=14,required=false,pkey=false,fuzzy=false,defValue=null,descrip=null,code="descrip",name="描述"},
|
|
new VmDbProp{field="create_time",csType="DateTime",length=0,digit=0,ordinal=15,required=true,pkey=false,fuzzy=false,defValue=null,descrip=null,code="createTime",name="创建时间"},
|
|
new VmDbProp{field="create_id",csType="string",length=50,digit=0,ordinal=16,required=false,pkey=false,fuzzy=false,defValue=null,descrip=null,code="createId",name="创建人"},
|
|
new VmDbProp{field="modify_time",csType="DateTime",length=0,digit=0,ordinal=17,required=false,pkey=false,fuzzy=false,defValue=null,descrip=null,code="modifyTime",name="修改时间"},
|
|
new VmDbProp{field="modify_id",csType="string",length=50,digit=0,ordinal=18,required=false,pkey=false,fuzzy=false,defValue=null,descrip=null,code="modifyId",name="修改人"}
|
|
},
|
|
navProps = [],
|
|
calProps = [],
|
|
ordinal = 0,
|
|
softDelete = 0,
|
|
enabled = 1,
|
|
deleted = 0,
|
|
descrip = null
|
|
};
|
|
return Task.FromResult(vm);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取一条 数据信息, q参数无效
|
|
/// </summary>
|
|
public override async Task<Vmodel> GetAsync(VmodelGetInput input)
|
|
{
|
|
input.LoadFromHttpContext(App.HttpContext);
|
|
var query = _db.Queryable<Vmodel>().Where(a => a.deleted == 0);
|
|
Vmodel? vm = null;
|
|
if (!string.IsNullOrEmpty(input.id))
|
|
{
|
|
vm = await _dataAccess.GetVmodelAsync(input.id);
|
|
}
|
|
else if (input.q.ContainsKey("areaCode") && input.q.ContainsKey("vmCode"))
|
|
{
|
|
vm = await _dataAccess.GetVmodelAsync(input.q["areaCode"].ToString()!, input.q["vmCode"].ToString()!);
|
|
}
|
|
ThrowIf.IsNull(vm, "输入参数有误, id 和 areaCode,vmCode 不可同时为空");
|
|
if (input.drill)
|
|
{
|
|
List<string> vmids = vm.navProps.Select(a => a.vmid).Distinct().ToList();
|
|
if (vmids.Count > 0)
|
|
{
|
|
List<Vmodel> ls = await _db.Queryable<Vmodel>().Where(a => vmids.Contains(a.id)).ToListAsync();
|
|
Dictionary<string, Vmodel> navs = ls.ToDictionary(a => a.id);
|
|
foreach (VmNavProp navProp in vm.navProps)
|
|
{
|
|
navProp.naviModel = navs.GetOrDefault(navProp.vmid);
|
|
}
|
|
}
|
|
}
|
|
//var serializer = new Newtonsoft.Json.JsonSerializer();
|
|
//var stringWriter = new StringWriter();
|
|
//using (var writer = new Newtonsoft.Json.JsonTextWriter(stringWriter))
|
|
//{
|
|
// writer.QuoteName = false;
|
|
// serializer.Serialize(writer, vm);
|
|
//}
|
|
//var json = stringWriter.ToString();
|
|
|
|
return vm;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取多条 数据列表
|
|
/// </summary>
|
|
public override async Task<PagedOutput<Vmodel>> GetListAsync(VmQueryInput input)
|
|
{
|
|
input.LoadFromHttpContext(App.HttpContext);
|
|
return await ListAsync(input);
|
|
}
|
|
|
|
[NonAction]
|
|
public override async Task<PagedOutput<Vmodel>> ListAsync(VmQueryInput input)
|
|
{
|
|
PagedOutput<Vmodel> result = new PagedOutput<Vmodel>();
|
|
var vm = await GetVmodelAsync();
|
|
var ls = await _dataAccess.QueryDataAsync(vm, input);
|
|
foreach (var item in ls.items)
|
|
{
|
|
item["dbProps"] = JsonConvert.DeserializeObject<List<VmDbProp>>(item["dbProps"].ToString()!)!;
|
|
item["navProps"] = JsonConvert.DeserializeObject<List<VmNavProp>>(item["navProps"].ToString()!)!;
|
|
item["calProps"] = JsonConvert.DeserializeObject<List<VmCalProp>>(item["calProps"].ToString()!)!;
|
|
}
|
|
|
|
return ls.ToPagedOutput<Vmodel>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增 模型
|
|
/// </summary>
|
|
public override async Task<dynamic> CreateAsync(VmodelCreateInput input)
|
|
{
|
|
ThrowIf.IsNull(input.data);
|
|
Vmodel vm = input.data.Adapt<Vmodel>();
|
|
vm.id = YitIdHelper.NextId().ToString();
|
|
vm.areaCode = vm.areaCode.ToKebab();
|
|
vm.vmCode = vm.vmCode.ToKebab();
|
|
vm.createTime = DateTime.Now;
|
|
vm.createId = _user.UserId;
|
|
await _db.Insertable(vm).ExecuteCommandAsync();
|
|
return vm;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新 数据
|
|
/// </summary>
|
|
public override async Task<dynamic> UpdateAsync(VmEditInput input)
|
|
{
|
|
var ret = await base.UpdateAsync(input);
|
|
if (input.data != null)
|
|
{
|
|
var id = ret as string;
|
|
await _cache.DelAsync(_dataAccess.GetVmodelCacheKey(id!));
|
|
}
|
|
else if (input.items != null)
|
|
{
|
|
var ids = ret as List<string>;
|
|
foreach (var item in ids!)
|
|
{
|
|
await _cache.DelAsync(_dataAccess.GetVmodelCacheKey(item));
|
|
}
|
|
}
|
|
return ret;
|
|
//vm.areaCode = vm.areaCode.ToKebab();
|
|
//vm.vmCode = vm.vmCode.ToKebab();
|
|
//vm.navProps.ForEach(a => a.naviModel = null);
|
|
//vm.modifyTime = DateTime.Now;
|
|
//vm.modifyId = _user.UserId;
|
|
//await _db.Updateable(vm).WhereColumns(a => a.id).ExecuteCommandAsync();
|
|
//return vm;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除 数据
|
|
/// </summary>
|
|
public override async Task<dynamic> DeleteAsync(VmDeleteInput input)
|
|
{
|
|
List<string> ids = new();
|
|
if (input.id != null)
|
|
{
|
|
ids.Add(input.id);
|
|
}
|
|
else if (input.ids != null)
|
|
{
|
|
ids.AddRange(input.ids);
|
|
}
|
|
|
|
var ret = await _db.Updateable<Vmodel>().SetColumns(a => a.deleted, 1).Where(a => ids.Contains(a.id)).ExecuteCommandAsync();
|
|
foreach (var item in ids)
|
|
{
|
|
await _cache.DelAsync(_dataAccess.GetVmodelCacheKey(item));
|
|
}
|
|
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;
|
|
}
|
|
|
|
}
|