diff --git a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs index 005fbaa..e1f6c61 100644 --- a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs +++ b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs @@ -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 dbColumnInfos = _CodeGeneraterService.GetColumnInfo(table.DbName, tableName); List dbTableColumns = CodeGeneratorTool.InitGenTableColumn(table, dbColumnInfos); - GenTableService.SynchDb(tableId, table, dbTableColumns); - return SUCCESS(true); + bool result = GenTableService.SynchDb(tableId, table, dbTableColumns); + return SUCCESS(result); } } } diff --git a/ZR.Service/System/GenTableService.cs b/ZR.Service/System/GenTableService.cs index 7cc0523..9490f85 100644 --- a/ZR.Service/System/GenTableService.cs +++ b/ZR.Service/System/GenTableService.cs @@ -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 /// 同步数据库 /// /// 表id - /// /// - public void SynchDb(long tableId, GenTable genTable, List dbTableColumns) + /// 数据库表最新列初始化信息集合 + public bool 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 tableColumnNames = genTable.Columns.Select(f => f.ColumnName).ToList();//老列明 + List dbTableColumneNames = dbTableColumns.Select(f => f.ColumnName).ToList();//新列明 List insertColumns = new(); + List 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 delColumns = tableColumns.FindAll(column => !dbTableColumneNames.Contains(column.ColumnName)); + List 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 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(); } diff --git a/ZR.Service/System/IService/IGenTableService.cs b/ZR.Service/System/IService/IGenTableService.cs index 7358ed4..eaafa58 100644 --- a/ZR.Service/System/IService/IGenTableService.cs +++ b/ZR.Service/System/IService/IGenTableService.cs @@ -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 GetGenTables(GenTable genTable, PagerInfo pagerInfo); GenTable GetGenTableInfo(long tableId); - void SynchDb(long tableId, GenTable genTable, List dbTableColumns); + bool SynchDb(long tableId, GenTable genTable, List genTableColumns); List GetGenTableAll(); int UpdateGenTable(GenTable genTable); }