SycnSpBindService.cs 4.4 KB

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