SycnSpMerchantService.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.SpModels;
  5. using Library;
  6. using LitJson;
  7. namespace MySystem
  8. {
  9. public class SycnSpMerchantService
  10. {
  11. public readonly static SycnSpMerchantService Instance = new SycnSpMerchantService();
  12. private SycnSpMerchantService()
  13. { }
  14. public void Start()
  15. {
  16. WebCMSEntities spdb = new WebCMSEntities();
  17. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  18. DateTime start = DateTime.Now.AddDays(-2);
  19. var Mers = spdb.Merchants.Where(m => m.CreateTime >= start && m.Status == 1).OrderByDescending(m => m.Id).ToList();
  20. foreach (var Mer in Mers)
  21. {
  22. var tran = db.Database.BeginTransaction();
  23. try
  24. {
  25. PxcModels.MachineForMerNo posFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.MerNo) ?? new PxcModels.MachineForMerNo();
  26. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId) ?? new PxcModels.PosMachinesTwo();
  27. if (pos.BindingState == 1)
  28. {
  29. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new PxcModels.Users();
  30. int TopUserId = 0;
  31. if (!string.IsNullOrEmpty(user.ParentNav))
  32. {
  33. TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  34. }
  35. int BrandId = int.Parse(Mer.ProductType);
  36. if (Mer.MerNo.StartsWith("M900"))
  37. {
  38. BrandId = 3;
  39. }
  40. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  41. {
  42. CreateDate = Mer.CreateTime,
  43. UpdateDate = Mer.UpdateTime,
  44. TopUserId = TopUserId,
  45. MerUserType = user.MerchantType,
  46. BrandId = BrandId,
  47. SnStoreId = pos.StoreId,
  48. SnType = pos.PosSnType,
  49. UserId = pos.UserId,
  50. MgrName = Mer.AgentName,
  51. MerStatus = 1,
  52. KqSnNo = Mer.SnNo,
  53. KqMerNo = Mer.MerNo,
  54. MerIdcardNo = Mer.MerIdcardNo,
  55. MerRealName = Mer.MerRealName,
  56. MerchantMobile = Mer.MerMobile,
  57. MerchantName = Mer.MerName,
  58. MerchantNo = Mer.MerNo,
  59. }).Entity;
  60. db.SaveChanges();
  61. pos.BindMerchantId = add.Id;
  62. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  63. if (edit != null)
  64. {
  65. edit.Status = 2;
  66. spdb.SaveChanges();
  67. }
  68. tran.Commit();
  69. }
  70. }
  71. catch (Exception ex)
  72. {
  73. tran.Rollback();
  74. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户数据到MAIN异常");
  75. }
  76. tran.Dispose();
  77. }
  78. spdb.SaveChanges();
  79. spdb.Dispose();
  80. db.SaveChanges();
  81. db.Dispose();
  82. }
  83. }
  84. }