diff --git a/src/Archiver/Commands/CopyTable2Command.cs b/src/Archiver/Commands/CopyTable2Command.cs new file mode 100644 index 0000000..e6f00da --- /dev/null +++ b/src/Archiver/Commands/CopyTable2Command.cs @@ -0,0 +1,159 @@ +using Chenfeng.MES.Archiver.Core; +using Chenfeng.MES.Archiver.Data; +using CliFx; +using CliFx.Attributes; +using CliFx.Infrastructure; +using Dapper; +using MySqlConnector; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Archiver.Commands +{ + [Command("copy2")] + public class CopyTableCommand : ICommand + { + public Action Print { get; set; } = (text) => { }; + + [CommandOption("legacy", Description = "旧版MES")] + public bool Legacy { get; set; } = false; + + [CommandParameter(0, Description = "数据库连接")] + public string Connection { get; set; } = ""; + + public MySqlConnection? Db { get; private set; } + + public ValueTask ExecuteAsync(IConsole console) + { + Print = (text) => console.Output.WriteLine($"{DateTime.Now.ToLongTimeString()} {text}"); + if (this.Connection.Length > 0) + { + var connBuilder = new MySqlConnectionStringBuilder(this.Connection); + connBuilder.SslMode = MySqlSslMode.None; + connBuilder.CharacterSet = "utf8"; + //connBuilder.ConnectionTimeout = 5 * 60; + Db = new MySqlConnection(connBuilder.ConnectionString); + + SqlMapper.Settings.CommandTimeout = 0; + } + + var starMonth = DateTime.Now.AddMonths(-12).ToString("yyyyMM"); + if (Legacy) + { + MesLegacyTables(starMonth); + } + else + { + MesTables(starMonth); + } + Print("操作完成"); + + return ValueTask.CompletedTask; + } + + private void ExecuteRow(string table, string key, object param, MySqlTransaction? transaction) + { + Db.Execute($"INSERT INTO `{table}_new` SELECT * FROM `{table}` where {key}=@{key}", param, transaction); + } + + private IEnumerable GetIdList(string table, string field, string condition, object param) + { + return Db.Query($"select {field} from `{table}` where {condition}", param); + } + + public void MesTables(string starMonth) + { + Print("复制订单"); + foreach (var orderno in GetIdList("order", "orderno", "LEFT(orderno,6) >= @starMonth", new { starMonth })) + { + using (var trans = GetTransaction()) + { + foreach (var childTable in new[] { "order_box_block", "order_data_block", "order_data_goods", "order_data_parts", "order_item", "order_module_extra", "order_module_item", "order_package" }) + { + ExecuteRow(childTable, "orderno", new { orderno }, trans); + } + ExecuteRow("order", "orderno", new { orderno }, trans); + trans.Commit(); + } + } + + Print("复制工序"); + + foreach (var id in GetIdList("order_process", "id", "LEFT(orderno,6) >= @starMonth", new { starMonth })) + { + using (var trans = GetTransaction()) + { + foreach (var childTable in new[] { "order_process_step_item", "order_process_step" }) + { + ExecuteRow(childTable, "OrderProcessID", new { OrderProcessID = id }, trans); + } + ExecuteRow("order_process", "id", new { id }, trans); + trans.Commit(); + } + } + + Print("复制标准版订单"); + foreach (var id in GetIdList("simple_plan_order", "PlaceNo", "LEFT(DATE_FORMAT(CreateTime,'%Y%m'),6) >= @starMonth", new { starMonth })) + { + ExecuteRow("simple_plan_order", "PlaceNo", new { PlaceNo = id }, null); + } + + Print("复制排单"); + foreach (var id in GetIdList("order_block_plan", "id", "LEFT(DATE_FORMAT(CreateTime,'%Y%m'),6) >= @starMonth", new { starMonth })) + { + //if (Db.State != System.Data.ConnectionState.Open) Db.Open(); + using (var trans = GetTransaction()) + { + ExecuteRow("order_block_plan", "ID", new { ID = id }, trans); + ExecuteRow("order_block_plan_result", "ID", new { ID = id }, trans); + trans.Commit(); + } + } + + //CopyStep((table, newTable) => + //{ + // CopyData(table, newTable, "concat('20',left(id,4)) "+op+" @starMonth ", new { starMonth }); + //}, "order_scrap_board"); + } + + public MySqlTransaction GetTransaction() + { + if (Db.State != System.Data.ConnectionState.Open) Db.Open(); + return Db.BeginTransaction(); + } + + public void MesLegacyTables( string starMonth) + { + Print("复制旧版订单"); + + foreach (var orderno in GetIdList("saleorder", "orderno", "LEFT(DATE_FORMAT(SaleDate,'%Y%m'),6) >= @starMonth", new { starMonth })) + { + using (var trans = GetTransaction()) + { + foreach (var childTable in new[] { "SaleOrderItem", "saleblock", "saleobject", "saleorderobject", "saleorderoffer", "saleorderpackage", "saleblockobjids", "saleobjectobjids", "saleorder_block_cadmodelinfo", "saleorderblockbaseposition", + "saleordergoodsinfo", "saleorder_block_point", "saleorder_block_pointinfo", "orderprocess", "orderprocessstep", "orderprocessstepitem"}) + { + ExecuteRow(childTable, "orderno", new { orderno }, trans); + } + ExecuteRow("saleorder", "orderno", new { orderno }, trans); + trans.Commit(); + } + } + + Print("复制旧版排单"); + + foreach (var id in GetIdList("orderblockprocessplan", "id", "LEFT(DATE_FORMAT(createtime,'%Y%m'),6) >= @starMonth", new { starMonth })) + { + using (var trans = GetTransaction()) + { + ExecuteRow("orderblockprocessplan", "ID", new { ID = id }, trans); + ExecuteRow("orderblockprocessplanplaceresult", "ID", new { ID = id }, trans); + trans.Commit(); + } + } + } + } +} diff --git a/src/Archiver/Commands/CopyTableCommand.cs b/src/Archiver/Commands/CopyTableCommand.cs index 558b8dc..e1b4e9d 100644 --- a/src/Archiver/Commands/CopyTableCommand.cs +++ b/src/Archiver/Commands/CopyTableCommand.cs @@ -48,6 +48,9 @@ namespace Chenfeng.MES.Archiver.Commands [CommandOption("verbose", 'v', Description = "显示sql")] public bool Verbose { get; set; } + [CommandOption("copy-data",Description ="拷贝数据")] + public bool CopyData { get; set; } = false; + public ValueTask ExecuteAsync(IConsole console) { Print = (text) => console.Output.WriteLine($"{DateTime.Now.ToLongTimeString()} {text}"); @@ -158,7 +161,7 @@ namespace Chenfeng.MES.Archiver.Commands ExecuteSql($"DROP TABLE IF EXISTS `{TableDic[table]}`"); ExecuteSql($"CREATE TABLE `{TableDic[table]}` LIKE `{table}`"); // IF NOT EXISTS Print(TableDic[table] + " 创建完成"); - return true; + return CopyData; } return false; } diff --git a/src/Archiver/Commands/DeleteTableCommand.cs b/src/Archiver/Commands/DeleteTableCommand.cs new file mode 100644 index 0000000..0f57c66 --- /dev/null +++ b/src/Archiver/Commands/DeleteTableCommand.cs @@ -0,0 +1,159 @@ +using Chenfeng.MES.Archiver.Core; +using Chenfeng.MES.Archiver.Data; +using CliFx; +using CliFx.Attributes; +using CliFx.Infrastructure; +using Dapper; +using MySqlConnector; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Archiver.Commands +{ + [Command("delete")] + public class DeleteTableCommand : ICommand + { + public Action Print { get; set; } = (text) => { }; + + [CommandOption("legacy", Description = "旧版MES")] + public bool Legacy { get; set; } = false; + + [CommandParameter(0, Description = "数据库连接")] + public string Connection { get; set; } = ""; + + public MySqlConnection? Db { get; private set; } + + public ValueTask ExecuteAsync(IConsole console) + { + Print = (text) => console.Output.WriteLine($"{DateTime.Now.ToLongTimeString()} {text}"); + if (this.Connection.Length > 0) + { + var connBuilder = new MySqlConnectionStringBuilder(this.Connection); + connBuilder.SslMode = MySqlSslMode.None; + connBuilder.CharacterSet = "utf8"; + //connBuilder.ConnectionTimeout = 5 * 60; + Db = new MySqlConnection(connBuilder.ConnectionString); + + SqlMapper.Settings.CommandTimeout = 0; + } + + var starMonth = DateTime.Now.AddMonths(-12).ToString("yyyyMM"); + if (Legacy) + { + MesLegacyTables(starMonth); + } + else + { + MesTables(starMonth); + } + Print("操作完成"); + + return ValueTask.CompletedTask; + } + + private void DeleteRow(string table, string key, object param, MySqlTransaction? transaction) + { + Db.Execute($"DELETE FROM `{table}` where {key}=@{key}", param, transaction); + } + + private IEnumerable GetIdList(string table, string field, string condition, object param) + { + return Db.Query($"select {field} from `{table}` where {condition}", param); + } + + public void MesTables(string starMonth, string op = "<") + { + Print("清理订单"); + foreach (var orderno in GetIdList("order", "orderno", "LEFT(orderno,6) < @starMonth", new { starMonth })) + { + using (var trans = GetTransaction()) + { + foreach (var childTable in new[] { "order_box_block", "order_data_block", "order_data_goods", "order_data_parts", "order_item", "order_module_extra", "order_module_item", "order_package" }) + { + DeleteRow(childTable, "orderno", new { orderno }, trans); + } + DeleteRow("order", "orderno", new { orderno }, trans); + trans.Commit(); + } + } + + Print("清理工序"); + + foreach (var id in GetIdList("order_process", "id", "LEFT(orderno,6) < @starMonth", new { starMonth })) + { + using (var trans = GetTransaction()) + { + foreach (var childTable in new[] { "order_process_step_item", "order_process_step" }) + { + DeleteRow(childTable, "OrderProcessID", new { OrderProcessID = id }, trans); + } + DeleteRow("order_process", "id", new { id }, trans); + trans.Commit(); + } + } + + Print("清理标准版订单"); + foreach (var id in GetIdList("simple_plan_order", "PlaceNo", "LEFT(DATE_FORMAT(CreateTime,'%Y%m'),6) < @starMonth", new { starMonth })) + { + DeleteRow("simple_plan_order", "PlaceNo", new { PlaceNo = id }, null); + } + + Print("清理排单"); + foreach (var id in GetIdList("order_block_plan", "id", "LEFT(DATE_FORMAT(CreateTime,'%Y%m'),6) < @starMonth", new { starMonth })) + { + //if (Db.State != System.Data.ConnectionState.Open) Db.Open(); + using (var trans = GetTransaction()) + { + DeleteRow("order_block_plan", "ID", new { ID = id }, trans); + DeleteRow("order_block_plan_result", "ID", new { ID = id }, trans); + trans.Commit(); + } + } + + //CopyStep((table, newTable) => + //{ + // CopyData(table, newTable, "concat('20',left(id,4)) "+op+" @starMonth ", new { starMonth }); + //}, "order_scrap_board"); + } + + public MySqlTransaction GetTransaction() + { + if (Db.State != System.Data.ConnectionState.Open) Db.Open(); + return Db.BeginTransaction(); + } + + public void MesLegacyTables( string starMonth) + { + Print("清理旧版订单"); + + foreach (var orderno in GetIdList("saleorder", "orderno", "LEFT(DATE_FORMAT(SaleDate,'%Y%m'),6) < @starMonth", new { starMonth })) + { + using (var trans = GetTransaction()) + { + foreach (var childTable in new[] { "SaleOrderItem", "saleblock", "saleobject", "saleorderobject", "saleorderoffer", "saleorderpackage", "saleblockobjids", "saleobjectobjids", "saleorder_block_cadmodelinfo", "saleorderblockbaseposition", + "saleordergoodsinfo", "saleorder_block_point", "saleorder_block_pointinfo", "orderprocess", "orderprocessstep", "orderprocessstepitem"}) + { + DeleteRow(childTable, "orderno", new { orderno }, trans); + } + DeleteRow("saleorder", "orderno", new { orderno }, trans); + trans.Commit(); + } + } + + Print("清理旧版排单"); + + foreach (var id in GetIdList("orderblockprocessplan", "id", "LEFT(DATE_FORMAT(createtime,'%Y%m'),6) < @starMonth", new { starMonth })) + { + using (var trans = GetTransaction()) + { + DeleteRow("orderblockprocessplan", "ID", new { ID = id }, trans); + DeleteRow("orderblockprocessplanplaceresult", "ID", new { ID = id }, trans); + trans.Commit(); + } + } + } + } +} diff --git a/src/Archiver/Commands/OptimizeTableCommand.cs b/src/Archiver/Commands/OptimizeTableCommand.cs new file mode 100644 index 0000000..12ccb61 --- /dev/null +++ b/src/Archiver/Commands/OptimizeTableCommand.cs @@ -0,0 +1,99 @@ +using Chenfeng.MES.Archiver.Core; +using Chenfeng.MES.Archiver.Data; +using CliFx; +using CliFx.Attributes; +using CliFx.Infrastructure; +using Dapper; +using MySqlConnector; +using System.Data; +using System.Diagnostics; +using System.Text; + +namespace Chenfeng.MES.Archiver.Commands +{ + [Command("optimize")] + public class OptimizeTableCommand : ICommand, IArchiveReader + { + [CommandParameter(0, Description = "数据库连接")] + public string Connection { get; set; } = ""; + + public MySqlConnection? Db { get; private set; } + + public Action Print { get; set; } = (text) => { }; + + [CommandOption("legacy", Description = "旧版MES")] + public bool Legacy { get; set; } = false; + + [CommandOption("timeout", Description = "sql命令超时时间")] + public int Timeout { get; set; } = 0; + + + + public ValueTask ExecuteAsync(IConsole console) + { + var startTime = DateTime.Now; + Print = (text) => console.Output.WriteLine($"{DateTime.Now.ToLongTimeString()} {text}"); + + var connBuilder = new MySqlConnectionStringBuilder(this.Connection); + connBuilder.SslMode = MySqlSslMode.None; + connBuilder.CharacterSet = "utf8"; + + var starMonth = DateTime.Now.AddMonths(-12).ToString("yyyyMM"); + + SqlMapper.Settings.CommandTimeout = 60 * Timeout; + + console.Output.WriteLine($"优化表数据"); + + try + { + Db = new MySqlConnection(connBuilder.ConnectionString); + + Db.Open(); + if (Legacy) + { + TableQueryHelper.MesLegacyTables(this, starMonth); + } + else + { + TableQueryHelper.MesTables(this, starMonth); + } + + + Print("操作成功"); + + } + catch (Exception ex) + { + console.Output.WriteLine(ex.ToString()); + } + finally + { + Db?.Close(); + } + console.Output.WriteLine("执行耗时:" + (DateTime.Now - startTime).TotalMilliseconds + "ms"); + return default; + } + + public void ExecuteData(string table, string where, object param) + { + var sql = $"alter table `{table}` engine = innodb "; + Print("开始优化:" + table); + Db.Execute(sql); + Print("结束优化:" + table); + } + + public void ExecuteChildData(string table, string parentTable, string condition) + { + ExecuteData(table, "", null); + } + + + public void Step(Action action, params string[] tables) + { + foreach (var item in tables) + { + action(item); + } + } + } +}