DataClearHandler.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using DBHelper;
  2. using Microsoft.EntityFrameworkCore;
  3. using System;
  4. using System.ComponentModel;
  5. using WCS.Core;
  6. using WCS.Entity;
  7. namespace WCS.Service
  8. {
  9. [Description("数据清理")]
  10. internal class DataClearHandler : LogicHandler
  11. {
  12. public override void Start()
  13. {
  14. }
  15. private DateTime last = DateTime.MinValue;
  16. public override void Update(double milliseconds)
  17. {
  18. if ((DateTime.Now - last).TotalMilliseconds < 1000 * 60 * 60 * 24)
  19. return;
  20. last = DateTime.Now;
  21. DB.Do(db =>
  22. {
  23. db.Default.Database.ExecuteSqlRaw("delete WCS_EXCEPTION where datediff(dd,UPDATETIME,getdate())>7");
  24. var ps = db.Default.GetType().GetProperties();
  25. foreach (var p in ps)
  26. {
  27. if (!p.PropertyType.IsGenericType)
  28. continue;
  29. var tType = p.PropertyType.GenericTypeArguments[0];
  30. if (!tType.IsSubclassOf(typeof(WCS_PROTOCOLDATA)))
  31. continue;
  32. var sSql = $"Delete {tType.Name} where datediff(dd,Frame,getdate())>7";
  33. db.Default.Database.ExecuteSqlRaw(sSql);
  34. DBHelper.DbLog.DB_CLEAN(sSql);
  35. }
  36. });
  37. }
  38. }
  39. }