SycnSpBindZkbService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 SycnSpBindZkbService
  11. {
  12. public readonly static SycnSpBindZkbService Instance = new SycnSpBindZkbService();
  13. private SycnSpBindZkbService()
  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. if (Utils.PushTimeCheck())
  28. {
  29. List<string> BrandIds = new List<string>();
  30. BrandIds.Add("17");
  31. List<string> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
  32. WebCMSEntities spdb = new WebCMSEntities();
  33. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  34. DateTime start = DateTime.Now.AddDays(-30);
  35. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/BindRecordId.txt")));
  36. var Binds = spdb.BindRecord.Where(m => m.Id >= StartId && m.CreateTime >= start && BrandIds.Contains(m.ProductType) && SaveDataBindIds.Contains(m.ProductType) && m.Status == 1).OrderByDescending(m => m.Id).Take(20).ToList();
  37. foreach (var Bind in Binds)
  38. {
  39. var tran = db.Database.BeginTransaction();
  40. try
  41. {
  42. PxcModels.UserForMakerCode userForMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == Bind.Field3) ?? new PxcModels.UserForMakerCode();
  43. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == userForMakerCode.UserId) ?? new PxcModels.Users();
  44. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == Bind.MerSnNo);
  45. if (pos == null)
  46. {
  47. pos = db.PosMachinesTwo.Add(new PxcModels.PosMachinesTwo()
  48. {
  49. CreateDate = DateTime.Now,
  50. PosSn = Bind.MerSnNo, //SN编号
  51. BrandId = 17, //产品类型
  52. BatchNo = Bind.Field1,
  53. DeviceName = "吱客宝",
  54. BuyUserId = user.Id,
  55. UserId = user.Id,
  56. BindingState = 1,
  57. BindingTime = Bind.CreateTime,
  58. ActivationState = 1,
  59. ActivationTime = Bind.CreateTime,
  60. }).Entity;
  61. db.SaveChanges();
  62. }
  63. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqSnNo == Bind.MerSnNo);
  64. if (merchant == null)
  65. {
  66. merchant = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  67. {
  68. CreateDate = DateTime.Now,
  69. MerStandardDate = DateTime.Now,
  70. KqSnNo = Bind.MerSnNo, //SN编号
  71. KqMerNo = Bind.MerNo, //商户号
  72. MerchantNo = Bind.MerNo,
  73. MerchantName = Bind.MerName,
  74. MerRealName = Bind.MerName,
  75. MerStatus = 1,
  76. ActiveStatus = 1,
  77. BrandId = 17, //产品类型
  78. UserId = user.Id,
  79. MerIdcardNo = Bind.SeoKeyword,
  80. MerchantMobile = Bind.MerNewSnNo,
  81. }).Entity;
  82. db.SaveChanges();
  83. }
  84. pos.BindMerchantId = merchant.Id;
  85. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo);
  86. if (posFor == null)
  87. {
  88. posFor = db.MachineForSnNo.Add(new PxcModels.MachineForSnNo()
  89. {
  90. SnNo = Bind.MerSnNo,
  91. SnId = pos.Id,
  92. }).Entity;
  93. }
  94. else
  95. {
  96. posFor.SnId = pos.Id;
  97. }
  98. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  99. if (merFor == null)
  100. {
  101. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  102. {
  103. MerNo = Bind.MerNo,
  104. SnId = pos.Id,
  105. }).Entity;
  106. }
  107. else
  108. {
  109. merFor.SnId = pos.Id;
  110. }
  111. db.SaveChanges();
  112. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  113. if (edit != null)
  114. {
  115. edit.Status = 2;
  116. spdb.SaveChanges();
  117. }
  118. tran.Commit();
  119. }
  120. catch (Exception ex)
  121. {
  122. tran.Rollback();
  123. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP吱客宝绑定数据到MAIN异常");
  124. }
  125. tran.Dispose();
  126. }
  127. spdb.SaveChanges();
  128. spdb.Dispose();
  129. db.SaveChanges();
  130. db.Dispose();
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  136. }
  137. Thread.Sleep(1000);
  138. }
  139. }
  140. }
  141. }