162 lines
7.2 KiB
C#
162 lines
7.2 KiB
C#
using GalaSoft.MvvmLight;
|
||
using GalaSoft.MvvmLight.Command;
|
||
using GroupLine.App;
|
||
using GroupLine.Com;
|
||
using NLog;
|
||
using NPOI.SS.Formula.Functions;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
|
||
namespace YC5.App.ViewModel
|
||
{
|
||
public class DbDataClearViewModel : ViewModelBase
|
||
{
|
||
//日志的实例化
|
||
private static Logger _Logger = LogManager.GetCurrentClassLogger();
|
||
|
||
/// <summary>
|
||
/// 设备集合
|
||
/// 供备份和删除使用
|
||
/// </summary>
|
||
private List<string> ListMachine { get; set; }
|
||
|
||
public DbDataClearViewModel()
|
||
{
|
||
ActionCmdDate = DateTime.Now.AddMonths(-6).ToString();
|
||
ListMachine = new List<string>()
|
||
{
|
||
"MagTorque",
|
||
"OilPumpInstall",
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// 搜索条件-开始时间
|
||
/// </summary>
|
||
private string actionCmdDate;
|
||
|
||
public string ActionCmdDate
|
||
{
|
||
get
|
||
{
|
||
return actionCmdDate;
|
||
}
|
||
set
|
||
{
|
||
actionCmdDate = value;
|
||
RaisePropertyChanged(() => ActionCmdDate);
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 执行命令
|
||
/// </summary>
|
||
private RelayCommand actionCmd;
|
||
public RelayCommand ActionCmd
|
||
{
|
||
get
|
||
{
|
||
if (actionCmd == null) return new RelayCommand(() => Cmd());
|
||
return actionCmd;
|
||
}
|
||
set
|
||
{
|
||
actionCmd = value;
|
||
}
|
||
}
|
||
|
||
private void Cmd()
|
||
{
|
||
var Location = ConfigHelper.GetValue("Location");
|
||
|
||
var dataInfo = DateTime.Parse(ActionCmdDate);
|
||
|
||
DialogResult dialogResult = System.Windows.Forms.MessageBox.Show("你确定要删除数据吗?请谨慎确认时间,否则可能影响系统运行!!!将会花费一些时间", "删除数据", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
|
||
if (dialogResult == DialogResult.Cancel)
|
||
{
|
||
return;
|
||
}
|
||
|
||
_Logger.Info($"时间:{DateTime.Now.ToString()} -删除了数据,时间节点为:{dataInfo.ToString()}");
|
||
|
||
// 编写备份数据库的 T-SQL 语句
|
||
string sql = "BACKUP DATABASE YourDatabaseName TO DISK = 'C:\\Backup\\YourDatabaseName.bak'";
|
||
|
||
// 执行 SQL 语句
|
||
//fsql.Ado.ExecuteNonQuery(sql);
|
||
|
||
|
||
Task.Run(() =>
|
||
{
|
||
//BACKUP DATABASE[YourDatabase] TO DISK = N'YourDatabase_backup.bak';
|
||
FSqlContext.FDb.Ado.ExecuteNonQuery($"BACKUP DATABASE GroupLine TO DISK = N'GroupLine_backup_{DateTime.Now.ToString("yyyy-MM-dd")}.bak'");
|
||
|
||
foreach (var item in ListMachine)
|
||
{
|
||
// 字符串类型名称
|
||
string typeName = $"GroupLine.Model.{item}";
|
||
|
||
// 获取类型对象
|
||
Type type = Type.GetType(typeName);
|
||
if (type != null)
|
||
{
|
||
// 创建实例
|
||
object instance = Activator.CreateInstance(type);
|
||
|
||
// 将实例转换为相应类型
|
||
var typedInstance = Convert.ChangeType(instance, type);
|
||
|
||
// 现在你可以使用 typedInstance,它是具有你指定类型的实例
|
||
// 这里可以进行其他操作,如调用方法、设置属性等
|
||
|
||
//FSqlContext.FDb.Delete<type>().Where(a => a.Crea <= dataInfo).ExecuteAffrows();
|
||
}
|
||
|
||
}
|
||
|
||
//FSqlContext.FDb.Delete<B_9SpotWeld1>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<B_9SpotWeld2>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<B_BracketWeld>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<B_Check>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<B_CylinderHeatPr>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<B_CylinderHeatSe>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<B_InsertDev>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<B_LaserPrint>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<B_LugWeld1>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<B_LugWeld2>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<B_NutWeld>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<B_TransferInsDev>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_6SpotWeld1>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_6SpotWeld2>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_DimensCheck>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_GaugeAutoIn>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_InsulaResCheck>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_LowCoverWeld1>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_LowCoverWeld2>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_RobotHand>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_RobotHandIn>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_RobotHandOut>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_SegmentDiffMes>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_SuctionPePre1>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_SuctionPePre2>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_UpCoverWeld1>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<H_UpCoverWeld2>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<Z_MagnetizDev>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<Z_RobotHand>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
//FSqlContext.FDb.Delete<Z_RotorHotPres>().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
|
||
|
||
System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
||
{
|
||
System.Windows.MessageBox.Show("处理完成");
|
||
});
|
||
});
|
||
|
||
}
|
||
}
|
||
}
|