using Bro.Common.Helper;
|
using Bro.M135.DBManager;
|
using System.Diagnostics;
|
|
namespace Bro.M141.Process
|
{
|
public class OldDataClear
|
{
|
public Lazy<Manager_P_PRODUCT> managerLazy = new Lazy<Manager_P_PRODUCT>();
|
public Manager_P_PRODUCT manager =>managerLazy.Value;
|
|
static object _instanceLock = new object();
|
static OldDataClear instance = null;
|
public static OldDataClear Instance
|
{
|
get
|
{
|
if (instance == null)
|
{
|
lock (_instanceLock)
|
{
|
if (instance == null)
|
{
|
instance = new OldDataClear();
|
}
|
}
|
}
|
return instance;
|
}
|
}
|
|
volatile bool _isStarted = false;
|
bool _isAllowDeleteOp = true;
|
int _dayLimit = 0;
|
|
public void SetAllowFlag(bool isAllowed, int dayLimit)
|
{
|
_isAllowDeleteOp = isAllowed;
|
_dayLimit = dayLimit;
|
}
|
|
public void RunClearOldData(int dayLimit)
|
{
|
if (_isStarted)
|
return;
|
|
_dayLimit = dayLimit;
|
_isStarted = true;
|
|
Task.Run(() =>
|
{
|
while (_isStarted)
|
{
|
if (_dayLimit <= 0)
|
continue;
|
|
if (_isAllowDeleteOp)
|
{
|
Stopwatch sw = new Stopwatch();
|
sw.Start();
|
bool isMigrationOK = manager.DeletOldData(DateTime.Now.Date.AddDays(0 - _dayLimit), 100, out int dataNums, out string msg, out bool isNoData);
|
sw.Stop();
|
|
if (!isMigrationOK)
|
{
|
CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"数据删除失败,{msg}");
|
}
|
else
|
{
|
if (dataNums > 0)
|
{
|
CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{dataNums}条产品数据删除完成,耗时{sw.ElapsedMilliseconds}ms");
|
}
|
}
|
|
if (isNoData)
|
{
|
_isAllowDeleteOp = false;
|
}
|
}
|
|
Task.Delay(3000).Wait();
|
}
|
});
|
}
|
}
|
}
|