refactor: 😀代码优化
This commit is contained in:
parent
007cddb34c
commit
98930a4add
@ -88,10 +88,10 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
/// <returns></returns>
|
||||
private List<SysCodeGenTemplateRelation> GetCodeGenTemplateRelation(long codeGenId, List<long> templateIds)
|
||||
{
|
||||
List<SysCodeGenTemplateRelation> list = new List<SysCodeGenTemplateRelation>();
|
||||
List<SysCodeGenTemplateRelation> list = new();
|
||||
foreach (var item in templateIds)
|
||||
{
|
||||
SysCodeGenTemplateRelation relation = new SysCodeGenTemplateRelation();
|
||||
SysCodeGenTemplateRelation relation = new();
|
||||
relation.CodeGenId = codeGenId;
|
||||
relation.TemplateId = item;
|
||||
list.Add(relation);
|
||||
@ -408,20 +408,116 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
[DisplayName("执行代码生成")]
|
||||
public async Task<dynamic> RunLocal(SysCodeGen input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input.GenerateType))
|
||||
input.GenerateType = "200";
|
||||
if (string.IsNullOrEmpty(input.GenerateType)) input.GenerateType = "200";
|
||||
|
||||
string outputPath = Path.Combine(App.WebHostEnvironment.WebRootPath, "codeGen", input.TableName!);
|
||||
if (Directory.Exists(outputPath))
|
||||
Directory.Delete(outputPath, true);
|
||||
if (Directory.Exists(outputPath)) Directory.Delete(outputPath, true);
|
||||
|
||||
var tableFieldList = await _codeGenConfigService.GetList(new CodeGenConfig() { CodeGenId = input.Id }); // 字段集合
|
||||
var tableFieldList = await _codeGenConfigService.GetList(new CodeGenConfig { CodeGenId = input.Id }); // 字段集合
|
||||
|
||||
#region 构造前端需要验证的数据
|
||||
ProcessTableFieldList(tableFieldList); // 处理字段集合
|
||||
|
||||
var queryWhetherList = tableFieldList.Where(u => u.QueryWhether == YesNoEnum.Y.ToString()).ToList(); // 前端查询集合
|
||||
var joinTableList = tableFieldList.Where(u => u.EffectType is "Upload" or "ForeignKey" or "ApiTreeSelector").ToList(); // 需要连表查询的字段
|
||||
|
||||
var data = CreateCustomViewEngine(input, tableFieldList, queryWhetherList, joinTableList); // 创建视图引擎数据
|
||||
|
||||
// 获得菜单
|
||||
var menuList = await GetMenus(input.TableName!, input.BusName!, input.MenuPid ?? 0, input.MenuIcon!, input.PagePath!, tableFieldList);
|
||||
if (input.GenerateMenu)
|
||||
{
|
||||
await AddMenu(menuList, input.MenuPid ?? 0); // 添加菜单
|
||||
}
|
||||
|
||||
// 模板
|
||||
var templateList = GetTemplateList(input);
|
||||
var templatePath = Path.Combine(App.WebHostEnvironment.WebRootPath, "template");
|
||||
for (var i = 0; i < templateList.Count; i++)
|
||||
{
|
||||
string tResult = await ProcessTemplate(templateList[i], input, templatePath, data, menuList); // 处理模板
|
||||
|
||||
string targetFile = templateList[i].OutputFile
|
||||
.Replace("{PagePath}", input.PagePath)
|
||||
.Replace("{TableName}", input.TableName)
|
||||
.Replace("{TableNameLower}", input.TableName?.ToFirstLower() ?? "");
|
||||
|
||||
string tmpPath;
|
||||
if (!input.GenerateType.StartsWith('1'))
|
||||
{
|
||||
if (templateList[i].Type == CodeGenTypeEnum.Frontend)
|
||||
tmpPath = Path.Combine(new DirectoryInfo(App.WebHostEnvironment.ContentRootPath).Parent!.Parent!.FullName, _codeGenOptions.FrontRootPath, "src");
|
||||
else
|
||||
tmpPath = Path.Combine(new DirectoryInfo(App.WebHostEnvironment.ContentRootPath).Parent!.FullName, input.NameSpace!);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmpPath = templateList[i].Type == CodeGenTypeEnum.Frontend ? Path.Combine(outputPath, _codeGenOptions.FrontRootPath, "src") : Path.Combine(outputPath, input!.NameSpace!);
|
||||
}
|
||||
targetFile = Path.Combine(tmpPath, targetFile);
|
||||
|
||||
var dirPath = new DirectoryInfo(targetFile).Parent!.FullName;
|
||||
if (!Directory.Exists(dirPath))
|
||||
Directory.CreateDirectory(dirPath);
|
||||
await File.WriteAllTextAsync(targetFile, tResult, Encoding.UTF8);
|
||||
}
|
||||
|
||||
// 非ZIP压缩返回空
|
||||
if (!input.GenerateType.StartsWith('1')) return null;
|
||||
|
||||
var downloadPath = outputPath + ".zip";
|
||||
if (File.Exists(downloadPath)) File.Delete(downloadPath); // 删除同名文件
|
||||
ZipFile.CreateFromDirectory(outputPath, downloadPath);
|
||||
return new { url = $"{App.HttpContext.Request.Scheme}://{App.HttpContext.Request.Host.Value}/codeGen/{input.TableName}.zip" };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取代码生成预览 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取代码生成预览")]
|
||||
public async Task<Dictionary<string, string>> Preview(SysCodeGen input)
|
||||
{
|
||||
var tableFieldList = await _codeGenConfigService.GetList(new CodeGenConfig { CodeGenId = input.Id }); // 字段集合
|
||||
|
||||
ProcessTableFieldList(tableFieldList); // 处理字段集合
|
||||
|
||||
var queryWhetherList = tableFieldList.Where(u => u.QueryWhether == YesNoEnum.Y.ToString()).ToList(); // 前端查询集合
|
||||
var joinTableList = tableFieldList.Where(u => u.EffectType is "Upload" or "ForeignKey" or "ApiTreeSelector").ToList(); // 需要连表查询的字段
|
||||
|
||||
var data = CreateCustomViewEngine(input, tableFieldList, queryWhetherList, joinTableList); // 创建视图引擎数据
|
||||
|
||||
// 获取模板文件并替换
|
||||
var templateList = GetTemplateList(input);
|
||||
var templatePath = Path.Combine(App.WebHostEnvironment.WebRootPath, "template");
|
||||
|
||||
await _db.Ado.BeginTranAsync();
|
||||
try
|
||||
{
|
||||
var menuList = await GetMenus(input.TableName!, input.BusName!, input.MenuPid ?? 0, input.MenuIcon!,
|
||||
input.PagePath!, tableFieldList);
|
||||
var result = new Dictionary<string, string>();
|
||||
foreach (var template in templateList)
|
||||
{
|
||||
string tResult = await ProcessTemplate(template, input, templatePath, data, menuList); // 处理模板
|
||||
result.Add(template.Name?.TrimEnd(".vm")!, tResult);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
finally
|
||||
{
|
||||
await _db.Ado.RollbackTranAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理字段集合
|
||||
/// </summary>
|
||||
/// <param name="tableFieldList"></param>
|
||||
private void ProcessTableFieldList(List<CodeGenConfig> tableFieldList)
|
||||
{
|
||||
foreach (var item in tableFieldList)
|
||||
{
|
||||
List<VerifyRuleItem> list = new List<VerifyRuleItem>();
|
||||
List<VerifyRuleItem> list = new();
|
||||
if (!string.IsNullOrWhiteSpace(item.Rules))
|
||||
{
|
||||
if (item.Rules != "[]")
|
||||
@ -438,14 +534,19 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
item.AnyRule = list.Count > 0;
|
||||
item.RemoteVerify = list.Any(t => t.Type == "remote");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 构造前端需要验证的数据
|
||||
|
||||
var queryWhetherList = tableFieldList.Where(u => u.QueryWhether == YesNoEnum.Y.ToString()).ToList(); // 前端查询集合
|
||||
var joinTableList = tableFieldList.Where(u => u.EffectType is "Upload" or "ForeignKey" or "ApiTreeSelector").ToList(); // 需要连表查询的字段
|
||||
//(string joinTableNames, string lowerJoinTableNames) = GetJoinTableStr(joinTableList); // 获取连表的实体名和别名
|
||||
|
||||
var data = new CustomViewEngine(_db)
|
||||
/// <summary>
|
||||
/// 创建视图引擎数据
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="tableFieldList"></param>
|
||||
/// <param name="queryWhetherList"></param>
|
||||
/// <param name="joinTableList"></param>
|
||||
/// <returns></returns>
|
||||
private CustomViewEngine CreateCustomViewEngine(SysCodeGen input, List<CodeGenConfig> tableFieldList, List<CodeGenConfig> queryWhetherList, List<CodeGenConfig> joinTableList)
|
||||
{
|
||||
return new CustomViewEngine(_db)
|
||||
{
|
||||
ConfigId = input.ConfigId!, // 库定位器名
|
||||
AuthorName = input.AuthorName!, // 作者
|
||||
@ -463,250 +564,85 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
IsApiService = input.IsApiService,
|
||||
RemoteVerify = tableFieldList.Any(t => t.RemoteVerify), // 远程验证
|
||||
TreeName = input.TreeName,
|
||||
LowerTreeName = string.IsNullOrEmpty(input.TreeName) ? "" : input.TreeName[..1].ToLower() + input.TreeName[1..], // 首字母小写
|
||||
LowerTreeName = input.TreeName?.ToFirstLower() ?? "", // 首字母小写
|
||||
LeftTab = input.LeftTab,
|
||||
LeftKey = input.LeftKey!,
|
||||
LeftPrimaryKey = input.LeftPrimaryKey,
|
||||
LeftName = input.LeftName,
|
||||
LowerLeftTab = string.IsNullOrEmpty(input.LeftTab) ? "" : input.LeftTab[..1].ToLower() + input.LeftTab[1..], // 首字母小写
|
||||
LowerLeftKey = string.IsNullOrEmpty(input.LeftKey) ? "" : input.LeftKey[..1].ToLower() + input.LeftKey[1..], // 首字母小写
|
||||
LowerLeftPrimaryKey = string.IsNullOrEmpty(input.LeftPrimaryKey) ? "" : input.LeftPrimaryKey[..1].ToLower() + input.LeftPrimaryKey[1..], // 首字母小写
|
||||
//LowerLeftPrimaryKey = CodeGenUtil.CamelColumnName(input.LeftPrimaryKey, entityBasePropertyNames),
|
||||
LowerLeftTab = input.LeftTab?.ToFirstLower() ?? "", // 首字母小写
|
||||
LowerLeftKey = input.LeftKey?.ToFirstLower() ?? "", // 首字母小写
|
||||
LowerLeftPrimaryKey = input.LeftPrimaryKey?.ToFirstLower() ?? "", // 首字母小写
|
||||
BottomTab = input.BottomTab,
|
||||
BottomKey = input.BottomKey!,
|
||||
BottomPrimaryKey = input.BottomPrimaryKey,
|
||||
LowerBottomTab = string.IsNullOrEmpty(input.BottomTab) ? "" : input.BottomTab[..1].ToLower() + input.BottomTab[1..], // 首字母小写
|
||||
LowerBottomKey = string.IsNullOrEmpty(input.BottomKey) ? "" : input.BottomKey[..1].ToLower() + input.BottomKey[1..], // 首字母小写
|
||||
LowerBottomPrimaryKey = string.IsNullOrEmpty(input.BottomPrimaryKey) ? "" : input.BottomPrimaryKey[..1].ToLower() + input.BottomPrimaryKey[1..], // 首字母小写
|
||||
LowerBottomTab = input.BottomTab?.ToFirstLower() ?? "", // 首字母小写
|
||||
LowerBottomKey = input.BottomKey?.ToFirstLower() ?? "", // 首字母小写
|
||||
LowerBottomPrimaryKey = input.BottomPrimaryKey?.ToFirstLower() ?? "", // 首字母小写
|
||||
};
|
||||
|
||||
// 获得菜单
|
||||
var menuList = await GetMenus(input.TableName!, input.BusName!, input.MenuPid ?? 0, input.MenuIcon!, input.PagePath!, tableFieldList);
|
||||
if (input.GenerateMenu)
|
||||
{
|
||||
await AddMenu(menuList, input.MenuPid ?? 0); // 添加菜单
|
||||
}
|
||||
|
||||
// 模板
|
||||
var templateList = GetTemplateList(input);
|
||||
var templatePath = Path.Combine(App.WebHostEnvironment.WebRootPath, "template");
|
||||
for (var i = 0; i < templateList.Count; i++)
|
||||
{
|
||||
string tResult; // 模板生成结果
|
||||
|
||||
var filename = templateList[i].Name;
|
||||
// 更改默认首页模板
|
||||
if (filename == "web_views_index.vue.vm")
|
||||
{
|
||||
filename = string.IsNullOrEmpty(input.LeftTab) ? filename : "web_views_LeftTree.vue.vm"; // 左树右列表
|
||||
filename = string.IsNullOrEmpty(input.BottomTab) ? filename : "web_views_BottomIndx.vue.vm"; // 左数右上列表下列表属性
|
||||
}
|
||||
var templateFilePath = Path.Combine(templatePath, filename);
|
||||
if (!File.Exists(templateFilePath)) continue;
|
||||
var tContent = await File.ReadAllTextAsync(templateFilePath);
|
||||
|
||||
if (templateList[i].Type == CodeGenTypeEnum.SeedData)
|
||||
{
|
||||
// 种子模板
|
||||
var seedData = new
|
||||
{
|
||||
AuthorName = input.AuthorName!, // 作者
|
||||
BusName = input.BusName!, // 业务名称
|
||||
NameSpace = input.NameSpace!, // 命名空间
|
||||
ClassName = input.TableName!, // 类名称
|
||||
ConfigId = input.ConfigId, // 库标识
|
||||
MenuList = menuList, // 菜单集合
|
||||
PrintType = input.PrintType!
|
||||
};
|
||||
tResult = await _viewEngine.RunCompileAsync(tContent, seedData, builderAction: builder =>
|
||||
{
|
||||
builder.AddAssemblyReferenceByName("System.Linq");
|
||||
builder.AddAssemblyReferenceByName("System.Collections");
|
||||
builder.AddAssemblyReferenceByName("System.Text.RegularExpressions");
|
||||
builder.AddUsing("System.Text.RegularExpressions");
|
||||
builder.AddUsing("System.Collections.Generic");
|
||||
builder.AddUsing("System.Linq");
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
tResult = await _viewEngine.RunCompileAsync(tContent, data, builderAction: builder =>
|
||||
{
|
||||
builder.AddAssemblyReferenceByName("System.Linq");
|
||||
builder.AddAssemblyReferenceByName("System.Collections");
|
||||
builder.AddAssemblyReferenceByName("System.Text.RegularExpressions");
|
||||
builder.AddUsing("System.Text.RegularExpressions");
|
||||
builder.AddUsing("System.Collections.Generic");
|
||||
builder.AddUsing("System.Linq");
|
||||
});
|
||||
}
|
||||
|
||||
string targetFile = templateList[i].OutputFile
|
||||
.Replace("{PagePath}", input.PagePath)
|
||||
.Replace("{TableName}", input.TableName)
|
||||
.Replace("{TableNameLower}", ToFirstLetterLowerCase(input.TableName!));
|
||||
|
||||
string tmpPath;
|
||||
if (!input.GenerateType.StartsWith('1'))
|
||||
{
|
||||
if (templateList[i].Type == CodeGenTypeEnum.Frontend)
|
||||
tmpPath = Path.Combine(new DirectoryInfo(App.WebHostEnvironment.ContentRootPath).Parent!.Parent!.FullName, _codeGenOptions.FrontRootPath, "src");
|
||||
else
|
||||
tmpPath = Path.Combine(new DirectoryInfo(App.WebHostEnvironment.ContentRootPath).Parent!.FullName, input.NameSpace);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmpPath = templateList[i].Type == CodeGenTypeEnum.Frontend ? Path.Combine(outputPath, _codeGenOptions.FrontRootPath, "src") : Path.Combine(outputPath, input!.NameSpace!);
|
||||
}
|
||||
targetFile = Path.Combine(tmpPath, targetFile);
|
||||
|
||||
var dirPath = new DirectoryInfo(targetFile).Parent!.FullName;
|
||||
if (!Directory.Exists(dirPath))
|
||||
Directory.CreateDirectory(dirPath);
|
||||
await File.WriteAllTextAsync(targetFile, tResult, Encoding.UTF8);
|
||||
}
|
||||
|
||||
// 非ZIP压缩返回空
|
||||
if (!input.GenerateType.StartsWith('1'))
|
||||
return null;
|
||||
|
||||
var downloadPath = outputPath + ".zip";
|
||||
// 判断是否存在同名称文件
|
||||
if (File.Exists(downloadPath))
|
||||
File.Delete(downloadPath);
|
||||
ZipFile.CreateFromDirectory(outputPath, downloadPath);
|
||||
return new { url = $"{App.HttpContext.Request.Scheme}://{App.HttpContext.Request.Host.Value}/codeGen/{input.TableName}.zip" };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取代码生成预览 🔖
|
||||
/// 处理模板
|
||||
/// </summary>
|
||||
/// <param name="template"></param>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="templatePath"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="menuList"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取代码生成预览")]
|
||||
public async Task<Dictionary<string, string>> Preview(SysCodeGen input)
|
||||
private async Task<string> ProcessTemplate(SysCodeGenTemplate template, SysCodeGen input, string templatePath, CustomViewEngine data, List<SysMenu> menuList)
|
||||
{
|
||||
var tableFieldList = await _codeGenConfigService.GetList(new CodeGenConfig() { CodeGenId = input.Id }); // 字段集合
|
||||
string tResult;
|
||||
|
||||
#region 构造前端需要验证的数据
|
||||
|
||||
foreach (var item in tableFieldList)
|
||||
var filename = template.Name;
|
||||
// 更改默认首页模板
|
||||
if (filename == "web_views_index.vue.vm")
|
||||
{
|
||||
List<VerifyRuleItem> list = new();
|
||||
if (!string.IsNullOrWhiteSpace(item.Rules))
|
||||
filename = string.IsNullOrEmpty(input.LeftTab) ? filename : "web_views_LeftTree.vue.vm"; // 左树右列表
|
||||
filename = string.IsNullOrEmpty(input.BottomTab) ? filename : "web_views_BottomIndx.vue.vm"; // 左数右上列表下列表属性
|
||||
}
|
||||
var templateFilePath = Path.Combine(templatePath, filename);
|
||||
if (!File.Exists(templateFilePath)) return null;
|
||||
|
||||
var tContent = await File.ReadAllTextAsync(templateFilePath);
|
||||
|
||||
if (template.Type == CodeGenTypeEnum.SeedData)
|
||||
{
|
||||
// 种子模板
|
||||
var seedData = new
|
||||
{
|
||||
if (item.Rules != "[]") list = JSON.Deserialize<List<VerifyRuleItem>>(item.Rules);
|
||||
}
|
||||
else
|
||||
AuthorName = input.AuthorName!, // 作者
|
||||
BusName = input.BusName!, // 业务名称
|
||||
NameSpace = input.NameSpace!, // 命名空间
|
||||
ClassName = input.TableName!, // 类名称
|
||||
ConfigId = input.ConfigId, // 库标识
|
||||
MenuList = menuList, // 菜单集合
|
||||
PrintType = input.PrintType!
|
||||
};
|
||||
tResult = await _viewEngine.RunCompileAsync(tContent, seedData, builderAction: builder =>
|
||||
{
|
||||
item.Rules = "[]";
|
||||
}
|
||||
item.RuleItems = list;
|
||||
item.WhetherRequired = list.Any(t => t.Type == "required") ? YesNoEnum.Y.ToString() : YesNoEnum.N.ToString();
|
||||
item.AnyRule = list.Count > 0;
|
||||
item.RemoteVerify = list.Any(t => t.Type == "remote");
|
||||
builder.AddAssemblyReferenceByName("System.Linq");
|
||||
builder.AddAssemblyReferenceByName("System.Collections");
|
||||
builder.AddAssemblyReferenceByName("System.Text.RegularExpressions");
|
||||
builder.AddUsing("System.Text.RegularExpressions");
|
||||
builder.AddUsing("System.Collections.Generic");
|
||||
builder.AddUsing("System.Linq");
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
tResult = await _viewEngine.RunCompileAsync(tContent, data, builderAction: builder =>
|
||||
{
|
||||
builder.AddAssemblyReferenceByName("System.Linq");
|
||||
builder.AddAssemblyReferenceByName("System.Collections");
|
||||
builder.AddAssemblyReferenceByName("System.Text.RegularExpressions");
|
||||
builder.AddUsing("System.Text.RegularExpressions");
|
||||
builder.AddUsing("System.Collections.Generic");
|
||||
builder.AddUsing("System.Linq");
|
||||
});
|
||||
}
|
||||
|
||||
#endregion 构造前端需要验证的数据
|
||||
|
||||
var queryWhetherList = tableFieldList.Where(u => u.QueryWhether == YesNoEnum.Y.ToString()).ToList(); // 前端查询集合
|
||||
var joinTableList = tableFieldList.Where(u => u.EffectType is "Upload" or "ForeignKey" or "ApiTreeSelector").ToList(); // 需要连表查询的字段
|
||||
//(string joinTableNames, string lowerJoinTableNames) = GetJoinTableStr(joinTableList); // 获取连表的实体名和别名
|
||||
|
||||
var data = new CustomViewEngine(_db)
|
||||
{
|
||||
ConfigId = input.ConfigId!, // 库定位器名
|
||||
AuthorName = input.AuthorName!, // 作者
|
||||
BusName = input.BusName!, // 业务名称
|
||||
NameSpace = input.NameSpace!, // 命名空间
|
||||
ClassName = input.TableName!, // 类名称
|
||||
PagePath = input.PagePath!, // 页面目录
|
||||
ProjectLastName = input.NameSpace!.Split('.').Last(), // 项目最后个名称,生成的时候赋值
|
||||
TableUniqueConfigList = input.TableUniqueList ?? new(), // 唯一字段列表
|
||||
QueryWhetherList = queryWhetherList, // 查询条件
|
||||
TableField = tableFieldList, // 表字段配置信息
|
||||
IsJoinTable = joinTableList.Count > 0,//是否联表
|
||||
IsUpload = joinTableList.Any(u => u.EffectType == "Upload"), // 是否上传
|
||||
PrintType = input.PrintType!, // 支持打印类型
|
||||
PrintName = input.PrintName!, // 打印模板名称
|
||||
IsApiService = input.IsApiService,
|
||||
RemoteVerify = tableFieldList.Any(t => t.RemoteVerify), // 远程验证
|
||||
TreeName = input.TreeName,
|
||||
LowerTreeName = string.IsNullOrEmpty(input.TreeName) ? "" : input.TreeName[..1].ToLower() + input.TreeName[1..], // 首字母小写
|
||||
LeftTab = input.LeftTab,
|
||||
LeftKey = input.LeftKey!,
|
||||
LeftPrimaryKey = input.LeftPrimaryKey,
|
||||
LeftName = input.LeftName,
|
||||
LowerLeftTab = string.IsNullOrEmpty(input.LeftTab) ? "" : input.LeftTab[..1].ToLower() + input.LeftTab[1..], // 首字母小写
|
||||
LowerLeftKey = string.IsNullOrEmpty(input.LeftKey) ? "" : input.LeftKey[..1].ToLower() + input.LeftKey[1..], // 首字母小写
|
||||
LowerLeftPrimaryKey = string.IsNullOrEmpty(input.LeftPrimaryKey) ? "" : input.LeftPrimaryKey[..1].ToLower() + input.LeftPrimaryKey[1..], // 首字母小写
|
||||
//LowerLeftPrimaryKey = CodeGenUtil.CamelColumnName(input.LeftPrimaryKey, entityBasePropertyNames),
|
||||
BottomTab = input.BottomTab,
|
||||
BottomKey = input.BottomKey!,
|
||||
BottomPrimaryKey = input.BottomPrimaryKey,
|
||||
LowerBottomTab = string.IsNullOrEmpty(input.BottomTab) ? "" : input.BottomTab[..1].ToLower() + input.BottomTab[1..], // 首字母小写
|
||||
LowerBottomKey = string.IsNullOrEmpty(input.BottomKey) ? "" : input.BottomKey[..1].ToLower() + input.BottomKey[1..], // 首字母小写
|
||||
LowerBottomPrimaryKey = string.IsNullOrEmpty(input.BottomPrimaryKey) ? "" : input.BottomPrimaryKey[..1].ToLower() + input.BottomPrimaryKey[1..], // 首字母小写
|
||||
};
|
||||
|
||||
// 获取模板文件并替换
|
||||
var templateList = GetTemplateList(input);
|
||||
var templatePath = Path.Combine(App.WebHostEnvironment.WebRootPath, "template");
|
||||
|
||||
var result = new Dictionary<string, string>();
|
||||
for (var i = 0; i < templateList.Count; i++)
|
||||
{
|
||||
string tResult; // 模板生成结果
|
||||
|
||||
var filename = templateList[i].Name;
|
||||
// 更改默认首页模板
|
||||
if (filename == "web_views_index.vue.vm")
|
||||
{
|
||||
filename = string.IsNullOrEmpty(input.LeftTab) ? filename : "web_views_LeftTree.vue.vm"; // 左树右列表
|
||||
filename = string.IsNullOrEmpty(input.BottomTab) ? filename : "web_views_BottomIndx.vue.vm"; // 左数右上列表下列表属性
|
||||
}
|
||||
var templateFilePath = Path.Combine(templatePath, filename);
|
||||
if (!File.Exists(templateFilePath)) continue;
|
||||
var tContent = await File.ReadAllTextAsync(templateFilePath);
|
||||
|
||||
if (templateList[i].Type == CodeGenTypeEnum.SeedData)
|
||||
{
|
||||
// 种子模板
|
||||
var menuList = await GetMenus(input.TableName!, input.BusName!, input.MenuPid ?? 0, input.MenuIcon!, input.PagePath!, tableFieldList);
|
||||
var seedData = new
|
||||
{
|
||||
AuthorName = input.AuthorName!, // 作者
|
||||
BusName = input.BusName!, // 业务名称
|
||||
NameSpace = input.NameSpace!, // 命名空间
|
||||
ClassName = input.TableName!, // 类名称
|
||||
ConfigId = input.ConfigId, // 库标识
|
||||
MenuList = menuList, // 菜单集合
|
||||
PrintType = input.PrintType!
|
||||
};
|
||||
tResult = await _viewEngine.RunCompileAsync(tContent, seedData, builderAction: builder =>
|
||||
{
|
||||
builder.AddAssemblyReferenceByName("System.Linq");
|
||||
builder.AddAssemblyReferenceByName("System.Collections");
|
||||
builder.AddAssemblyReferenceByName("System.Text.RegularExpressions");
|
||||
builder.AddUsing("System.Text.RegularExpressions");
|
||||
builder.AddUsing("System.Collections.Generic");
|
||||
builder.AddUsing("System.Linq");
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
tResult = await _viewEngine.RunCompileFromCachedAsync(tContent, data, builderAction: builder =>
|
||||
{
|
||||
builder.AddAssemblyReferenceByName("System.Linq");
|
||||
builder.AddAssemblyReferenceByName("System.Collections");
|
||||
builder.AddUsing("System.Collections.Generic");
|
||||
builder.AddUsing("System.Linq");
|
||||
});
|
||||
}
|
||||
|
||||
result.Add(templateList[i].Name?.TrimEnd(".vm")!, tResult);
|
||||
}
|
||||
|
||||
return result;
|
||||
return tResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -802,6 +738,8 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
SysMenu menuType0 = null;
|
||||
long tempPid = pid;
|
||||
var menuList = new List<SysMenu>();
|
||||
var classNameLower = className.ToLower();
|
||||
var classNameFirstLower = className.ToFirstLower();
|
||||
if (pid == 0)
|
||||
{
|
||||
// 目录
|
||||
@ -812,8 +750,8 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
Title = busName + "管理",
|
||||
Type = MenuTypeEnum.Dir,
|
||||
Icon = "robot",
|
||||
Path = "/" + className.ToLower() + "Manage",
|
||||
Name = className[..1].ToLower() + className[1..] + "Manage",
|
||||
Path = "/" + classNameLower + "Manage",
|
||||
Name = classNameFirstLower + "Manage",
|
||||
Component = "Layout",
|
||||
OrderNo = 100,
|
||||
CreateTime = DateTime.Now
|
||||
@ -833,11 +771,11 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
Id = YitIdHelper.NextId(),
|
||||
Pid = pid,
|
||||
Title = busName + "管理",
|
||||
Name = className[..1].ToLower() + className[1..],
|
||||
Name = classNameFirstLower,
|
||||
Type = MenuTypeEnum.Menu,
|
||||
Icon = menuIcon,
|
||||
Path = pPath + "/" + className.ToLower(),
|
||||
Component = "/" + pagePath + "/" + className[..1].ToLower() + className[1..] + "/index",
|
||||
Path = pPath + "/" + classNameLower,
|
||||
Component = "/" + pagePath + "/" + classNameFirstLower + "/index",
|
||||
CreateTime = DateTime.Now
|
||||
};
|
||||
|
||||
@ -850,7 +788,7 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
Pid = menuPid,
|
||||
Title = "查询",
|
||||
Type = MenuTypeEnum.Btn,
|
||||
Permission = className[..1].ToLower() + className[1..] + "/page",
|
||||
Permission = classNameFirstLower + "/page",
|
||||
OrderNo = menuOrder,
|
||||
CreateTime = DateTime.Now
|
||||
};
|
||||
@ -864,7 +802,7 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
Pid = menuPid,
|
||||
Title = "详情",
|
||||
Type = MenuTypeEnum.Btn,
|
||||
Permission = className[..1].ToLower() + className[1..] + "/detail",
|
||||
Permission = classNameFirstLower + "/detail",
|
||||
OrderNo = menuOrder,
|
||||
CreateTime = DateTime.Now
|
||||
};
|
||||
@ -878,7 +816,7 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
Pid = menuPid,
|
||||
Title = "增加",
|
||||
Type = MenuTypeEnum.Btn,
|
||||
Permission = className[..1].ToLower() + className[1..] + "/add",
|
||||
Permission = classNameFirstLower + "/add",
|
||||
OrderNo = menuOrder,
|
||||
CreateTime = DateTime.Now
|
||||
};
|
||||
@ -892,7 +830,7 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
Pid = menuPid,
|
||||
Title = "删除",
|
||||
Type = MenuTypeEnum.Btn,
|
||||
Permission = className[..1].ToLower() + className[1..] + "/delete",
|
||||
Permission = classNameFirstLower + "/delete",
|
||||
OrderNo = menuOrder,
|
||||
CreateTime = DateTime.Now
|
||||
};
|
||||
@ -906,7 +844,7 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
Pid = menuPid,
|
||||
Title = "编辑",
|
||||
Type = MenuTypeEnum.Btn,
|
||||
Permission = className[..1].ToLower() + className[1..] + "/update",
|
||||
Permission = classNameFirstLower + "/update",
|
||||
OrderNo = menuOrder,
|
||||
CreateTime = DateTime.Now
|
||||
};
|
||||
@ -916,15 +854,15 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
// 加入ForeignKey、Upload、ApiTreeSelector 等接口的权限
|
||||
// 在生成表格时,有些字段只是查询时显示,不需要填写(WhetherAddUpdate),所以这些字段没必要生成相应接口
|
||||
var fkTableList = tableFieldList.Where(u => u.EffectType == "ForeignKey" && (u.WhetherAddUpdate == "Y" || u.QueryWhether == "Y")).ToList();
|
||||
foreach (var @column in fkTableList)
|
||||
foreach (var column in fkTableList)
|
||||
{
|
||||
var menuType1 = new SysMenu
|
||||
{
|
||||
Id = YitIdHelper.NextId(),
|
||||
Pid = menuPid,
|
||||
Title = "外键" + @column.ColumnName,
|
||||
Title = "外键" + column.ColumnName,
|
||||
Type = MenuTypeEnum.Btn,
|
||||
Permission = className[..1].ToLower() + className[1..] + "/" + column.FkEntityName + column.ColumnName + "Dropdown",
|
||||
Permission = classNameFirstLower + "/" + column.FkEntityName + column.ColumnName + "Dropdown",
|
||||
OrderNo = menuOrder,
|
||||
CreateTime = DateTime.Now
|
||||
};
|
||||
@ -932,15 +870,15 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
menuList.Add(menuType1);
|
||||
}
|
||||
var treeSelectTableList = tableFieldList.Where(u => u.EffectType == "ApiTreeSelector").ToList();
|
||||
foreach (var @column in treeSelectTableList)
|
||||
foreach (var column in treeSelectTableList)
|
||||
{
|
||||
var menuType1 = new SysMenu
|
||||
{
|
||||
Id = YitIdHelper.NextId(),
|
||||
Pid = menuPid,
|
||||
Title = "树型" + @column.ColumnName,
|
||||
Title = "树型" + column.ColumnName,
|
||||
Type = MenuTypeEnum.Btn,
|
||||
Permission = className[..1].ToLower() + className[1..] + "/" + column.FkEntityName + "Tree",
|
||||
Permission = classNameFirstLower + "/" + column.FkEntityName + "Tree",
|
||||
OrderNo = menuOrder,
|
||||
CreateTime = DateTime.Now
|
||||
};
|
||||
@ -948,15 +886,15 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
menuList.Add(menuType1);
|
||||
}
|
||||
var uploadTableList = tableFieldList.Where(u => u.EffectType == "Upload").ToList();
|
||||
foreach (var @column in uploadTableList)
|
||||
foreach (var column in uploadTableList)
|
||||
{
|
||||
var menuType1 = new SysMenu
|
||||
{
|
||||
Id = YitIdHelper.NextId(),
|
||||
Pid = menuPid,
|
||||
Title = "上传" + @column.ColumnName,
|
||||
Title = "上传" + column.ColumnName,
|
||||
Type = MenuTypeEnum.Btn,
|
||||
Permission = className[..1].ToLower() + className[1..] + "/Upload" + column.ColumnName,
|
||||
Permission = classNameFirstLower + "/Upload" + column.ColumnName,
|
||||
OrderNo = menuOrder,
|
||||
CreateTime = DateTime.Now
|
||||
};
|
||||
@ -1002,18 +940,4 @@ public class SysCodeGenService : IDynamicApiController, ITransient
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 小驼峰命名法(camelCase):首字母小写
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToFirstLetterLowerCase(string input)
|
||||
{
|
||||
// 检查字符串是否为空或空格
|
||||
if (string.IsNullOrWhiteSpace(input)) return input;
|
||||
|
||||
// 只转换首字母为小写
|
||||
return char.ToLower(input[0]) + input.Substring(1);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user