代码生成新增加同步功能
This commit is contained in:
parent
d1fc87bcc3
commit
f237719735
@ -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 });
|
||||
}
|
||||
|
||||
/// <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);
|
||||
if (table == null) { throw new CustomException("原表不存在"); }
|
||||
|
||||
List<DbColumnInfo> dbColumnInfos = _CodeGeneraterService.GetColumnInfo(table.DbName, tableName);
|
||||
List<GenTableColumn> dbTableColumns = CodeGeneratorTool.InitGenTableColumn(table, dbColumnInfos);
|
||||
|
||||
GenTableService.SynchDb(tableId, table, dbTableColumns);
|
||||
return SUCCESS(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,6 +64,5 @@ namespace ZR.CodeGenerator.Service
|
||||
{
|
||||
return GetSugarDbContext(dbName).DbMaintenance.GetColumnInfosByTableName(tableName, true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,6 +17,10 @@ namespace ZR.Model.System.Generate
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int TableId { get; set; }
|
||||
/// <summary>
|
||||
/// 数据库名
|
||||
/// </summary>
|
||||
public string DbName { get; set; }
|
||||
/// <summary>
|
||||
/// 表名
|
||||
/// </summary>
|
||||
public string TableName { get; set; }
|
||||
|
||||
@ -130,6 +130,35 @@ namespace ZR.Service.System
|
||||
genTable.Update_time = db.GetDate();
|
||||
return db.Updateable(genTable).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步数据库
|
||||
/// </summary>
|
||||
/// <param name="tableId">表id</param>
|
||||
/// <param name="dbTableColumns"></param>
|
||||
/// <param name="genTable"></param>
|
||||
public void SynchDb(long tableId, GenTable genTable, List<GenTableColumn> dbTableColumns)
|
||||
{
|
||||
List<GenTableColumn> tableColumns = GenTableColumnService.GenTableColumns(tableId);
|
||||
List<string> tableColumnNames = tableColumns.Select(f => f.ColumnName).ToList();
|
||||
List<string> dbTableColumneNames = dbTableColumns.Select(f => f.ColumnName).ToList();
|
||||
|
||||
List<GenTableColumn> insertColumns = new();
|
||||
foreach (var column in dbTableColumns)
|
||||
{
|
||||
if (!tableColumnNames.Contains(column.ColumnName))
|
||||
{
|
||||
insertColumns.Add(column);
|
||||
}
|
||||
}
|
||||
GenTableColumnService.Insert(insertColumns);
|
||||
|
||||
List<GenTableColumn> delColumns = tableColumns.FindAll(column => !dbTableColumneNames.Contains(column.ColumnName));
|
||||
if (delColumns!= null && delColumns.Count > 0)
|
||||
{
|
||||
GenTableColumnService.Delete(delColumns.Select(f => f.ColumnId).ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -12,8 +12,9 @@ namespace ZR.Service.System.IService
|
||||
|
||||
int DeleteGenTableByIds(long[] tableIds);
|
||||
int DeleteGenTableByTbName(string tableName);
|
||||
PagedInfo<GenTable> GetGenTables(GenTable genTable, Model.PagerInfo pagerInfo);
|
||||
PagedInfo<GenTable> GetGenTables(GenTable genTable, PagerInfo pagerInfo);
|
||||
GenTable GetGenTableInfo(long tableId);
|
||||
void SynchDb(long tableId, GenTable genTable, List<GenTableColumn> dbTableColumns);
|
||||
List<GenTable> GetGenTableAll();
|
||||
int UpdateGenTable(GenTable genTable);
|
||||
}
|
||||
|
||||
@ -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
|
||||
})
|
||||
}
|
||||
@ -28,10 +28,11 @@
|
||||
<span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="tableId" label="表id" width="80"/>
|
||||
<el-table-column prop="tableName" label="表名" sortable="custom" width="180" />
|
||||
<el-table-column prop="dbName" label="数据库名" width="100" />
|
||||
<el-table-column prop="tableId" label="表id" width="80" />
|
||||
<el-table-column prop="tableName" label="表名" sortable="custom" width="110" :show-overflow-tooltip="true" />
|
||||
<el-table-column prop="tableComment" label="表描述" :show-overflow-tooltip="true" />
|
||||
<el-table-column prop="className" label="实体" />
|
||||
<el-table-column prop="className" label="实体" :show-overflow-tooltip="true" />
|
||||
<el-table-column prop="createTime" label="创建时间" />
|
||||
<el-table-column prop="updateTime" label="更新时间" />
|
||||
<el-table-column label="操作" align="center" width="350">
|
||||
@ -39,7 +40,7 @@
|
||||
<el-button type="text" icon="el-icon-view" @click="handleShowDialog(scope.row, 'preview')" v-hasPermi="['tool:gen:preview']">预览</el-button>
|
||||
<el-button type="text" icon="el-icon-edit" @click="handleEditTable(scope.row)" v-hasPermi="['tool:gen:edit']">编辑</el-button>
|
||||
<el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['tool:gen:remove']">删除</el-button>
|
||||
<!-- <el-button type="text" icon="el-icon-refresh" @click="handleRefresh(scope.row)" v-hasPermi="['tool:gen:refresh']">同步</el-button> -->
|
||||
<el-button type="text" icon="el-icon-refresh" @click="handleSynchDb(scope.row)" v-hasPermi="['tool:gen:edit']">同步</el-button>
|
||||
<el-button type="text" icon="el-icon-download" @click="handleShowDialog(scope.row, 'generate')" v-hasPermi="['tool:gen:code']">生成代码
|
||||
</el-button>
|
||||
</template>
|
||||
@ -51,7 +52,8 @@
|
||||
<el-dialog :title="preview.title" :visible.sync="preview.open" width="80%" top="5vh" append-to-body>
|
||||
<el-tabs v-model="preview.activeName">
|
||||
<el-tab-pane v-for="(item, key) in preview.data" :label="item.title" :name="key.toString()" :key="key">
|
||||
<el-link :underline="false" icon="el-icon-document-copy" v-clipboard:copy="item.content" v-clipboard:success="clipboardSuccess" style="float:right">复制</el-link>
|
||||
<el-link :underline="false" icon="el-icon-document-copy" v-clipboard:copy="item.content" v-clipboard:success="clipboardSuccess"
|
||||
style="float:right">复制</el-link>
|
||||
<pre><code class="hljs" v-html="highlightedCode(item.content, item.title)"></code></pre>
|
||||
</el-tab-pane>
|
||||
</el-tabs>
|
||||
@ -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(() => {});
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user