SycnSpBindService.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. pos.Status = 0;
  43. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  44. if (merFor == null)
  45. {
  46. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  47. {
  48. MerNo = Bind.MerNo,
  49. SnId = pos.Id,
  50. }).Entity;
  51. }
  52. else
  53. {
  54. merFor.SnId = pos.Id;
  55. }
  56. db.SaveChanges();
  57. RedisDbconn.Instance.Set("PosMachinesTwo", pos);
  58. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  59. if (edit != null)
  60. {
  61. edit.Status = 2;
  62. spdb.SaveChanges();
  63. }
  64. string IdBrand = pos.UserId + "_" + pos.BrandId;
  65. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  66. if (MachineData == null)
  67. {
  68. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  69. {
  70. IdBrand = IdBrand,
  71. }).Entity;
  72. db.SaveChanges();
  73. }
  74. MachineData.BindCount += 1;
  75. MachineData.UnBindCount -= 1;
  76. db.SaveChanges();
  77. tran.Commit();
  78. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, MachineData);
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. tran.Rollback();
  84. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  85. }
  86. tran.Dispose();
  87. }
  88. spdb.SaveChanges();
  89. spdb.Dispose();
  90. db.SaveChanges();
  91. db.Dispose();
  92. }
  93. catch (Exception ex)
  94. {
  95. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  96. }
  97. Thread.Sleep(1000);
  98. }
  99. }
  100. }
  101. }