SycnSpMerchantService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. int RecordId = PublicFunction.FeeSetRecord(db, pos, check, 0.63M);
  95. string info = "{\"RecordId\":\"" + RecordId + "\",\"PosId\":\"" + pos.Id + "\",\"Fee\": \"0.63\",\"Kind\": \"2\",\"OpMan\": \""+"系统"+"\"}";
  96. RedisDbconn.Instance.AddList("SetDepositQueue", info);
  97. }
  98. }
  99. add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  100. {
  101. CreateDate = Mer.CreateTime,
  102. KqMerNo = Mer.MerNo,
  103. MerchantNo = Mer.MerNo.Length > 16 ? Mer.MerNo.Substring(0, 16) : Mer.MerNo,
  104. }).Entity;
  105. //查询商户备份表,如果有记录则继承之前的商户Id
  106. if(Mer.ProductType == "1") //判断是否金控
  107. {
  108. function.WriteLog(DateTime.Now.ToString(), "监控金控换绑机具日志");
  109. function.WriteLog("Mer.MerNo:" + Mer.MerNo, "监控金控换绑机具日志");
  110. PxcModels.PosMerchantInfoBak MerBak = db.PosMerchantInfoBak.FirstOrDefault(m => m.KqMerNo == Mer.MerNo);
  111. if(MerBak != null)
  112. {
  113. function.WriteLog("有备份商户-机具SN:" + MerBak.KqSnNo, "监控金控换绑机具日志");
  114. PxcModels.PosMachinesTwo OldPos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == MerBak.KqSnNo);
  115. if(OldPos != null)
  116. {
  117. function.WriteLog("首台机标记:新机具" + pos.IsFirst + ",老机具" + OldPos.IsFirst, "监控金控换绑机具日志");
  118. pos.IsFirst = OldPos.IsFirst;
  119. }
  120. add.Id = MerBak.Id;
  121. db.PosMerchantInfoBak.Remove(MerBak);
  122. }
  123. else
  124. {
  125. function.WriteLog("无备份商户", "监控金控换绑机具日志");
  126. pos.IsFirst = IsFirst;
  127. }
  128. function.WriteLog("\n\n", "监控金控换绑机具日志");
  129. }
  130. else
  131. {
  132. pos.IsFirst = IsFirst;
  133. }
  134. db.SaveChanges();
  135. function.WriteLog("MerNo:" + Mer.MerNo + ",PosSn:" + pos.PosSn + ",IsFirst:" + IsFirst + "\n\n", "监控机具是否互斥");
  136. }
  137. add.UpdateDate = Mer.UpdateTime;
  138. add.TopUserId = TopUserId;
  139. add.BrandId = BrandId;
  140. add.SnStoreId = pos.StoreId;
  141. add.SnType = pos.PosSnType;
  142. add.UserId = pos.UserId;
  143. add.MgrName = Mer.AgentName;
  144. add.MerStatus = 1;
  145. add.KqSnNo = Mer.SnNo;
  146. string CertId = function.CheckNull(Mer.MerIdcardNo).ToUpper();
  147. if(BrandId == 15 || BrandId == 16)
  148. {
  149. CertId = CertId.Substring(0, 6) + "********" + CertId.Substring(CertId.Length - 4);
  150. }
  151. add.MerIdcardNo = CertId;
  152. add.MerRealName = Mer.MerRealName;
  153. add.MerchantMobile = Mer.MerMobile;
  154. add.MerchantName = Mer.MerName;
  155. pos.BindMerchantId = add.Id;
  156. pos.LastMerchantId = add.Id;
  157. db.SaveChanges();
  158. PxcModels.Users buser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new PxcModels.Users();
  159. if(buser.BusinessFlag == 1)
  160. {
  161. bool checkMer = db.BusinessPartnerMerchant.Any(m => m.MerchantId == add.Id);
  162. if(!checkMer)
  163. {
  164. PxcModels.BusinessPartnerPos bpos = db.BusinessPartnerPos.FirstOrDefault(m => m.PosId == pos.Id) ?? new PxcModels.BusinessPartnerPos();
  165. db.BusinessPartnerMerchant.Add(new PxcModels.BusinessPartnerMerchant()
  166. {
  167. CreateDate = DateTime.Now,
  168. MerNo = Mer.MerNo,
  169. MerchantId = add.Id,
  170. PartnerId = bpos.PartnerId,
  171. UserId = pos.BuyUserId,
  172. });
  173. db.SaveChanges();
  174. }
  175. }
  176. // if(pos.PrizeParams == "0")
  177. // {
  178. // int RecordId = PublicFunction.FeeSetRecord(db, pos, add, 0.63M);
  179. // string info = "{\"RecordId\":\"" + RecordId + "\",\"PosId\":\"" + pos.Id + "\",\"Fee\": \"0.63\",\"Kind\": \"2\",\"OpMan\": \""+"系统"+"\"}";
  180. // RedisDbconn.Instance.AddList("SetDepositQueue", info);
  181. // }
  182. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  183. if (edit != null)
  184. {
  185. edit.Status = 2;
  186. spdb.SaveChanges();
  187. }
  188. tran.Commit();
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. tran.Rollback();
  194. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户数据到MAIN异常");
  195. }
  196. tran.Dispose();
  197. }
  198. spdb.SaveChanges();
  199. spdb.Dispose();
  200. db.SaveChanges();
  201. db.Dispose();
  202. }
  203. catch (Exception ex)
  204. {
  205. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户数据到MAIN异常");
  206. }
  207. Thread.Sleep(1000);
  208. }
  209. }
  210. }
  211. }