SycnSpBindZkbService.cs 6.6 KB

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