SycnSpUnBindService.cs 6.1 KB

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