SycnSpMerchantService.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 SycnSpMerchantService
  11. {
  12. public readonly static SycnSpMerchantService Instance = new SycnSpMerchantService();
  13. private SycnSpMerchantService()
  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 Mers = spdb.Merchants.Where(m => m.CreateTime >= start && m.Status == 1).OrderByDescending(m => m.Id).ToList();
  31. foreach (var Mer in Mers)
  32. {
  33. var tran = db.Database.BeginTransaction();
  34. try
  35. {
  36. PxcModels.MachineForMerNo posFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.MerNo) ?? new PxcModels.MachineForMerNo();
  37. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId) ?? new PxcModels.PosMachinesTwo();
  38. if (pos.BindingState == 1)
  39. {
  40. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new PxcModels.Users();
  41. int TopUserId = 0;
  42. if (!string.IsNullOrEmpty(user.ParentNav))
  43. {
  44. TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  45. }
  46. int BrandId = int.Parse(Mer.ProductType);
  47. if (BrandId == 1 && Mer.MerNo.StartsWith("M900"))
  48. {
  49. BrandId = 3;
  50. }
  51. if (BrandId == 4 && Mer.Field1 == "200")
  52. {
  53. BrandId = 5;
  54. }
  55. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mer.MerNo);
  56. if (add == null)
  57. {
  58. int IsFirst = 1;
  59. if(!string.IsNullOrEmpty(Mer.MerIdcardNo))
  60. {
  61. string startNo = Mer.MerIdcardNo.Substring(0, 6);
  62. string endNo = Mer.MerIdcardNo.Substring(Mer.MerIdcardNo.Length - 4, 4);
  63. string Name = Mer.MerName;
  64. if (Mer.ProductType == "2")
  65. {
  66. if (Name.Contains("-"))
  67. {
  68. Name = Name.Split('-')[1];
  69. }
  70. else if (Name.Contains("_"))
  71. {
  72. Name = Name.Split('_')[1];
  73. }
  74. }
  75. else if (Mer.ProductType == "4" || Mer.ProductType == "8" || Mer.ProductType == "9")
  76. {
  77. Name = Mer.SeoTitle;
  78. }
  79. Name = Name.Replace("个体户", "");
  80. Name = Name.Replace("个体商户", "");
  81. Name = Name.Replace("企业户", "");
  82. Name = Name.Replace("企业商户", "");
  83. bool check = db.PosMerchantInfo.Any(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerchantName.Contains(Name));
  84. if(check)
  85. {
  86. IsFirst = 0;
  87. }
  88. }
  89. pos.IsFirst = IsFirst;
  90. add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  91. {
  92. CreateDate = Mer.CreateTime,
  93. KqMerNo = Mer.MerNo,
  94. MerchantNo = Mer.MerNo,
  95. }).Entity;
  96. db.SaveChanges();
  97. }
  98. else
  99. {
  100. string SnNo = add.KqSnNo;
  101. PxcModels.MachineForMerNo oldPosFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == SnNo) ?? new PxcModels.MachineForMerNo();
  102. PxcModels.PosMachinesTwo oldPos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == oldPosFor.SnId) ?? new PxcModels.PosMachinesTwo();
  103. pos.IsFirst = oldPos.IsFirst;
  104. }
  105. add.UpdateDate = Mer.UpdateTime;
  106. add.TopUserId = TopUserId;
  107. add.BrandId = BrandId;
  108. add.SnStoreId = pos.StoreId;
  109. add.SnType = pos.PosSnType;
  110. add.UserId = pos.UserId;
  111. add.MgrName = Mer.AgentName;
  112. add.MerStatus = 1;
  113. add.KqSnNo = Mer.SnNo;
  114. add.MerIdcardNo = Mer.MerIdcardNo;
  115. add.MerRealName = Mer.MerRealName;
  116. add.MerchantMobile = Mer.MerMobile;
  117. add.MerchantName = Mer.MerName;
  118. db.SaveChanges();
  119. pos.BindMerchantId = add.Id;
  120. db.SaveChanges();
  121. PxcModels.Users buser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new PxcModels.Users();
  122. if(buser.BusinessFlag == 1)
  123. {
  124. bool checkMer = db.BusinessPartnerMerchant.Any(m => m.MerchantId == add.Id);
  125. if(!checkMer)
  126. {
  127. PxcModels.BusinessPartnerPos bpos = db.BusinessPartnerPos.FirstOrDefault(m => m.PosId == pos.Id) ?? new PxcModels.BusinessPartnerPos();
  128. db.BusinessPartnerMerchant.Add(new PxcModels.BusinessPartnerMerchant()
  129. {
  130. MerNo = Mer.MerNo,
  131. MerchantId = add.Id,
  132. PartnerId = bpos.PartnerId,
  133. UserId = pos.BuyUserId,
  134. });
  135. db.SaveChanges();
  136. }
  137. }
  138. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  139. if (edit != null)
  140. {
  141. edit.Status = 2;
  142. spdb.SaveChanges();
  143. }
  144. tran.Commit();
  145. }
  146. }
  147. catch (Exception ex)
  148. {
  149. tran.Rollback();
  150. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户数据到MAIN异常");
  151. }
  152. tran.Dispose();
  153. }
  154. spdb.SaveChanges();
  155. spdb.Dispose();
  156. db.SaveChanges();
  157. db.Dispose();
  158. }
  159. catch (Exception ex)
  160. {
  161. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户数据到MAIN异常");
  162. }
  163. Thread.Sleep(1000);
  164. }
  165. }
  166. }
  167. }