SycnSpMerchantService.cs 12 KB

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