SycnSpUnBindService.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.SpModels;
  5. using Library;
  6. using LitJson;
  7. using System.Threading;
  8. namespace MySystem
  9. {
  10. public class SycnSpUnBindService
  11. {
  12. public readonly static SycnSpUnBindService Instance = new SycnSpUnBindService();
  13. private SycnSpUnBindService()
  14. { }
  15. public void Start()
  16. {
  17. Thread th = new Thread(StartDo);
  18. th.IsBackground = true;
  19. th.Start();
  20. }
  21. public void StartDo()
  22. {
  23. while (true)
  24. {
  25. try
  26. {
  27. if (Utils.PushTimeCheck())
  28. {
  29. List<string> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
  30. WebCMSEntities spdb = new WebCMSEntities();
  31. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  32. DateTime start = DateTime.Now.AddDays(-30);
  33. List<UnBindRecord> Binds = spdb.UnBindRecord.Where(m => m.UnBindTime >= start && SaveDataBindIds.Contains(m.ProductType) && m.Status == 1).OrderByDescending(m => m.Id).ToList();
  34. foreach (UnBindRecord Bind in Binds)
  35. {
  36. var tran = db.Database.BeginTransaction();
  37. try
  38. {
  39. PosPushDataNewHelper.UnBind(Bind);
  40. UnBindRecord edit = spdb.UnBindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  41. if (edit != null)
  42. {
  43. edit.Status = 2;
  44. spdb.SaveChanges();
  45. }
  46. tran.Commit();
  47. }
  48. catch (Exception ex)
  49. {
  50. tran.Rollback();
  51. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP解绑数据到MAIN异常");
  52. }
  53. tran.Dispose();
  54. }
  55. spdb.Dispose();
  56. db.Dispose();
  57. }
  58. }
  59. catch (Exception ex)
  60. {
  61. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP解绑数据到MAIN异常");
  62. }
  63. Thread.Sleep(1000);
  64. }
  65. }
  66. }
  67. }