/////////////////////////////////////////////////////////////////////////////////
// 宁波拓通e智造平台 ToTong Next Builder //
// https://git.tuotong-tech.com/tnb/tnb-server //
/////////////////////////////////////////////////////////////////////////////////
using JNPF;
using JNPF.Common.Manager;
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;
///
/// 视图模型服务类
///
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, KeepVerb = true, Order = 1102)]
[Route("api/[area]/[controller]/[action]")]
public class VmodelAppService : VengineAppService, IVmodelAppService
{
private readonly IViewEngine _viewEngine;
private readonly ICacheManager _cache;
///
/// 构造函数
///
public VmodelAppService(IDataAccess da, IViewEngine viewEngine, ICacheManager cache) : base(da)
{
_viewEngine = viewEngine;
_cache = cache;
}
///
/// 获取一条 数据信息, q参数无效
///
public override async Task GetAsync(VmodelGetInput input)
{
var query = _db.Queryable().Where(a => a.deleted == 0);
Vmodel? vm = null;
if (!string.IsNullOrEmpty(input.id))
{
vm = await _dataAccess.GetVmodelAsync(input.id, input.drill);
}
else if (!string.IsNullOrEmpty(input.areaCode) && !string.IsNullOrEmpty(input.vmCode))
{
vm = await _dataAccess.GetVmodelAsync(input.areaCode, input.vmCode, input.drill);
}
ThrowIf.IsNull(vm, "输入参数有误, id 和 areaCode,vmCode 不可同时为空");
return vm;
}
///
/// 获取多条 数据列表
///
public override async Task> GetListAsync(VmGetListInput input)
{
return await ListAsync(input.Adapt());
}
[NonAction]
public override async Task> ListAsync(VmQueryInput input)
{
var q = _db.Queryable().WhereIF(!string.IsNullOrEmpty(input.k), a => a.vmCode.Contains(input.k!) || a.vmName.Contains(input.k!));
RefAsync total = 0;
var data = await q.OrderBy(input.sort).ToPageListAsync((input.pnum - 1) * input.psize, input.psize, total);
return PagedOutput.Create(total, data);
}
///
/// 新增 模型
///
public override async Task CreateAsync(VmodelCreateInput input)
{
ThrowIf.IsNull(input.data);
//ArgumentNullException.ThrowIfNull(input.data);
Vmodel vm = input.data.ToObject();
vm.areaCode = vm.areaCode.ToKebab();
vm.vmCode = vm.vmCode.ToKebab();
await _db.Insertable(vm).ExecuteCommandAsync();
return vm;
}
///
/// 更新 数据
///
public override async Task UpdateAsync(VmodelUpdateInput input)
{
ThrowIf.IsNull(input.data);
//ArgumentNullException.ThrowIfNull(input.data);
Vmodel vm = input.data.ToObject();
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();
await _cache.DelAsync(_dataAccess.GetVmodelCacheKey(vm.id));
return vm;
}
///
/// 删除 数据
///
public override async Task DeleteAsync(VmDeleteInput input)
{
var ret = await _db.Deleteable(input.id).ExecuteCommandAsync();
await _cache.DelAsync(_dataAccess.GetVmodelCacheKey(input.id!));
return ret;
}
///
/// 从数据表创建模型
///
public async Task> 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 lsToAdd = new List();
List lsToUpdate = new List();
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();
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().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();
}
///
/// 生成模型代码
///
[HttpGet]
public async Task 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;
}
}