SycnSpChangeBindService.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 SycnSpChangeBindService
  11. {
  12. public readonly static SycnSpChangeBindService Instance = new SycnSpChangeBindService();
  13. private SycnSpChangeBindService()
  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. WebCMSEntities spdb = new WebCMSEntities();
  28. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  29. DateTime start = DateTime.Now.AddDays(-30);
  30. List<ChangeBindRecord> Binds = spdb.ChangeBindRecord.Where(m => m.ReBindTime >= start && m.Status == 1).OrderByDescending(m => m.Id).ToList();
  31. foreach (ChangeBindRecord Bind in Binds)
  32. {
  33. var tran = db.Database.BeginTransaction();
  34. try
  35. {
  36. PosPushDataNewHelper.ChangeBind(Bind);
  37. ChangeBindRecord edit = spdb.ChangeBindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  38. if (edit != null)
  39. {
  40. edit.Status = 2;
  41. spdb.SaveChanges();
  42. }
  43. tran.Commit();
  44. }
  45. catch (Exception ex)
  46. {
  47. tran.Rollback();
  48. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP换绑数据到MAIN异常");
  49. }
  50. tran.Dispose();
  51. }
  52. spdb.Dispose();
  53. db.Dispose();
  54. }
  55. catch (Exception ex)
  56. {
  57. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP换绑数据到MAIN异常");
  58. }
  59. Thread.Sleep(1000);
  60. }
  61. }
  62. }
  63. }