新增命令

This commit is contained in:
xf 2022-02-07 01:24:47 +08:00
parent 330793aa25
commit 62191e3932
4 changed files with 421 additions and 1 deletions

View File

@ -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<string> 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<T> GetIdList<T>(string table, string field, string condition, object param)
{
return Db.Query<T>($"select {field} from `{table}` where {condition}", param);
}
public void MesTables(string starMonth)
{
Print("复制订单");
foreach (var orderno in GetIdList<long>("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<long>("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<long>("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<long>("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<string>("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<string>("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();
}
}
}
}
}

View File

@ -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;
}

View File

@ -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<string> 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<T> GetIdList<T>(string table, string field, string condition, object param)
{
return Db.Query<T>($"select {field} from `{table}` where {condition}", param);
}
public void MesTables(string starMonth, string op = "<")
{
Print("清理订单");
foreach (var orderno in GetIdList<long>("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<long>("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<long>("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<long>("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<string>("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<string>("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();
}
}
}
}
}

View File

@ -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<string> 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<string> action, params string[] tables)
{
foreach (var item in tables)
{
action(item);
}
}
}
}