You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
834 lines
32 KiB
834 lines
32 KiB
using Newtonsoft.Json.Linq;
|
|
using System.Data;
|
|
using Tiobon.Core.Common.DB.Dapper;
|
|
using Tiobon.Core.Common.DB.Dapper.DBManager;
|
|
using Tiobon.Core.Common.Https.HttpPolly;
|
|
|
|
namespace Tiobon.Core.Controllers;
|
|
|
|
/// <summary>
|
|
/// 公共服务
|
|
/// </summary>
|
|
[Produces("application/json")]
|
|
[Route("api/Common")]
|
|
[Authorize(Permissions.Name), ApiExplorerSettings(GroupName = Grouping.GroupName_System)]
|
|
public class CommonController : BaseApiController
|
|
{
|
|
private readonly ICommonServices _commonServices;
|
|
private readonly IHttpPollyHelper _httpPollyHelper;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="logger"></param>
|
|
/// <param name="commonServices"></param>
|
|
/// <param name="httpPollyHelper"></param>
|
|
public CommonController(
|
|
ICommonServices commonServices,
|
|
IHttpPollyHelper httpPollyHelper)
|
|
{
|
|
_commonServices = commonServices;
|
|
_httpPollyHelper = httpPollyHelper;
|
|
}
|
|
|
|
#region 获取菜单
|
|
/// <summary>
|
|
/// 获取菜单
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
[HttpPost, Route("GetMenu")]
|
|
public async Task<ServiceResult<MenuReturn>> GetMenuAsync([FromBody] MenuParam param)
|
|
{
|
|
return await _commonServices.GetMenuAsync(param);
|
|
}
|
|
#endregion
|
|
|
|
#region 获取模块信息接口
|
|
/// <summary>
|
|
/// 获取模块信息接口
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
[HttpPost, Route("GetModuleInfo")]
|
|
public async Task<ServiceResult<ModuleReturn>> GetModuleInfoAsync([FromBody] ModuleParam param)
|
|
{
|
|
return await _commonServices.GetModuleInfoAsync(param);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取模块信息接口
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
[HttpPost, Route("GetESSModuleInfo")]
|
|
public async Task<ServiceResult<ModuleESSReturn>> GetESSModuleInfoAsync([FromBody] ModuleParam param)
|
|
{
|
|
return await _commonServices.GetESSModuleInfoAsync(param);
|
|
}
|
|
#endregion
|
|
|
|
#region 获取模块信息接口
|
|
/// <summary>
|
|
/// 获取模块信息接口
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
/// <param name="Keywords"></param>
|
|
/// <returns></returns>
|
|
[HttpPost, Route("GetSelect/{type}")]
|
|
public async Task<ServiceResult<CommonSelect>> GetSelectAsync(string type, string Keywords)
|
|
{
|
|
return await _commonServices.GetSelectAsync(type, Keywords);
|
|
}
|
|
#endregion
|
|
|
|
#region 导出Excel
|
|
/// <summary>
|
|
/// 导出Excel
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
[HttpPost, Route("ExportExcel")]
|
|
public async Task<ServiceResult<QueryExportReturn>> ExportExcelAsync([FromBody] QueryExport param)
|
|
{
|
|
return await _commonServices.ExportExcelAsync(param);
|
|
}
|
|
#endregion
|
|
|
|
#region Excel导入
|
|
/// <summary>
|
|
/// Excel导入
|
|
/// </summary>
|
|
/// <param name="file">文件</param>
|
|
/// <param name="menuName">菜单编号</param>
|
|
/// <param name="userId">用户ID</param>
|
|
/// <param name="langId">多语</param>
|
|
/// <returns></returns>
|
|
[HttpPost, Route("ImportExcel")]
|
|
public async Task<ServiceResult<QueryExportReturn>> ImportExcelAsync(IFormFile file, string menuName, int userId, int langId = 1)
|
|
{
|
|
return await _commonServices.ImportExcelAsync(file, menuName, langId, userId);
|
|
}
|
|
#endregion
|
|
|
|
#region 获取流程信息
|
|
/// <summary>
|
|
/// 获取流程信息
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
[HttpPost, Route("GetFlowInfo")]
|
|
public async Task<ServiceResult<FlowReturn>> GetFlowInfoAsync([FromBody] FlowParam param)
|
|
{
|
|
return await _commonServices.GetFlowInfoAsync(param);
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region 流程提交处理
|
|
/// <summary>
|
|
/// 流程提交处理
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
[HttpPost, Route("CommitFlow")]
|
|
public async Task<ServiceResult> CommitFlowAsync([FromBody] FlowCommitParam param)
|
|
{
|
|
return await _commonServices.CommitFlowAsync(param);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 调用存储过程封装
|
|
/// <summary>
|
|
/// 调用存储过程封装
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
[HttpPost, Route("getPostCommonAES")]
|
|
public async Task<Dictionary<String, Object>> getPostCommonAES([FromBody] JObject param)
|
|
{
|
|
return await _commonServices.getPostCommonAES(param);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 调用存储过程封装
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
[HttpPost, Route("getPostCommon")]
|
|
public async Task<Dictionary<String, Object>> getPostCommon([FromBody] JObject param)
|
|
{
|
|
return await _commonServices.getPostCommon(param);
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
[HttpGet, AllowAnonymous, Route("Compare")]
|
|
public IActionResult Download(Guid id)
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
DBServerProvider.SetConnection("defalut1", "Data Source=47.99.54.186;User ID=GHR;Password=Tiobon20190101;Database=Ghr5_MekTec;Encrypt=True;TrustServerCertificate=True;");
|
|
string sql = @"SELECT A.name AS COLUMN_NAME,
|
|
B.name TABLE_NAME,
|
|
C.value AS column_description,
|
|
C.[name],
|
|
D.NUMERIC_PRECISION,
|
|
D.NUMERIC_SCALE,
|
|
D.IS_NULLABLE,
|
|
D.data_type COLUMN_TYPE,
|
|
E.name AS ConstraintName,
|
|
E.definition AS COLUMN_DEFAULT,
|
|
A.object_id,
|
|
B.object_id
|
|
FROM sys.columns A
|
|
LEFT JOIN sys.tables B ON A.object_id = B.object_id
|
|
LEFT JOIN sys.extended_properties C
|
|
ON C.major_id = A.object_id AND C.minor_id = A.column_id
|
|
LEFT JOIN information_schema.columns D
|
|
ON D.column_name = A.name AND D.TABLE_NAME = B.[name]
|
|
LEFT JOIN sys.default_constraints E
|
|
ON E.parent_object_id = A.object_id
|
|
AND E.parent_column_id = A.column_id
|
|
WHERE B.name IS NOT NULL";
|
|
|
|
#region step 1.1 加载表结构
|
|
// 源数据库
|
|
DataTable dt = DbAccess.GetDataTable(sql);
|
|
var srcCloumns =
|
|
dt
|
|
.AsEnumerable()
|
|
.Select(o => new CompareStructureCloumn
|
|
{
|
|
TableName = o.Field<string>("TABLE_NAME"),
|
|
ColumnName = o.Field<string>("COLUMN_NAME"),
|
|
DefaultValue = o.Field<string>("COLUMN_DEFAULT"),
|
|
Null = o.Field<string>("IS_NULLABLE"),
|
|
DataType = o.Field<string>("COLUMN_TYPE"),
|
|
//Key = o.Field<string>("COLUMN_KEY"),
|
|
//Collsion = o.Field<string>("COLLATION_NAME"),
|
|
//CharacterSet = o.Field<string>("CHARACTER_SET_NAME")
|
|
})
|
|
.ToList();
|
|
Console.WriteLine("", $"加载源数据库[hdis_compare]信息完毕:共有表{srcCloumns.Select(o => o.TableName).Distinct().Count()} 字段 {srcCloumns.Count}");
|
|
|
|
// 目标数据库
|
|
dt = DBServerProvider.GetSqlDapper("defalut1").GetDataTable(sql);
|
|
var desrcCloumns =
|
|
dt
|
|
.AsEnumerable()
|
|
.Select(o => new CompareStructureCloumn
|
|
{
|
|
TableName = o.Field<string>("TABLE_NAME"),
|
|
ColumnName = o.Field<string>("COLUMN_NAME"),
|
|
DefaultValue = o.Field<string>("COLUMN_DEFAULT"),
|
|
Null = o.Field<string>("IS_NULLABLE"),
|
|
DataType = o.Field<string>("COLUMN_TYPE"),
|
|
//Key = o.Field<string>("COLUMN_KEY"),
|
|
//Collsion = o.Field<string>("COLLATION_NAME"),
|
|
//CharacterSet = o.Field<string>("CHARACTER_SET_NAME")
|
|
})
|
|
.ToList();
|
|
Console.WriteLine("", $"加载目标数据库[hdis]信息完毕:共有表{desrcCloumns.Select(o => o.TableName).Distinct().Count()} 字段 {desrcCloumns.Count}");
|
|
|
|
// #region 1.2 加载外键
|
|
// sql = @"SELECT T.TABLE_NAME, T.CONSTRAINT_NAME, T.CONSTRAINT_TYPE, K.COLUMN_NAME, K.REFERENCED_TABLE_NAME, K.REFERENCED_COLUMN_NAME
|
|
//FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS T
|
|
//JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE K ON T.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA AND T.TABLE_NAME = K.TABLE_NAME AND T.CONSTRAINT_NAME = K.CONSTRAINT_NAME
|
|
//JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS R ON T.CONSTRAINT_SCHEMA = R.CONSTRAINT_SCHEMA AND T.TABLE_NAME = R.TABLE_NAME AND T.CONSTRAINT_NAME = R.CONSTRAINT_NAME
|
|
//WHERE R.CONSTRAINT_SCHEMA = '{0}' AND T.CONSTRAINT_TYPE <> 'PRIMARY KEY';";
|
|
|
|
// // 源数据库
|
|
// dt = MySqlHelper.ExecuteDataset(m_ConnStr, string.Format(sql, "hdis_compare")).Tables[0];
|
|
// List<TableConstraint> srcConstraints = dt
|
|
// .AsEnumerable()
|
|
// .Select(m => new TableConstraint()
|
|
// {
|
|
// TABLE_NAME = m.Field<string>(nameof(TableConstraint.TABLE_NAME)),
|
|
// CONSTRAINT_NAME = m.Field<string>(nameof(TableConstraint.CONSTRAINT_NAME)),
|
|
// CONSTRAINT_TYPE = m.Field<string>(nameof(TableConstraint.CONSTRAINT_TYPE)),
|
|
// COLUMN_NAME = m.Field<string>(nameof(TableConstraint.COLUMN_NAME)),
|
|
// REFERENCED_TABLE_NAME = m.Field<string>(nameof(TableConstraint.REFERENCED_TABLE_NAME)),
|
|
// REFERENCED_COLUMN_NAME = m.Field<string>(nameof(TableConstraint.REFERENCED_COLUMN_NAME))
|
|
// })
|
|
// .ToList();
|
|
// Console.WriteLine("", $"加载源数据库[hdis_compare]外键完毕:共有外键 {srcConstraints.Count} 条");
|
|
|
|
// // 目标据库
|
|
// dt = MySqlHelper.ExecuteDataset(m_ConnStr, string.Format(sql, "hdis")).Tables[0];
|
|
// List<TableConstraint> destConstraints = dt
|
|
// .AsEnumerable()
|
|
// .Select(m => new TableConstraint()
|
|
// {
|
|
// TABLE_NAME = m.Field<string>(nameof(TableConstraint.TABLE_NAME)),
|
|
// CONSTRAINT_NAME = m.Field<string>(nameof(TableConstraint.CONSTRAINT_NAME)),
|
|
// CONSTRAINT_TYPE = m.Field<string>(nameof(TableConstraint.CONSTRAINT_TYPE)),
|
|
// COLUMN_NAME = m.Field<string>(nameof(TableConstraint.COLUMN_NAME)),
|
|
// REFERENCED_TABLE_NAME = m.Field<string>(nameof(TableConstraint.REFERENCED_TABLE_NAME)),
|
|
// REFERENCED_COLUMN_NAME = m.Field<string>(nameof(TableConstraint.REFERENCED_COLUMN_NAME))
|
|
// })
|
|
// .ToList();
|
|
// Console.WriteLine("", $"加载目标数据库[hdis]外键完毕:共有外键 {destConstraints.Count} 条");
|
|
// #endregion
|
|
|
|
// #region 1.3 加载索引
|
|
// sql = @"SELECT TABLE_NAME, NON_UNIQUE, INDEX_NAME, COLUMN_NAME, INDEX_TYPE
|
|
//FROM INFORMATION_SCHEMA.STATISTICS
|
|
//WHERE TABLE_SCHEMA = '{0}' AND INDEX_NAME <> 'PRIMARY';";
|
|
// // 源数据库
|
|
// dt = MySqlHelper.ExecuteDataset(m_ConnStr, string.Format(sql, "hdis_compare")).Tables[0];
|
|
// List<TableIndex> srcIndexes = dt
|
|
// .AsEnumerable()
|
|
// .Select(m => new TableIndex()
|
|
// {
|
|
// TABLE_NAME = m.Field<string>(nameof(TableIndex.TABLE_NAME)),
|
|
// NON_UNIQUE = m.Field<long>(nameof(TableIndex.NON_UNIQUE)),
|
|
// INDEX_NAME = m.Field<string>(nameof(TableIndex.INDEX_NAME)),
|
|
// COLUMN_NAME = m.Field<string>(nameof(TableIndex.COLUMN_NAME)),
|
|
// INDEX_TYPE = m.Field<string>(nameof(TableIndex.INDEX_TYPE))
|
|
// })
|
|
// .ToList();
|
|
// SenConsole.WriteLinedLog("", $"加载源数据库[hdis_compare]索引完毕:共有索引 {srcConstraints.Count} 条");
|
|
|
|
// // 源数据库
|
|
// dt = MySqlHelper.ExecuteDataset(m_ConnStr, string.Format(sql, "hdis")).Tables[0];
|
|
// List<TableIndex> destIndexes = dt
|
|
// .AsEnumerable()
|
|
// .Select(m => new TableIndex()
|
|
// {
|
|
// TABLE_NAME = m.Field<string>(nameof(TableIndex.TABLE_NAME)),
|
|
// NON_UNIQUE = m.Field<long>(nameof(TableIndex.NON_UNIQUE)),
|
|
// INDEX_NAME = m.Field<string>(nameof(TableIndex.INDEX_NAME)),
|
|
// COLUMN_NAME = m.Field<string>(nameof(TableIndex.COLUMN_NAME)),
|
|
// INDEX_TYPE = m.Field<string>(nameof(TableIndex.INDEX_TYPE))
|
|
// })
|
|
// .ToList();
|
|
// Console.WriteLine("", $"加载目标数据库[hdis]索引完毕:共有索引 {srcConstraints.Count} 条");
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region step 2 比对数据库结构
|
|
Console.WriteLine("", "开始比较数据库结构");
|
|
#region 2.1 比较字段
|
|
var sExcept = srcCloumns.Except(desrcCloumns, new CompareStructureCloumnComparer()).ToList();
|
|
var dExcept = desrcCloumns.Except(srcCloumns, new CompareStructureCloumnComparer()).ToList();
|
|
var except = new List<CompareStructureCloumn>();
|
|
dExcept.ForEach(o =>
|
|
{
|
|
bool has = false;
|
|
sExcept.ForEach(p =>
|
|
{
|
|
//具有相同的两个重复记录
|
|
if (o.TableName == p.TableName && o.ColumnName == p.ColumnName)
|
|
{
|
|
has = true;
|
|
return;
|
|
}
|
|
});
|
|
if (!has)
|
|
{
|
|
except.Add(o);
|
|
}
|
|
});
|
|
except = except.Union(sExcept).ToList();
|
|
Console.WriteLine("", "字段比较完毕");
|
|
if (except.Count <= 0)
|
|
{
|
|
Console.WriteLine("", "数据结构完全一致");
|
|
return Ok("数据结构完全一致");
|
|
}
|
|
#endregion
|
|
|
|
#region 2.2 生成需要创建的表
|
|
bool b_hasConetnt = false;
|
|
//sb.AppendLine("SET FOREIGN_KEY_CHECKS = 0;\r\n");
|
|
var temp = except.Select(o => o.TableName).Distinct();
|
|
Console.WriteLine("", $"字段不同数量为 {except.Count}");
|
|
var sTempNames = srcCloumns.Select(o => o.TableName);
|
|
var dTempNames = desrcCloumns.Select(o => o.TableName);
|
|
|
|
var createTablesNames = temp.Where(o => !dTempNames.Contains(o)).ToList();
|
|
Console.WriteLine("", $"生成需要创建的表 数量 {createTablesNames.Count}");
|
|
if (createTablesNames.Any())
|
|
{
|
|
createTablesNames.ForEach(tname =>
|
|
{
|
|
|
|
string sql1 = @$"DECLARE @TableName NVARCHAR(128) = '{tname}';
|
|
DECLARE @SQL NVARCHAR (MAX);
|
|
|
|
-- 获取表的创建语句
|
|
SET @SQL = 'CREATE TABLE ' + @TableName + ' (' + CHAR (13) + CHAR (10);
|
|
|
|
-- 获取列信息
|
|
SELECT @SQL =
|
|
@SQL
|
|
+ ' '
|
|
+ c.name
|
|
+ ' '
|
|
+ t.name
|
|
+ CASE
|
|
WHEN t.name IN ('nvarchar',
|
|
'varchar',
|
|
'char',
|
|
'nchar')
|
|
THEN
|
|
'('
|
|
+ CASE
|
|
WHEN c.max_length = -1 THEN 'max'
|
|
ELSE CAST (c.max_length AS NVARCHAR (10))
|
|
END
|
|
+ ')'
|
|
WHEN t.name IN ('decimal', 'numeric')
|
|
THEN
|
|
'('
|
|
+ CAST (c.precision AS NVARCHAR (10))
|
|
+ ', '
|
|
+ CAST (c.scale AS NVARCHAR (10))
|
|
+ ')'
|
|
ELSE
|
|
''
|
|
END
|
|
+ CASE WHEN c.is_nullable = 0 THEN ' NOT NULL' ELSE ' NULL' END
|
|
+ CASE
|
|
WHEN E.definition IS NOT NULL
|
|
THEN
|
|
' default '
|
|
+ REPLACE
|
|
(
|
|
REPLACE
|
|
(CAST (E.definition AS NVARCHAR (MAX)), ')', ''),
|
|
'(',
|
|
'')
|
|
ELSE
|
|
''
|
|
END
|
|
+ ','
|
|
+ CASE
|
|
WHEN ep.value IS NOT NULL
|
|
THEN
|
|
' -- ' + CAST (ep.value AS NVARCHAR (MAX))
|
|
ELSE
|
|
''
|
|
END
|
|
+ CHAR (13)
|
|
+ CHAR (10)
|
|
FROM sys.columns c
|
|
INNER JOIN sys.types t ON c.user_type_id = t.user_type_id
|
|
LEFT JOIN sys.extended_properties ep
|
|
ON ep.major_id = c.object_id
|
|
AND ep.minor_id = c.column_id
|
|
AND ep.class = 1
|
|
LEFT JOIN sys.default_constraints E
|
|
ON E.parent_object_id = c.object_id
|
|
AND E.parent_column_id = c.column_id
|
|
WHERE c.object_id = OBJECT_ID (@TableName);
|
|
|
|
-- 移除最后一个逗号
|
|
SET @SQL = LEFT (@SQL, LEN (@SQL) - 2);
|
|
|
|
-- 添加主键信息
|
|
SELECT @SQL =
|
|
@SQL
|
|
+ CHAR (13)
|
|
+ CHAR (10)
|
|
+ ' CONSTRAINT PK_'
|
|
+ @TableName
|
|
+ ' PRIMARY KEY ('
|
|
+ COL_NAME (ic.object_id, ic.column_id)
|
|
+ ')'
|
|
+ CHAR (13)
|
|
+ CHAR (10)
|
|
+ ')'
|
|
FROM sys.indexes i
|
|
INNER JOIN sys.index_columns ic
|
|
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
|
|
WHERE i.object_id = OBJECT_ID (@TableName) AND i.is_primary_key = 1;
|
|
|
|
-- 输出生成的 SQL 语句
|
|
SELECT @SQL SQL;";
|
|
DataTable ds = DbAccess.GetDataTable(sql1);
|
|
if (ds.Rows.Count > 0)
|
|
{
|
|
b_hasConetnt = true;
|
|
sb.AppendLine($"{ds.Rows[0]["SQL"].ToString()};\r\n");
|
|
Console.WriteLine("", $"生成表[{tname}]结构语句");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("", $"生成表[{tname}]结构语句异常,表不存在");
|
|
}
|
|
});
|
|
Console.WriteLine("", $"生成需要创建的表完毕");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 2.3 生成需要删除的表
|
|
var deleteTablesNames = temp.Where(o => !sTempNames.Contains(o)).ToList();
|
|
Console.WriteLine("", $"生成需要删除的表 数量 {deleteTablesNames.Count}");
|
|
if (deleteTablesNames.Any())
|
|
{
|
|
deleteTablesNames.ForEach(tname =>
|
|
{
|
|
b_hasConetnt = true;
|
|
sb.AppendLine($"DROP TABLE {tname};\r\n");
|
|
Console.WriteLine("", $"生成表[{tname}]删除语句");
|
|
});
|
|
Console.WriteLine("", $"生成需要删除的表完毕");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 2.4 生成需要修改的表
|
|
var deletedColumns = new List<(string table, string column)>();
|
|
var alterTablesNames = temp.Where(o => dTempNames.Contains(o) && sTempNames.Contains(o)).ToList();
|
|
Console.WriteLine("", $"生成需要修改的表 数量 {alterTablesNames.Count}");
|
|
if (alterTablesNames.Any())
|
|
{
|
|
alterTablesNames.ForEach(o =>
|
|
{
|
|
#region 2.4.1 创建列语句
|
|
except
|
|
.Where(t => t.TableName == o && !desrcCloumns.Where(p => p.TableName == o).Select(p => p.ColumnName).ToList().Contains(t.ColumnName))
|
|
.ToList()
|
|
.ForEach(t =>
|
|
{
|
|
b_hasConetnt = true;
|
|
//sb.AppendLine($"ALTER TABLE `hdis`.`{t.TableName}`" +
|
|
// $" ADD COLUMN `{t.ColumnName}` {t.DataType}{GetCharacter_Set_Name(t.CharacterSet)}{GetCollate(t.Collsion)}{GetNull(t.Null)}{GetDefaultValue(t.DefaultValue, t.DataType)};\r\n");
|
|
|
|
sb.AppendLine($"ALTER TABLE [{t.TableName}] ADD [{t.ColumnName}] {t.DataType} NULL;\r\n");
|
|
//ALTER TABLE[dbo].[Ghre_StudyRule] ADD[Column_42] int NOT NULL DEFAULT 1
|
|
});
|
|
#endregion
|
|
|
|
#region 2.4.2 删除列语句
|
|
except
|
|
.Where(t => t.TableName == o && !srcCloumns.Where(p => p.TableName == o).Select(p => p.ColumnName).ToList().Contains(t.ColumnName))
|
|
.ToList()
|
|
.ForEach(t =>
|
|
{
|
|
if (t.ColumnName == "deleted") // 如果添加删除列 deleted,生成删除垃圾数据的语句
|
|
{
|
|
sb.AppendLine($"/* 删除垃圾数据 */");
|
|
sb.AppendLine($"DELETE FROM hdis.{t.TableName} WHERE deleted = 1;");
|
|
}
|
|
//string forignKey = QueryForignKeyProduct("hdis", tableName: o, colName: t.ColumnName);
|
|
//string indexs = QueryIndexProduct("hdis", tableName: o, colName: t.ColumnName);
|
|
//b_hasConetnt = true;
|
|
//if (!string.IsNullOrEmpty(forignKey)) sb.AppendLine(forignKey);
|
|
//if (!string.IsNullOrEmpty(indexs)) sb.AppendLine(indexs);
|
|
|
|
sb.AppendLine($"ALTER TABLE [{t.TableName}] DROP COLUMN {t.ColumnName};\r\n");
|
|
//ALTER TABLE [dbo].[Ghre_StudyRule] DROP COLUMN [IsDeptLower]
|
|
deletedColumns.Add((t.TableName, t.ColumnName));
|
|
});
|
|
#endregion
|
|
|
|
#region 2.4.3 修改列语句
|
|
except
|
|
.Where(t => t.TableName == o && srcCloumns.Where(p => p.TableName == o).Select(p => p.ColumnName).ToList().Contains(t.ColumnName)
|
|
&& desrcCloumns.Where(p => p.TableName == o).Select(p => p.ColumnName).ToList().Contains(t.ColumnName))
|
|
.ToList()
|
|
.ForEach(t =>
|
|
{
|
|
b_hasConetnt = true;
|
|
|
|
var old = desrcCloumns.Where(x => x.TableName == t.TableName && x.ColumnName == t.ColumnName).FirstOrDefault();
|
|
|
|
if (old != null && old.DataType != t.DataType)
|
|
{
|
|
if (t.DataType == "varchar")
|
|
{
|
|
sb.AppendLine($"ALTER TABLE [dbo].[{t.TableName}]");
|
|
sb.AppendLine($"ALTER COLUMN [{t.ColumnName}] {t.DataType} COLLATE Chinese_PRC_CI_AS;\r\n");
|
|
}
|
|
else if (t.DataType == "date" || t.DataType == "datetime" || t.DataType == "datet" || t.DataType == "bigint" || t.DataType == "int")
|
|
{
|
|
sb.AppendLine($"ALTER TABLE [dbo].[{t.TableName}]");
|
|
sb.AppendLine($"ALTER COLUMN [{t.ColumnName}] {t.DataType};\r\n");
|
|
}
|
|
}
|
|
|
|
//string renovate = RenovateNotNull("hdis", t.TableName, t.ColumnName, t.Null, t.DefaultValue, t.DataType);
|
|
//sb.AppendLine(renovate);
|
|
//sb.AppendLine($"ALTER TABLE `hdis`.`{t.TableName}` CHANGE `{t.ColumnName}` `{t.ColumnName}` {t.DataType}{GetCharacter_Set_Name(t.CharacterSet)}{GetCollate(t.Collsion)}{GetNull(t.Null)}{GetDefaultValue(t.DefaultValue, t.DataType)};\r\n");
|
|
});
|
|
#endregion
|
|
});
|
|
Console.WriteLine("", $"生成需要删除的表完毕");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("", $"没有需要修改的表");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 2.5 外键
|
|
//int newConstraintCount = 0;
|
|
//foreach (var constraint in srcConstraints)
|
|
//{
|
|
// if (createTablesNames.Contains(constraint.TABLE_NAME))
|
|
// {
|
|
// continue;
|
|
// }
|
|
// if (deletedColumns.Contains((constraint.TABLE_NAME, constraint.COLUMN_NAME)))
|
|
// {
|
|
// continue;
|
|
// }
|
|
// if (!destConstraints.Any(x => x.CONSTRAINT_NAME == constraint.CONSTRAINT_NAME
|
|
// && x.TABLE_NAME == constraint.TABLE_NAME && x.COLUMN_NAME == constraint.COLUMN_NAME
|
|
// && x.REFERENCED_TABLE_NAME == constraint.REFERENCED_TABLE_NAME && x.REFERENCED_COLUMN_NAME == constraint.REFERENCED_COLUMN_NAME))
|
|
// {
|
|
// newConstraintCount++;
|
|
// sb.AppendLine($"ALTER TABLE `hdis`.`{constraint.TABLE_NAME}` ADD CONSTRAINT `{constraint.CONSTRAINT_NAME}` FOREIGN KEY(`{constraint.COLUMN_NAME}`) REFERENCES `{constraint.REFERENCED_TABLE_NAME}` (`{constraint.REFERENCED_COLUMN_NAME}`);");
|
|
// }
|
|
//}
|
|
//if (newConstraintCount > 0)
|
|
//{
|
|
// b_hasConetnt = true;
|
|
// Console.WriteLine("", $"生成需要添加的外键 数量 {newConstraintCount}");
|
|
// sb.AppendLine();
|
|
//}
|
|
//else
|
|
//{
|
|
// Console.WriteLine("", $"没有需要添加的外键");
|
|
//}
|
|
//int removeConstraintCount = 0;
|
|
//foreach (var constraint in destConstraints)
|
|
//{
|
|
// if (deletedColumns.Contains((constraint.TABLE_NAME, constraint.COLUMN_NAME)))
|
|
// {
|
|
// continue;
|
|
// }
|
|
// if (!srcConstraints.Any(x => x.CONSTRAINT_NAME == constraint.CONSTRAINT_NAME
|
|
// && x.TABLE_NAME == constraint.TABLE_NAME && x.COLUMN_NAME == constraint.COLUMN_NAME
|
|
// && x.REFERENCED_TABLE_NAME == constraint.REFERENCED_TABLE_NAME && x.REFERENCED_COLUMN_NAME == constraint.REFERENCED_COLUMN_NAME))
|
|
// {
|
|
// removeConstraintCount++;
|
|
// sb.AppendLine($"ALTER TABLE `hdis`.`{constraint.TABLE_NAME}` DROP FOREIGN KEY `{constraint.CONSTRAINT_NAME}`;");
|
|
// }
|
|
//}
|
|
//if (removeConstraintCount > 0)
|
|
//{
|
|
// b_hasConetnt = true;
|
|
// Console.WriteLine("", $"生成需要删除的外键 数量 {removeConstraintCount}");
|
|
// sb.AppendLine();
|
|
//}
|
|
//else
|
|
//{
|
|
// Console.WriteLine("", $"没有需要删除的外键");
|
|
//}
|
|
//lock (m_LockProgressBar)
|
|
//{
|
|
// m_PBValue = 5;
|
|
//}
|
|
#endregion
|
|
|
|
#region 2.6 索引
|
|
//int newIndexCount = 0;
|
|
//foreach (var index in srcIndexes)
|
|
//{
|
|
// if (createTablesNames.Contains(index.TABLE_NAME))
|
|
// {
|
|
// continue;
|
|
// }
|
|
// if (deletedColumns.Contains((index.TABLE_NAME, index.COLUMN_NAME)))
|
|
// {
|
|
// continue;
|
|
// }
|
|
// if (!destIndexes.Any(x => x.INDEX_NAME == index.INDEX_NAME
|
|
// && x.TABLE_NAME == index.TABLE_NAME && x.COLUMN_NAME == index.COLUMN_NAME
|
|
// && x.NON_UNIQUE == index.NON_UNIQUE && x.INDEX_TYPE == index.INDEX_TYPE))
|
|
// {
|
|
// newIndexCount++;
|
|
// if (index.NON_UNIQUE == 0)
|
|
// {
|
|
// sb.AppendLine($"ALTER TABLE `hdis`.`{index.TABLE_NAME}` ADD UNIQUE INDEX `{index.INDEX_NAME}` (`{index.COLUMN_NAME}`);");
|
|
// }
|
|
// else if (index.INDEX_TYPE == "FULLTEXT")
|
|
// {
|
|
// sb.AppendLine($"ALTER TABLE `hdis`.`{index.TABLE_NAME}` ADD FULLTEXT INDEX `{index.INDEX_NAME}` (`{index.COLUMN_NAME}`) USING {index.INDEX_TYPE};");
|
|
// }
|
|
// else if (index.INDEX_TYPE == "SPATIAL")
|
|
// {
|
|
// sb.AppendLine($"ALTER TABLE `hdis`.`{index.TABLE_NAME}` ADD SPATIAL INDEX `{index.INDEX_NAME}` (`{index.COLUMN_NAME}`) USING {index.INDEX_TYPE};");
|
|
// }
|
|
// else
|
|
// {
|
|
// sb.AppendLine($"ALTER TABLE `hdis`.`{index.TABLE_NAME}` ADD INDEX `{index.INDEX_NAME}` (`{index.COLUMN_NAME}`) USING {index.INDEX_TYPE};");
|
|
// }
|
|
// }
|
|
//}
|
|
//if (newIndexCount > 0)
|
|
//{
|
|
// b_hasConetnt = true;
|
|
// Console.WriteLine("", $"生成需要添加的索引 数量 {newIndexCount}");
|
|
// sb.AppendLine();
|
|
//}
|
|
//else
|
|
//{
|
|
// Console.WriteLine("", $"没有需要添加的索引");
|
|
//}
|
|
//int removeIndexCount = 0;
|
|
//foreach (var index in destIndexes)
|
|
//{
|
|
// if (deletedColumns.Contains((index.TABLE_NAME, index.COLUMN_NAME)))
|
|
// {
|
|
// continue;
|
|
// }
|
|
// if (!srcIndexes.Any(x => x.INDEX_NAME == index.INDEX_NAME
|
|
// && x.TABLE_NAME == index.TABLE_NAME && x.COLUMN_NAME == index.COLUMN_NAME
|
|
// && x.NON_UNIQUE == index.NON_UNIQUE && x.INDEX_TYPE == index.INDEX_TYPE))
|
|
// {
|
|
// removeIndexCount++;
|
|
// sb.AppendLine($"ALTER TABLE `hdis`.`{index.TABLE_NAME}` DROP INDEX `{index.INDEX_NAME}`;");
|
|
// }
|
|
//}
|
|
//if (removeIndexCount > 0)
|
|
//{
|
|
// b_hasConetnt = true;
|
|
// Console.WriteLine("", $"生成需要删除的索引 数量 {removeIndexCount}");
|
|
// sb.AppendLine();
|
|
//}
|
|
//else
|
|
//{
|
|
// Console.WriteLine("", $"没有需要删除的索引");
|
|
//}
|
|
//lock (m_LockProgressBar)
|
|
//{
|
|
// m_PBValue = 6;
|
|
//}
|
|
#endregion
|
|
|
|
//sb.AppendLine("SET FOREIGN_KEY_CHECKS = 1;\r\n");
|
|
|
|
if (b_hasConetnt)
|
|
{
|
|
Console.WriteLine("", "比较数据库结构完毕");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("", "比较数据库结构完毕,数据结构完全一致");
|
|
}
|
|
#endregion
|
|
|
|
FileHelper.WriteFile("C:/" + Guid.NewGuid() + ".sql", sb.ToString());
|
|
return Ok(sb.ToString());
|
|
//string sql = "SELECT * FROM FileAttachment where ID='{0}' and IsDeleted='false'";
|
|
|
|
|
|
//if (attachment != null)
|
|
//{
|
|
// var fileName = attachment.FileName;
|
|
// string path = "wwwroot/" + attachment.Path + fileName;
|
|
// FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
|
|
// fs.Close();
|
|
// return File(new FileStream(path, FileMode.Open), "application/octet-stream", fileName);
|
|
//}
|
|
//else
|
|
//{
|
|
// return Ok("无效ID");
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet, AllowAnonymous, Route("Test")]
|
|
public async Task<IActionResult> Test(Guid id)
|
|
{
|
|
|
|
await _httpPollyHelper.DownLoad("http://120.26.205.42:3002/DataUser/ImgAth/Upload/202152216596.png", $"/files/upload/{DateTimeHelper.ConvertToSecondString1(DateTime.Now)}.png");
|
|
return Ok("Success");
|
|
|
|
}
|
|
|
|
|
|
#region 表信息
|
|
/// <summary>
|
|
/// 表信息
|
|
/// </summary>
|
|
public class CompareStructureCloumn
|
|
{
|
|
/// <summary>
|
|
/// 表名称
|
|
/// </summary>
|
|
public string TableName { set; get; }
|
|
/// <summary>
|
|
/// 字段名称
|
|
/// </summary>
|
|
public string ColumnName { set; get; }
|
|
/// <summary>
|
|
/// 数据类型
|
|
/// </summary>
|
|
public string DataType { set; get; }
|
|
/// <summary>
|
|
/// 字符集
|
|
/// </summary>
|
|
public string CharacterSet { set; get; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string Collsion { set; get; }
|
|
/// <summary>
|
|
/// 是否为空
|
|
/// </summary>
|
|
public string Null { set; get; }
|
|
/// <summary>
|
|
/// 默认值
|
|
/// </summary>
|
|
public string DefaultValue { set; get; }
|
|
/// <summary>
|
|
/// 键
|
|
/// </summary>
|
|
public string Key { set; get; }
|
|
|
|
}
|
|
#endregion
|
|
|
|
|
|
|
|
#region 自定义比对器
|
|
/// <summary>
|
|
/// 自定义比对器
|
|
/// </summary>
|
|
public class CompareStructureCloumnComparer : IEqualityComparer<CompareStructureCloumn>
|
|
{
|
|
/// <summary>
|
|
/// 比对规则
|
|
/// </summary>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
/// <returns></returns>
|
|
public bool Equals(CompareStructureCloumn x, CompareStructureCloumn y)
|
|
{
|
|
var b = x.TableName == y.TableName && x.ColumnName == y.ColumnName
|
|
&& x.CharacterSet == y.CharacterSet && (x.Collsion == y.Collsion)
|
|
&& x.DataType == y.DataType && x.DefaultValue == y.DefaultValue
|
|
&& x.Key == y.Key && x.Null == y.Null;
|
|
return b;
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public int GetHashCode(CompareStructureCloumn obj)
|
|
{
|
|
return obj.ToString().GetHashCode();
|
|
}
|
|
}
|
|
#endregion
|
|
} |