MES-Toolkit/src/Archiver/Commands/CopyTableCommand.cs

155 lines
4.8 KiB
C#
Raw Normal View History

2022-01-08 15:35:27 +08:00
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using CliFx;
using CliFx.Infrastructure;
using CliFx.Attributes;
using System.Data;
using System.Data.Common;
using Chenfeng.MES.Archiver.Core;
using Chenfeng.MES.Archiver.Data;
2022-01-08 15:35:27 +08:00
namespace Chenfeng.MES.Archiver.Commands
{
[Command("copy")]
public class CopyTableCommand : ICommand,IArchiveReader
2022-01-08 15:35:27 +08:00
{
[CommandParameter(0, Description = "数据库连接")]
public string Connection { get; set; } = "";
public string TableSuffix { get; set; } = "new";
public DbConnection? Db { get; private set; }
public Action<string> Print { get; set; } = (text) => { };
[CommandOption("legacy", Description = "旧版MES")]
public bool Legacy { get; set; } = false;
[CommandOption("replace", Description = "替换表")]
public bool Replace { get; set; } = false;
[CommandOption("restore", Description = "回复表")]
public bool Restore { get; set; } = false;
2022-01-08 16:14:40 +08:00
[CommandOption("rm-backup", Description = "删除备份表")]
public bool RemoveBackup { get; set; }
2022-01-08 15:35:27 +08:00
[CommandOption("timeout", Description = "sql命令超时时间")]
public int Timeout { get; set; } = 3;
2022-01-08 16:14:40 +08:00
2022-01-08 15:35:27 +08:00
public ValueTask ExecuteAsync(IConsole console)
{
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;
2022-01-08 16:14:40 +08:00
if (RemoveBackup)
{
console.Output.WriteLine("删除备份表");
}
else if (Restore)
2022-01-08 15:35:27 +08:00
{
console.Output.WriteLine("还原备份表");
}
else
{
console.Output.WriteLine($"拷贝"+( Legacy ? "旧" :"新")+"版表数据"+(Replace?"并替换":""));
}
try
{
Db = new MySqlConnection(connBuilder.ConnectionString);
Db.Open();
if (Legacy)
{
TableQueryHelper.MesLegacyTables(this,starMonth);
2022-01-08 15:35:27 +08:00
}
else
{
TableQueryHelper.MesTables(this,starMonth);
2022-01-08 15:35:27 +08:00
}
Print("操作成功");
}
catch (Exception ex)
{
console.Output.WriteLine(ex.ToString());
}
finally
{
Db?.Close();
}
return default;
}
public void ExecuteData(string table, string where, object param)
2022-01-08 15:35:27 +08:00
{
Db.Execute($"INSERT INTO `{TableDic[table]}` SELECT * FROM `{table}` WHERE {where}", param);
2022-01-08 15:35:27 +08:00
}
public void ExecuteChildData(string table, string parentTable, string condition)
2022-01-08 15:35:27 +08:00
{
Db.Execute($"INSERT INTO `{TableDic[table]}` SELECT child.* FROM `{TableDic[parentTable]}` parent join `{table}` child on {condition} ");
2022-01-08 15:35:27 +08:00
}
public Dictionary<string,string> TableDic { get; set; } = new Dictionary<string,string>();
public void Step(Action<string> action, params string[] tables)
2022-01-08 15:35:27 +08:00
{
var list = tables.Select(table => new
{
Table = table,
NewTable = table + "_" + this.TableSuffix
});
foreach (var item in list)
{
2022-01-08 16:14:40 +08:00
if (RemoveBackup)
{
Db.Execute($"DROP TABLE IF EXISTS `{item.Table}_bak`");
}
else if (Restore)
2022-01-08 15:35:27 +08:00
{
Db.Execute($"DROP TABLE IF EXISTS `{item.Table}`");
Db.Execute($"RENAME TABLE `{item.Table}_bak` TO `{item.Table}`");
}
else
{
TableDic[item.Table] = item.NewTable;
2022-01-08 15:35:27 +08:00
Db.Execute($"DROP TABLE IF EXISTS `{item.NewTable}`");
Db.Execute($"CREATE TABLE `{item.NewTable}` LIKE `{item.Table}`"); // IF NOT EXISTS
Print(item.NewTable + " 创建完成");
action(item.Table);
2022-01-08 15:35:27 +08:00
Print(item.NewTable + " 拷贝完成");
if (Replace)
{
Db.Execute($"RENAME TABLE `{item.Table}` TO `{item.Table}_bak`");
Db.Execute($"RENAME TABLE `{item.NewTable}` TO `{item.Table}`");
}
}
}
}
}
}