Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
258b1a7556 | ||
![]() |
d6ee4c8588 | ||
![]() |
62191e3932 | ||
![]() |
330793aa25 | ||
![]() |
cd27c7e03a | ||
![]() |
a606282499 | ||
824ba00e0e | |||
d21e2d6660 | |||
190ee1cc87 | |||
![]() |
c0c17abea1 | ||
d2697baf37 | |||
![]() |
de141de3bd |
6
.gitignore
vendored
6
.gitignore
vendored
@@ -360,4 +360,8 @@ MigrationBackup/
|
||||
.ionide/
|
||||
|
||||
# Fody - auto-generated XML schema
|
||||
FodyWeavers.xsd
|
||||
FodyWeavers.xsd
|
||||
|
||||
.vscode
|
||||
|
||||
/src/Archiver/Properties/launchSettings.json
|
||||
|
@@ -1,24 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CliFx" Version="2.1.0" />
|
||||
<PackageReference Include="Dapper" Version="2.0.123" />
|
||||
<PackageReference Include="MySqlConnector" Version="2.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<Content Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@@ -1,213 +0,0 @@
|
||||
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;
|
||||
|
||||
namespace Chenfeng.MES.Archiver.Commands
|
||||
{
|
||||
[Command()]
|
||||
public class MysqlCommand : ICommand
|
||||
{
|
||||
[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;
|
||||
|
||||
[CommandOption("rm-backup", Description = "删除备份表")]
|
||||
public bool RemoveBackup { get; set; }
|
||||
|
||||
[CommandOption("timeout", Description = "sql命令超时时间")]
|
||||
public int Timeout { get; set; } = 3;
|
||||
|
||||
|
||||
|
||||
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;
|
||||
|
||||
if (RemoveBackup)
|
||||
{
|
||||
console.Output.WriteLine("删除备份表");
|
||||
}
|
||||
else if (Restore)
|
||||
{
|
||||
console.Output.WriteLine("还原备份表");
|
||||
}
|
||||
else
|
||||
{
|
||||
console.Output.WriteLine($"拷贝"+( Legacy ? "旧" :"新")+"版表数据"+(Replace?"并替换":""));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Db = new MySqlConnection(connBuilder.ConnectionString);
|
||||
|
||||
Db.Open();
|
||||
if (Legacy)
|
||||
{
|
||||
CopyMesLegacyTables(starMonth);
|
||||
}
|
||||
else
|
||||
{
|
||||
CopyMesTables(starMonth);
|
||||
}
|
||||
|
||||
|
||||
Print("操作成功");
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
console.Output.WriteLine(ex.ToString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
Db?.Close();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
private void CopyMesTables(string starMonth)
|
||||
{
|
||||
CopyStep((table, newTable) =>
|
||||
{
|
||||
CopyData(table, newTable, "LEFT(orderno,6) >= @starMonth", new { starMonth });
|
||||
}, "order", "order_box_block", "order_data_block", "order_data_goods", "order_data_parts", "order_item", "order_module_extra", "order_module_item", "order_package");
|
||||
|
||||
|
||||
CopyStep((tProcess, newProcess) =>
|
||||
{
|
||||
CopyData(tProcess, newProcess, "LEFT(orderno,6) >= @starMonth", new { starMonth });
|
||||
CopyStep((table, newTable) =>
|
||||
{
|
||||
CopyChildData(table, newTable, newProcess, "parent.ID = child.OrderProcessID");
|
||||
}, "order_process_step_item", "order_process_step");
|
||||
}, "order_process");
|
||||
|
||||
|
||||
|
||||
CopyStep((table, newTable) =>
|
||||
{
|
||||
CopyData(table, newTable, "LEFT(DATE_FORMAT(CreateTime,'%Y%m'),6) >= @starMonth", new { starMonth });
|
||||
}, "simple_plan_order");
|
||||
|
||||
|
||||
CopyStep((parent, newParent) =>
|
||||
{
|
||||
CopyData(parent, newParent, "LEFT(DATE_FORMAT(CreateTime,'%Y%m'),6) >= @starMonth", new { starMonth });
|
||||
CopyStep((child, newChild) =>
|
||||
{
|
||||
CopyChildData(child, newChild, newParent, "parent.ID = child.ID");
|
||||
}, "order_block_plan_result");
|
||||
}, "order_block_plan");
|
||||
|
||||
//CopyStep((table, newTable) =>
|
||||
//{
|
||||
// CopyData(table, newTable, "concat('20',left(id,4)) >= @starMonth ", new { starMonth });
|
||||
//}, "order_scrap_board");
|
||||
}
|
||||
|
||||
private void CopyMesLegacyTables(string starMonth)
|
||||
{
|
||||
CopyStep((order, newOrder) =>
|
||||
{
|
||||
CopyData(order, newOrder, "LEFT(DATE_FORMAT(SaleDate,'%Y%m'),6) >= @starMonth", new { starMonth });
|
||||
|
||||
CopyStep((child, newChild) =>
|
||||
{
|
||||
CopyChildData(child, newChild, newOrder, "parent.orderno=child.orderno");
|
||||
}, "SaleOrderItem", "saleblock", "saleobject", "saleorderobject", "saleorderoffer", "saleorderpackage", "saleblockobjids", "saleobjectobjids", "saleorder_block_cadmodelinfo", "saleorderblockbaseposition",
|
||||
"saleordergoodsinfo", "saleorder_block_point", "saleorder_block_pointinfo", "orderprocess", "orderprocessstep", "orderprocessstepitem");
|
||||
|
||||
}, "saleorder");
|
||||
|
||||
|
||||
CopyStep((plan, newPlan) =>
|
||||
{
|
||||
CopyData(plan, newPlan, "LEFT(DATE_FORMAT(createtime,'%Y%m'),6) >= @starMonth", new { starMonth });
|
||||
|
||||
CopyStep((child, newChild) =>
|
||||
{
|
||||
CopyChildData(child, newChild, newPlan, "parent.id=child.id");
|
||||
}, "orderblockprocessplanplaceresult");
|
||||
}, "orderblockprocessplan");
|
||||
}
|
||||
|
||||
private void CopyData(string table, string newTable, string where, object param)
|
||||
{
|
||||
Db.Execute($"INSERT INTO `{newTable}` SELECT * FROM `{table}` WHERE {where}", param);
|
||||
}
|
||||
private void CopyChildData(string table, string newTable, string parentTable, string condition)
|
||||
{
|
||||
Db.Execute($"INSERT INTO `{newTable}` SELECT child.* FROM `{parentTable}` parent join `{table}` child on {condition} ");
|
||||
}
|
||||
|
||||
private void CopyStep(Action<string, string> step, params string[] tables)
|
||||
{
|
||||
var list = tables.Select(table => new
|
||||
{
|
||||
Table = table,
|
||||
NewTable = table + "_" + this.TableSuffix
|
||||
});
|
||||
|
||||
foreach (var item in list)
|
||||
{
|
||||
if (RemoveBackup)
|
||||
{
|
||||
Db.Execute($"DROP TABLE IF EXISTS `{item.Table}_bak`");
|
||||
}
|
||||
else if (Restore)
|
||||
{
|
||||
Db.Execute($"DROP TABLE IF EXISTS `{item.Table}`");
|
||||
Db.Execute($"RENAME TABLE `{item.Table}_bak` TO `{item.Table}`");
|
||||
}
|
||||
else
|
||||
{
|
||||
Db.Execute($"DROP TABLE IF EXISTS `{item.NewTable}`");
|
||||
Db.Execute($"CREATE TABLE `{item.NewTable}` LIKE `{item.Table}`"); // IF NOT EXISTS
|
||||
|
||||
Print(item.NewTable + " 创建完成");
|
||||
step(item.Table, item.NewTable);
|
||||
Print(item.NewTable + " 拷贝完成");
|
||||
|
||||
if (Replace)
|
||||
{
|
||||
Db.Execute($"RENAME TABLE `{item.Table}` TO `{item.Table}_bak`");
|
||||
Db.Execute($"RENAME TABLE `{item.NewTable}` TO `{item.Table}`");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
|
||||
using CliFx;
|
||||
|
||||
//Console.WriteLine("Hello, World!");
|
||||
|
||||
await new CliApplicationBuilder()
|
||||
.AddCommandsFromThisAssembly()
|
||||
.Build()
|
||||
.RunAsync(args);
|
@@ -1,13 +0,0 @@
|
||||
{
|
||||
"profiles": {
|
||||
"WSL": {
|
||||
"commandName": "WSL2",
|
||||
"environmentVariables": {},
|
||||
"distributionName": ""
|
||||
},
|
||||
"copy-table": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "Server=192.168.1.127;UserId=root;Password=ruixinjie!@#123;Database=cferp_test;Port=3306;"
|
||||
}
|
||||
}
|
||||
}
|
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.0.32014.148
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chenfeng.MES.Archiver", "Chenfeng.MES.Archiver\Chenfeng.MES.Archiver.csproj", "{D42E639F-0E4D-4CE3-9793-230D6B07C3FE}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArchiverLite", "src\Archiver\ArchiverLite.csproj", "{D42E639F-0E4D-4CE3-9793-230D6B07C3FE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
25
src/Archiver/Archiver.sln
Normal file
25
src/Archiver/Archiver.sln
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.8.34316.72
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArchiverLite", "ArchiverLite.csproj", "{0013C7B6-2EE1-4228-9388-9171E929EDE2}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{0013C7B6-2EE1-4228-9388-9171E929EDE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0013C7B6-2EE1-4228-9388-9171E929EDE2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0013C7B6-2EE1-4228-9388-9171E929EDE2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0013C7B6-2EE1-4228-9388-9171E929EDE2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {052E86DD-349F-4BE2-A6CC-013C5E709894}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
20
src/Archiver/ArchiverLite.csproj
Normal file
20
src/Archiver/ArchiverLite.csproj
Normal file
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<TrimMode>partial</TrimMode>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CliFx" Version="2.3.5" />
|
||||
<PackageReference Include="Dapper" Version="2.1.28" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="MySqlConnector" Version="2.3.5" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
44
src/Archiver/Commands/DeleteTableCommand.cs
Normal file
44
src/Archiver/Commands/DeleteTableCommand.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using CliFx;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Infrastructure;
|
||||
using Dapper;
|
||||
using ArchiverLite.Services;
|
||||
|
||||
namespace ArchiverLite.Commands
|
||||
{
|
||||
[Command("delete",Description = "清理旧版MES历史数据")]
|
||||
public class DeleteDataCommand : ICommand
|
||||
{
|
||||
|
||||
[CommandParameter(0, Description = "数据库连接")]
|
||||
public string Connection { get; set; } = "";
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
var print = (string text) => console.Output.WriteLine($"{DateTime.Now.ToLongTimeString()} {text}");
|
||||
if (this.Connection.Length > 0)
|
||||
{
|
||||
SqlMapper.Settings.CommandTimeout = 0;
|
||||
}
|
||||
|
||||
var starMonth = DateTime.Now.AddYears(-1).ToString("yyyy") + "01";
|
||||
|
||||
|
||||
print($"开始清理旧版MES {starMonth}之前数据");
|
||||
|
||||
Task.WaitAll(Task.Run(() =>
|
||||
{
|
||||
var service = new MesLegacyService(this.Connection);
|
||||
service.ClearOrder(starMonth);
|
||||
}), Task.Run(() =>
|
||||
{
|
||||
var service = new MesLegacyService(this.Connection);
|
||||
service.ClearOrderBlockPlan(starMonth);
|
||||
}));
|
||||
|
||||
print("操作完成");
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
27
src/Archiver/Program.cs
Normal file
27
src/Archiver/Program.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
|
||||
using ArchiverLite.Commands;
|
||||
using CliFx;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
//Console.WriteLine("Hello, World!");
|
||||
|
||||
await new CliApplicationBuilder()
|
||||
.SetVersion("0.1.0")
|
||||
.SetExecutableName("ArchiverLite")
|
||||
.AddCommandsFromThisAssembly()
|
||||
//.UseTypeActivator(commandTypes =>
|
||||
//{
|
||||
// var services = new ServiceCollection();
|
||||
|
||||
// // Register services
|
||||
|
||||
// // Register commands
|
||||
// foreach (var commandType in commandTypes)
|
||||
// services.AddTransient(commandType);
|
||||
|
||||
// return services.BuildServiceProvider();
|
||||
//})
|
||||
.Build()
|
||||
.RunAsync(args);
|
70
src/Archiver/Services/MesLegacyService.cs
Normal file
70
src/Archiver/Services/MesLegacyService.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Dapper;
|
||||
using MySqlConnector;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ArchiverLite.Services
|
||||
{
|
||||
public class MesLegacyService
|
||||
{
|
||||
private readonly MySqlConnection _db;
|
||||
|
||||
public MesLegacyService(string connectionString)
|
||||
{
|
||||
var connBuilder = new MySqlConnectionStringBuilder(connectionString);
|
||||
connBuilder.SslMode = MySqlSslMode.None;
|
||||
connBuilder.CharacterSet = "utf8";
|
||||
//connBuilder.ConnectionTimeout = 5 * 60;
|
||||
_db = new MySqlConnection(connectionString);
|
||||
}
|
||||
|
||||
public void ClearOrder(string starMonth)
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearOrderBlockPlan(string starMonth)
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private MySqlTransaction GetTransaction()
|
||||
{
|
||||
if (_db.State != System.Data.ConnectionState.Open) _db.Open();
|
||||
return _db.BeginTransaction();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user