SycnSpBindService.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. //wifi解绑逻辑
  49. if(Utils.Instance.IsWifi(int.Parse(Bind.ProductType)))
  50. {
  51. string fileName = function.MD5_16(Guid.NewGuid().ToString());
  52. string content = "{\"posSn\":\"" + Bind.MerSnNo + "\"}";
  53. RedisDbconn.Instance.AddList("kxs_unbindwifi_list", fileName + "#cut#" + content + "#cut#" + fileName);
  54. }
  55. }
  56. else
  57. {
  58. if(Utils.Instance.IsWifi(int.Parse(Bind.ProductType)))
  59. {
  60. Bind.MerNo = Bind.MerSnNo;
  61. PosPushDataNewHelper.Bind0(Bind);
  62. }
  63. }
  64. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  65. if (edit != null)
  66. {
  67. edit.Status = 2;
  68. spdb.SaveChanges();
  69. }
  70. tran.Commit();
  71. }
  72. catch (Exception ex)
  73. {
  74. tran.Rollback();
  75. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  76. }
  77. tran.Dispose();
  78. }
  79. spdb.SaveChanges();
  80. spdb.Dispose();
  81. db.SaveChanges();
  82. db.Dispose();
  83. }
  84. catch (Exception ex)
  85. {
  86. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  87. }
  88. Thread.Sleep(1000);
  89. }
  90. }
  91. public void StartSim()
  92. {
  93. Thread th = new Thread(StartSimDo);
  94. th.IsBackground = true;
  95. th.Start();
  96. }
  97. public void StartSimDo()
  98. {
  99. while (true)
  100. {
  101. try
  102. {
  103. WebCMSEntities spdb = new WebCMSEntities();
  104. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  105. DateTime start = DateTime.Now.AddDays(-365);
  106. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/BindRecordId.txt")));
  107. 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();
  108. foreach (var Bind in Binds)
  109. {
  110. try
  111. {
  112. string KindName = Bind.Remark; //商品类型名称
  113. string Name = Bind.Field3; //商品名称
  114. decimal ActualAmount = decimal.Parse(function.CheckNum(Bind.SeoTitle)); //实付金额
  115. if(ActualAmount >= 10000) ActualAmount = ActualAmount / 100;
  116. // PosPushDataNewHelper.Trade(new TradeRecord()
  117. // {
  118. // TradeSnNo = Bind.MerSnNo,
  119. // MerNo = Bind.MerNo,
  120. // TradeAmount = 0,
  121. // TradeSerialNo = function.MD5_16("sim:" + Bind.Id.ToString()),
  122. // CreateDate = Bind.CreateTime,
  123. // ProductType = Bind.ProductType,
  124. // Field1 = Bind.Field3,
  125. // Field2 = Bind.Field5,
  126. // ErrorMsg = ActualAmount.ToString(),
  127. // });
  128. PosPushDataNewHelper.Deposit(new ActivateRecord()
  129. {
  130. SeoKeyword = Bind.Field3,
  131. SeoTitle = ActualAmount.ToString(),
  132. ProductType = Bind.ProductType,
  133. SnNo = Bind.MerSnNo,
  134. MerNo = Bind.MerNo,
  135. Id = Bind.Id,
  136. });
  137. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  138. if (edit != null)
  139. {
  140. edit.Status = 2;
  141. spdb.SaveChanges();
  142. }
  143. }
  144. catch (Exception ex)
  145. {
  146. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP广电卡绑定数据到MAIN异常");
  147. }
  148. }
  149. spdb.SaveChanges();
  150. spdb.Dispose();
  151. db.SaveChanges();
  152. db.Dispose();
  153. }
  154. catch (Exception ex)
  155. {
  156. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP广电卡绑定数据到MAIN异常");
  157. }
  158. Thread.Sleep(1000);
  159. }
  160. }
  161. }
  162. }