shgx_tz_mom/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs

317 lines
13 KiB
C#
Raw Permalink Normal View History

2024-06-07 11:04:26 +08:00
using Infrastructure.Extensions;
2021-09-06 18:35:36 +08:00
using Microsoft.AspNetCore.Mvc;
2021-09-07 21:52:44 +08:00
using SqlSugar;
using ZR.Admin.WebApi.Extensions;
2021-09-10 10:44:17 +08:00
using ZR.Admin.WebApi.Filters;
2021-09-07 18:37:21 +08:00
using ZR.CodeGenerator;
2021-09-09 18:18:37 +08:00
using ZR.CodeGenerator.Model;
2021-09-07 18:37:21 +08:00
using ZR.CodeGenerator.Service;
2021-09-06 18:35:36 +08:00
using ZR.Model;
using ZR.Model.System.Dto;
2021-09-17 18:19:53 +08:00
using ZR.Model.System.Generate;
using ZR.Service.System.IService;
2021-09-06 18:35:36 +08:00
namespace ZR.Admin.WebApi.Controllers
{
/// <summary>
/// 代码生成
/// </summary>
2021-12-04 08:24:38 +08:00
[Verify]
2021-09-09 18:18:37 +08:00
[Route("tool/gen")]
2021-09-06 18:35:36 +08:00
public class CodeGeneratorController : BaseController
{
2021-12-13 21:53:48 +08:00
private readonly CodeGeneraterService _CodeGeneraterService = new CodeGeneraterService();
private readonly IGenTableService GenTableService;
private readonly IGenTableColumnService GenTableColumnService;
private readonly ISysMenuService SysMenuService;
2021-12-13 21:53:48 +08:00
private readonly IWebHostEnvironment WebHostEnvironment;
public CodeGeneratorController(
IGenTableService genTableService,
IGenTableColumnService genTableColumnService,
IWebHostEnvironment webHostEnvironment,
ISysMenuService sysMenuService)
2021-09-17 18:19:53 +08:00
{
GenTableService = genTableService;
GenTableColumnService = genTableColumnService;
WebHostEnvironment = webHostEnvironment;
SysMenuService = sysMenuService;
2021-09-17 18:19:53 +08:00
}
2021-09-06 18:35:36 +08:00
/// <summary>
/// 获取所有数据库的信息
/// </summary>
/// <returns></returns>
2021-09-09 18:18:37 +08:00
[HttpGet("getDbList")]
2021-09-10 10:44:17 +08:00
[ActionPermissionFilter(Permission = "tool:gen:list")]
2021-09-06 18:35:36 +08:00
public IActionResult GetListDataBase()
{
2021-09-09 18:18:37 +08:00
var dbList = _CodeGeneraterService.GetAllDataBases();
var defaultDb = dbList?[0];
2021-09-09 18:18:37 +08:00
return SUCCESS(new { dbList, defaultDb });
2021-09-06 18:35:36 +08:00
}
/// <summary>
2021-12-13 21:53:48 +08:00
///获取所有表根据数据库名
2021-09-06 18:35:36 +08:00
/// </summary>
2021-09-07 18:37:21 +08:00
/// <param name="dbName">数据库名</param>
/// <param name="tableName">表名</param>
2021-09-06 21:37:32 +08:00
/// <param name="pager">分页信息</param>
2021-09-06 18:35:36 +08:00
/// <returns></returns>
2021-09-10 10:44:17 +08:00
[HttpGet("getTableList")]
[ActionPermissionFilter(Permission = "tool:gen:list")]
2022-03-06 14:26:05 +08:00
public IActionResult FindListTable(string dbName, string? tableName, PagerInfo pager)
2021-09-06 18:35:36 +08:00
{
List<DbTableInfo> list = _CodeGeneraterService.GetAllTables(dbName, tableName, pager);
2021-12-18 10:56:02 +08:00
var page = new PagedInfo<DbTableInfo>
{
TotalNum = pager.TotalNum,
PageSize = pager.PageSize,
PageIndex = pager.PageNum,
Result = list
};
return SUCCESS(page);
2021-09-06 21:37:32 +08:00
}
2021-09-17 18:19:53 +08:00
/// <summary>
2021-12-13 21:53:48 +08:00
/// 查询生成表数据
2021-09-17 18:19:53 +08:00
/// </summary>
/// <param name="tableName">表名</param>
2021-09-17 18:19:53 +08:00
/// <param name="pagerInfo">分页信息</param>
/// <returns></returns>
2021-12-13 21:53:48 +08:00
[HttpGet("list")]
[ActionPermissionFilter(Permission = "tool:gen:list")]
2022-03-06 14:26:05 +08:00
public IActionResult GetGenTable(string? tableName, PagerInfo pagerInfo)
2021-09-17 18:19:53 +08:00
{
//查询原表数据,部分字段映射到代码生成表字段
var rows = GenTableService.GetGenTables(new GenTable() { TableName = tableName }, pagerInfo);
return SUCCESS(rows, "MM月dd日 HH:mm");
2021-09-17 18:19:53 +08:00
}
/// <summary>
2021-12-13 21:53:48 +08:00
/// 修改代码生成业务查询
2021-09-17 18:19:53 +08:00
/// </summary>
/// <param name="tableId">genTable表id</param>
2021-09-17 18:19:53 +08:00
/// <returns></returns>
2021-12-13 21:53:48 +08:00
[HttpGet("{tableId}")]
[ActionPermissionFilter(Permission = "tool:gen:query")]
2021-09-17 18:19:53 +08:00
public IActionResult GetColumnList(long tableId)
{
var tableInfo = GenTableService.GetGenTableInfo(tableId);
2021-12-13 21:53:48 +08:00
var tables = GenTableService.GetGenTableAll();
return SUCCESS(new { info = tableInfo, tables });
2021-09-17 18:19:53 +08:00
}
2021-12-13 21:53:48 +08:00
/// <summary>
/// 根据表id查询表列
/// </summary>
/// <param name="tableId">genTable表id</param>
/// <returns></returns>
[HttpGet("column/{tableId}")]
[ActionPermissionFilter(Permission = "tool:gen:query")]
public IActionResult GetTableColumnList(long tableId)
{
var tableColumns = GenTableColumnService.GenTableColumns(tableId);
return SUCCESS(new { columns = tableColumns });
}
2021-09-17 18:19:53 +08:00
/// <summary>
/// 删除代码生成
2021-09-17 18:19:53 +08:00
/// </summary>
/// <param name="tableIds"></param>
/// <returns></returns>
[Log(Title = "代码生成", BusinessType = BusinessType.DELETE)]
[HttpDelete("{tableIds}")]
[ActionPermissionFilter(Permission = "tool:gen:remove")]
public IActionResult Remove(string tableIds)
{
long[] tableId = Tools.SpitLongArrary(tableIds);
2021-09-21 20:31:35 +08:00
int result = GenTableService.DeleteGenTableByIds(tableId);
return SUCCESS(result);
2021-09-17 18:19:53 +08:00
}
/// <summary>
/// 导入表结构(保存)
2021-09-17 18:19:53 +08:00
/// </summary>
/// <param name="tables"></param>
/// <param name="dbName"></param>
/// <returns></returns>
[HttpPost("importTable")]
[Log(Title = "代码生成", BusinessType = BusinessType.IMPORT)]
[ActionPermissionFilter(Permission = "tool:gen:import")]
public IActionResult ImportTableSave(string tables, string dbName)
{
if (string.IsNullOrEmpty(tables))
{
throw new CustomException("表不能为空");
}
2023-06-23 16:36:00 +08:00
DbConfigs dbConfig = AppSettings.Get<DbConfigs>(nameof(GlobalConstant.CodeGenDbConfig));
2021-09-17 18:19:53 +08:00
string[] tableNames = tables.Split(',', StringSplitOptions.RemoveEmptyEntries);
2022-04-10 11:13:29 +08:00
int result = 0;
foreach (var tableName in tableNames)
2021-09-17 18:19:53 +08:00
{
var tabInfo = _CodeGeneraterService.GetTableInfo(dbName, tableName);
2021-09-17 18:19:53 +08:00
if (tabInfo != null)
{
List<OracleSeq> seqs = new();
2022-04-10 11:13:29 +08:00
GenTable genTable = CodeGeneratorTool.InitTable(dbName, HttpContext.GetName(), tableName, tabInfo?.Description);
genTable.TableId = GenTableService.ImportGenTable(genTable);
2023-06-16 19:35:34 +08:00
if (dbConfig.DbType == 3)
{
seqs = _CodeGeneraterService.GetAllOracleSeqs(dbName);
}
if (genTable.TableId > 0)
2021-09-17 18:19:53 +08:00
{
//保存列信息
List<DbColumnInfo> dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dbName, tableName);
List<GenTableColumn> genTableColumns = CodeGeneratorTool.InitGenTableColumn(genTable, dbColumnInfos, seqs);
GenTableColumnService.DeleteGenTableColumnByTableName(tableName);
2021-09-17 18:19:53 +08:00
GenTableColumnService.InsertGenTableColumn(genTableColumns);
genTable.Columns = genTableColumns;
2022-04-10 11:13:29 +08:00
result++;
2021-09-17 18:19:53 +08:00
}
}
}
2022-04-10 11:13:29 +08:00
return ToResponse(result);
2021-09-17 18:19:53 +08:00
}
/// <summary>
/// 修改保存代码生成业务
/// </summary>
2021-10-15 14:19:30 +08:00
/// <param name="genTableDto">请求参数实体</param>
/// <returns></returns>
2021-09-21 20:31:35 +08:00
[HttpPut]
2021-10-15 14:19:30 +08:00
[Log(Title = "代码生成", BusinessType = BusinessType.GENCODE, IsSaveRequestData = false)]
[ActionPermissionFilter(Permission = "tool:gen:edit")]
2021-09-21 20:31:35 +08:00
public IActionResult EditSave([FromBody] GenTableDto genTableDto)
{
if (genTableDto == null) throw new CustomException("请求参数错误");
2021-12-29 18:39:42 +08:00
if (genTableDto.BusinessName.Equals(genTableDto.ModuleName, StringComparison.OrdinalIgnoreCase))
{
return ToResponse(ResultCode.CUSTOM_ERROR, "模块名不能和业务名一样");
}
2021-09-21 20:31:35 +08:00
var genTable = genTableDto.Adapt<GenTable>().ToUpdate(HttpContext);
//将前端额外参数转成字符串存入Options中
genTable.Options = genTableDto.Params.Adapt<Options>();
2021-12-29 18:39:42 +08:00
DbResult<bool> result = GenTableService.UseTran(() =>
{
int rows = GenTableService.UpdateGenTable(genTable);
if (rows > 0)
{
2021-12-29 18:39:42 +08:00
GenTableColumnService.UpdateGenTableColumn(genTable.Columns);
}
});
2021-12-29 18:39:42 +08:00
return SUCCESS(result.IsSuccess);
}
/// <summary>
/// 预览代码
/// </summary>
2023-06-23 16:36:00 +08:00
/// <param name="dto"></param>
/// <param name="tableId"></param>
/// <returns></returns>
[HttpPost("preview/{tableId}")]
[ActionPermissionFilter(Permission = "tool:gen:preview")]
2023-06-23 16:36:00 +08:00
public IActionResult Preview([FromQuery] GenerateDto dto, [FromRoute] int tableId = 0)
{
2023-06-23 16:36:00 +08:00
dto.TableId = tableId;
if (dto == null || dto.TableId <= 0)
{
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
}
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
2023-06-23 16:36:00 +08:00
var dbConfig = AppSettings.Get<DbConfigs>(nameof(GlobalConstant.CodeGenDbConfig));
dto.DbType = dbConfig.DbType;
dto.GenTable = genTableInfo;
dto.IsPreview = true;
CodeGeneratorTool.Generate(dto);
return SUCCESS(dto.GenCodes);
}
/// <summary>
2021-11-28 17:40:43 +08:00
/// 生成代码(下载方式)
/// </summary>
/// <param name="dto">数据传输对象</param>
/// <returns></returns>
[HttpPost("genCode")]
[Log(Title = "代码生成", BusinessType = BusinessType.GENCODE)]
[ActionPermissionFilter(Permission = "tool:gen:code")]
2021-12-14 21:50:30 +08:00
public IActionResult CodeGenerate([FromBody] GenerateDto dto)
{
2022-08-15 20:22:19 +08:00
if (dto == null || dto.TableId <= 0)
{
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
}
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
2023-06-23 16:36:00 +08:00
var dbConfig = AppSettings.Get<DbConfigs>(nameof(GlobalConstant.CodeGenDbConfig));
2021-11-28 17:40:43 +08:00
dto.DbType = dbConfig.DbType;
dto.GenTable = genTableInfo;
//自定义路径
if (genTableInfo.GenType == "1")
{
var genPath = genTableInfo.GenPath;
string tempPath = WebHostEnvironment.ContentRootPath;
2022-12-06 14:49:30 +08:00
var parentPath = tempPath[..tempPath.LastIndexOf(@"\")];
//代码生成文件夹路径
dto.GenCodePath = (genPath.IsEmpty() || genPath.Equals("/")) ? parentPath : genTableInfo.GenPath;
}
else
{
dto.ZipPath = Path.Combine(WebHostEnvironment.WebRootPath, "Generatecode");
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)
{
2023-07-28 17:37:36 +08:00
SysMenuService.AddSysMenu(genTableInfo, dto.ReplaceDto.PermissionPrefix, dto.ReplaceDto.ShowBtnEdit, dto.ReplaceDto.ShowBtnExport, dto.ReplaceDto.ShowBtnImport);
}
foreach (var item in dto.GenCodes)
{
item.Path = Path.Combine(dto.GenCodePath, item.Path);
FileUtil.WriteAndSave(item.Path, item.Content);
}
//下载文件
FileUtil.ZipGenCode(dto.ZipPath, dto.GenCodePath, zipReturnFileName);
return SUCCESS(new { path = "/Generatecode/" + zipReturnFileName, fileName = dto.ZipFileName });
}
2021-12-14 15:41:58 +08:00
/// <summary>
/// 同步数据库
/// </summary>
/// <param name="tableId"></param>
/// <param name="tableName"></param>
/// <returns></returns>
[ActionPermissionFilter(Permission = "tool:gen:edit")]
[Log(Title = "代码生成", BusinessType = BusinessType.UPDATE)]
[HttpGet("synchDb/{tableId}")]
public IActionResult SynchDb(string tableName, long tableId = 0)
{
if (string.IsNullOrEmpty(tableName) || tableId <= 0) throw new CustomException("参数错误");
GenTable table = GenTableService.GetGenTableInfo(tableId);
2023-05-03 18:39:01 +08:00
if (table == null) { throw new CustomException("同步数据失败,原表结构不存在"); }
table.Update_by = HttpContext.GetName();
2021-12-14 15:41:58 +08:00
List<DbColumnInfo> dbColumnInfos = _CodeGeneraterService.GetColumnInfo(table.DbName, tableName);
List<GenTableColumn> dbTableColumns = CodeGeneratorTool.InitGenTableColumn(table, dbColumnInfos);
2023-05-03 18:39:01 +08:00
bool result = GenTableService.SynchDb(tableId, table, dbTableColumns);
return SUCCESS(result);
2021-12-14 15:41:58 +08:00
}
2021-09-06 18:35:36 +08:00
}
}