SycnSpBindZkbService.cs 6.7 KB

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