2024-05-14 16:30:56 +08:00
|
|
|
|
using WcsMain.ExtendMethod;
|
|
|
|
|
|
using WcsMain.DataBase.Dao;
|
|
|
|
|
|
using WcsMain.WcsAttribute.AutoFacAttribute;
|
2024-06-21 14:37:27 +08:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
using WcsMain.WcsAttribute.Clear;
|
|
|
|
|
|
using WcsMain.Common;
|
2024-05-14 16:30:56 +08:00
|
|
|
|
|
|
|
|
|
|
namespace WcsMain.Business.CommonAction;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清理数据类
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Component]
|
2024-06-11 12:33:14 +08:00
|
|
|
|
public class ClearData(AppWmsTaskDao wmsTaskDao, AppWcsTaskDao wcsTaskDao, AppApiRequestDao apiRequestDao,
|
2024-06-20 14:05:21 +08:00
|
|
|
|
AppApiAcceptDao apiAcceptDao, AppElTagTaskDao elTagTaskDao, AppConveyTaskDao conveyTaskDao, AppScanRecordDao scanRecordDao)
|
2024-05-14 16:30:56 +08:00
|
|
|
|
{
|
2024-06-21 14:37:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 扫描清理数据库
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void ClearDataBase()
|
|
|
|
|
|
{
|
|
|
|
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
|
|
|
|
var types = assembly.GetTypes();
|
|
|
|
|
|
if (types.Length == 0) return;
|
|
|
|
|
|
foreach (Type type in types)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sqlSuagarTableAttribute = type.GetCustomAttribute<SugarTable>();
|
|
|
|
|
|
if (sqlSuagarTableAttribute == null) continue;
|
|
|
|
|
|
var clearTableAttribute = type.GetCustomAttribute<ClearTableAttribute>();
|
|
|
|
|
|
if(clearTableAttribute == null) continue;
|
|
|
|
|
|
var tableName = sqlSuagarTableAttribute.TableName; // 表名
|
|
|
|
|
|
var timeColumn = clearTableAttribute.TimeColumn; // 时间字段
|
|
|
|
|
|
var saveDays = clearTableAttribute.SaveDays; // 需要保存的天数
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
int deleteCount = CommonTool.DbServe.Deleteable<object>().AS($"{tableName}").Where($"{timeColumn} < '{DateTime.Now.AddDays(-saveDays)}'").ExecuteCommand(); // 删除超过一定天数的记录
|
|
|
|
|
|
ConsoleLog.Info(deleteCount > 0, $"清除表:{tableName},保留:{saveDays} 天,清除记录数目:{deleteCount}");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch(Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
ConsoleLog.Exception($"【异常】尝试清除指定表的记录失败,表名:{tableName},异常信息:{e.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-14 16:30:56 +08:00
|
|
|
|
|
|
|
|
|
|
/* 定时清除 WMS 任务表 */
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清理超过一定时间的 WMS 任务表数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="days"></param>
|
2024-06-19 16:51:35 +08:00
|
|
|
|
public int ClearWmsTaskData(int days) => wmsTaskDao.ClearData(days);
|
2024-05-14 16:30:56 +08:00
|
|
|
|
|
|
|
|
|
|
/* 定时清除 WCS 任务备份表 */
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清理超过一定天数的 WCS 任务备份表数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="days"></param>
|
2024-06-19 16:51:35 +08:00
|
|
|
|
public int ClearWcsTaskData(int days) => wcsTaskDao.ClearData(days);
|
2024-06-11 12:33:14 +08:00
|
|
|
|
|
|
|
|
|
|
/* 定时清除接口请求表 */
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清理接口发送信息表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="days"></param>
|
2024-06-19 16:51:35 +08:00
|
|
|
|
public int ClearApiRequestData(int days) => apiRequestDao.ClearData(days);
|
2024-06-11 12:33:14 +08:00
|
|
|
|
|
|
|
|
|
|
/* 定时清除接口接收表 */
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清理接口接收信息表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="days"></param>
|
2024-06-19 16:51:35 +08:00
|
|
|
|
public int ClearApiAcceptData(int days) => apiAcceptDao.ClearData(days);
|
2024-06-11 12:33:14 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清理多少天之前的电子标签任务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="days"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-06-19 16:51:35 +08:00
|
|
|
|
public int ClearElTagTaskData(int days) => elTagTaskDao.ClearData(days);
|
2024-06-11 12:33:14 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清理输送线任务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="days"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-06-19 16:51:35 +08:00
|
|
|
|
public int ClearConveyTaskData(int days) => conveyTaskDao.ClearData(days);
|
2024-06-11 12:33:14 +08:00
|
|
|
|
|
2024-06-20 14:05:21 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清理多少天之前的数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="days"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public int ClearScanRecordData(int days) => scanRecordDao.ClrearWithDays(days);
|
|
|
|
|
|
|
2024-05-14 16:30:56 +08:00
|
|
|
|
|
|
|
|
|
|
/* 定时清除 日志文件 */
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清理日志文件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="days"></param>
|
|
|
|
|
|
public int ClearLogFile(int days)
|
|
|
|
|
|
{
|
|
|
|
|
|
int clearCount = 0;
|
|
|
|
|
|
string LogAddress = AppDomain.CurrentDomain.BaseDirectory + "\\Log";
|
|
|
|
|
|
DirectoryInfo di = new(LogAddress);
|
|
|
|
|
|
if (!di.Exists) return clearCount;
|
|
|
|
|
|
var directories = di.GetDirectories();
|
|
|
|
|
|
if (directories.Length <= 0) return clearCount;
|
|
|
|
|
|
foreach (DirectoryInfo directory in directories)
|
|
|
|
|
|
{
|
|
|
|
|
|
var files = directory.GetFiles();
|
|
|
|
|
|
if (files.Length <= 0) continue;
|
|
|
|
|
|
foreach (FileInfo fileInfo in files)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string fileName = fileInfo.Name;
|
|
|
|
|
|
DateTime? time = fileName.ToDateTime();
|
|
|
|
|
|
if (time == default) continue;
|
|
|
|
|
|
TimeSpan? ts = (DateTime.Now - time!);
|
|
|
|
|
|
if (ts?.Days > days) // --------------------------------- 日志保存的天数
|
|
|
|
|
|
{
|
|
|
|
|
|
fileInfo.Delete();
|
|
|
|
|
|
clearCount++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch { } // 无论任何情况都放弃执行
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return clearCount;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|