SycnSpMerchantService.cs 5.1 KB

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