代码生成优化

This commit is contained in:
不做码农 2023-12-16 16:18:44 +08:00
parent d67d401f14
commit 7c78ce8ed5
4 changed files with 36 additions and 58 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using SqlSugar; using SqlSugar;
using ZR.Admin.WebApi.Filters; using ZR.Admin.WebApi.Filters;
using ZR.CodeGenerator; using ZR.CodeGenerator;
@ -23,16 +24,20 @@ namespace ZR.Admin.WebApi.Controllers
private readonly IGenTableColumnService GenTableColumnService; private readonly IGenTableColumnService GenTableColumnService;
private readonly ISysMenuService SysMenuService; private readonly ISysMenuService SysMenuService;
private readonly IWebHostEnvironment WebHostEnvironment; private readonly IWebHostEnvironment WebHostEnvironment;
private readonly OptionsSetting OptionsSetting;
public CodeGeneratorController( public CodeGeneratorController(
IGenTableService genTableService, IGenTableService genTableService,
IGenTableColumnService genTableColumnService, IGenTableColumnService genTableColumnService,
IWebHostEnvironment webHostEnvironment, IWebHostEnvironment webHostEnvironment,
IOptions<OptionsSetting> options,
ISysMenuService sysMenuService) ISysMenuService sysMenuService)
{ {
GenTableService = genTableService; GenTableService = genTableService;
GenTableColumnService = genTableColumnService; GenTableColumnService = genTableColumnService;
WebHostEnvironment = webHostEnvironment; WebHostEnvironment = webHostEnvironment;
SysMenuService = sysMenuService; SysMenuService = sysMenuService;
OptionsSetting = options.Value;
} }
/// <summary> /// <summary>
@ -143,8 +148,6 @@ namespace ZR.Admin.WebApi.Controllers
{ {
throw new CustomException("表或数据库不能为空"); throw new CustomException("表或数据库不能为空");
} }
DbConfigs dbConfig = AppSettings.Get<DbConfigs>(nameof(GenConstants.CodeGenDbConfig));
CodeGen codeGen = AppSettings.Get<CodeGen>("codeGen");
int result = 0; int result = 0;
foreach (var table in dto.Tables) foreach (var table in dto.Tables)
@ -156,12 +159,12 @@ namespace ZR.Admin.WebApi.Controllers
UserName = HttpContext.GetName(), UserName = HttpContext.GetName(),
TableName = table.Name, TableName = table.Name,
Desc = table.Description, Desc = table.Description,
CodeGen = codeGen CodeGen = OptionsSetting.CodeGen
}; };
GenTable genTable = CodeGeneratorTool.InitTable(initTableDto); GenTable genTable = CodeGeneratorTool.InitTable(initTableDto);
genTable.TableId = GenTableService.ImportGenTable(genTable); genTable.TableId = GenTableService.ImportGenTable(genTable);
if (dbConfig.DbType == 3) if (OptionsSetting.CodeGenDbConfig.DbType == 3)
{ {
seqs = _CodeGeneraterService.GetAllOracleSeqs(table.Name); seqs = _CodeGeneraterService.GetAllOracleSeqs(table.Name);
} }
@ -169,7 +172,7 @@ namespace ZR.Admin.WebApi.Controllers
{ {
//保存列信息 //保存列信息
List<DbColumnInfo> dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dto.DbName, table.Name); List<DbColumnInfo> dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dto.DbName, table.Name);
List<GenTableColumn> genTableColumns = CodeGeneratorTool.InitGenTableColumn(genTable, dbColumnInfos, seqs, codeGen); List<GenTableColumn> genTableColumns = CodeGeneratorTool.InitGenTableColumn(genTable, dbColumnInfos, seqs, OptionsSetting.CodeGen);
GenTableColumnService.DeleteGenTableColumnByTableName(table.Name); GenTableColumnService.DeleteGenTableColumnByTableName(table.Name);
GenTableColumnService.InsertGenTableColumn(genTableColumns); GenTableColumnService.InsertGenTableColumn(genTableColumns);
genTable.Columns = genTableColumns; genTable.Columns = genTableColumns;
@ -198,7 +201,7 @@ namespace ZR.Admin.WebApi.Controllers
var genTable = genTableDto.Adapt<GenTable>().ToUpdate(HttpContext); var genTable = genTableDto.Adapt<GenTable>().ToUpdate(HttpContext);
//将前端额外参数转成字符串存入Options中 //将前端额外参数转成字符串存入Options中
genTable.Options = genTableDto.Params.Adapt<Options>(); genTable.Options = genTableDto.Params.Adapt<CodeOptions>();
DbResult<bool> result = GenTableService.UseTran(() => DbResult<bool> result = GenTableService.UseTran(() =>
{ {
int rows = GenTableService.UpdateGenTable(genTable); int rows = GenTableService.UpdateGenTable(genTable);
@ -227,9 +230,8 @@ namespace ZR.Admin.WebApi.Controllers
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
} }
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
var dbConfig = AppSettings.Get<DbConfigs>(nameof(GenConstants.CodeGenDbConfig));
dto.DbType = dbConfig.DbType; dto.DbType = OptionsSetting.CodeGenDbConfig.DbType;
dto.GenTable = genTableInfo; dto.GenTable = genTableInfo;
dto.IsPreview = true; dto.IsPreview = true;
@ -252,15 +254,21 @@ namespace ZR.Admin.WebApi.Controllers
{ {
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
} }
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
var dbConfig = AppSettings.Get<DbConfigs>(nameof(GenConstants.CodeGenDbConfig));
dto.DbType = dbConfig.DbType; dto.DbType = OptionsSetting.CodeGenDbConfig.DbType;
dto.GenTable = genTableInfo; dto.GenTable = GenTableService.GetGenTableInfo(dto.TableId);
//自定义路径 //生成压缩包
if (genTableInfo.GenType == "1") string zipReturnFileName = $"ZrAdmin.NET-{dto.GenTable.TableName}-{DateTime.Now:MMddHHmmss}.zip";
//生成代码到指定文件夹
CodeGeneratorTool.Generate(dto);
if (dto.GenTable.Options.GenerateMenu)
{ {
var genPath = genTableInfo.GenPath; SysMenuService.AddSysMenu(dto.GenTable, dto.ReplaceDto.PermissionPrefix, dto.ReplaceDto.ShowBtnEdit, dto.ReplaceDto.ShowBtnExport, dto.ReplaceDto.ShowBtnImport);
}
if (dto.GenTable.GenType == "1")//自定义路径
{
var genPath = dto.GenTable.GenPath;
string parentPath; string parentPath;
string tempPath = WebHostEnvironment.ContentRootPath; string tempPath = WebHostEnvironment.ContentRootPath;
@ -274,32 +282,25 @@ namespace ZR.Admin.WebApi.Controllers
} }
Console.WriteLine("代码生成路径" + parentPath); Console.WriteLine("代码生成路径" + parentPath);
//代码生成文件夹路径 //代码生成文件夹路径
dto.GenCodePath = (genPath.IsEmpty() || genPath.Equals("/")) ? parentPath : genTableInfo.GenPath; dto.GenCodePath = (genPath.IsEmpty() || genPath.Equals("/")) ? parentPath : genPath;
} }
else else
{ {
dto.ZipPath = Path.Combine(WebHostEnvironment.WebRootPath, "Generatecode"); dto.ZipPath = Path.Combine(WebHostEnvironment.WebRootPath, "Generatecode");
dto.GenCodePath = Path.Combine(dto.ZipPath, DateTime.Now.ToString("yyyyMMdd")); dto.GenCodePath = Path.Combine(dto.ZipPath, DateTime.Now.ToString("yyyyMMdd"));
} }
//生成压缩包
string zipReturnFileName = $"ZrAdmin.NET-{genTableInfo.TableComment}-{DateTime.Now:MMddHHmmss}.zip";
//生成代码到指定文件夹
CodeGeneratorTool.Generate(dto);
if (genTableInfo.Options.GenerateMenu)
{
SysMenuService.AddSysMenu(genTableInfo, dto.ReplaceDto.PermissionPrefix, dto.ReplaceDto.ShowBtnEdit, dto.ReplaceDto.ShowBtnExport, dto.ReplaceDto.ShowBtnImport);
}
//写入文件
foreach (var item in dto.GenCodes) foreach (var item in dto.GenCodes)
{ {
item.Path = Path.Combine(dto.GenCodePath, item.Path); item.Path = Path.Combine(dto.GenCodePath, item.Path);
FileUtil.WriteAndSave(item.Path, item.Content); FileUtil.WriteAndSave(item.Path, item.Content);
} }
if (dto.GenTable.GenType != "1")
//下载文件 {
FileUtil.ZipGenCode(dto.ZipPath, dto.GenCodePath, zipReturnFileName); //压缩文件
FileUtil.ZipGenCode(dto.ZipPath, dto.GenCodePath, zipReturnFileName);
}
return SUCCESS(new { path = "/Generatecode/" + zipReturnFileName, fileName = dto.ZipFileName }); return SUCCESS(new { path = "/Generatecode/" + zipReturnFileName, fileName = dto.ZipFileName });
} }

View File

@ -8,7 +8,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Xml.Linq;
using ZR.CodeGenerator.Model; using ZR.CodeGenerator.Model;
using ZR.Model.System.Generate; using ZR.Model.System.Generate;
@ -443,7 +442,7 @@ namespace ZR.CodeGenerator
TableComment = dto.Desc, TableComment = dto.Desc,
FunctionName = dto.Desc, FunctionName = dto.Desc,
Create_by = dto.UserName, Create_by = dto.UserName,
Options = new Options() Options = new CodeOptions()
{ {
SortType = "asc", SortType = "asc",
CheckedBtn = new int[] { 1, 2, 3 }, CheckedBtn = new int[] { 1, 2, 3 },

View File

@ -1,5 +1,4 @@
using System.Collections.Generic; using ZR.Model.System.Generate;
using ZR.Model.System.Generate;
namespace ZR.Model.System.Dto namespace ZR.Model.System.Dto
{ {
@ -24,28 +23,10 @@ namespace ZR.Model.System.Dto
/// <summary> /// <summary>
/// 额外参数 /// 额外参数
/// </summary> /// </summary>
public Options Params { get; set; } public CodeOptions Params { get; set; }
public List<GenTableColumnDto> Columns { get; set; } public List<GenTableColumnDto> Columns { get; set; }
} }
/// <summary>
/// 额外参数
/// ****注意里面参数统一首字母小写*****
/// </summary>
public class Params
{
public string TreeCode { get; set; }
public string TreeName { get; set; }
public string TreeParentCode { get; set; }
public int? ParentMenuId { get; set; }
public string SortField { get; set; }
public string SortType { get; set; }
/// <summary>
/// 额外参数字符串
/// </summary>
public string CheckedBtn { get; set; }
public string PermissionPrefix { get; set; }
}
public class GenTableColumnDto public class GenTableColumnDto
{ {
public int ColumnId { get; set; } public int ColumnId { get; set; }

View File

@ -1,7 +1,4 @@
using SqlSugar; namespace ZR.Model.System.Generate
using System.Collections.Generic;
namespace ZR.Model.System.Generate
{ {
/// <summary> /// <summary>
/// 代码生成表 /// 代码生成表
@ -86,7 +83,7 @@ namespace ZR.Model.System.Generate
/// 其他生成选项 /// 其他生成选项
/// </summary> /// </summary>
[SugarColumn(IsJson = true)] [SugarColumn(IsJson = true)]
public Options Options { get; set; } public CodeOptions Options { get; set; }
#region #region
/// <summary> /// <summary>
@ -103,7 +100,7 @@ namespace ZR.Model.System.Generate
#endregion #endregion
} }
public class Options public class CodeOptions
{ {
public long ParentMenuId { get; set; } public long ParentMenuId { get; set; }
public string SortType { get; set; } = "asc"; public string SortType { get; set; } = "asc";