SycnSpMerchantService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.SeoTitle == "200")
  52. {
  53. BrandId = 5;
  54. }
  55. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  56. {
  57. CreateDate = Mer.CreateTime,
  58. UpdateDate = Mer.UpdateTime,
  59. TopUserId = TopUserId,
  60. BrandId = BrandId,
  61. SnStoreId = pos.StoreId,
  62. SnType = pos.PosSnType,
  63. UserId = pos.UserId,
  64. MgrName = Mer.AgentName,
  65. MerStatus = 1,
  66. KqSnNo = Mer.SnNo,
  67. KqMerNo = Mer.MerNo,
  68. MerIdcardNo = Mer.MerIdcardNo,
  69. MerRealName = Mer.MerRealName,
  70. MerchantMobile = Mer.MerMobile,
  71. MerchantName = Mer.MerName,
  72. MerchantNo = Mer.MerNo,
  73. }).Entity;
  74. db.SaveChanges();
  75. pos.BindMerchantId = add.Id;
  76. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  77. if (edit != null)
  78. {
  79. edit.Status = 2;
  80. spdb.SaveChanges();
  81. }
  82. tran.Commit();
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. tran.Rollback();
  88. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户数据到MAIN异常");
  89. }
  90. tran.Dispose();
  91. }
  92. spdb.SaveChanges();
  93. spdb.Dispose();
  94. db.SaveChanges();
  95. db.Dispose();
  96. }
  97. catch (Exception ex)
  98. {
  99. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户数据到MAIN异常");
  100. }
  101. Thread.Sleep(1000);
  102. }
  103. }
  104. }
  105. }