SycnSpBindService.cs 9.0 KB

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