SycnSpUnBindService.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 SycnSpUnBindService
  11. {
  12. public readonly static SycnSpUnBindService Instance = new SycnSpUnBindService();
  13. private SycnSpUnBindService()
  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(-30);
  30. List<UnBindRecord> Binds = spdb.UnBindRecord.Where(m => m.UnBindTime >= start && m.Status == 1).OrderByDescending(m => m.Id).ToList();
  31. foreach (UnBindRecord Bind in Binds)
  32. {
  33. var tran = db.Database.BeginTransaction();
  34. try
  35. {
  36. PosPushDataNewHelper.UnBind(Bind);
  37. string PosSn = Bind.MerSnNo;
  38. string MerNo = Bind.MerNo;
  39. PxcModels.MachineForSnNo forSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == PosSn) ?? new PxcModels.MachineForSnNo();
  40. PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == MerNo) ?? new PxcModels.MachineForMerNo();
  41. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId) ?? new PxcModels.PosMachinesTwo();
  42. if (pos.ActivationState == 0 && pos.BuyUserId > 0)
  43. {
  44. string BrandName = RelationClass.GetKqProductsInfo(pos.BrandId);
  45. if (forMerNo != null)
  46. {
  47. // 删除对应商户
  48. PxcModels.MachineForMerNo MerEdit = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == MerNo);
  49. if (MerEdit != null)
  50. {
  51. db.MachineForMerNo.Remove(MerEdit);
  52. db.SaveChanges();
  53. }
  54. PxcModels.PosMachinesTwo EditPos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn) ?? new PxcModels.PosMachinesTwo();
  55. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == EditPos.BindMerchantId);
  56. if(merchant != null)
  57. {
  58. pos.BindMerchantId = 0;
  59. pos.BindingState = 0;
  60. pos.BindingTime = null;
  61. db.PosMerchantInfo.Remove(merchant);
  62. db.SaveChanges();
  63. }
  64. }
  65. db.MachineUnBind.Add(new PxcModels.MachineUnBind()
  66. {
  67. CreateDate = DateTime.Now,
  68. MerchantId = pos.BindMerchantId,
  69. AuditDate = DateTime.Now,
  70. AuditDesc = BrandName + "推送解绑",
  71. AuditStatus = 1,
  72. SnNo = pos.PosSn,
  73. BrandId = pos.BrandId,
  74. UserId = pos.BuyUserId,
  75. ApplyNo = "U" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
  76. });
  77. pos.BindMerchantId = 0;
  78. pos.BindingState = 0;
  79. pos.BindingTime = DateTime.Parse("1900-01-01");
  80. pos.UserId = pos.BuyUserId;
  81. string IdBrand = pos.BuyUserId + "_" + pos.BrandId;
  82. PxcModels.UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  83. if (userData == null)
  84. {
  85. userData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  86. {
  87. IdBrand = IdBrand,
  88. }).Entity;
  89. db.SaveChanges();
  90. }
  91. userData.BindCount -= 1;
  92. userData.UnBindCount += 1;
  93. db.SaveChanges();
  94. UnBindRecord edit = spdb.UnBindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  95. if (edit != null)
  96. {
  97. edit.Status = 2;
  98. spdb.SaveChanges();
  99. }
  100. }
  101. tran.Commit();
  102. }
  103. catch (Exception ex)
  104. {
  105. tran.Rollback();
  106. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP解绑数据到MAIN异常");
  107. }
  108. tran.Dispose();
  109. }
  110. spdb.Dispose();
  111. db.Dispose();
  112. }
  113. catch (Exception ex)
  114. {
  115. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP解绑数据到MAIN异常");
  116. }
  117. Thread.Sleep(1000);
  118. }
  119. }
  120. }
  121. }