优化代码生成同步
This commit is contained in:
parent
4e628e58db
commit
01b304634e
@ -103,10 +103,6 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
{
|
||||
var tableInfo = GenTableService.GetGenTableInfo(tableId);
|
||||
var tables = GenTableService.GetGenTableAll();
|
||||
if (tableInfo != null)
|
||||
{
|
||||
tableInfo.Columns = GenTableColumnService.GenTableColumns(tableId);
|
||||
}
|
||||
return SUCCESS(new { info = tableInfo, tables });
|
||||
}
|
||||
|
||||
@ -232,7 +228,6 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
|
||||
}
|
||||
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
|
||||
genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId);
|
||||
|
||||
dto.DbType = AppSettings.GetAppConfig("gen:dbType", 0);
|
||||
dto.GenTable = genTableInfo;
|
||||
@ -258,7 +253,6 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
|
||||
}
|
||||
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
|
||||
genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId);
|
||||
|
||||
dto.DbType = AppSettings.GetAppConfig("gen:dbType", 0);
|
||||
dto.GenTable = genTableInfo;
|
||||
@ -299,13 +293,14 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
{
|
||||
if (string.IsNullOrEmpty(tableName) || tableId <= 0) throw new CustomException("参数错误");
|
||||
GenTable table = GenTableService.GetGenTableInfo(tableId);
|
||||
if (table == null) { throw new CustomException("原表不存在"); }
|
||||
if (table == null) { throw new CustomException("同步数据失败,原表结构不存在"); }
|
||||
table.Update_by = HttpContext.GetName();
|
||||
|
||||
List<DbColumnInfo> dbColumnInfos = _CodeGeneraterService.GetColumnInfo(table.DbName, tableName);
|
||||
List<GenTableColumn> dbTableColumns = CodeGeneratorTool.InitGenTableColumn(table, dbColumnInfos);
|
||||
|
||||
GenTableService.SynchDb(tableId, table, dbTableColumns);
|
||||
return SUCCESS(true);
|
||||
bool result = GenTableService.SynchDb(tableId, table, dbTableColumns);
|
||||
return SUCCESS(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,10 +51,14 @@ namespace ZR.Service.System
|
||||
public GenTable GetGenTableInfo(long tableId)
|
||||
{
|
||||
GenTable info = GetId(tableId);
|
||||
if (info != null && !info.SubTableName.IsEmpty())
|
||||
if (info != null)
|
||||
{
|
||||
info.SubTable = Queryable().Where(f => f.TableName == info.SubTableName).First();
|
||||
info.SubTable.Columns = GenTableColumnService.GenTableColumns(info.SubTable.TableId);
|
||||
info.Columns = GenTableColumnService.GenTableColumns(tableId);
|
||||
if (!info.SubTableName.IsEmpty())
|
||||
{
|
||||
info.SubTable = Queryable().Where(f => f.TableName == info.SubTableName).First();
|
||||
info.SubTable.Columns = GenTableColumnService.GenTableColumns(info.SubTable.TableId);
|
||||
}
|
||||
}
|
||||
return info;
|
||||
}
|
||||
@ -118,32 +122,62 @@ namespace ZR.Service.System
|
||||
/// 同步数据库
|
||||
/// </summary>
|
||||
/// <param name="tableId">表id</param>
|
||||
/// <param name="dbTableColumns"></param>
|
||||
/// <param name="genTable"></param>
|
||||
public void SynchDb(long tableId, GenTable genTable, List<GenTableColumn> dbTableColumns)
|
||||
/// <param name="dbTableColumns">数据库表最新列初始化信息集合</param>
|
||||
public bool 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<string> tableColumnNames = genTable.Columns.Select(f => f.ColumnName).ToList();//老列明
|
||||
List<string> dbTableColumneNames = dbTableColumns.Select(f => f.ColumnName).ToList();//新列明
|
||||
|
||||
List<GenTableColumn> insertColumns = new();
|
||||
List<GenTableColumn> updateColumns = new();
|
||||
foreach (var column in dbTableColumns)
|
||||
{
|
||||
if (!tableColumnNames.Contains(column.ColumnName))
|
||||
{
|
||||
insertColumns.Add(column);
|
||||
}
|
||||
else
|
||||
{
|
||||
GenTableColumn prevColumn = genTable.Columns.Find(f => f.ColumnName == column.ColumnName);
|
||||
column.ColumnId = prevColumn.ColumnId;
|
||||
column.IsEdit = prevColumn.IsEdit;
|
||||
column.AutoFillType = prevColumn.AutoFillType;
|
||||
column.Sort = prevColumn.Sort;
|
||||
column.IsExport = prevColumn.IsExport;
|
||||
column.IsSort = prevColumn.IsSort;
|
||||
column.Update_time = DateTime.Now;
|
||||
column.Update_by = genTable.Update_by;
|
||||
if (column.IsList)
|
||||
{
|
||||
column.DictType = prevColumn.DictType;
|
||||
column.QueryType = prevColumn.QueryType;
|
||||
}
|
||||
if (column.ColumnComment.IsEmpty())
|
||||
{
|
||||
column.ColumnComment = prevColumn.ColumnComment;
|
||||
}
|
||||
updateColumns.Add(column);
|
||||
}
|
||||
}
|
||||
bool result = UseTran2(() =>
|
||||
{
|
||||
GenTableColumnService.Insert(insertColumns);
|
||||
if (insertColumns.Count > 0)
|
||||
{
|
||||
GenTableColumnService.Insert(insertColumns);
|
||||
}
|
||||
if (updateColumns.Count > 0)
|
||||
{
|
||||
GenTableColumnService.UpdateGenTableColumn(updateColumns);
|
||||
}
|
||||
|
||||
List<GenTableColumn> delColumns = tableColumns.FindAll(column => !dbTableColumneNames.Contains(column.ColumnName));
|
||||
List<GenTableColumn> delColumns = genTable.Columns.FindAll(column => !dbTableColumneNames.Contains(column.ColumnName));
|
||||
if (delColumns != null && delColumns.Count > 0)
|
||||
{
|
||||
GenTableColumnService.Delete(delColumns.Select(f => f.ColumnId).ToList());
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@ -211,7 +245,7 @@ namespace ZR.Service.System
|
||||
public int UpdateGenTableColumn(List<GenTableColumn> tableColumn)
|
||||
{
|
||||
return Context.Updateable(tableColumn)
|
||||
.WhereColumns(it => new { it.ColumnId, it.TableId })
|
||||
.WhereColumns(it => new { it.ColumnId })
|
||||
.UpdateColumns(it => new
|
||||
{
|
||||
it.ColumnComment,
|
||||
@ -229,9 +263,9 @@ namespace ZR.Service.System
|
||||
it.DictType,
|
||||
it.Update_by,
|
||||
it.Remark,
|
||||
it.IsSort,
|
||||
it.IsSort,//
|
||||
it.IsExport,
|
||||
it.AutoFillType
|
||||
it.AutoFillType,
|
||||
})
|
||||
.ExecuteCommand();
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using SqlSugar;
|
||||
using System.Collections.Generic;
|
||||
using ZR.Model;
|
||||
using ZR.Model.System.Generate;
|
||||
|
||||
@ -14,7 +15,7 @@ namespace ZR.Service.System.IService
|
||||
int DeleteGenTableByTbName(string tableName);
|
||||
PagedInfo<GenTable> GetGenTables(GenTable genTable, PagerInfo pagerInfo);
|
||||
GenTable GetGenTableInfo(long tableId);
|
||||
void SynchDb(long tableId, GenTable genTable, List<GenTableColumn> dbTableColumns);
|
||||
bool SynchDb(long tableId, GenTable genTable, List<GenTableColumn> genTableColumns);
|
||||
List<GenTable> GetGenTableAll();
|
||||
int UpdateGenTable(GenTable genTable);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user