DataClearHandler.cs 1.4 KB

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