SycnSpBindService.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 SycnSpBindService
  11. {
  12. public readonly static SycnSpBindService Instance = new SycnSpBindService();
  13. private SycnSpBindService()
  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(-5);
  30. var Binds = spdb.BindRecord.Select(m => new { m.Id, m.CreateTime, m.Status, m.MerNo, m.MerSnNo }).Where(m => m.CreateTime >= start && m.Status == 1).OrderByDescending(m => m.Id).ToList();
  31. foreach (var Bind in Binds)
  32. {
  33. var tran = db.Database.BeginTransaction();
  34. try
  35. {
  36. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
  37. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  38. if (pos != null)
  39. {
  40. pos.BindingState = 1;
  41. pos.BindingTime = Bind.CreateTime;
  42. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  43. if (merFor == null)
  44. {
  45. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  46. {
  47. MerNo = Bind.MerNo,
  48. SnId = pos.Id,
  49. }).Entity;
  50. }
  51. else
  52. {
  53. merFor.SnId = pos.Id;
  54. }
  55. db.SaveChanges();
  56. RedisDbconn.Instance.Set("PosMachinesTwo", pos);
  57. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  58. if (edit != null)
  59. {
  60. edit.Status = 2;
  61. spdb.SaveChanges();
  62. }
  63. string IdBrand = pos.UserId + "_" + pos.BrandId;
  64. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  65. if (MachineData == null)
  66. {
  67. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  68. {
  69. IdBrand = IdBrand,
  70. }).Entity;
  71. db.SaveChanges();
  72. }
  73. MachineData.BindCount += 1;
  74. MachineData.UnBindCount -= 1;
  75. db.SaveChanges();
  76. tran.Commit();
  77. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, MachineData);
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. tran.Rollback();
  83. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  84. }
  85. tran.Dispose();
  86. }
  87. spdb.SaveChanges();
  88. spdb.Dispose();
  89. db.SaveChanges();
  90. db.Dispose();
  91. }
  92. catch (Exception ex)
  93. {
  94. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  95. }
  96. Thread.Sleep(1000);
  97. }
  98. }
  99. }
  100. }