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();
///
/// 设备集合
/// 供备份和删除使用
///
private List ListMachine { get; set; }
public DbDataClearViewModel()
{
ActionCmdDate = DateTime.Now.AddMonths(-6).ToString();
ListMachine = new List()
{
"MagTorque",
"OilPumpInstall",
};
}
///
/// 搜索条件-开始时间
///
private string actionCmdDate;
public string ActionCmdDate
{
get
{
return actionCmdDate;
}
set
{
actionCmdDate = value;
RaisePropertyChanged(() => ActionCmdDate);
}
}
///
/// 执行命令
///
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().Where(a => a.Crea <= dataInfo).ExecuteAffrows();
}
}
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
//FSqlContext.FDb.Delete().Where(a => a.EndTime <= dataInfo).ExecuteAffrows();
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
System.Windows.MessageBox.Show("处理完成");
});
});
}
}
}