SycnSpBindService.cs 9.7 KB

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