SycnSpBindService.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. if (Utils.PushTimeCheck())
  28. {
  29. List<string> BrandIds = new List<string>();
  30. BrandIds.Add("14");
  31. BrandIds.Add("17");
  32. List<string> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
  33. WebCMSEntities spdb = new WebCMSEntities();
  34. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  35. DateTime start = DateTime.Now.AddDays(-60);
  36. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/BindRecordId.txt")));
  37. var Binds = spdb.BindRecord.Where(m => m.Id >= StartId && m.CreateTime >= start && !BrandIds.Contains(m.ProductType) && SaveDataBindIds.Contains(m.ProductType) && m.Status == 1).OrderByDescending(m => m.Id).Take(20).ToList();
  38. foreach (var Bind in Binds)
  39. {
  40. var tran = db.Database.BeginTransaction();
  41. try
  42. {
  43. if ((Bind.Field1 == "解绑" || Bind.Field1 == "UNBIND" || Bind.Field1 == "TRUE" || Bind.Field1 == "FORCED_UNBIND") && (Bind.ProductType == "1" || Bind.ProductType == "23" || Bind.ProductType == "27" || Bind.ProductType == "30" || Bind.ProductType == "34"))
  44. {
  45. PosPushDataNewHelper.UnBind(new UnBindRecord()
  46. {
  47. MerSnNo = Bind.MerSnNo,
  48. MerNo = Bind.MerNo,
  49. ProductType = Bind.ProductType,
  50. });
  51. //wifi解绑逻辑
  52. if (Utils.Instance.IsWifi(int.Parse(Bind.ProductType)))
  53. {
  54. string fileName = function.MD5_16(Guid.NewGuid().ToString());
  55. string content = "{\"posSn\":\"" + Bind.MerSnNo + "\"}";
  56. RedisDbconn.Instance.AddList("kxs_unbindwifi_list", fileName + "#cut#" + content + "#cut#" + fileName);
  57. }
  58. }
  59. else if (Bind.Field1 == "CHANGE_BIND" && Bind.ProductType == "34")
  60. {
  61. PosPushDataNewHelper.ChangeBind(new ChangeBindRecord()
  62. {
  63. MerNo = Bind.MerNo,
  64. MerSnNo = Bind.Field2,
  65. ProductType = Bind.ProductType,
  66. MerNewSnNo = Bind.MerSnNo,
  67. });
  68. }
  69. else
  70. {
  71. if (Utils.Instance.IsWifi(int.Parse(Bind.ProductType)))
  72. {
  73. Bind.MerNo = Bind.MerSnNo;
  74. PosPushDataNewHelper.Bind0(Bind);
  75. }
  76. }
  77. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  78. if (edit != null)
  79. {
  80. edit.Status = 2;
  81. spdb.SaveChanges();
  82. }
  83. tran.Commit();
  84. }
  85. catch (Exception ex)
  86. {
  87. tran.Rollback();
  88. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  89. }
  90. tran.Dispose();
  91. }
  92. spdb.SaveChanges();
  93. spdb.Dispose();
  94. db.SaveChanges();
  95. db.Dispose();
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  101. }
  102. Thread.Sleep(1000);
  103. }
  104. }
  105. public void StartSim()
  106. {
  107. Thread th = new Thread(StartSimDo);
  108. th.IsBackground = true;
  109. th.Start();
  110. }
  111. public void StartSimDo()
  112. {
  113. while (true)
  114. {
  115. try
  116. {
  117. if (Utils.PushTimeCheck())
  118. {
  119. List<string> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
  120. WebCMSEntities spdb = new WebCMSEntities();
  121. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  122. DateTime start = DateTime.Now.AddDays(-365);
  123. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/BindRecordId.txt")));
  124. var Binds = spdb.BindRecord.Where(m => m.Id >= StartId && m.CreateTime >= start && m.ProductType == "14" && SaveDataBindIds.Contains(m.ProductType) && m.Status == 1).OrderByDescending(m => m.Id).Take(20).ToList();
  125. foreach (var Bind in Binds)
  126. {
  127. try
  128. {
  129. string KindName = Bind.Remark; //商品类型名称
  130. string Name = Bind.Field3; //商品名称
  131. decimal ActualAmount = decimal.Parse(function.CheckNum(Bind.SeoTitle)); //实付金额
  132. if (ActualAmount >= 10000) ActualAmount = ActualAmount / 100;
  133. // PosPushDataNewHelper.Trade(new TradeRecord()
  134. // {
  135. // TradeSnNo = Bind.MerSnNo,
  136. // MerNo = Bind.MerNo,
  137. // TradeAmount = 0,
  138. // TradeSerialNo = function.MD5_16("sim:" + Bind.Id.ToString()),
  139. // CreateDate = Bind.CreateTime,
  140. // ProductType = Bind.ProductType,
  141. // Field1 = Bind.Field3,
  142. // Field2 = Bind.Field5,
  143. // ErrorMsg = ActualAmount.ToString(),
  144. // });
  145. // bool op = false;
  146. // if(!Bind.MerSnNo.StartsWith("CS") && Bind.Field5 == "已激活" && Bind.Field3.Contains("双百套餐") && ActualAmount >= 100)
  147. // {
  148. // op = true;
  149. // }
  150. // if(Bind.MerSnNo.StartsWith("CS") && Bind.Field5 == "已激活" && ActualAmount >= 100)
  151. // {
  152. // op = true;
  153. // }
  154. if (Bind.Field5 == "已激活")
  155. {
  156. PosPushDataNewHelper.Deposit(new ActivateRecord()
  157. {
  158. SeoKeyword = Bind.Field3,
  159. SeoTitle = ActualAmount.ToString(),
  160. ProductType = Bind.ProductType,
  161. SnNo = Bind.MerSnNo,
  162. MerNo = Bind.MerNo,
  163. Id = Bind.Id,
  164. });
  165. }
  166. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  167. if (edit != null)
  168. {
  169. edit.Status = 2;
  170. spdb.SaveChanges();
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP广电卡绑定数据到MAIN异常");
  176. }
  177. }
  178. spdb.SaveChanges();
  179. spdb.Dispose();
  180. db.SaveChanges();
  181. db.Dispose();
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP广电卡绑定数据到MAIN异常");
  187. }
  188. Thread.Sleep(1000);
  189. }
  190. }
  191. }
  192. }