SycnSpBindService.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, m.Field1 }).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. if(Bind.Field1 == "解绑")
  37. {
  38. string PosSn = Bind.MerSnNo;
  39. string MerNo = Bind.MerNo;
  40. PxcModels.MachineForSnNo forSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == PosSn);
  41. PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == MerNo);
  42. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId);
  43. if (pos.ActivationState == 0 && pos.BuyUserId > 0 && pos.BindingState == 0)
  44. {
  45. if (forMerNo != null)
  46. {
  47. db.MachineForMerNo.Remove(forMerNo);
  48. db.SaveChanges();
  49. }
  50. db.MachineUnBind.Add(new PxcModels.MachineUnBind()
  51. {
  52. CreateDate = DateTime.Now,
  53. MerchantId = pos.BindMerchantId,
  54. AuditDate = DateTime.Now,
  55. AuditDesc = "金控推送解绑",
  56. AuditStatus = 1,
  57. SnNo = pos.PosSn,
  58. BrandId = pos.BrandId,
  59. UserId = pos.BuyUserId,
  60. ApplyNo = "U" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
  61. });
  62. pos.BindMerchantId = 0;
  63. pos.BindingState = 0;
  64. pos.BindingTime = DateTime.Parse("1900-01-01");
  65. pos.UserId = pos.BuyUserId;
  66. string IdBrand = pos.BuyUserId + "_" + pos.BrandId;
  67. PxcModels.UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  68. if (userData == null)
  69. {
  70. userData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  71. {
  72. IdBrand = IdBrand,
  73. }).Entity;
  74. db.SaveChanges();
  75. }
  76. userData.BindCount -= 1;
  77. userData.UnBindCount += 1;
  78. db.SaveChanges();
  79. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  80. if (edit != null)
  81. {
  82. edit.Status = 2;
  83. spdb.SaveChanges();
  84. }
  85. }
  86. }
  87. else
  88. {
  89. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
  90. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  91. if (pos != null)
  92. {
  93. pos.BindingState = 1;
  94. pos.BindingTime = Bind.CreateTime;
  95. pos.Status = 0;
  96. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  97. if (merFor == null)
  98. {
  99. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  100. {
  101. MerNo = Bind.MerNo,
  102. SnId = pos.Id,
  103. }).Entity;
  104. }
  105. else
  106. {
  107. merFor.SnId = pos.Id;
  108. }
  109. db.SaveChanges();
  110. RedisDbconn.Instance.Set("PosMachinesTwo", pos);
  111. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  112. if (edit != null)
  113. {
  114. edit.Status = 2;
  115. spdb.SaveChanges();
  116. }
  117. string IdBrand = pos.UserId + "_" + pos.BrandId;
  118. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  119. if (MachineData == null)
  120. {
  121. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  122. {
  123. IdBrand = IdBrand,
  124. }).Entity;
  125. db.SaveChanges();
  126. }
  127. MachineData.BindCount += 1;
  128. MachineData.UnBindCount -= 1;
  129. db.SaveChanges();
  130. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, MachineData);
  131. }
  132. }
  133. tran.Commit();
  134. }
  135. catch (Exception ex)
  136. {
  137. tran.Rollback();
  138. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  139. }
  140. tran.Dispose();
  141. }
  142. spdb.SaveChanges();
  143. spdb.Dispose();
  144. db.SaveChanges();
  145. db.Dispose();
  146. }
  147. catch (Exception ex)
  148. {
  149. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  150. }
  151. Thread.Sleep(1000);
  152. }
  153. }
  154. }
  155. }