SycnSpMerchantService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 = 0;
  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")
  76. {
  77. Name = Mer.SeoTitle;
  78. }
  79. Name = Name.Replace("个体户", "");
  80. Name = Name.Replace("个体商户", "");
  81. bool check = db.PosMerchantInfo.Any(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerRealName.Contains(Name));
  82. if(!check)
  83. {
  84. IsFirst = 1;
  85. }
  86. pos.IsFirst = IsFirst;
  87. }
  88. add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  89. {
  90. CreateDate = Mer.CreateTime,
  91. KqMerNo = Mer.MerNo,
  92. MerchantNo = Mer.MerNo,
  93. }).Entity;
  94. db.SaveChanges();
  95. }
  96. add.UpdateDate = Mer.UpdateTime;
  97. add.TopUserId = TopUserId;
  98. add.BrandId = BrandId;
  99. add.SnStoreId = pos.StoreId;
  100. add.SnType = pos.PosSnType;
  101. add.UserId = pos.UserId;
  102. add.MgrName = Mer.AgentName;
  103. add.MerStatus = 1;
  104. add.KqSnNo = Mer.SnNo;
  105. add.MerIdcardNo = Mer.MerIdcardNo;
  106. add.MerRealName = Mer.MerRealName;
  107. add.MerchantMobile = Mer.MerMobile;
  108. add.MerchantName = Mer.MerName;
  109. pos.BindMerchantId = add.Id;
  110. db.SaveChanges();
  111. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  112. if (edit != null)
  113. {
  114. edit.Status = 2;
  115. spdb.SaveChanges();
  116. }
  117. tran.Commit();
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. tran.Rollback();
  123. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户数据到MAIN异常");
  124. }
  125. tran.Dispose();
  126. }
  127. spdb.SaveChanges();
  128. spdb.Dispose();
  129. db.SaveChanges();
  130. db.Dispose();
  131. }
  132. catch (Exception ex)
  133. {
  134. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户数据到MAIN异常");
  135. }
  136. Thread.Sleep(1000);
  137. }
  138. }
  139. }
  140. }