SycnSpMerchantService.cs 9.5 KB

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