SycnSpMerchantService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 SycnSpMerchantService
  11. {
  12. public readonly static SycnSpMerchantService Instance = new SycnSpMerchantService();
  13. private SycnSpMerchantService()
  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> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
  30. WebCMSEntities spdb = new WebCMSEntities();
  31. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  32. DateTime start = DateTime.Now.AddDays(-100);
  33. DateTime end = DateTime.Now.AddMinutes(-2);
  34. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/MerchantsId.txt")));
  35. var Mers = spdb.Merchants.Where(m => m.Id >= StartId && m.CreateTime >= start && m.CreateTime <= end && m.Status == 1 && m.ProductType != "14" && SaveDataBindIds.Contains(m.ProductType)).OrderByDescending(m => m.Id).Take(20).ToList();
  36. // var Mers = spdb.Merchants.Where(m => m.Id == 765841).ToList();
  37. foreach (var Mer in Mers)
  38. {
  39. var tran = db.Database.BeginTransaction();
  40. try
  41. {
  42. if (Mer.Field2 != "解绑" && Mer.Field2 != "UNBIND" && Mer.Field2 != "CHANGE_BIND" && Mer.Field2 != "TRUE")
  43. {
  44. string rnd = "";
  45. if (!Utils.Instance.IsWifi(int.Parse(Mer.ProductType)) && !Utils.Instance.IsHaoDa(int.Parse(Mer.ProductType)) && Mer.ProductType != "14")
  46. {
  47. rnd = GetRandom(spdb, Mer.SnNo, Mer.MerNo);
  48. }
  49. if (Utils.Instance.IsWifi(int.Parse(Mer.ProductType)))
  50. {
  51. Mer.MerNo = Mer.SnNo;
  52. PosPushDataNewHelper.Bind(Mer);
  53. if (function.CheckNull(Mer.AgentName).StartsWith("YCJG"))
  54. {
  55. PosPushDataNewHelper.Deposit(new ActivateRecord()
  56. {
  57. SeoTitle = "0.00",
  58. ProductType = Mer.ProductType,
  59. SnNo = Mer.SnNo,
  60. MerNo = Mer.SnNo,
  61. Id = Mer.Id,
  62. });
  63. }
  64. }
  65. else if (Mer.ProductType == "12" || Mer.ProductType == "30" || Mer.ProductType == "34" || Mer.ProductType == "38")
  66. {
  67. MerchantRecord merInfo = new MerchantRecord();
  68. if (Mer.ProductType == "12" || Mer.ProductType == "34" || Mer.ProductType == "38") merInfo = spdb.MerchantRecord.FirstOrDefault(m => m.MerNo == Mer.MerNo);
  69. if (Mer.ProductType == "30")
  70. {
  71. merInfo = spdb.MerchantRecord.FirstOrDefault(m => m.Field3 == Mer.MerNo);
  72. if (merInfo == null) merInfo = spdb.MerchantRecord.FirstOrDefault(m => m.MerNo == Mer.MerNo);
  73. }
  74. if (merInfo != null)
  75. {
  76. if (Mer.SeoTitle == "PAPER_CODE" && string.IsNullOrEmpty(Mer.Field5))
  77. {
  78. Mer.Field5 = Mer.MerNo;
  79. }
  80. Mer.MerNo = merInfo.MerNo;
  81. Mer.MerIdcardNo = merInfo.LegalIdCard;
  82. Mer.MerName = merInfo.MerName;
  83. if (Mer.ProductType == "30")
  84. {
  85. Mer.MerMobile = merInfo.Field2;
  86. }
  87. if (Mer.ProductType == "34" || Mer.ProductType == "38")
  88. {
  89. Mer.MerMobile = merInfo.MerNewSnNo;
  90. }
  91. PosPushDataNewHelper.Bind(Mer, rnd);
  92. }
  93. }
  94. else if (!Utils.Instance.IsHaoDa(int.Parse(Mer.ProductType)))
  95. {
  96. PosPushDataNewHelper.Bind(Mer, rnd);
  97. }
  98. }
  99. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  100. if (edit != null)
  101. {
  102. edit.Status = 2;
  103. spdb.SaveChanges();
  104. }
  105. tran.Commit();
  106. }
  107. catch (Exception ex)
  108. {
  109. tran.Rollback();
  110. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户数据到MAIN异常");
  111. }
  112. tran.Dispose();
  113. }
  114. spdb.SaveChanges();
  115. spdb.Dispose();
  116. db.SaveChanges();
  117. db.Dispose();
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户数据到MAIN异常");
  123. }
  124. Thread.Sleep(1000);
  125. }
  126. }
  127. public string GetRandom(WebCMSEntities spdb, string sn, string merno)
  128. {
  129. var list = spdb.BindRecord.Select(m => new { m.MerSnNo, m.MerNo, m.CreateTime }).Where(m => m.MerSnNo == sn && m.MerNo == merno).ToList().Select(m => m.CreateTime).Distinct().ToList();
  130. if (list.Count > 1)
  131. {
  132. return function.get_Random(5);
  133. }
  134. return "";
  135. }
  136. public void WifiSendPrize(PxcModels.WebCMSEntities db, PxcModels.PosMachinesTwo pos, PxcModels.PosMerchantInfo add)
  137. {
  138. PxcModels.WifiTradeRecord trade = db.WifiTradeRecord.FirstOrDefault(m => m.SnNo == pos.PosSn && m.Status == 0);
  139. if (trade != null)
  140. {
  141. trade.Status = 1;
  142. db.SaveChanges();
  143. decimal Prize = trade.TradeAmount * 0.15M;
  144. StatService.Instance.doActiveReward(db, add, pos, pos.BuyUserId, "", 1, Prize);
  145. if (pos.CardType < 100)
  146. {
  147. RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
  148. }
  149. if (pos.LeaderUserId > 0 && db.Leaders.Any(m => m.Id == pos.LeaderUserId && m.ExpiredDate > DateTime.Now) && pos.CardType < 100)
  150. {
  151. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  152. }
  153. if (pos.CardType < 100)
  154. {
  155. RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
  156. }
  157. }
  158. }
  159. public void StartSim()
  160. {
  161. Thread th = new Thread(StartSimDo);
  162. th.IsBackground = true;
  163. th.Start();
  164. }
  165. public void StartSimDo()
  166. {
  167. while (true)
  168. {
  169. try
  170. {
  171. if (Utils.PushTimeCheck())
  172. {
  173. List<string> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
  174. WebCMSEntities spdb = new WebCMSEntities();
  175. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  176. DateTime start = DateTime.Now.AddDays(-365);
  177. DateTime end = DateTime.Now.AddMinutes(-2);
  178. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/MerchantsId.txt")));
  179. var Mers = spdb.Merchants.Where(m => m.Id >= StartId && m.CreateTime >= start && m.CreateTime <= end && m.Status == 1 && m.ProductType == "14" && SaveDataBindIds.Contains(m.ProductType)).OrderByDescending(m => m.Id).Take(20).ToList();
  180. foreach (var Mer in Mers)
  181. {
  182. var tran = db.Database.BeginTransaction();
  183. try
  184. {
  185. if (Mer.Field2 != "解绑" && Mer.Field2 != "UNBIND")
  186. {
  187. if (!Utils.Instance.IsHaoDa(int.Parse(Mer.ProductType)))
  188. {
  189. PosPushDataNewHelper.Bind(Mer);
  190. }
  191. }
  192. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  193. if (edit != null)
  194. {
  195. edit.Status = 2;
  196. spdb.SaveChanges();
  197. }
  198. tran.Commit();
  199. }
  200. catch (Exception ex)
  201. {
  202. tran.Rollback();
  203. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP广电卡商户数据到MAIN异常");
  204. }
  205. tran.Dispose();
  206. }
  207. spdb.SaveChanges();
  208. spdb.Dispose();
  209. db.SaveChanges();
  210. db.Dispose();
  211. }
  212. }
  213. catch (Exception ex)
  214. {
  215. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP广电卡商户数据到MAIN异常");
  216. }
  217. Thread.Sleep(1000);
  218. }
  219. }
  220. }
  221. }