SycnSpMerchantService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.PosSn == Mer.SnNo) ?? new PxcModels.PosMachinesTwo();
  39. if (pos.BindingState == 1 && Mer.Field2 != "解绑")
  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 = pos.BrandId;
  48. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mer.MerNo);
  49. if (add == null)
  50. {
  51. int IsFirst = 1;
  52. if(!string.IsNullOrEmpty(Mer.MerIdcardNo))
  53. {
  54. string startNo = Mer.MerIdcardNo.Substring(0, 6);
  55. string endNo = Mer.MerIdcardNo.Substring(Mer.MerIdcardNo.Length - 4, 4).ToUpper();
  56. string Name = Mer.MerName;
  57. if (Mer.ProductType == "2")
  58. {
  59. if (Name.Contains("-"))
  60. {
  61. Name = Name.Split('-')[1];
  62. }
  63. else if (Name.Contains("_"))
  64. {
  65. Name = Name.Split('_')[1];
  66. }
  67. }
  68. else if (Mer.ProductType == "4" || Mer.ProductType == "8" || Mer.ProductType == "9")
  69. {
  70. Name = Mer.SeoTitle;
  71. }
  72. else if (Mer.ProductType == "10")
  73. {
  74. Name = Name.Replace("*", "");
  75. }
  76. Name = Name.Replace("个体户", "");
  77. Name = Name.Replace("个体商户", "");
  78. Name = Name.Replace("企业户", "");
  79. Name = Name.Replace("企业商户", "");
  80. function.WriteLog(DateTime.Now.ToString() + "-----startNo:" + startNo + ",endNo:" + endNo + ",Name:" + Name, "监控机具是否互斥");
  81. bool check = db.PosMerchantInfo.Any(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerchantName.Contains(Name));
  82. if(check)
  83. {
  84. IsFirst = 0;
  85. }
  86. }
  87. add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  88. {
  89. CreateDate = Mer.CreateTime,
  90. KqMerNo = Mer.MerNo,
  91. MerchantNo = Mer.MerNo,
  92. }).Entity;
  93. //查询商户备份表,如果有记录则继承之前的商户Id
  94. if(Mer.ProductType == "1") //判断是否金控
  95. {
  96. function.WriteLog(DateTime.Now.ToString(), "监控金控换绑机具日志");
  97. function.WriteLog("Mer.MerNo:" + Mer.MerNo, "监控金控换绑机具日志");
  98. PxcModels.PosMerchantInfoBak MerBak = db.PosMerchantInfoBak.FirstOrDefault(m => m.KqMerNo == Mer.MerNo);
  99. if(MerBak != null)
  100. {
  101. function.WriteLog("有备份商户-机具SN:" + MerBak.KqSnNo, "监控金控换绑机具日志");
  102. PxcModels.PosMachinesTwo OldPos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == MerBak.KqSnNo);
  103. if(OldPos != null)
  104. {
  105. function.WriteLog("首台机标记:新机具" + pos.IsFirst + ",老机具" + OldPos.IsFirst, "监控金控换绑机具日志");
  106. pos.IsFirst = OldPos.IsFirst;
  107. }
  108. add.Id = MerBak.Id;
  109. db.PosMerchantInfoBak.Remove(MerBak);
  110. }
  111. else
  112. {
  113. function.WriteLog("无备份商户", "监控金控换绑机具日志");
  114. pos.IsFirst = IsFirst;
  115. }
  116. function.WriteLog("\n\n", "监控金控换绑机具日志");
  117. }
  118. else
  119. {
  120. pos.IsFirst = IsFirst;
  121. }
  122. db.SaveChanges();
  123. function.WriteLog("MerNo:" + Mer.MerNo + ",PosSn:" + pos.PosSn + ",IsFirst:" + IsFirst + "\n\n", "监控机具是否互斥");
  124. }
  125. add.UpdateDate = Mer.UpdateTime;
  126. add.TopUserId = TopUserId;
  127. add.BrandId = BrandId;
  128. add.SnStoreId = pos.StoreId;
  129. add.SnType = pos.PosSnType;
  130. add.UserId = pos.UserId;
  131. add.MgrName = Mer.AgentName;
  132. add.MerStatus = 1;
  133. add.KqSnNo = Mer.SnNo;
  134. add.MerIdcardNo = function.CheckNull(Mer.MerIdcardNo).ToUpper();
  135. add.MerRealName = Mer.MerRealName;
  136. add.MerchantMobile = Mer.MerMobile;
  137. add.MerchantName = Mer.MerName;
  138. pos.BindMerchantId = add.Id;
  139. db.SaveChanges();
  140. PxcModels.Users buser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new PxcModels.Users();
  141. if(buser.BusinessFlag == 1)
  142. {
  143. bool checkMer = db.BusinessPartnerMerchant.Any(m => m.MerchantId == add.Id);
  144. if(!checkMer)
  145. {
  146. PxcModels.BusinessPartnerPos bpos = db.BusinessPartnerPos.FirstOrDefault(m => m.PosId == pos.Id) ?? new PxcModels.BusinessPartnerPos();
  147. db.BusinessPartnerMerchant.Add(new PxcModels.BusinessPartnerMerchant()
  148. {
  149. CreateDate = DateTime.Now,
  150. MerNo = Mer.MerNo,
  151. MerchantId = add.Id,
  152. PartnerId = bpos.PartnerId,
  153. UserId = pos.BuyUserId,
  154. });
  155. db.SaveChanges();
  156. }
  157. }
  158. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  159. if (edit != null)
  160. {
  161. edit.Status = 2;
  162. spdb.SaveChanges();
  163. }
  164. tran.Commit();
  165. }
  166. }
  167. catch (Exception ex)
  168. {
  169. tran.Rollback();
  170. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户数据到MAIN异常");
  171. }
  172. tran.Dispose();
  173. }
  174. spdb.SaveChanges();
  175. spdb.Dispose();
  176. db.SaveChanges();
  177. db.Dispose();
  178. }
  179. catch (Exception ex)
  180. {
  181. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户数据到MAIN异常");
  182. }
  183. Thread.Sleep(1000);
  184. }
  185. }
  186. }
  187. }