SycnSpChangeBindService.cs 2.5 KB

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