SycnSpBindService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.ProductType, 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 == "解绑" && Bind.ProductType == "1")
  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. PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  88. if (forMerNo != null)
  89. {
  90. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forMerNo.SnId) ?? new PxcModels.PosMachinesTwo();
  91. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  92. if(merchant != null)
  93. {
  94. db.PosMerchantInfo.Remove(merchant);
  95. }
  96. db.MachineForMerNo.Remove(forMerNo);
  97. db.SaveChanges();
  98. }
  99. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  100. if (edit != null)
  101. {
  102. edit.Status = 2;
  103. spdb.SaveChanges();
  104. }
  105. }
  106. else
  107. {
  108. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
  109. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  110. if (pos != null)
  111. {
  112. pos.BindingState = 1;
  113. pos.BindingTime = Bind.CreateTime;
  114. pos.Status = 0;
  115. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  116. if (merFor == null)
  117. {
  118. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  119. {
  120. MerNo = Bind.MerNo,
  121. SnId = pos.Id,
  122. }).Entity;
  123. }
  124. else
  125. {
  126. merFor.SnId = pos.Id;
  127. }
  128. db.SaveChanges();
  129. RedisDbconn.Instance.Set("PosMachinesTwo", pos);
  130. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  131. if (edit != null)
  132. {
  133. edit.Status = 2;
  134. spdb.SaveChanges();
  135. }
  136. string IdBrand = pos.UserId + "_" + pos.BrandId;
  137. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  138. if (MachineData == null)
  139. {
  140. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  141. {
  142. IdBrand = IdBrand,
  143. }).Entity;
  144. db.SaveChanges();
  145. }
  146. MachineData.BindCount += 1;
  147. MachineData.UnBindCount -= 1;
  148. db.SaveChanges();
  149. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, MachineData);
  150. }
  151. }
  152. tran.Commit();
  153. }
  154. catch (Exception ex)
  155. {
  156. tran.Rollback();
  157. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  158. }
  159. tran.Dispose();
  160. }
  161. spdb.SaveChanges();
  162. spdb.Dispose();
  163. db.SaveChanges();
  164. db.Dispose();
  165. }
  166. catch (Exception ex)
  167. {
  168. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  169. }
  170. Thread.Sleep(1000);
  171. }
  172. }
  173. }
  174. }