using Infrastructure.Attribute; using Infrastructure.Extensions; using Newtonsoft.Json; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using ARW.Common; using ARW.Model; using ARW.Model.System.Generate; using ARW.Repository.System; using ARW.Service.System.IService; namespace ARW.Service.System { /// /// 代码生成表 /// [AppService(ServiceType = typeof(IGenTableService), ServiceLifetime = LifeTime.Transient)] public class GenTableService : BaseService, IGenTableService { private GenTableRepository GenTableRepository; private IGenTableColumnService GenTableColumnService; public GenTableService(IGenTableColumnService genTableColumnService, GenTableRepository genTableRepository) { GenTableColumnService = genTableColumnService; GenTableRepository = genTableRepository; } /// /// 删除表 /// /// 需要删除的表id /// public int DeleteGenTableByIds(long[] tableIds) { GenTableRepository.Delete(f => tableIds.Contains(f.TableId)); return GenTableColumnService.DeleteGenTableColumn(tableIds); } /// /// 删除表根据表名 /// /// /// public int DeleteGenTableByTbName(string tableName) { return GenTableRepository.Delete(f => f.TableName == tableName) ? 1 : 0; } /// /// 获取表信息 /// /// /// public GenTable GetGenTableInfo(long tableId) { var info = GenTableRepository.GetId(tableId); if (info != null && !info.SubTableName.IsEmpty()) { info.SubTable = GenTableRepository.Queryable().Where(f => f.TableName == info.SubTableName).First(); } return info; } /// /// 获取所有代码生成表 /// /// public List GetGenTableAll() { return GenTableRepository.GetAll(); } /// /// 查询代码生成表信息 /// /// /// /// public PagedInfo GetGenTables(GenTable genTable, PagerInfo pagerInfo) { var predicate = Expressionable.Create(); predicate = predicate.AndIF(genTable.TableName.IfNotEmpty(), it => it.TableName.Contains(genTable.TableName)); return GenTableRepository.GetPages(predicate.ToExpression(), pagerInfo, x => x.TableId, OrderByType.Desc); } /// /// 插入代码生成表 /// /// /// public int ImportGenTable(GenTable table) { table.Create_time = DateTime.Now; //导入前删除现有表 //DeleteGenTableByIds(new long[] { table.TableId }); DeleteGenTableByTbName(table.TableName); return GenTableRepository.Context.Insertable(table).IgnoreColumns(ignoreNullColumn: true).ExecuteReturnIdentity(); } /// /// 获取表数据 /// /// /// public List SelectDbTableListByNamess(string[] tableNames) { throw new NotImplementedException(); } public int UpdateGenTable(GenTable genTable) { var db = GenTableRepository.Context; 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()); } } } /// /// 代码生成表列 /// [AppService(ServiceType = typeof(IGenTableColumnService), ServiceLifetime = LifeTime.Transient)] public class GenTableColumnService : BaseService, IGenTableColumnService { private GenTableColumnRepository GetTableColumnRepository; public GenTableColumnService(GenTableColumnRepository genTableColumnRepository) { GetTableColumnRepository = genTableColumnRepository; } /// /// 删除表字段 /// /// /// public int DeleteGenTableColumn(long tableId) { return GetTableColumnRepository.DeleteGenTableColumn(new long[] { tableId }); } /// /// 根据表id批量删除表字段 /// /// /// public int DeleteGenTableColumn(long[] tableId) { return GetTableColumnRepository.DeleteGenTableColumn(tableId); } /// /// 根据表名删除字段 /// /// /// public int DeleteGenTableColumnByTableName(string tableName) { return GetTableColumnRepository.DeleteGenTableColumnByTableName(tableName); } /// /// 获取表所有字段 /// /// /// public List GenTableColumns(long tableId) { return GetTableColumnRepository.GenTableColumns(tableId); } /// /// 插入表字段 /// /// /// public int InsertGenTableColumn(List tableColumn) { return GetTableColumnRepository.InsertGenTableColumn(tableColumn); } /// /// 批量更新表字段 /// /// /// public int UpdateGenTableColumn(List tableColumn) { return GetTableColumnRepository.UpdateGenTableColumn(tableColumn); } } }