From f237719735796e6286cc14f77abbac4f97e20a8d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com>
Date: Tue, 14 Dec 2021 15:41:58 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E6=96=B0?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../System/CodeGeneratorController.cs | 24 +++++++++++-
.../Service/CodeGeneraterService.cs | 1 -
ZR.Model/System/Generate/GenTable.cs | 4 ++
ZR.Service/System/GenTableService.cs | 29 +++++++++++++++
.../System/IService/IGenTableService.cs | 3 +-
ZR.Vue/src/api/tool/gen.js | 27 +++++++++-----
ZR.Vue/src/views/tool/gen/index.vue | 37 ++++++++++++-------
7 files changed, 99 insertions(+), 26 deletions(-)
diff --git a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs
index a8f3e96..5f5d8a0 100644
--- a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs
+++ b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs
@@ -32,7 +32,7 @@ namespace ZR.Admin.WebApi.Controllers
private readonly CodeGeneraterService _CodeGeneraterService = new CodeGeneraterService();
private readonly IGenTableService GenTableService;
private readonly IGenTableColumnService GenTableColumnService;
-
+
private readonly IWebHostEnvironment WebHostEnvironment;
public CodeGeneratorController(
IGenTableService genTableService,
@@ -159,6 +159,7 @@ namespace ZR.Admin.WebApi.Controllers
{
GenTable genTable = new()
{
+ DbName = dbName,
BaseNameSpace = "ZR.",//导入默认命名空间前缀
ModuleName = "business",//导入默认模块名
ClassName = CodeGeneratorTool.GetClassName(tableName),
@@ -277,5 +278,26 @@ namespace ZR.Admin.WebApi.Controllers
return SUCCESS(new { path = "/Generatecode/" + dto.ZipFileName, fileName = dto.ZipFileName });
}
+ ///
+ /// 同步数据库
+ ///
+ ///
+ ///
+ ///
+ [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);
+ if (table == null) { throw new CustomException("原表不存在"); }
+
+ List dbColumnInfos = _CodeGeneraterService.GetColumnInfo(table.DbName, tableName);
+ List dbTableColumns = CodeGeneratorTool.InitGenTableColumn(table, dbColumnInfos);
+
+ GenTableService.SynchDb(tableId, table, dbTableColumns);
+ return SUCCESS(true);
+ }
}
}
diff --git a/ZR.CodeGenerator/Service/CodeGeneraterService.cs b/ZR.CodeGenerator/Service/CodeGeneraterService.cs
index fd0ae3b..3b2990d 100644
--- a/ZR.CodeGenerator/Service/CodeGeneraterService.cs
+++ b/ZR.CodeGenerator/Service/CodeGeneraterService.cs
@@ -64,6 +64,5 @@ namespace ZR.CodeGenerator.Service
{
return GetSugarDbContext(dbName).DbMaintenance.GetColumnInfosByTableName(tableName, true);
}
-
}
}
diff --git a/ZR.Model/System/Generate/GenTable.cs b/ZR.Model/System/Generate/GenTable.cs
index 6bbdd16..008233f 100644
--- a/ZR.Model/System/Generate/GenTable.cs
+++ b/ZR.Model/System/Generate/GenTable.cs
@@ -17,6 +17,10 @@ namespace ZR.Model.System.Generate
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int TableId { get; set; }
///
+ /// 数据库名
+ ///
+ public string DbName { get; set; }
+ ///
/// 表名
///
public string TableName { get; set; }
diff --git a/ZR.Service/System/GenTableService.cs b/ZR.Service/System/GenTableService.cs
index 00d649a..18ec65f 100644
--- a/ZR.Service/System/GenTableService.cs
+++ b/ZR.Service/System/GenTableService.cs
@@ -130,6 +130,35 @@ namespace ZR.Service.System
genTable.Update_time = db.GetDate();
return db.Updateable(genTable).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
}
+
+ ///
+ /// 同步数据库
+ ///
+ /// 表id
+ ///
+ ///
+ public void SynchDb(long tableId, GenTable genTable, List dbTableColumns)
+ {
+ List tableColumns = GenTableColumnService.GenTableColumns(tableId);
+ List tableColumnNames = tableColumns.Select(f => f.ColumnName).ToList();
+ List dbTableColumneNames = dbTableColumns.Select(f => f.ColumnName).ToList();
+
+ List insertColumns = new();
+ foreach (var column in dbTableColumns)
+ {
+ if (!tableColumnNames.Contains(column.ColumnName))
+ {
+ insertColumns.Add(column);
+ }
+ }
+ GenTableColumnService.Insert(insertColumns);
+
+ List delColumns = tableColumns.FindAll(column => !dbTableColumneNames.Contains(column.ColumnName));
+ if (delColumns!= null && delColumns.Count > 0)
+ {
+ GenTableColumnService.Delete(delColumns.Select(f => f.ColumnId).ToList());
+ }
+ }
}
///
diff --git a/ZR.Service/System/IService/IGenTableService.cs b/ZR.Service/System/IService/IGenTableService.cs
index dc06cb8..9d118ae 100644
--- a/ZR.Service/System/IService/IGenTableService.cs
+++ b/ZR.Service/System/IService/IGenTableService.cs
@@ -12,8 +12,9 @@ namespace ZR.Service.System.IService
int DeleteGenTableByIds(long[] tableIds);
int DeleteGenTableByTbName(string tableName);
- PagedInfo GetGenTables(GenTable genTable, Model.PagerInfo pagerInfo);
+ PagedInfo GetGenTables(GenTable genTable, PagerInfo pagerInfo);
GenTable GetGenTableInfo(long tableId);
+ void SynchDb(long tableId, GenTable genTable, List dbTableColumns);
List GetGenTableAll();
int UpdateGenTable(GenTable genTable);
}
diff --git a/ZR.Vue/src/api/tool/gen.js b/ZR.Vue/src/api/tool/gen.js
index 04ce81c..59317ef 100644
--- a/ZR.Vue/src/api/tool/gen.js
+++ b/ZR.Vue/src/api/tool/gen.js
@@ -9,8 +9,8 @@ import request from '@/utils/request'
// }
/**
- * 创建数据库连接
- */
+ * 创建数据库连接
+ */
// export function createGetDBConn(data) {
// return request({
// url: 'tool/gen/CreateDBConn',
@@ -19,8 +19,8 @@ import request from '@/utils/request'
// })
// }
/**
- * 获取数据库
- */
+ * 获取数据库
+ */
export function codeGetDBList() {
return request({
url: 'tool/gen/getDbList',
@@ -28,8 +28,8 @@ export function codeGetDBList() {
})
}
/**
- * 获取数据库表
- */
+ * 获取数据库表
+ */
export function listDbTable(data) {
return request({
url: 'tool/gen/getTableList',
@@ -38,8 +38,8 @@ export function listDbTable(data) {
})
}
/**
- * 生成代码
- */
+ * 生成代码
+ */
export async function codeGenerator(data) {
return await request({
url: 'tool/gen/genCode',
@@ -107,6 +107,15 @@ export function previewTable(tableId, data) {
return request({
url: '/tool/gen/preview/' + tableId,
method: 'post',
- data: data
+ data: data
})
}
+
+// 同步数据库
+export function synchDb(tableId, data) {
+ return request({
+ url: '/tool/gen/synchDb/' + tableId,
+ method: 'get',
+ params: data
+ })
+}
\ No newline at end of file
diff --git a/ZR.Vue/src/views/tool/gen/index.vue b/ZR.Vue/src/views/tool/gen/index.vue
index d1370eb..58e8910 100644
--- a/ZR.Vue/src/views/tool/gen/index.vue
+++ b/ZR.Vue/src/views/tool/gen/index.vue
@@ -28,10 +28,11 @@
{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}
-
-
+
+
+
-
+
@@ -39,7 +40,7 @@
预览
编辑
删除
-
+ 同步
生成代码
@@ -51,7 +52,8 @@
- 复制
+ 复制
@@ -95,6 +97,7 @@ import {
listTable,
delTable,
previewTable,
+ synchDb
} from "@/api/tool/gen";
import importTable from "./importTable";
import { Loading } from "element-ui";
@@ -275,11 +278,11 @@ export default {
});
});
},
- /** 复制代码成功 */
- clipboardSuccess(){
+ /** 复制代码成功 */
+ clipboardSuccess() {
this.msgSuccess("复制成功");
},
- // 多选框选中数据
+ // 多选框选中数据
handleSelectionChange(section) {
this.tableIds = section.map((item) => item.tableId);
this.multiple = !section.length;
@@ -287,15 +290,21 @@ export default {
},
/** 高亮显示 */
highlightedCode(code, key) {
- // var language = key.substring(key.lastIndexOf(".") , key.length)
+ // var language = key.substring(key.lastIndexOf(".") , key.length)
const result = hljs.highlightAuto(code || "");
return result.value || " ";
},
- handleRefresh(row) {
- this.$message({
- type: "info",
- message: "敬请期待",
- });
+ // 同步代码
+ handleSynchDb(row) {
+ const tableName = row.tableName;
+ this.$confirm('确认要强制同步"' + tableName + '"表结构吗?')
+ .then(function () {
+ return synchDb(row.tableId, { tableName, dbName: row.dbName });
+ })
+ .then(() => {
+ this.msgSuccess("同步成功");
+ })
+ .catch(() => {});
},
},
};