SycnSpUnBindService.cs 4.9 KB

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