SycnSpUnBindService.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. //推送数据给java
  59. PosPushDataHelper.UnBind(merchant);
  60. pos.BindMerchantId = 0;
  61. pos.BindingState = 0;
  62. pos.BindingTime = null;
  63. db.PosMerchantInfo.Remove(merchant);
  64. db.SaveChanges();
  65. }
  66. }
  67. db.MachineUnBind.Add(new PxcModels.MachineUnBind()
  68. {
  69. CreateDate = DateTime.Now,
  70. MerchantId = pos.BindMerchantId,
  71. AuditDate = DateTime.Now,
  72. AuditDesc = BrandName + "推送解绑",
  73. AuditStatus = 1,
  74. SnNo = pos.PosSn,
  75. BrandId = pos.BrandId,
  76. UserId = pos.BuyUserId,
  77. ApplyNo = "U" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
  78. });
  79. pos.BindMerchantId = 0;
  80. pos.BindingState = 0;
  81. pos.BindingTime = DateTime.Parse("1900-01-01");
  82. pos.UserId = pos.BuyUserId;
  83. string IdBrand = pos.BuyUserId + "_" + pos.BrandId;
  84. PxcModels.UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  85. if (userData == null)
  86. {
  87. userData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  88. {
  89. IdBrand = IdBrand,
  90. }).Entity;
  91. db.SaveChanges();
  92. }
  93. userData.BindCount -= 1;
  94. userData.UnBindCount += 1;
  95. db.SaveChanges();
  96. UnBindRecord edit = spdb.UnBindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  97. if (edit != null)
  98. {
  99. edit.Status = 2;
  100. spdb.SaveChanges();
  101. }
  102. }
  103. tran.Commit();
  104. }
  105. catch (Exception ex)
  106. {
  107. tran.Rollback();
  108. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP解绑数据到MAIN异常");
  109. }
  110. tran.Dispose();
  111. }
  112. spdb.Dispose();
  113. db.Dispose();
  114. }
  115. catch (Exception ex)
  116. {
  117. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP解绑数据到MAIN异常");
  118. }
  119. Thread.Sleep(1000);
  120. }
  121. }
  122. }
  123. }