SycnSpBindService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 SycnSpBindService
  11. {
  12. public readonly static SycnSpBindService Instance = new SycnSpBindService();
  13. private SycnSpBindService()
  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. List<string> BrandIds = new List<string>();
  28. BrandIds.Add("14");
  29. BrandIds.Add("17");
  30. WebCMSEntities spdb = new WebCMSEntities();
  31. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  32. DateTime start = DateTime.Now.AddDays(-60);
  33. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/BindRecordId.txt")));
  34. var Binds = spdb.BindRecord.Where(m => m.Id >= StartId && m.CreateTime >= start && !BrandIds.Contains(m.ProductType) && m.Status == 1).OrderByDescending(m => m.Id).Take(20).ToList();
  35. foreach (var Bind in Binds)
  36. {
  37. var tran = db.Database.BeginTransaction();
  38. try
  39. {
  40. if((Bind.Field1 == "解绑" || Bind.Field1 == "UNBIND") && (Bind.ProductType == "1" || Bind.ProductType == "23" || Bind.ProductType == "27" || Bind.ProductType == "30"))
  41. {
  42. PosPushDataNewHelper.UnBind(new UnBindRecord()
  43. {
  44. MerSnNo = Bind.MerSnNo,
  45. MerNo = Bind.MerNo,
  46. ProductType = Bind.ProductType,
  47. });
  48. }
  49. else
  50. {
  51. if(Utils.Instance.IsWifi(int.Parse(Bind.ProductType)))
  52. {
  53. Bind.MerNo = Bind.MerSnNo;
  54. PosPushDataNewHelper.Bind0(Bind);
  55. }
  56. }
  57. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  58. if (edit != null)
  59. {
  60. edit.Status = 2;
  61. spdb.SaveChanges();
  62. }
  63. tran.Commit();
  64. }
  65. catch (Exception ex)
  66. {
  67. tran.Rollback();
  68. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  69. }
  70. tran.Dispose();
  71. }
  72. spdb.SaveChanges();
  73. spdb.Dispose();
  74. db.SaveChanges();
  75. db.Dispose();
  76. }
  77. catch (Exception ex)
  78. {
  79. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  80. }
  81. Thread.Sleep(1000);
  82. }
  83. }
  84. public void StartSim()
  85. {
  86. Thread th = new Thread(StartSimDo);
  87. th.IsBackground = true;
  88. th.Start();
  89. }
  90. public void StartSimDo()
  91. {
  92. while (true)
  93. {
  94. try
  95. {
  96. WebCMSEntities spdb = new WebCMSEntities();
  97. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  98. DateTime start = DateTime.Now.AddDays(-60);
  99. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/BindRecordId.txt")));
  100. var Binds = spdb.BindRecord.Where(m => m.Id >= StartId && m.CreateTime >= start && m.ProductType == "14" && m.Status == 1).OrderByDescending(m => m.Id).Take(20).ToList();
  101. foreach (var Bind in Binds)
  102. {
  103. try
  104. {
  105. string KindName = Bind.Remark; //商品类型名称
  106. string Name = Bind.Field3; //商品名称
  107. decimal ActualAmount = decimal.Parse(function.CheckNum(Bind.SeoTitle)); //实付金额
  108. if(ActualAmount >= 10000) ActualAmount = ActualAmount / 100;
  109. // PosPushDataNewHelper.Trade(new TradeRecord()
  110. // {
  111. // TradeSnNo = Bind.MerSnNo,
  112. // MerNo = Bind.MerNo,
  113. // TradeAmount = 0,
  114. // TradeSerialNo = function.MD5_16("sim:" + Bind.Id.ToString()),
  115. // CreateDate = Bind.CreateTime,
  116. // ProductType = Bind.ProductType,
  117. // Field1 = Bind.Field3,
  118. // Field2 = Bind.Field5,
  119. // ErrorMsg = ActualAmount.ToString(),
  120. // });
  121. PosPushDataNewHelper.Deposit(new ActivateRecord()
  122. {
  123. SeoKeyword = Bind.Field3,
  124. SeoTitle = ActualAmount.ToString(),
  125. ProductType = Bind.ProductType,
  126. SnNo = Bind.MerSnNo,
  127. MerNo = Bind.MerNo,
  128. Id = Bind.Id,
  129. });
  130. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  131. if (edit != null)
  132. {
  133. edit.Status = 2;
  134. spdb.SaveChanges();
  135. }
  136. }
  137. catch (Exception ex)
  138. {
  139. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  140. }
  141. }
  142. spdb.SaveChanges();
  143. spdb.Dispose();
  144. db.SaveChanges();
  145. db.Dispose();
  146. }
  147. catch (Exception ex)
  148. {
  149. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  150. }
  151. Thread.Sleep(1000);
  152. }
  153. }
  154. }
  155. }